Skip to content

Commit b593a95

Browse files
committed
Squiz/ForLoopDeclaration: improve the semicolon spacing determination
The `SpacingBeforeFirst/Second`/`SpacingAfterFirst/Second` checks did not handle situations where the spacing found was a new line. The determination of `$spaces` has now been brought in line with other sniffs and will report `newline` when a new line was found. Includes using the predetermined `length` of the token instead of doing length calculations. For the "spaces before" situation, only a minor tweak has been made to the error message to acknowledge that more than just a space may have been found. **Note**: This _does_ change the behaviour of the sniff for multi-line `for` declarations. Previously a new line character would be regarded as "one space" and left alone. Now, the new line (and other extraneous whitespace) will be removed. Note: spacing _within_ expressions is not affected by this change.
1 parent 3f8ec82 commit b593a95

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/Standards/Squiz/Sniffs/ControlStructures/ForLoopDeclarationSniff.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public function process(File $phpcsFile, $stackPtr)
200200
$prevNonWhiteSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($semicolon - 1), $openingBracket, true);
201201
if ($semicolonCount !== 1 || $prevNonWhiteSpace !== $openingBracket) {
202202
if ($tokens[($semicolon - 1)]['code'] === T_WHITESPACE) {
203-
$error = 'Space found before %s semicolon of FOR loop';
203+
$error = 'Whitespace found before %s semicolon of FOR loop';
204204
$errorCode = 'SpacingBefore'.$humanReadableCode;
205205
$fix = $phpcsFile->addFixableError($error, $semicolon, $errorCode, $data);
206206
if ($fix === true) {
@@ -225,7 +225,11 @@ public function process(File $phpcsFile, $stackPtr)
225225
} else if ($tokens[($semicolon + 1)]['code'] === T_WHITESPACE
226226
&& $tokens[$nextNonWhiteSpace]['code'] !== T_SEMICOLON
227227
) {
228-
$spaces = strlen($tokens[($semicolon + 1)]['content']);
228+
$spaces = $tokens[($semicolon + 1)]['length'];
229+
if ($tokens[$semicolon]['line'] !== $tokens[$nextNonWhiteSpace]['line']) {
230+
$spaces = 'newline';
231+
}
232+
229233
if ($spaces !== 1) {
230234
$error = 'Expected 1 space after %s semicolon of FOR loop; %s found';
231235
$errorCode = 'SpacingAfter'.$humanReadableCode;

0 commit comments

Comments
 (0)