WordPress-Coding-Standards: Faulty core fix: Array elements aren't aligned at the =>
Given this example:
<?php
array(
'aaa' => 'b',
'c' => 'd',
'eeeeeee' => 'f',
);
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.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 19 (19 by maintainers)
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.
@JDGrimes Having thought about this some more based on the questions you posed, I think removing part of the
Arrays.AssignmentOperatorSpacingsniff will in actual fact be the best option. I will also rename the sniff to something likeArrays.MultipleStatementAlignment(better name suggestions welcome).In the WIP branch, I was checking both spacing before as well as after the
=>array assignment operator, similar to what the upstreamSquiz.Arrays.ArrayDeclarationsniff does and the - now deprecated - WP version of that once did. For single line arrays, this meant: enforcing exactly one space before and after the double arrow. For multi-line arrays, enforcing alignment before the double arrow and one space after. (with some configuration options, but that’s besides the point)This is basically already covered by the
WhiteSpace.OperatorSpacingexcept for the alignment before the arrow. So, I think it’s best to narrow the focus of the sniff and only target the multi statement alignment for multi-line arrays. That would still yield an overlapping message when no space at all is found between an array index key and the double arrow, but would eliminate the other duplications.I will adjust the WIP branch and expect to be able to pull this tomorrow.
It actually is configurable (and looks like it always has been) using the
$maxPaddingproperty: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#genericformattingmultiplestatementalignmentI intend to implement a similar
$maxPaddingproperty for the WP Array assignment alignment sniff, so for those of us who want a little more pragmatism, it will be configurable.