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
880 stars 54 forks source link

Modifier keyword order for PER 2.0 #567

Closed rikvdh closed 1 month ago

rikvdh commented 1 month ago

For PER 2.0 we need a 'updated' variant for checking modifier keywords. In PSR-12/PER 1.0, only abstract, final and static were checked (see PSR2.Methods.MethodDeclaration). But for PER 2.0, this is extended:

4.6 Modifier Keywords Classes, properties, and methods have numerous keyword modifiers that alter how the engine and language handles them. When present, they MUST be in the following order:

  • Inheritance modifier: abstract or final
  • Visibility modifier: public, protected, or private
  • Scope modifier: static
  • Mutation modifier: readonly
  • Type declaration
  • Name

All keywords MUST be on a single line, and MUST be separated by a single space.

An updated version of PSR2.Methods.MethodDeclaration might be a good approach and might be quite simple to do for someone wanting to get into developing sniffs.

jrfnl commented 1 month ago

@rikvdh I don't understand what change you are proposing/suggesting. Maybe adding some code samples would help (also for the other issue a good idea).

If I look at the specs above, the PSR2.Methods.MethodDeclaration sniff already handles the checking of the order perfectly for methods as the readonly keyword does not apply to methods and methods can also not be typed in this way.

jrfnl commented 1 month ago

Also note that for properties this is checked via the PSR2.Classes.PropertyDeclaration sniff and, again, that sniff already handles this correctly.

rikvdh commented 1 month ago

I was not sure if this was fully covered yet, I'll check this.

jrfnl commented 1 month ago

I was not sure if this was fully covered yet, I'll check this.

It may be, it may not be. Please check before opening an issue....

rikvdh commented 1 month ago

Ok, this was not fully covered for PER 2.0 currently. Solved via PHPCSExtra.