Skip to content

Tokenizer/PHP: bug fix for missing parenthesis owner for method named "fn" #1128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jun 10, 2025

Description

When a function is named "fn", the function keyword should be set as the parenthesis owner.

While this works on PHPCS 3.x, I found while making PHPCSUtils compatible with 4.x, that this was broken on PHPCS 4.x for PHP 7.2 and 7.3 since commit da0d224.

This was due to token being seen as a T_FN token after the initial tokenization fixing round, so the Tokenizer::createTokenMap() would receive the T_FN token and not set the parenthesis owner index. The additional token processing run after that, would realize this is not in actual fact an arrow function and undo the retokenization (from T_FN back to T_STRING), which leaves the parentheses for this function declaration without an owner.

Fixed now by preventing the initial T_STRING to T_FN retokenization if the keyword is used in a context where the token should always be a T_STRING.

Suggested changelog entry

Fixed: missing 'parenthesis_owner' index for T_FUNCTION token on PHP < 7.4 when function is named "fn".

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

… "fn"

When a function is named "fn", the `function` keyword should be set as the parenthesis owner.

While this works on PHPCS 3.x, I found while making PHPCSUtils compatible with 4.x, that this was broken on PHPCS 4.x for PHP 7.2 and 7.3 since commit da0d224.

This was due to token being seen as a `T_FN` token after the initial tokenization fixing round, so the `Tokenizer::createTokenMap()` would receive the `T_FN` token and not set the parenthesis owner index.
The additional token processing run after that, would realize this is not in actual fact an arrow function and undo the retokenization (from `T_FN` back to `T_STRING`), which leaves the parentheses for this function declaration without an owner.

Fixed now by preventing the initial `T_STRING` to `T_FN` retokenization if the keyword is used in a context where the token should always be a `T_STRING`.
@jrfnl jrfnl added this to the 4.0.0 milestone Jun 10, 2025
@jrfnl jrfnl marked this pull request as ready for review June 10, 2025 05:16
@jrfnl jrfnl merged commit 025de13 into 4.x Jun 10, 2025
66 checks passed
@jrfnl jrfnl deleted the feature/createtokenmap-bugfix-fn-functionname branch June 10, 2025 05:26
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.

1 participant