Skip to content

Tokenizer/PHP: prevent fatal error on unfinished const declaration - #1462

Closed
SAY-5 wants to merge 1 commit into
PHPCSStandards:4.xfrom
SAY-5:fix-const-tokenizer-fatal
Closed

SAY-5 wants to merge 1 commit into
PHPCSStandards:4.xfrom
SAY-5:fix-const-tokenizer-fatal

Conversation

@SAY-5

@SAY-5 SAY-5 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Description

When the name of a const declaration is the last non-empty token in a file (for example const A with nothing following it during live coding), PHPCS aborts tokenizing with a fatal Undefined array key error.

Inside the const-declaration handling in PHP::tokenize(), a loop walks forward from the constant name looking for the next non-empty token to check whether it is an =. When the constant name is the last non-empty token, the loop ends with $i === $numTokens, so the following $tokens[$i] === '=' check reads an index that does not exist. This adds an $i < $numTokens guard before that access, so the file tokenizes without a fatal error.

Suggested changelog entry

Fixed a fatal "Undefined array key" error when tokenizing a file where a const name is the last non-empty token.

Related issues/external references

Fixes #1451

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.
  • I have opened a sister-PR in the documentation repository to update the Wiki.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
@jrfnl

jrfnl commented Aug 2, 2026

Copy link
Copy Markdown
Member

@SAY-5 Thank you for this PR and your willingness to contribute to PHP_CodeSniffer. Sorry for the delayed response.

I've identified this PR as largely a duplicate of #1461. As #1461 was opened earlier, I will accept that PR.
Having said that, there are small differences between the two PRs, with both PRs having their own merits. With that in mind, I will mash up the two PRs to be a "best of both worlds" and give both authors credit for their contribution.

I hope to be able to accept your next contribution outright ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tokenizer: fatal error when a const name is the last non-empty token in a file

2 participants