Improve/clarify the error message for the WordPress.Arrays.ArrayKeySpacingRestrictions.NoSpacesAroundArrayKeys sniff
Is your feature request related to a problem?
Suppose I have the following code:
$variable = 'foo';
$my_array["a_string_{$variable}"] = 'bar';
This results in the following error message:
ERROR | Array keys must be surrounded by spaces unless they contain a string or an integer. | (WordPress.Arrays.ArrayKeySpacingRestrictions.NoSpacesAroundArrayKeys)
Which is not technically correct, since the array key is a string...it just contains an interpolation.
Describe the solution you'd like
The error message would be more correct if it were something like:
ERROR | Array keys must be surrounded by spaces unless they contain an uninterpolated string or an integer. | (WordPress.Arrays.ArrayKeySpacingRestrictions.NoSpacesAroundArrayKeys)
@pbiron What about using "unless they contain a plain string or an integer" ? Want to send in a PR ?
Yeah, uninterpolated string is probably too "technical" :-) But plain string is not clear either. Let me think about it (and read the PHP manual to see how it describes uninterpolated strings) and see if I can come up with better wording.
And then I do a PR.
thanx.
"...single-quotable string..." ?
How about an integer or string without embedded variables?
"...single-quotable string..." ?
A double quoted string without interpolated variables is also fine. $a["mc'donald"]. This may give the wrong impression that single quotes are required.
How about
an integer or string without embedded variables?
Not sure how well that would read as integers cannot contain embedded variables.
Also, it's not just about embedded or interpolated variables. The rule also applies to concatenated keys or keys created via a calculation: $a[ 'a' . $text ] or $a[ 1 + $i ]
The manual describes it like so:
When referring to array items, only include a space around the index if it is a variable...
But the sniff was put in place largely as it is now and has been around since 2014, so we may also want to improve the text in the coding standards manual.
Also, it's not just about embedded or interpolated variables. The rule also applies to concatenated keys or keys created via a calculation:
$a[ 'a' . $text ]or$a[ 1 + $i ]
Ah...that I didn't know. Just as an FYI: I opened this issue after getting the error in a plugin I'm writing that uses WPCS (i.e., not part of a core patch).
The manual describes it like so:
When referring to array items, only include a space around the index if it is a variable...
But the sniff was put in place largely as it is now and has been around since 2014, so we may also want to improve the text in the coding standards manual.
That sounds like a good idea. At the very least, interpolated/concatenated strings and calculated ints should be added to the manual as incorrect examples.
This is probably out-of-scope (for this issue at least), but how much work would be involved to add links to the relevant section of the manual in error messages? A substantial amount I suspect. Just a thought.
Has anyone come up with better phrasing in the mean time ? Now would be the time to address this. If not, I suggest we close this issue.
Closing for lack of response.