-
-
Notifications
You must be signed in to change notification settings - Fork 495
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
Proposal: Add rules about formatting of the spread operator #1762
Comments
Should we put in this sniff the check, in the case of multiple arguments used with the spread operator, that the variadic argument is always placed on the last place? // Correct.
function foo( $required, $optional = null, ...$variadic ) {
// Code goes here
}
// Incorrect. Will output a fatal error.
function foo( $required, ...$variadic, $optional = null ) {
// Code goes here
} |
Good point. In my opinion, no, this should not be covered here. This topic is about the formatting of the spread operator, i.e. whitespace and such. Parse errors are covered by the If we would want to check the order of parameters, I think a more generic Such a sniff could then check:
If such a sniff would be desired, a separate issue should be opened about it and we'd need to check if anything of the above is already covered and if there are any upstream sniffs available we could use for this. |
I agree with rules 1, 2, and 3. 🎉 |
FYI: The upstream PR for rule 2 has been merged and a sniff for rule 3 has also been added. Both will be included in PHPCS 3.5.0. |
FYI: PHPCS 3.5.0 has been released. |
FYI: Based on the PHPCS 3.5.0 changelog, rule 3 is covered via the
Similar for rule 2, though only in the case of function declarations, so adding the PHPCS 3.5.0
Src: https://github.com/squizlabs/PHP_CodeSniffer/releases/tag/3.5.0 |
@dingo-d The Note: that sniff is currently not included in any of the WPCS rulesets. |
Is your feature request related to a problem?
PHP 5.6 introduced the spread operator
...
for packing arguments in function declarations (variadic functions) and unpacking them in function calls.PHP 7.4 will introduce unpacking of arrays using the spread operator.
Refs:
At this moment WPCS has no formatting rules for the spread operator.
Describe the solution you'd like
I would like to propose adding the following rules regarding the spread operator to the WP Coding Standards handbook:
If this proposal is accepted, we can then add sniff(s) to WPCS to check these rules automatically and - except for issues with comments - auto-fix them.
Notes for the WPCS Implementation:
For the function declaration part, PSR-12 demands the same - no space between the spread operator and the variable/function call -. For usage within array declarations and function calls, PSR-12 has no explicit opinion.
With that in mind, I have pulled a comprehensive sniff to PHPCS itself which - once merged - could be used to cover this rule.
See: New Generic.WhiteSpace.SpreadOperatorSpacingAfter sniff (PSR12 related) squizlabs/PHP_CodeSniffer#2548
/cc @pento
The text was updated successfully, but these errors were encountered: