From c52b6ed2a5024bbbf364567bdf472e1f913ebbfd Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 2 Sep 2024 13:30:33 +0100 Subject: [PATCH] Code review feedback --- .../ControlStructureSpacingSniff.php | 4 +++ .../ControlStructureSpacingUnitTest.inc | 26 ++++++++++++----- .../ControlStructureSpacingUnitTest.inc.fixed | 29 +++++++++++++------ .../ControlStructureSpacingUnitTest.php | 4 ++- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php b/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php index b05cf9fd67..56c4192aeb 100644 --- a/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php +++ b/src/Standards/PSR12/Sniffs/ControlStructures/ControlStructureSpacingSniff.php @@ -104,6 +104,10 @@ public function process(File $phpcsFile, $stackPtr) $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, ''); } } diff --git a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc index 8ad29e9645..f37069699f 100644 --- a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc +++ b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc @@ -99,23 +99,35 @@ $expr2 && // structure body }; -// Ensure the sniff handles too many newlines (not just too few). This was copied from src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.1.inc +// Ensure the sniff handles too many newlines (not just too few). for ( - $i = 0 + $i = 0; + $i < 10; + $i++ - ; +) {} +// Ensure the sniff does not remove indentation whitespace when comments are involved. +for ( - $i < 10 + // 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 f319a8aea3..d6c3f48c2f 100644 --- a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed +++ b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.inc.fixed @@ -102,19 +102,30 @@ match ( // structure body }; -// Ensure the sniff handles too many newlines (not just too few). This was copied from src/Standards/Squiz/Tests/ControlStructures/ForLoopDeclarationUnitTest.1.inc +// Ensure the sniff handles too many newlines (not just too few). for ( - $i = 0 - - - ; - - - $i < 10 + $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 8c81dfe0c2..3763b5d3c6 100644 --- a/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.php +++ b/src/Standards/PSR12/Tests/ControlStructures/ControlStructureSpacingUnitTest.php @@ -51,7 +51,9 @@ public function getErrorList() 97 => 1, 98 => 2, 106 => 1, - 121 => 1, + 111 => 1, + 117 => 1, + 127 => 1, ]; }//end getErrorList()