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

PSR-12: wrong fix when instanciating an anonymous class #6536

Closed
guvra opened this issue Aug 4, 2022 · 6 comments
Closed

PSR-12: wrong fix when instanciating an anonymous class #6536

guvra opened this issue Aug 4, 2022 · 6 comments
Labels

Comments

@guvra
Copy link

guvra commented Aug 4, 2022

Bug report

When instanciating an anonymous class, php-cs-fixer adds parentheses after the class keyword, which contradicts the PSR-12 rule defined here: https://www.php-fig.org/psr/psr-12/#8-anonymous-classes

Additional information:

  • PHP CS Fixer version and PHP runtime:
    PHP CS Fixer 3.9.5 Grand Awaiting by Fabien Potencier and Dariusz Ruminski.
    PHP runtime: 7.4.3
    
  • Command to use to reproduce the issue:
    vendor/bin/php-cs-fixer fix test.php --rules=@PSR12 --dry-run --diff -vvv
    
  • Configuration file to use: none

Code snippet that reproduces the problem

test.php:

<?php

$test = new class {
    // ...
};

Expected behavior

Nothing to fix.

Actual behavior

   1) test.php (new_with_braces, class_definition)
      ---------- begin diff ----------
--- /home/guvra/test.php
+++ /home/guvra/test.php
@@ -1,5 +1,5 @@
 <?php
 
-$test = new class {
+$test = new class () {
     // ...
 };

      ----------- end diff -----------


Checked all files in 0.006 seconds, 14.000 MB memory used
@guvra guvra added the kind/bug label Aug 4, 2022
@SpacePossum
Copy link
Contributor

SpacePossum commented Aug 4, 2022

This is likely done by class_definition, however it is not done by @PSR12.

If you want this fix, please fill in the bug report template, it is there so we can help you, if you don't want to fill it in I can only assume you don't want help either.

@guvra
Copy link
Author

guvra commented Aug 8, 2022

@SpacePossum I've updated the issue description to match the template and with additional relevant information (e.g. command to run, and output of the command).

The culprit is most probably new_with_braces.

To comply to the PSR-12 standard, the following configuration is required:

  • anonymous_class: false (default value is true)

@guvra
Copy link
Author

guvra commented Aug 12, 2022

FYI it will probably be enforced in PER:
https://github.com/php-fig/per-coding-style/pull/37/files

@julienfalque
Copy link
Member

Given the discussions, I would say that PSR-12 intended to enforce parentheses even without arguments, but provided examples inconsistent with that rule. I propose to keep @PSR12 unchanged.

@VincentLanglet
Copy link
Contributor

Given the discussions, I would say that PSR-12 intended to enforce parentheses even without arguments, but provided examples inconsistent with that rule. I propose to keep @PSR12 unchanged.

Then this issue can be closed, no ?

@guvra
Copy link
Author

guvra commented Sep 5, 2022

You probably can, but please keep in mind that this rule will be enforced in the upcoming PER standard: https://github.com/php-fig/per-coding-style/blob/master/spec.md#8-anonymous-classes.

If the anonymous class has no arguments, the () after class MUST be omitted.

This is something to keep in mind if you plan to implement the PER standard into php-cs-fixer in the future.

@julienfalque julienfalque closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants