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

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

Open
Bilge opened this issue Jun 27, 2024 · 3 comments
Open

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

Bilge opened this issue Jun 27, 2024 · 3 comments

Comments

@Bilge
Copy link

Bilge commented Jun 27, 2024

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
Copy link
Contributor

mmoll commented Jul 12, 2024

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

@Bilge
Copy link
Author

Bilge commented Jul 16, 2024

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
Copy link
Contributor

mmoll commented Jul 16, 2024

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


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants