Skip to content

Tokenizer/PHP: fix error when a const name is the last non-empty token in a file - #1461

Merged
jrfnl merged 2 commits into
PHPCSStandards:4.xfrom
lazerg:issue-1451-const-name-as-last-token
Aug 2, 2026
Merged

jrfnl merged 2 commits into
PHPCSStandards:4.xfrom
lazerg:issue-1451-const-name-as-last-token

Conversation

@lazerg

@lazerg lazerg commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Description

Tokenizing a file where the name of a const declaration is the last non-empty token, such as const A with nothing after it, aborts with An error occurred during processing; checking has been aborted. The error message was: Undefined array key ... in src/Tokenizers/PHP.php.

The loop that walks forward from the constant name to the next non-empty token ends with $i equal to $numTokens when there is no such token, and the $tokens[$i] === '=' check that follows then reads an index that does not exist. This adds an isset() guard so the check only runs when that token is present.

Suggested changelog entry

Fixed a tokenizer error ("Undefined array key") which could occur when the name of a const declaration was the last non-empty token in a file.

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.

…ration

Co-authored-by: Sai Asish Y <say.apm35@gmail.com>
@jrfnl jrfnl added this to the 4.0.2 milestone Aug 2, 2026

@jrfnl jrfnl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lazerg Thank you for another good PR. Sorry for the delayed response.

I identified PR #1462 as as largely a duplicate of this PR. As this one was opened earlier, I will accept this 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.

The main reason for doing the mash-up:

  • The $i < $numTokens condition is more descriptive as to why the condition is needed in comparison to isset($tokens[$i]) === true.
  • I always like to be explicit in documenting that a test case file contains a deliberate parse error / is intended to test a live coding situation.

Thank you again for your contribution. I will merge the updated PR once the build has passed.

@jrfnl
jrfnl merged commit 5c40089 into PHPCSStandards:4.x Aug 2, 2026
62 checks passed
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