-
-
Notifications
You must be signed in to change notification settings - Fork 480
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 fix: Array elements aren't aligned at the => #1122
Comments
Discussion over on the Core ticket: https://core.trac.wordpress.org/ticket/41057 |
Additional notes:
|
It looks to me like it was already commented out prior to that: https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/pull/378/files#diff-8eb86ff485de5031b1b1644112295065L386 And this goes all the way back to the initial commit: Or is there another alignment check that I missed? |
@JDGrimes I see what you mean. Seems I was looking in the wrong place (assignment operators in single line arrays which was not previously commented out): https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/blob/04fd547c691ca2baae3fa8e195a46b0c9dd671c5/Sniffs/Arrays/ArrayDeclarationSniff.php#L117-L145 |
I personally do not think alignment is a good practice to recommend. Adding or removing an item from the array then means all of the items in the array will need to get re-aligned, resulting in messy diffs and conflicting patches. It's also a direct contradiction of the subsequent point in the coding standards, about use of a comma after the last item in an array:
|
That could be partially accounted for by an |
The array might re-aligned, depending on whether you're adding or removing the longest key. I also find myself less and less concerned about the cleanliness of a diff, vs. the cleanliness of the actual code - I look at diffs and revision history that would be affected by this occasionally, I work with the actual code all day, every day. It seems like a reasonable trade off to make the common case nicer. |
To paraphrase - making the resulting code easier to read for everyone >>> making the diff cleaner for the few people who need to read the commit / chase down blames. |
I've been working on this sniff and have a couple of questions:
|
I think the main issue I had with the sniff was that it always aligned assignments that were on consecutive lines, regardless of the difference in length between the variable names. I usually only align the assignments if they are within like 5-10 spaces. Sometimes when the difference is greater than that aligning just doesn't make sense (depending on the values being assigned), and can actually decrease readability. I don't think this was configurable, but I might be remembering wrong. Of course, for array alignment, I tend to not worry about how many spaces need to be used, since it is all part of a single statement. Aligning in an array is almost always more readable. Though I will admit that I'm sometimes pragmatic about alignment even then. |
It actually is configurable (and looks like it always has been) using the
I intend to implement a similar |
On a different note: Based on what I have so far, the sniff would have some overlap with the
That would currently result in slightly conflicting error messages addressing the same issue:
Options:
Opinions ? |
I think these are really independent checks that someone might decide to use one without the other. So I don't think that either of them should be removed. Perhaps the ruleset should simply silence |
The problem with that is that the upstream sniff handles the whitespace checking for a large number of operators and there is no difference in the error codes, so we can't silence it only for the double arrow assignment operator. |
Ah, right. Maybe just remove the double arrow from the |
@JDGrimes Having thought about this some more based on the questions you posed, I think removing part of the In the WIP branch, I was checking both spacing before as well as after the This is basically already covered by the I will adjust the WIP branch and expect to be able to pull this tomorrow. |
Ok, so I've pulled two branches now:
One important thing to note about these PRs: The
I've made the choice to implement a An array however is never "broken" up like that, so I've let consistency prevail. Regarding the fact that only blank lines, comments and non-assigment statements break a group of assignments in the $code = '
<?php
$s = 8;
echo /* WP_I18N_GUGU*/ "yes" /* /WP_I18N_UGU */;
if ($x == "18181") { wp_die(sprintf(/*WP_I18N_DIE*/\'We died %d times!\'/*WP_I18N_DIE*/)); }
?>';
$tokens = token_get_all( $code ); This has to do with it There is currently no option in the sniff to break the grouping if there are "more than x lines" within in an assignment statement, which when an assignment is an array or a closure could be desirable. Opinions welcome. |
I don't think it would, or necessarily should, get much use - that feels like it's introducing an exception. If someone doesn't want an alignment (as I wouldn't if my own code contained this), then they can add a blank line. |
This must be why I stopped using this sniff before. IMO this makes code far less readable, rather than more readable. That said, as @GaryJones pointed out, you can just add a blank line to overcome this, so an option isn't really needed. I tend to add blank lines to my code liberally anyway, so I probably could live with it. In fact, I'd prefer if blank lines were added for most of the fixes in WPCS for multiline assignments rather than padding being added. |
Given this example:
The array elements aren't being aligned at the
=>
. This behaviour isn't explicitly noted in the handbook, just recommended. I can make it more explicit in the Hanbook, thought.The text was updated successfully, but these errors were encountered: