Skip to content

Comments

fix: handle missing semicolon in abstract function declarations#3969

Closed
dmzoneill wants to merge 1 commit intosquizlabs:masterfrom
dmzoneill-forks:fix/issue-3917-undefined-index-function-declaration
Closed

fix: handle missing semicolon in abstract function declarations#3969
dmzoneill wants to merge 1 commit intosquizlabs:masterfrom
dmzoneill-forks:fix/issue-3917-undefined-index-function-declaration

Conversation

@dmzoneill
Copy link
Contributor

Summary

  • Prevents crash when processing malformed abstract/interface function declarations
  • Adds early return when semicolon is not found
  • Allows syntax errors to be reported by appropriate sniffs

Problem

When linting PHP code with malformed abstract/interface methods (missing both semicolon and curly braces), the FunctionDeclarationSniff crashes with:

Undefined array key -1 in FunctionDeclarationSniff.php on line 130

Example malformed code:

<?php
public function process(Schema $schema, string $query, array $context, array $vars, callable $next)

The crash occurs because:

  1. Line 129 calls findNext(T_SEMICOLON, $closeBracket) which returns false when no semicolon exists
  2. Line 130 attempts to access $tokens[($end - 1)] where $end = false
  3. This evaluates to $tokens[-1], causing the undefined array key error

Solution

Added a check after findNext() to detect when no semicolon is found:

  • If $end === false, the function returns early
  • This prevents the undefined array access
  • Other sniffs can properly report the actual syntax error in the PHP code

Testing

Tested with the reproduction case from the issue:

  • Before: Fatal error with "Undefined array key -1"
  • After: Graceful handling, allowing proper error reporting

Fixes #3917

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
@jrfnl
Copy link
Contributor

jrfnl commented Feb 10, 2026

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.

@jrfnl jrfnl closed this Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Undefined index in FunctionDeclarationSniff

2 participants