fix: handle missing semicolon in abstract function declarations#3969
Closed
dmzoneill wants to merge 1 commit intosquizlabs:masterfrom
Closed
fix: handle missing semicolon in abstract function declarations#3969dmzoneill wants to merge 1 commit intosquizlabs:masterfrom
dmzoneill wants to merge 1 commit intosquizlabs:masterfrom
Conversation
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 squizlabs#3917
Contributor
|
Didn't I tell you this already today ? Please don't open issues in this repo. This repo is abandoned and https://github.com/PHPCSStandards/PHP_CodeSniffer is its successor. See: #3932 P.S.: and don't use AI. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Problem
When linting PHP code with malformed abstract/interface methods (missing both semicolon and curly braces), the FunctionDeclarationSniff crashes with:
Example malformed code:
The crash occurs because:
findNext(T_SEMICOLON, $closeBracket)which returnsfalsewhen no semicolon exists$tokens[($end - 1)]where$end = false$tokens[-1], causing the undefined array key errorSolution
Added a check after
findNext()to detect when no semicolon is found:$end === false, the function returns earlyTesting
Tested with the reproduction case from the issue:
Fixes #3917