Skip to content

Commit 5457312

Browse files
committed
fix: handle missing semicolon in abstract function declarations
When processing malformed PHP code where an abstract function declaration is missing both the semicolon and curly braces, the FunctionDeclarationSniff would crash with "Undefined array key -1". This occurred because findNext(T_SEMICOLON) returns false when no semicolon is found, and the code attempted to access tokens[false - 1], which evaluates to tokens[-1]. The fix checks if findNext() returns false and exits early, allowing other sniffs to report the actual syntax error rather than crashing. Fixes #3917
1 parent c6c65ca commit 5457312

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public function process(File $phpcsFile, $stackPtr)
127127
// Must be no space before semicolon in abstract/interface methods.
128128
if ($phpcsFile->getMethodProperties($stackPtr)['has_body'] === false) {
129129
$end = $phpcsFile->findNext(T_SEMICOLON, $closeBracket);
130+
if ($end === false) {
131+
// Malformed function declaration without semicolon - skip this check.
132+
return;
133+
}
134+
130135
if ($tokens[($end - 1)]['content'] === $phpcsFile->eolChar) {
131136
$spaces = 'newline';
132137
} else if ($tokens[($end - 1)]['code'] === T_WHITESPACE) {

0 commit comments

Comments
 (0)