-
-
Notifications
You must be signed in to change notification settings - Fork 492
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
Function declaration verification #1101
Comments
I added the visibility indicators sniff to my custom ruleset some time ago. These other related sniffs look good too.
I would say that the first three definitely ought to be, IMO. For the modifier order check, on the other hand, I'm not so sure. I agree that
I agree with the suggested rules for multiline function declarations.
I've always favored one space (#439 (comment)), so I'd be fine with requiring that and using an upstream sniff. But this might disagree with others' preferred style, and seems to be different than the JS standards, FWIW. |
+1 to
I can see both sides (no space or one space), so I'm not too fussed which is chosen, so long as one is chosen and added to the Handbook, with the following factors (applies to most code standards discussions):
Saying that, since closures have been around for considerably longer than some of the PHP 7 syntaxes, I could imagine that many companies have plugins and themes that already favour no space or one space for closures. While we might agree and document to have one or the other, and set it as a default, I could imagine that those companies would like the ability to be able to choose the other. This is not the same as WPCS not checking / expressing a preference though. |
With regard to anonymous functions, I just ran across the new class( $var ) {}; // OK.
new class ( $var ) {}; // Flagged. My instinct here again was to have a space (which is why it got flagged by WPCS). Might be worth considering consistency with anonymous classes when deciding what to do with anonymous functions. |
PSR-12 does seem to favour having a space, just like the |
Proposal 1 is being further investigated and discussed in issue #1474 |
…ose brace > 1. There should be no blank line between the content of a function and the function’s closing brace. Includes removing this rule from the WPCS native ruleset in which we already enforced it (as it will now be inherited from the `WordPress` ruleset). Refs: * https://make.wordpress.org/core/2020/03/20/updating-the-coding-standards-for-modern-php/ - Function closing brace section Loosely related to WordPress#1101
…ose brace > 1. There should be no blank line between the content of a function and the function’s closing brace. Includes removing this rule from the WPCS native ruleset in which we already enforced it (as it will now be inherited from the `WordPress` ruleset). Refs: * https://make.wordpress.org/core/2020/03/20/updating-the-coding-standards-for-modern-php/ - Function closing brace section Loosely related to WordPress#1101
…ose brace > 1. There should be no blank line between the content of a function and the function’s closing brace. Includes removing this rule from the WPCS native ruleset in which we already enforced it (as it will now be inherited from the `WordPress` ruleset). Refs: * https://make.wordpress.org/core/2020/03/20/updating-the-coding-standards-for-modern-php/ - Function closing brace section Loosely related to WordPress#1101
…ose brace > 1. There should be no blank line between the content of a function and the function’s closing brace. Includes removing this rule from the WPCS native ruleset in which we already enforced it (as it will now be inherited from the `WordPress` ruleset). Refs: * https://make.wordpress.org/core/2020/03/20/updating-the-coding-standards-for-modern-php/ - Function closing brace section Loosely related to WordPress#1101
…ose brace > 1. There should be no blank line between the content of a function and the function’s closing brace. Includes removing this rule from the WPCS native ruleset in which we already enforced it (as it will now be inherited from the `WordPress` ruleset). Refs: * https://make.wordpress.org/core/2020/03/20/updating-the-coding-standards-for-modern-php/ - Function closing brace section Loosely related to WordPress#1101
…ose brace > 1. There should be no blank line between the content of a function and the function’s closing brace. Includes removing this rule from the WPCS native ruleset in which we already enforced it (as it will now be inherited from the `WordPress` ruleset). Refs: * https://make.wordpress.org/core/2020/03/20/updating-the-coding-standards-for-modern-php/ - Function closing brace section Loosely related to WordPress#1101
…ose brace > 1. There should be no blank line between the content of a function and the function’s closing brace. Includes removing this rule from the WPCS native ruleset in which we already enforced it (as it will now be inherited from the `WordPress` ruleset). Refs: * https://make.wordpress.org/core/2020/03/20/updating-the-coding-standards-for-modern-php/ - Function closing brace section * https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#remove-trailing-spaces * WordPress/wpcs-docs#110 Loosely related to #1101
Following up on my remark here: #1084 (comment)
Inventarisation
Function declaration verification basically can be broken down into a number of separate issues/checks:
function
/use
keywords.Status: Problematic. Currently covered by the
WordPress.WhiteSpace.ControlStructureSpacing
sniff, but this is leading to issues.Viable upstream sniffs:
PEAR.Functions.FunctionDeclaration
,Squiz.Functions.FunctionDeclaration
andSquiz.Functions.MultiLineFunctionDeclaration
use
statement over multiple lines.Status: Currently not covered.
Viable upstream sniffs:
Squiz.Functions.MultiLineFunctionDeclaration
Status: Covered in
WordPress-Core
Sniff:
Squiz.Functions.FunctionDeclarationArgumentSpacing
use
statement and the comma's separating these.Status: Covered in
WordPress-Core
Sniff:
Squiz.Functions.FunctionDeclarationArgumentSpacing
Status: Covered in
WordPress-Core
Sniff:
Squiz.Functions.FunctionDeclarationArgumentSpacing
Status: Open PR Add ReturnType Sniff #1084
Status: Covered in
Wordpress-Core
Sniff:
Generic.Functions.OpeningFunctionBraceKernighanRitchie
Status: Partially covered in
Wordpress-Core
. If the closing brace is the first non whitespace token of a new line, the indent is checked. That it should be on a new line, is, however, currently NOT checked.Sniff:
Generic.WhiteSpace.ScopeIndent
Viable upstream sniffs:
PEAR.WhiteSpace.ScopeClosingBrace
orSquiz.WhiteSpace.ScopeClosingBrace
Status: Currently not covered.
Viable upstream sniffs:
Squiz.Scope.MethodScope
Related issue: whether properties in a class have visibility declared is also not covered and could be covered by the
Squiz.Scope.MemberVarScope
sniff.public
,final
etc.Status: Currently not covered.
Viable upstream sniffs:
Squiz.WhiteSpace.ScopeKeywordSpacing
Status: Currently not covered.
Viable upstream sniffs:
PSR2.Methods.MethodDeclaration
enforces "final abstract visibility static" order.Proposal 1 - check 8:
Add either the
PEAR.WhiteSpace.ScopeClosingBrace
or theSquiz.WhiteSpace.ScopeClosingBrace
sniff toWordPress-Core
.I'd need to run some tests with these sniffs to see which one would suit best. The logic is different in both and the PEAR sniff has some additional differentiation for the scope closer indentation when
case
/default
statements are "closed" by abreak
/return
statement.I don't expect either to cause fixer conflicts, but would like to verify this.
Proposal 2 - check 9 to 11:
Squiz.Scope.MethodScope
sniff toWordPress-Extra
. The sniff throws anerror
for missing method visibility indicators.Squiz.Scope.MemberVarScope
sniff toWordPress-Extra
. The sniff throws anerror
for missing property visibility indicators.Squiz.WhiteSpace.ScopeKeywordSpacing
sniff toWordPress-Extra
. Theerror
thrown by this sniff is auto-fixable.PSR2.Methods.MethodDeclaration
sniff toWordPress-Extra
. Theerror
thrown by this sniff is auto-fixable.This sniff also throws a
warning
when method names are prefixed by a single underscore (PHP4 style visibility indicator). If so desired, that warning can be excluded. Personally, I'd like to keep it as IMHO the warning has merit.Any of these errors can, of course, be downgraded to
warning
s if so desired.I would welcome a discussion on whether any of the rules covered by the above mentioned sniffs should be added to/made explicit in the WP Core handbook. /cc @pento @ntwb
To discuss - check 1 + 3:
I've been looking into what upstream sniffs are available to handle this and if these would be suitable for use in WPCS.
The sniff would basically need to check:
function
keyword should be followed by one space with the exception of closures. For closures whether this should be checked and if so whether a space should be enforced was previously determined based on a public property.use
keyword should be both preceded and followed by one space.Optionally:
There are three upstream sniffs which each in their own way cover this:
PEAR.Functions.FunctionDeclaration
,Squiz.Functions.FunctionDeclaration
andSquiz.Functions.MultiLineFunctionDeclaration
.The
Squiz.Functions.FunctionDeclaration
does not allow for closures nor return type declarations, so IMHO that's not really a useful option.The
Squiz.Functions.MultiLineFunctionDeclaration
sniff extends thePEAR.Functions.FunctionDeclaration
sniff. They mainly differ in how they sniff multi-line function declarations.The PEAR sniff (and by extending it the Squiz sniff) also checks the brace placement for single line declarations in a way which conflicts with the
Generic.Functions.OpeningFunctionBraceKernighanRitchie
sniff.That part can, however, easily be disabled by extending the upstream sniff and just overloading the
processSingleLineDeclaration()
method.AFAICS the space after closing parenthesis is not checked by these sniffs, but the
Generic.Functions.OpeningFunctionBraceKernighanRitchie
sniff combined with the "one space beforeuse
keyword" checks should cover all applicable cases.The one real problem I see with using either of the above sniffs, is that the "space between
function
keyword and open parenthesis" for closures is NOT configurable and is enforced to be one space in both sniffs.❓ Have we reached a point in time yet where we are willing to take a hard decision on this ? Enforce no or one space between the
function
keyword and the open parenthesis for closures ?Depending on that decision, we'd either need to write our own sniff or can use one of the upstream ones.
If we'd use one of the upstream ones, we could also benefit from the multi-line checks, though I imagine some discussion on whether any specific formatting for multi-line should be enforced and if so, which, will need to had before that time.
Once a decision regarding this has been taken and a replacement sniff has been pulled, the function specific code can be removed from the
WordPress.WhiteSpace.ControlStructureSpacing
sniff and the exclusion of theSquiz.Functions.FunctionDeclarationArgumentSpacing.SpacingBeforeClose
error code should be removed from theWordPress-Core
ruleset (was added to prevent duplicate messages being thrown).@WordPress-Coding-Standards/wpcs-collaborators @pento @ntwb Opinions ?
The text was updated successfully, but these errors were encountered: