PHP-CS-Fixer icon indicating copy to clipboard operation
PHP-CS-Fixer copied to clipboard

Incorrect PSR-12 brace placement for anonymous classes

Open TimWolla opened this issue 3 years ago • 1 comments
trafficstars

Bug report

$ phpcs-fixer -V
PHP CS Fixer 3.2.1 Mountains by Fabien Potencier and Dariusz Ruminski (13ae36a)
$ cat .php-cs-fixer.dist.php 
<?php
$finder = PhpCsFixer\Finder::create()
    ->in(__DIR__);

return (new PhpCsFixer\Config())
    ->setRiskyAllowed(true)
    ->setRules([
        '@PSR1' => true,
        '@PSR2' => true,
        '@PSR12' => true,
    ])
    ->setFinder($finder);

Code snippet that reproduces the problem

<?php

interface Foo
{
}
interface Bar
{
}
$foo = new class () implements
    Foo,
    Bar
{
    // Class content
};

This file is correctly formatted according to PSR-12, section 8 which requires the brace to be on a standalone line if a multi-line interface list is given:

If the list of interfaces wraps, the brace MUST be placed on the line immediately following the last interface.

However when running PHP-CS-Fixer, the brace is moved immediately after the Bar, thus following the MUST:

$ phpcs-fixer fix -vvv --diff
PHP CS Fixer 3.2.1 Mountains by Fabien Potencier and Dariusz Ruminski (13ae36a)
Runtime: PHP 8.0.18
Loaded config default from "/pwd/.php-cs-fixer.dist.php".
Using cache file ".php-cs-fixer.cache".
F                                                                                                                                                                                                      1 / 1 (100%)
Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error
   1) test.php (braces)
      ---------- begin diff ----------
--- /pwd/test.php
+++ /pwd/test.php
@@ -8,7 +8,6 @@
 }
 $foo = new class () implements
     Foo,
-    Bar
-{
+    Bar {
     // Class content
 };

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


Fixed all files in 0.019 seconds, 12.000 MB memory used

TimWolla avatar May 18 '22 07:05 TimWolla

This feature request might be related: #6118.

TimWolla avatar May 18 '22 07:05 TimWolla

Hey!

Thanks for your report.

We identified that braces fixer (that is applied in your example) is too complex and has too many responsibilities. With that, we decided to deprecate it.

That also means that we will not be extending, changing the behaviour or fixing anything around braces fixer. Please, use replacement rules instead.

If you see the issue that you reported to be present in one of the replacement rules, please report a new issue.

Big thanks!

Wirone avatar May 12 '23 23:05 Wirone