Skip to content

Commit c882c2b

Browse files
committed
Squiz/ForLoopDeclaration: bugfix with closures being used in expressions
When a closure would be used within an expression, the code within the closure would also contain semicolons. The existing code did not take that into account and would throw false positives for spacing around semicolons in the closures (not the concern of this sniff) and would incorrectly neglect to throw errors for spacing around semicolons after the closure. This is now fixed.
1 parent de2ac26 commit c882c2b

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,26 @@ public function process(File $phpcsFile, $stackPtr)
176176
* Check whitespace around each of the semicolon tokens.
177177
*/
178178

179-
$semicolonCount = 0;
180-
$semicolon = $openingBracket;
179+
$semicolonCount = 0;
180+
$semicolon = $openingBracket;
181+
$targetNestinglevel = 0;
182+
if (isset($tokens[$openingBracket]['conditions']) === true) {
183+
$targetNestinglevel += count($tokens[$openingBracket]['conditions']);
184+
}
181185

182186
do {
183187
$semicolon = $phpcsFile->findNext(T_SEMICOLON, ($semicolon + 1), $closingBracket);
184188
if ($semicolon === false) {
185189
break;
186190
}
187191

192+
if (isset($tokens[$semicolon]['conditions']) === true
193+
&& count($tokens[$semicolon]['conditions']) > $targetNestinglevel
194+
) {
195+
// Semicolon doesn't belong to the for().
196+
continue;
197+
}
198+
188199
++$semicolonCount;
189200

190201
$humanReadableCount = 'first';

0 commit comments

Comments
 (0)