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

PSR12/ClassInstantiation: bug fix for attributes with anonymous classes #3457

Conversation

jrfnl
Copy link
Contributor

@jrfnl jrfnl commented Oct 21, 2021

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

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
@gsherwood gsherwood added this to Idea Bank in PHPCS v3 Development via automation Dec 12, 2021
@gsherwood gsherwood added this to the 3.6.2 milestone Dec 12, 2021
gsherwood added a commit that referenced this pull request Dec 12, 2021
@gsherwood gsherwood merged commit 40afa3d into squizlabs:master Dec 12, 2021
PHPCS v3 Development automation moved this from Idea Bank to Ready for Release Dec 12, 2021
@gsherwood
Copy link
Member

Thanks for fixing this

@jrfnl jrfnl deleted the feature/psr12-classinstantiation-bug-fix-attributes-vs-anon-class branch December 12, 2021 21:49
@jrfnl
Copy link
Contributor Author

jrfnl commented Dec 12, 2021

You're welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PHPCS v3 Development
Ready for Release
Development

Successfully merging this pull request may close these issues.

PSR12.Classes.ClassInstantiation.MissingParentheses false positive using attributes on anonymous class
2 participants