From d9e164dbc4d9ce0a434e1cc70592c0970cf9c584 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 29 Jul 2024 22:34:22 +0100 Subject: [PATCH] Fix conflict within PSR12.ControlStructures.ControlStructureSpacing For multi-line control structures, the first line of code must be on the next line after the control structure. This sniff correctly identified such cases. When the first line of code was on the same line as the control structure, the sniff correctly fixed this by adding a newline between these. However, when there were multiple blank lines between these, the fixer would continue adding new newlines. This change fixes this bug by first removing all non-indentation white-space before adding the one expected newline. Includes test. --- .../ControlStructureSpacingSniff.php | 12 ++++++ .../ControlStructureSpacingUnitTest.inc | 33 +++++++++++++++ .../ControlStructureSpacingUnitTest.inc.fixed | 28 +++++++++++++ .../ControlStructureSpacingUnitTest.php | 42 ++++++++++--------- 4 files changed, 96 insertions(+), 19 deletions(-) diff --git a/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php b/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php index 9a200aa81c..56c4192aeb 100644 --- a/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php @@ -101,7 +101,19 @@ public function process(File $phpcsFile, $stackPtr) $error = 'The first expression of a multi-line control structure must be on the line after the opening parenthesis'; $fix = $phpcsFile->addFixableError($error, $next, 'FirstExpressionLine'); if ($fix === true) { + $phpcsFile->fixer->beginChangeset(); + if ($tokens[$next]['line'] > ($tokens[$parenOpener]['line'] + 1)) { + for ($i = ($parenOpener + 1); $i < $next; $i++) { + if ($tokens[$next]['line'] === $tokens[$i]['line']) { + break; + } + + $phpcsFile->fixer->replaceToken($i, ''); + } + } + $phpcsFile->fixer->addNewline($parenOpener); + $phpcsFile->fixer->endChangeset(); } } diff --git a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc index 9c037d6cd3..f37069699f 100644 --- a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc +++ b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc @@ -98,3 +98,36 @@ $expr2 && $expr3) { // structure body }; + +// Ensure the sniff handles too many newlines (not just too few). +for ( + + + $i = 0; + $i < 10; + $i++ + + +) {} + +// Ensure the sniff does not remove indentation whitespace when comments are involved. +for ( + + + // comment. + $i = 0; + $i < 10; + $i++ +) {} + +// The sniff treats a comment (ie non-whitespace) as content, but only at the +// start / end of the control structure. So the inner-whitespace here is +// intentionally ignored by this sniff. Additionally, the comment is not indented +// by this sniff when fixing. +for (// comment. + + + $i = 0; + $i < 10; + $i++ +) {} diff --git a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed index 7ea61b1e2a..d6c3f48c2f 100644 --- a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed +++ b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed @@ -101,3 +101,31 @@ match ( ) { // structure body }; + +// Ensure the sniff handles too many newlines (not just too few). +for ( + $i = 0; + $i < 10; + $i++ +) {} + +// Ensure the sniff does not remove indentation whitespace when comments are involved. +for ( + // comment. + $i = 0; + $i < 10; + $i++ +) {} + +// The sniff treats a comment (ie non-whitespace) as content, but only at the +// start / end of the control structure. So the inner-whitespace here is +// intentionally ignored by this sniff. Additionally, the comment is not indented +// by this sniff when fixing. +for ( +// comment. + + + $i = 0; + $i < 10; + $i++ +) {} diff --git a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.php b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.php index 69ef8b7c36..3763b5d3c6 100644 --- a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.php +++ b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.php @@ -31,25 +31,29 @@ final class ControlStructureSpacingUnitTest extends AbstractSniffUnitTest public function getErrorList() { return [ - 2 => 2, - 16 => 1, - 17 => 1, - 18 => 1, - 22 => 1, - 23 => 1, - 32 => 1, - 33 => 1, - 34 => 1, - 37 => 1, - 38 => 1, - 39 => 1, - 48 => 2, - 58 => 1, - 59 => 1, - 92 => 1, - 96 => 1, - 97 => 1, - 98 => 2, + 2 => 2, + 16 => 1, + 17 => 1, + 18 => 1, + 22 => 1, + 23 => 1, + 32 => 1, + 33 => 1, + 34 => 1, + 37 => 1, + 38 => 1, + 39 => 1, + 48 => 2, + 58 => 1, + 59 => 1, + 92 => 1, + 96 => 1, + 97 => 1, + 98 => 2, + 106 => 1, + 111 => 1, + 117 => 1, + 127 => 1, ]; }//end getErrorList()