PHPCSStandards / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
937 stars 56 forks source link

Anonymous Class parentheses check for PER 2.0 #568

Closed rikvdh closed 3 months ago

rikvdh commented 3 months ago

For PER 2.0 we need a sniff to check the absence of parentheses when no arguments are needed for an anonymous class.

  1. Anonymous Classes

...

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

The current existing sniff for 'anonymous classes' (PSR12.Classes.AnonClassDeclaration) could be extended with an configuration option. But maybe this specific behavior is easily checked in a new Sniff as well.

jrfnl commented 3 months ago

@rikvdh Please add a code sample of the desired behaviour (and what's not working).

rikvdh commented 3 months ago

This is invalid, the parentheses are not allowed there:

<?php

$foo = new class() {};

Valid should be:

<?php

$foo = new class {};

This is currently not covered by any sniff.

jrfnl commented 3 months ago

Confirmed for PHPCS. For what it's worth, PHPCSExtra does contain a sniff for this: Universal.Classes.DisallowAnonClassParentheses.

rikvdh commented 3 months ago

Ok, I've overlooked this one. We'll go with this one for now.. 👍