PSR12/ClassInstantiation: bug fix for attributes with anonymous classes#3457
Merged
gsherwood merged 1 commit intosquizlabs:masterfrom Dec 12, 2021
Conversation
The `class` keyword was correctly tokenized as `T_ANON_CLASS`, even when there is an attribute between the `new` and the `class` keyword, however, the `PSR12.Classes.ClassInstantiation` sniff did not take that possibility into account, leading to false positives.
This commit:
* Adds code to skip over attributes.
Note: I've elected to skip over attributes, but as these attributes AFAICS can only be added in combination with an anonymous class and anonymous classes are ignored by this sniff, it would also be a valid option to bow out of the sniff when an attribute is encountered. As this would be inconsistent with how the `T_ANON_CLASS` token was handled so far, I've not gone with that option.
* Adds some additional extra guard code to prevent future false positives for when the class name could not be determined.
Includes unit test.
Fixes 3456
Member
|
Thanks for fixing this |
Contributor
Author
|
You're welcome. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
classkeyword was correctly tokenized asT_ANON_CLASS, even when there is an attribute between thenewand theclasskeyword, however, thePSR12.Classes.ClassInstantiationsniff did not take that possibility into account, leading to false positives.This commit:
Note: I've elected to skip over attributes, but as these attributes AFAICS can only be added in combination with an anonymous class and anonymous classes are ignored by this sniff, it would also be a valid option to bow out of the sniff when an attribute is encountered. As this would be inconsistent with how the
T_ANON_CLASStoken was handled so far, I've not gone with that option.Includes unit test.
Fixes #3456