Skip to content

Fix keywords used as constant names resetting parser state#257

Merged
janedbal merged 1 commit intomasterfrom
fix-keyword-as-constant-name
Jan 28, 2026
Merged

Fix keywords used as constant names resetting parser state#257
janedbal merged 1 commit intomasterfrom
fix-keyword-as-constant-name

Conversation

@janedbal
Copy link
Member

PHP reserved keywords (NAMESPACE, CLASS, INTERFACE, TRAIT, ENUM) can be used as class constant names. When accessed (e.g., Foo::NAMESPACE) or defined (e.g., const NAMESPACE = 'value'), PHP still tokenizes them as their keyword type (T_NAMESPACE, T_CLASS, etc.).

This caused the parser to incorrectly:

  • Reset use statements when encountering T_NAMESPACE as a constant
  • Corrupt $inClassLevel tracking when encountering T_CLASS/etc.

The fix checks if the keyword is preceded by T_DOUBLE_COLON or T_CONST before applying the keyword-specific handling.

Resolves #256

PHP reserved keywords (NAMESPACE, CLASS, INTERFACE, TRAIT, ENUM) can be
used as class constant names. When accessed (e.g., Foo::NAMESPACE) or
defined (e.g., const NAMESPACE = 'value'), PHP still tokenizes them as
their keyword type (T_NAMESPACE, T_CLASS, etc.).

This caused the parser to incorrectly:
- Reset use statements when encountering T_NAMESPACE as a constant
- Corrupt $inClassLevel tracking when encountering T_CLASS/etc.

The fix checks if the keyword is preceded by T_DOUBLE_COLON or T_CONST
before applying the keyword-specific handling.
@janedbal janedbal changed the title Fix keywords used as constant names resetting parser state (#256) Fix keywords used as constant names resetting parser state Jan 28, 2026
@janedbal janedbal merged commit 37cbe36 into master Jan 28, 2026
83 of 96 checks passed
@janedbal janedbal deleted the fix-keyword-as-constant-name branch January 28, 2026 12:20
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.

Accessing class constant called 'NAMESPACE' forgets previous use statements.

1 participant