Skip to content

Commit c7fad6f

Browse files
committed
Generic/JumbledIncrementer: fix non-problematic bug
The sniff was wrongly assuming that the inner for loop would always have a closing parenthesis which is not true when PHPCS is used with live coding. This meant that `$end` could be set to `null` (https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/84acf4e56f110db8e75cb9a575c5727df637643c/src/Standards/Generic/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php#L116). This did not cause any issues as `$next <= $end` when `$end` is `null` is always false, so the for loop that looks for incrementers was not executed. The code was changed to bail early when the inner for loop is missing the closing parenthesis and a test was added.
1 parent 9b3c5fb commit c7fad6f

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function process(File $phpcsFile, $stackPtr)
108108
protected function findIncrementers(array $tokens, array $token)
109109
{
110110
// Skip invalid statement.
111-
if (isset($token['parenthesis_opener']) === false) {
111+
if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
112112
return [];
113113
}
114114

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// Intentional parse error (inner for loop missing closing parenthesis).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
for ($i = 0; $i < 20; $i++) {
7+
for ($i = 0; $i < 20; $i++
8+
}

0 commit comments

Comments
 (0)