-
-
Notifications
You must be signed in to change notification settings - Fork 483
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
Faulty core fixes: switch/case indentation #982
Comments
Now the standard for The Error messages in the sniff:
Aside from the The case/default body must start on the line following the statement message, I think they would all be good additions to WPCS. I propose adding this sniff to The Note: Adding this sniff would not (yet) solve the docblock/escaping out of PHP indentation issues. Those are probably issues with the The alternative would be to add a WPCS native sniff to only check the indentation of Opinions ? /cc @pento |
FYI: I've also opened an upstream issue regarding the docblock/inline HTML indentation: squizlabs/PHP_CodeSniffer#1539 |
Do these two not contradict each other? |
No they don't, here's some examples: case 'a':
// ^ ^
// 1 2
case'b': // is a violation of `SpacingAfterCase`
case 'c' : // is a violation of `SpaceBeforeColon`
case'd' : // is a violation of both `SpacingAfterCase` and `SpaceBeforeColon` |
@GaryJones No, they don't - between the switch ( $a ) {
case 'b':
// One space between `case` and `'b'`.
// No space between `'b'` and `:`.
break;
} |
@ntwb Ah, sorry, I didn't see your comment, should have refreshed the page before replying. |
Ah, of course - I was forgetting the value. I apparently don't use I've got no objection to these being pushed through |
Ok, so I've dug into this a bit deeper. Current practices used in CoreI figured it might make sense to actually see what's currently used in Core to decide which rules to adopt anyhow. The The results were both interesting as well as surprising in some aspects:
Updated proposalBased on this, I believe we should add the sniff:
and exclude:
Handbook updates neededBased on the rules I mentioned above and what's already covered in a different way by WPCS (and therefore already in the handbook), I believe the only things which would still need to be added to the handbook would be:
Oh - just noticed: the new example which was added to the handbook for the Detailed look at some of the results of the sniffWrongOpener
I've opened a ticket in trac with a patch for these: https://core.trac.wordpress.org/ticket/41234 TerminatingCommentI've also looked at the Side-note: Adding this sniff and making these rules explicit in the handbook, funnily enough fills a void already pointed out in 2013 in some comments on the handbook https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#comment-9283 and https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#comment-9984 /cc @JDGrimes |
I'm a little surprised at the space before colon stats, I thought there would be more cases where spaces have been used for alignment: Here's some examples:
case 'AUTH_KEY' :
case 'SECURE_AUTH_KEY' :
case 'LOGGED_IN_KEY' :
case 'NONCE_KEY' :
case 'AUTH_SALT' :
case 'SECURE_AUTH_SALT' :
case 'LOGGED_IN_SALT' :
case 'NONCE_SALT' :
case 'delete' :
case 'approve' :
case 'trash' :
case 'spam' :
case 'deletecomment' :
case 'trashcomment' :
case 'untrashcomment' :
case 'spamcomment' :
case 'unspamcomment' :
case 'approvecomment' :
case 'unapprovecomment' : |
Done 👍 |
@WordPress-Coding-Standards/wpcs-collaborators I'd like some opinions on the proposed additions to the handbook. Once that's been decided on, the new sniff could be added - with or without exclusions - to fix the issue. |
Yes.
Yes.
Just to clarify, this would only apply when there's a body to the case statement? A series of empty cases wouldn't require a comment for each. |
Correct. // This would be fine.
switch ( $foo ) {
case 'a':
case 'b':
break;
}
// This would be fine too.
switch ( $foo ) {
case 'a':
case 'b':
return;
}
// This would require a comment for `case 'a'`.
switch ( $foo ) {
case 'a':
echo $foo;
case 'b':
return;
} |
Okay, they've all been added to the handbook. |
…` ruleset As proposed in #982 (comment) Also adds the newly added section + rules from the handbook which came out of the discussion in #982. Partially fixes 982
Thanks @pento. I've pulled the rule addition now as PR #1021. Note: This doesn't completely fix the issue yet. There is an open issue upstream regarding the docblock/inline HTML indentation: squizlabs/PHP_CodeSniffer#1539 |
I fully agree with the other two additions to the handbook, but I would note that personally I prefer to add a blank line at the start of any control structure block that is more than one or two lines long. I find this more readable. But I do realize that core usually doesn't follow this pattern, so I'll understand if core is going to standardize on no blank lines at the start of control structures. (And even I usually omit the blank line at the start of a |
Same here, especially as a dyslectic. Whitespace can make a lot of difference in code readability. |
Hrrm, that's an excellent point, it can improve readability. It's really not used very often in Core, though. Given variations of the following command, here are the stats I got.
It's not at all common, though it does get slight more use as blocks get longer, which is to be expected. I'm not really excited about any sort of hard rule depending on number of lines, though. Perhaps the best option is to soften the language a bit. The first line of a new code block should not be blank, unless it clearly improves readability. The exception to this is the block inside a |
That's open to wildly different interpretation, and that's not what we should be encouraging in the Handbook IMO. |
Practical point: It's a discussion which is unrelated to this particular issue (switch layout not being fixed correctly) - and as far as I can see at this moment - not one which is currently causing fixer conflicts -, so we can leave deciding upon whether to have a soft/hard rule about blank lines and if so, the wording and the sniffs we'd need, for a later point in time. |
Okay, I missed that it wasn't required for this fix. 🙂 I've removed the Blank Lines section from the handbook, pending further discussion. |
…` ruleset As proposed in #982 (comment) Also adds the newly added section + rules from the handbook which came out of the discussion in #982. Partially fixes 982
@pento Sounds good to me. I've updated the open PR to reflect this now. |
Reopened for the scope indent parts of the original report - the auto-close wasn't supposed to get triggered by the PR. |
Just an observation: the rule of From the readability standpoint, I am in favor of spacing and empty lines to make the code much more easy to follow. Consistent rules make the code more easy to understand/implement/extend. In this case, a more consistent rule would mean to treat the colon in the same way in all the contexts, either when it's used in an alternative syntax for control structures, or as a ternary operator. |
The comment indent part will be fixed by the upcoming That leaves the PHP open/close tags and associated indentation. This has been discussed in other issues. That concludes this issue as far as I'm concerned. |
Summary:
There are a number of places in WP core which use - very large -
switch
/case
constructs which are incorrectly indented.For these, the current fixer seems to create a worse inconsistency in the indentations for:
break
- indentation not correctedMost likely cause:
Generic.WhiteSpace.ScopeIndent
I've seen other bugs with this sniff before and it may actually be simpler to pre-indent the
case
statement manually before runningphpcbf
than to fix this sniff.Alternatively, turning that particular sniff off for the fixer might also save some grief.
Example:
File: various files across the board
src/wp-admin/comment.php
to name just one,src/wp-admin/edit-tags.php
for another.Example snippet:
Fixed as:
Expected fix:
The text was updated successfully, but these errors were encountered: