djoos / Symfony-coding-standard

Development repository for the Symfony coding standard
MIT License
401 stars 102 forks source link

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

Open Bilge opened 2 months ago

Bilge commented 2 months ago

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.

mmoll commented 2 months ago

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

Bilge commented 1 month ago

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).

mmoll commented 1 month ago

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 -----------