Symfony-coding-standard icon indicating copy to clipboard operation
Symfony-coding-standard copied to clipboard

Symfony.Objects.ObjectInstantiation.Invalid rejects `new class ()`

Open Bilge opened this issue 1 year ago • 3 comments

PSR-12 sniff, PSR12.Classes.AnonClassDeclaration.SpaceAfterKeyword rejects new class() with: Expected 1 space after class keyword; 0 found.

However, Symfony.Objects.ObjectInstantiation.Invalid rejects new class () with: Use parentheses when instantiating classes.

It seems to me this is a bug in this sniff, because to be so satisfied, this sniff should only be concerned with the presence of parens, not the formatting thereof. Ergo it should not be attempting to enforce that the parens are adjacent to class, but currently it has that (unwanted) side-effect.

Bilge avatar Jun 27 '24 19:06 Bilge

@Bilge Which version are you using? fde073240365ac2ec8bfaffa3c8b3d316eca799e fixed this, AFAICT.

mmoll avatar Jul 12 '24 17:07 mmoll

I am using 3.13.0. I believe the "fix" you are referencing only permitted new class(); it does not allow for new class () (though it should not specify a preference since that is not the point of the sniff).

Bilge avatar Jul 16 '24 13:07 Bilge

Ah, it was already a long time ago... Skimming through the history of the downstream repo I'm pulling this Symfony ruleset into, I saw that PSR12.Classes.AnonClassDeclaration.SpaceAfterKeyword got excluded because of this.

I'm unsure what would be the real source of truth for this, but php-cs-fixer also does delete the space with its Symfony standard:

$ /tmp/php-cs-fixer-v3.phar fix --dry-run /tmp/anon_class.php --diff --rules=@Symfony --verbose
PHP CS Fixer 3.59.3 (064efa1) 7th Gear by Fabien Potencier, Dariusz Ruminski and contributors.
[...]

   1) /tmp/anon_class.php (class_definition)
      ---------- begin diff ----------
--- /tmp/anon_class.php
+++ /tmp/anon_class.php
@@ -12,7 +12,7 @@
      */
     public function createDemo(): DemoInterface
     {
-        return new class () implements DemoInterface {
+        return new class() implements DemoInterface {
             private $property;
 
             /**

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


mmoll avatar Jul 16 '24 20:07 mmoll