Problem/Motivation
The PHP Manual seems to imply that when the list() function skips an array value the corresponding position should be occupied by a single space and a comma. It gives this example:
$info = array('coffee', 'brown', 'caffeine');
// Let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!\n";
This might be seen as conflicting with our policy that opening parentheses should NOT be followed by any spaces. Can we define an explicit policy and add it to the documentation? Thanks!
Benefits
If we adopted this change, the Drupal Project would benefit by ...
Three supporters required
- https://www.drupal.org/u/drunken-monkey (2024-06-22)
- https://www.drupal.org/u/kingdutch (2024-07-03)
- https://www.drupal.org/u/{userid} (yyyy-mm-dd they added support)
Proposed changes
Provide all proposed changes to the Drupal Coding standards. Give a link to each section that will be changed, and show the current text and proposed text as in the following layout:
1. New subsection “Array unpacking” under PHP coding standards » Arrays
Current text
(None)
Proposed text
To assign variables to an array, use the
[…] =syntax. When skipping the first entry, there should be no space between the opening bracket[and the comma,.Examples:
$stars = ['blue', 'yellow', 'red']; [$hottest, $hotter, $coldest] = $stars; [$hottest] = $stars; [, , $coldest] = $stars; [1 => $hotter] = $stars;
Remaining tasks
Create this issue in the Coding Standards queue, using the defined template- Add supporters
- Create a Change Record
- Review by the Coding Standards Committee
- Coding Standards Committee takes action as required
- Discussed by the Core Committer Committee, if it impacts Drupal Core
- Final review by Coding Standards Committee
- Documentation updates
- Edit all pages
- Publish change record
- Remove 'Needs documentation edits' tag
- If applicable, create follow-up issues for PHPCS rules/sniffs changes
For a full explanation of these steps see the Coding Standards project page
Comments
Comment #1
jhodgdontagging
Comment #2
tim.plunkettHere are a couple examples from core:
modules/field/field.attach.inc:191: list(, , $bundle) = entity_extract_ids($entity_type, $entity);modules/field/field.form.inc:15: list($id, , ) = entity_extract_ids($entity_type, $entity);modules/field/field.module:911: list($id) = entity_extract_ids($entity_type, $entity);I think #3 is definitely wrong as well.
Comment #3
chx commentedNope, it's not wrong. Compare 1 to 3. But, I think 2 needs those commas removed for consistency. As for the issue itself, I disagree. There is nothing in PHP manual that would talk about spaces. There is no need for list parenthesis space comma -- list parenthesis comma is just fine.
Comment #4
tim.plunkettThen that can be our documented standard. This issue title only says we have to have one, not that we have to follow what is suggested in the OP.
And if
list($id)is preferred overlist($id, , ), then we should document that too.Comment #4.0
traviscarden commentedSoftened assertion. :-P
Comment #5
jhodgdonCoding standards decisions are now supposed to be made by the TWG
Comment #6
tizzo commentedMoving this issue to the Coding Standards queue per the new workflow defined in #2428153: Create and document a process for updating coding standards.
Comment #7
quietone commentedDiscussed at #3456119: Coding Standards Meeting Tuesday 2024-06-18 2100 UTC and there was agreement that this is something to pursue. I have updated the IS with the new template.
Comment #8
drunken monkeyI propose using the following syntax, and explicitly encouraging (or even requiring)
[]overlist():Comment #9
quietone commentedThat looks like what needs to be on the left side of the assignment. But I think we should have a simpler example, the current suggestion expects the reader to know what
$this->getPlaceInfo()and$this->getPlaceInfoAssociative(). And I would like to have an example that is inclusive regardless of where people live or work.Comment #10
quietone commentedWhat about using stars?
Comment #11
kingdutchI'm happy to second quietone's example since it's understandable for people who may have never seen this syntax (you can see
$starsis an array).I initially wasn't sure about not have a space before the first command (proposed
[, , $foo]vs my brain's[ , , $foo]) but after writing them out and trying to change them, the proposed spaceless first comma is closest to how it would look if there was a variable, since in that case there would be a variable there and not space. So you can go from[, , $foo]to[$bar, , $foo]without having to remove any characters. That makes sense to me.Comment #12
borisson_I fully agree with #11, I think that example makes sense.
Comment #13
quietone commentedThanks. I updated the Issue summary with the 'stars' example. I also modified the text a) so it only shows what to do b) uses similar language to the php manual page for lists and c) remove parentheses around characters to be consistent with the coding standards page.
Comment #14
quietone commentedI did some testing.
No errors on these lines:
Errors on these lines
[ $hottest] = $stars;ERROR | [x] There should be no white space after an opening "["[ , , $coldest ] = $stars;ERROR | [x] There should be no white space after an opening "[" and ERROR | [x] There should be no white space before a closing "]"So, perhaps the sniff just needs to detect 'list(.*)'?
Comment #15
kingdutchDated as per #1539738-11: list() syntax
Comment #16
catchDoes this now need to cover the shorthand array destructuring syntax?
Comment #17
drunken monkeyThanks, @quietone, I agree that doing this without an invented method makes more sense.
The new example just has the slight downside that it doesn’t include an example for destructuring an associative array.
Do you think just adding
[1 => $hotter] = $stars;would help, maybe preceded by a comment explaining that this is how you’d destructure an associative array? Or do we add a second, associative array? (Or is the majority against adding an example for associative destructuring?)Could you please elaborate, what syntax do you mean?
Comment #18
quietone commentedExtra examples are usually beneficial so I added that. I don't think it necessary to do more because the style and spacing doesn't change.
Comment #19
catch@drunken_monkey:
I mean:
See https://stitcher.io/blog/array-destructuring-with-list-in-php