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.Classes.ClassInstantiation.MissingParentheses false positive using attributes on anonymous class #3456

Closed
vaceletm opened this issue Oct 21, 2021 · 3 comments · Fixed by #3457

Comments

@vaceletm
Copy link

Describe the bug

When attributes are set on anonymous classes, phpcs inappropriately flag PSR12.Classes.ClassInstantiation.MissingParentheses violation

Code sample

<?php

$class1 = new class {
    #[SomeAttribute('summary')]
    public const SOME_STUFF = 'foo';
};

$class2 = new #[SomeAttribute('summary')] class {
    public const SOME_STUFF = 'foo';
};

Custom ruleset

PSR12

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run ./vendor/bin/phpcs --standard=PSR12 test.php ...
  3. See error message displayed
-------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------------------------------
 8 | ERROR | [x] Parentheses must be used when instantiating a new class
-------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------

Expected behavior

  1. Should'nt report an error at Line 8
  2. phpcbf shouldn't attempt to fix by producing invalid code

Versions (please complete the following information):

  • OS: Linux Fedora 34
  • PHP: 8.0
  • PHPCS: 3.6.1
  • Standard: PSR12

Additional context

phpcbf will produce invalid code:

./src/vendor/bin/phpcbf --standard=PSR12 test.php 

PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
/home/mvacelet/workspace/tuleap/test.php             1      0
----------------------------------------------------------------------
A TOTAL OF 1 ERROR WERE FIXED IN 1 FILE
----------------------------------------------------------------------

and resulting file:

<?php

$class1 = new class {
    #[SomeAttribute('summary')]
    public const SOME_STUFF = 'foo';
};

$class2 = new() #[SomeAttribute('summary')] class {
    public const SOME_STUFF = 'foo';
};
@jrfnl
Copy link
Contributor

jrfnl commented Oct 21, 2021

@vaceletm Thanks for reporting this! I've been able to reproduce this issue and PR #3457 should fix this. Testing would be appreciated.

@vaceletm
Copy link
Author

I confirm your PR fixes the issue.

That was amazingly fast, thx !

@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 gsherwood changed the title Usage of attributes on anonymous class is not properly raise PSR12.Classes.ClassInstantiation.MissingParentheses PSR12.Classes.ClassInstantiation.MissingParentheses false positive using attributes on anonymous class Dec 12, 2021
gsherwood added a commit that referenced this issue 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 the bug report. The fix will be in 3.6.2.

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 a pull request may close this issue.

3 participants