Skip to content

Commit 1f77d55

Browse files
committed
Squiz/ForLoopDeclaration: improve the accuracy of the SpaceAfterOpen/SpaceBeforeClose error messages
The error messages for `SpaceAfterOpen`/`SpaceBeforeClose` did not handle situations where the spacing found was a new line. The determination of `$spaceAfterOpen` and `$spaceBeforeClose` 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 found, no spaces allowed" situations, only a minor tweak has been made to the error message to acknowledge that more than just a space may have been found.
1 parent b6ffa20 commit 1f77d55

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,18 @@ public function process(File $phpcsFile, $stackPtr)
7777
$closingBracket = $tokens[$openingBracket]['parenthesis_closer'];
7878

7979
if ($this->requiredSpacesAfterOpen === 0 && $tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
80-
$error = 'Space found after opening bracket of FOR loop';
80+
$error = 'Whitespace found after opening bracket of FOR loop';
8181
$fix = $phpcsFile->addFixableError($error, $openingBracket, 'SpacingAfterOpen');
8282
if ($fix === true) {
8383
$phpcsFile->fixer->replaceToken(($openingBracket + 1), '');
8484
}
8585
} else if ($this->requiredSpacesAfterOpen > 0) {
86-
$spaceAfterOpen = 0;
87-
if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
88-
$spaceAfterOpen = strlen($tokens[($openingBracket + 1)]['content']);
86+
$nextNonWhiteSpace = $phpcsFile->findNext(T_WHITESPACE, ($openingBracket + 1), $closingBracket, true);
87+
$spaceAfterOpen = 0;
88+
if ($tokens[$openingBracket]['line'] !== $tokens[$nextNonWhiteSpace]['line']) {
89+
$spaceAfterOpen = 'newline';
90+
} else if ($tokens[($openingBracket + 1)]['code'] === T_WHITESPACE) {
91+
$spaceAfterOpen = $tokens[($openingBracket + 1)]['length'];
8992
}
9093

9194
if ($spaceAfterOpen !== $this->requiredSpacesAfterOpen) {
@@ -107,15 +110,18 @@ public function process(File $phpcsFile, $stackPtr)
107110
}//end if
108111

109112
if ($this->requiredSpacesBeforeClose === 0 && $tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
110-
$error = 'Space found before closing bracket of FOR loop';
113+
$error = 'Whitespace found before closing bracket of FOR loop';
111114
$fix = $phpcsFile->addFixableError($error, $closingBracket, 'SpacingBeforeClose');
112115
if ($fix === true) {
113116
$phpcsFile->fixer->replaceToken(($closingBracket - 1), '');
114117
}
115118
} else if ($this->requiredSpacesBeforeClose > 0) {
116-
$spaceBeforeClose = 0;
117-
if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
118-
$spaceBeforeClose = strlen($tokens[($closingBracket - 1)]['content']);
119+
$prevNonWhiteSpace = $phpcsFile->findPrevious(T_WHITESPACE, ($closingBracket - 1), $openingBracket, true);
120+
$spaceBeforeClose = 0;
121+
if ($tokens[$closingBracket]['line'] !== $tokens[$prevNonWhiteSpace]['line']) {
122+
$spaceBeforeClose = 'newline';
123+
} else if ($tokens[($closingBracket - 1)]['code'] === T_WHITESPACE) {
124+
$spaceBeforeClose = $tokens[($closingBracket - 1)]['length'];
119125
}
120126

121127
if ($this->requiredSpacesBeforeClose !== $spaceBeforeClose) {

0 commit comments

Comments
 (0)