Skip to content

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Sep 22, 2025

Description

Tokenizer/AttributesTest: move parse error test to own file

Tokenizer/AttributesTest: expand test coverage

In light of an upcoming change, let's also test the token code/content of the first token after the attribute sequence to safeguard that no token duplication is being introduced via the array_slicing and dicing done in the PHP < 8.0 "comment to attribute" retokenization.

Tokenizer/PHP: fix tokens missing/file content being removed by attribute retokenization

This commit fixes the issue detailed in ticket #1279, where in case of an unfinished (parse error) attribute, the contents on the same line as the attribute opener would be removed from the token stream when PHPCS is run on PHP < 8.0.

On PHP < 8.0, attributes tokenize as comments and the Tokenizer reparses the contents of the comment to make it a usable token stream.

As things were, if the attribute closer could not be found, the PHP::parsePhpAttribute() method would return null instead of returning the array of (reparsed) tokens which were on the same line as the attribute opener, which meant the "comment content"/attribute content was effectively removed from the token stream.

The fix includes changing the return type of the (private) PHP::parsePhpAttribute() method from array|null to array, which also allows for simplifying some code handling the return value of this method.

Includes additional tests and improvements to the pre-existing parse error test.

Suggested changelog entry

Fixed: Tokenizer/PHP: on PHP < 8.0, an unclosed attribute (parse error) could end up removing some tokens from the token stream.

  • This could lead to false positives and false negative from sniffs, but could also lead to incorrect fixes being made mangling the file under scan.

Related issues/external references

Fixes #1279

In light of an upcoming change, let's also test the token code/content of the first token _after_ the attribute sequence to safeguard that no token duplication is being introduced via the array_slicing and dicing done in the PHP < 8.0 "comment to attribute" retokenization.
…bute retokenization

This commit fixes the issue detailed in ticket 1279, where in case of an unfinished (parse error) attribute, the contents on the same line as the attribute opener would be removed from the token stream when PHPCS is run on PHP < 8.0.

On PHP < 8.0, attributes tokenize as comments and the Tokenizer reparses the contents of the comment to make it a usable token stream.

As things were, if the attribute closer could not be found, the `PHP::parsePhpAttribute()` method would return `null` instead of returning the array of (reparsed) tokens which were on the same line as the attribute opener, which meant the "comment content"/attribute content was effectively removed from the token stream.

The fix includes changing the return type of the (private) `PHP::parsePhpAttribute()` method from `array|null` to `array`, which also allows for simplifying some code handling the return value of this method.

Includes additional tests and improvements to the pre-existing parse error test.

Fixes 1279
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