From d856bfc4f9ceb75391c5643f47d8a00484a7de6f Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 26 May 2023 12:37:06 +0100 Subject: [PATCH 1/4] Add additional test cases --- Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc | 9 +++++++++ Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.php | 2 ++ 2 files changed, 11 insertions(+) diff --git a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc index 280a33cd..5ff6064e 100644 --- a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc +++ b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc @@ -11,6 +11,15 @@ class Foo $someValue = 1; + $someValue = 2; + + + + $someValue = 3; + + + + return $someValue; } } diff --git a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.php b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.php index f8b70b23..ae9b8e38 100644 --- a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.php +++ b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.php @@ -25,6 +25,8 @@ public function getWarningList() return [ 6 => 1, 12 => 1, + 15 => 1, + 19 => 1, ]; } } From 5e29af7c7c3ae366d0fabe1542d0df76b420abb6 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Fri, 26 May 2023 12:37:36 +0100 Subject: [PATCH 2/4] Copy testcase ready for auto-fixing --- .../MultipleEmptyLinesUnitTest.inc.fixed | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed diff --git a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed new file mode 100644 index 00000000..5ff6064e --- /dev/null +++ b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed @@ -0,0 +1,25 @@ + Date: Fri, 26 May 2023 12:38:59 +0100 Subject: [PATCH 3/4] Auto-fix `Magento2.Whitespace.MultipleEmptyLines` --- Magento2/Sniffs/Whitespace/MultipleEmptyLinesSniff.php | 6 +++++- .../Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed | 7 ------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Magento2/Sniffs/Whitespace/MultipleEmptyLinesSniff.php b/Magento2/Sniffs/Whitespace/MultipleEmptyLinesSniff.php index 4d74a29e..1f5655c0 100644 --- a/Magento2/Sniffs/Whitespace/MultipleEmptyLinesSniff.php +++ b/Magento2/Sniffs/Whitespace/MultipleEmptyLinesSniff.php @@ -53,12 +53,16 @@ public function process(File $phpcsFile, $stackPtr) $next = $phpcsFile->findNext(T_WHITESPACE, $stackPtr, null, true); $lines = $tokens[$next]['line'] - $tokens[$stackPtr]['line']; if ($lines > 1) { - $phpcsFile->addWarning( + $fix = $phpcsFile->addFixableWarning( $this->warningMessage, $stackPtr, $this->warningCode, [$lines] ); + + if ($fix) { + $phpcsFile->fixer->replaceToken($stackPtr, ''); + } } } } diff --git a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed index 5ff6064e..8f292602 100644 --- a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed +++ b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed @@ -4,22 +4,15 @@ class Foo private $foo; private $bar; - private $baz; public function test() { $someValue = 1; - $someValue = 2; - - $someValue = 3; - - - return $someValue; } } From c6067f366cd1810ea28cec52b65c9aa1b6de6745 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Thu, 1 Jun 2023 10:37:49 +0100 Subject: [PATCH 4/4] Handle more edge cases --- .../Whitespace/MultipleEmptyLinesSniff.php | 13 ++++- .../Whitespace/MultipleEmptyLinesUnitTest.inc | 55 +++++++++++++++++++ .../MultipleEmptyLinesUnitTest.inc.fixed | 6 ++ .../Whitespace/MultipleEmptyLinesUnitTest.php | 5 ++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/Magento2/Sniffs/Whitespace/MultipleEmptyLinesSniff.php b/Magento2/Sniffs/Whitespace/MultipleEmptyLinesSniff.php index 1f5655c0..3aaf0973 100644 --- a/Magento2/Sniffs/Whitespace/MultipleEmptyLinesSniff.php +++ b/Magento2/Sniffs/Whitespace/MultipleEmptyLinesSniff.php @@ -1,8 +1,10 @@ fixer->replaceToken($stackPtr, ''); + // $stackPtr + 1 to keep one empty line. + // $next - 1 to keep the indentation + for ($i = $stackPtr + 1; $i < $next - 1; $i++) { + $phpcsFile->fixer->replaceToken($i, ''); + } + + // Handle case where the next line isn't indented + if ($tokens[$next - 1]['content'] === PHP_EOL) { + $phpcsFile->fixer->replaceToken($next - 1, ''); + } } } } diff --git a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc index 5ff6064e..e591ebe0 100644 --- a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc +++ b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc @@ -19,6 +19,61 @@ class Foo + $someValue = 40; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + $someValue = 'newlines and spaces and tabs'; + + + + + + + + + + + +$someValue = 'no indentation'; + + return $someValue; } diff --git a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed index 8f292602..837c8b69 100644 --- a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed +++ b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.inc.fixed @@ -13,6 +13,12 @@ class Foo $someValue = 3; + $someValue = 40; + + $someValue = 'newlines and spaces and tabs'; + +$someValue = 'no indentation'; + return $someValue; } } diff --git a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.php b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.php index ae9b8e38..0853d4af 100644 --- a/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.php +++ b/Magento2/Tests/Whitespace/MultipleEmptyLinesUnitTest.php @@ -1,8 +1,10 @@ 1, 15 => 1, 19 => 1, + 23 => 1, + 63 => 1, + 75 => 1, ]; } }