Skip to content

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

Merged
gsherwood merged 1 commit intosquizlabs:masterfrom
jrfnl:feature/psr12-classinstantiation-bug-fix-attributes-vs-anon-class
Dec 12, 2021
Merged

PSR12/ClassInstantiation: bug fix for attributes with anonymous classes#3457
gsherwood merged 1 commit intosquizlabs:masterfrom
jrfnl:feature/psr12-classinstantiation-bug-fix-attributes-vs-anon-class

Conversation

@jrfnl
Copy link
Copy Markdown
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
Copy link
Copy Markdown
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
Copy Markdown
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

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants