Skip to content
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

PHPCS 4.x | Tokenizer: fix stray "parenthesis_..." indexes being set for non-closure use #3104

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Sep 5, 2020

Commit 08824f3 (issue #2593) added support for T_USE tokens for closures being parentheses owners.

However, the net effect was that every T_USE token - including import/trait use statement T_USE tokens, would have the parenthesis_owner, parenthesis_opener and parenthesis_closer indexes set.

For closure use statements, those were set correctly.

However, for import/trait use statements, the parenthesis_owner would point to the T_USE keyword which doesn't have parentheses and the parenthesis_opener and parenthesis_closer indexes would be null in most cases, but they would still be set.

Also, in some cases, the parenthesis_opener and parenthesis_closer indexes for import/trait use statements would incorrectly be set if to arbitrary, unrelated parenthesis, if no open curly or open square bracket was encountered between the use statement and the next set of parenthesis.

That makes the parenthesis_... indexes harder and less intuitive to work with as any of the parenthesis_... indexes could be set, even when the token has no parenthesis.

I've fixed this now by:

  • Not just resetting the $openOwner variable when we know there won't be any parenthesis, but by also resetting the parenthesis_... indexes of the incorrectly set owner.
  • Doing the reset on more tokens, including doing it on a T_OPEN_USE_GROUP token, as the tokenize() method has already run, so group use tokens have already been retokenized to their dedicated token.
    Note: There may be some more tokens which can be added to this list, but this should be a reasonable start.

I've also added a few continue statements to skip the rest of the code within the loop when we know in advance none of the other conditions would match anyway.

Includes adding dedicated unit tests verifying the correct setting of the parenthesis_... indexes.

Commit 08824f3 added support for `T_USE` tokens for closures being parentheses owners.

However, the net effect was that *every* `T_USE` token - including import/trait use statement `T_USE` tokens, would have the `parenthesis_owner`, `parenthesis_opener` and `parenthesis_closer` indexes set.

For closure `use` statements, those were set correctly.

However, for import/trait `use` statements, the `parenthesis_owner` would point to the `T_USE` keyword which doesn't have parentheses and the `parenthesis_opener` and `parenthesis_closer`  indexes would be `null` in most cases, but they would still be set.

Also, in some cases, the `parenthesis_opener` and `parenthesis_closer`  indexes for import/trait `use` statements would incorrectly be set if to arbitrary, unrelated parenthesis, if no open curly or open square bracket was encountered between the `use` statement and the next set of parenthesis.

That makes the `parenthesis_...` indexes harder and less intuitive to work with as any of the `parenthesis_...` indexes could be set, even when the token has no parenthesis.

I've fixed this now by:
* Not just resetting the `$openOwner` variable when we know there won't be any parenthesis, but by also resetting the `parenthesis_...` indexes of the incorrectly set owner.
* Doing the reset on more tokens, including doing it on a `T_OPEN_USE_GROUP` token, as the `tokenize()` method has already run, so group use tokens have already been retokenized to their dedicated token.

I've also added a few `continue` statements to skip the rest of the code within the loop when we know in advance none of the other conditions would match anyway.

Includes adding dedicated unit tests verifying the correct setting of the `parenthesis_...` indexes.
@jrfnl jrfnl force-pushed the phpcs-4.x/tokenizer-fix-parenthesis-owners-for-use branch from 1bd5a12 to 44fba8d Compare May 7, 2021 06:17
@jrfnl
Copy link
Contributor Author

jrfnl commented May 7, 2021

Rebased yet again for merge conflicts. @gsherwood Would be great if this PR could get some attention as this bug is currently the main reason why external standards will fail against PHPCS 4.x when running tests.

@gsherwood gsherwood merged commit 3935729 into squizlabs:4.0 May 9, 2021
@gsherwood
Copy link
Member

Sorry, merged now. Thanks a lot for fixing this up.

@jrfnl jrfnl deleted the phpcs-4.x/tokenizer-fix-parenthesis-owners-for-use branch May 9, 2021 23:08
@jrfnl
Copy link
Contributor Author

jrfnl commented May 9, 2021

@gsherwood Thanks for that!

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.

2 participants