Closed jrfnl closed 1 year ago
The only thing I'm in doubt about is whether or not to also exclude the FirstExtendsInterfaceSameLine (and related) error code(s) ?
If it was excluded, could that be re-enabled in WordPress-Extra?
The only thing I'm in doubt about is whether or not to also exclude the FirstExtendsInterfaceSameLine (and related) error code(s) ?
If it was excluded, could that be re-enabled in WordPress-Extra?
Of course it could ;-)
Having said that, I'm not sure we should disable them. I mean, we don't really want multi-line declarations, but if those are used (and they are not completely forbidden via this sniff), making sure that the formatting is consistent seems like a good thihng.
Is your feature request related to a problem?
I was looking at this part of the March 2020 Make post:
And then realized that there appears to be a gap in the sniffs included in the
Core
ruleset which check the formatting of class (and interface/trait/enum) declaration statements, even though there are explicit and implied rules in the coding standards and these are being consistently followed (mostly).Explicit rules:
PEAR.NamingConventions.ValidClassName
. Ref: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#naming-conventionsGeneric.Files.OneObjectStructurePerFile
. Ref: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#only-one-object-structure-class-interface-trait-per-fileRules I can discern based on code samples and applied practices in WP Core are as follows:
Generic.PHP.LowerCaseKeyword
sniff.Generic.Classes.OpeningBraceSameLine
sniffextends...
andimplements...
should be on the same line as theclass
keyword.Describe the solution you'd like
I would like to propose to add one or more sniffs to enforce the implied rules which are currently not being checked.
Analysis of available sniffs
PHPCS contains the following sniffs checking OO declaration statements:
PSR1.Classes.ClassDeclaration
PSR2.Classes.ClassDeclaration
PEAR.Classes.ClassDeclaration
Squiz.Classes.ClassDeclaration
PSR1.Classes.ClassDeclaration
The
PSR1
sniff is stand-alone and checks the following:Rule 1 applies, but we already check this via another sniff. Rule 2 does not apply to WP.
Conclusion: Nope, not a good match.
PEAR/PSR2/Squiz.Classes.ClassDeclaration
These three sniffs are closely related. The
PEAR
sniff acts as the base sniff, thePSR2
sniff extends on thePEAR
sniff, theSquiz
sniff extends thePSR2
sniff.The
PEAR
sniff largely only checks the open brace position with the expectation that it should be on the line after theclass
keyword. This contradicts the WP coding standards where we want to open brace on the same line as theclass
keyword.Conclusion: The
PEAR
sniff does not do enough + what it does is not what we want, so no matter what sniff we end up with, we will need to exclude one or more error codes related to the open brace.The
PSR2
sniff builds on thePEAR
sniff and adds additional checks for:extends
andimplements
keywords are on the same line as theclass
keyword.CloseBraceAfterBody
check would currently yield ~136 errors for WP Core, which also means that the vast majority of the classes in WP Core comply with the rule. As we also added a rule for the function close brace to directly follow the body, I think keeping theCloseBraceAfterBody
check should be acceptable and the current errors can be auto-fixed when WP Core upgrades.The sniff does allow for multi-line
extends
/implements
declarations, but that should not be problematic.Conclusion: The
PSR2
sniff is a good candidate to add and would only need an exclusion for the open brace check.The
Squiz
sniff builds on thePSR2
sniff and adds additional checks for:class
keyword is at the start of a line. This is problematic for conditional class declarations, which are being used in WP in themes and test files.Conclusion: The extras in the
Squiz
sniff would need to be excluded anyway, so thePSR2
sniff is a better fit.Proposal
Add the
PSR2
sniff with an exclusion only for the "open brace on new line" error.With the above code sample and excluding sniffs which we already include in the ruleset, this would yield the following errors for the code sample listed above:
The only thing I'm in doubt about is whether or not to also exclude the
FirstExtendsInterfaceSameLine
(and related) error code(s) ?Note: when run against WP Core, only the above mentioned ~136 errors for "blank line between structure body and close brace" are thrown. Other than that, WP Core already fully complies with this proposed addition.