Skip to content

Commit 6be4ea7

Browse files
committed
Generic/InlineControlStructure: fix unchecked findNext() return value
On line 67, `findNext()` searches for the first non-empty token after a `T_ELSE` to detect the `else if` pattern. When `else` is the last non-empty token in the file (live coding/parse error), `findNext()` returns `false`. This `false` value was used directly as an array index on line 68 (`$tokens[$next]`), which silently becomes `$tokens[0]`, reading the `T_OPEN_TAG` token instead. The bug was silent because the code would eventually bail out at line 81-83. This commit adds a `$next === false` check, which also allows the sniff to bail out earlier for an `else` at end of file instead of continuing to do unnecessary work before reaching the existing live coding bail-out further down. Existing test file InlineControlStructureUnitTest.12.inc already covers this scenario.
1 parent 960d09c commit 6be4ea7

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function process(File $phpcsFile, int $stackPtr)
6565
// Ignore the ELSE in ELSE IF. We'll process the IF part later.
6666
if ($tokens[$stackPtr]['code'] === T_ELSE) {
6767
$next = $phpcsFile->findNext(Tokens::EMPTY_TOKENS, ($stackPtr + 1), null, true);
68-
if ($tokens[$next]['code'] === T_IF) {
68+
if ($next === false || $tokens[$next]['code'] === T_IF) {
6969
return;
7070
}
7171
}

0 commit comments

Comments
 (0)