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()