Created on 19 April 2012, almost 13 years ago
Updated 23 July 2024, 9 months ago

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

  1. https://www.drupal.org/u/drunken-monkey β†’ (2024-06-22)
  2. https://www.drupal.org/u/kingdutch β†’ (2024-07-03)
  3. 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 β†’

(None)

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

  1. Add supporters
  2. Create a Change Record
  3. Review by the Coding Standards Committee
  4. Coding Standards Committee takes action as required
  5. Discussed by the Core Committer Committee, if it impacts Drupal Core
  6. Final review by Coding Standards Committee
  7. Documentation updates
    1. Edit all pages
    2. Publish change record
    3. Remove 'Needs documentation edits' tag
  8. If applicable, create follow-up issues for PHPCS rules/sniffs changes

For a full explanation of these steps see the Coding Standards project page β†’

πŸ“Œ Task
Status

Active

Component

Coding Standards

Created by

πŸ‡ΊπŸ‡ΈUnited States traviscarden

Live updates comments and jobs are added and updated live.
  • Coding standards

    It involves compliance with, or the content of coding standards. Requires broad community agreement.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Discussed 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.

  • πŸ‡¦πŸ‡ΉAustria drunken monkey Vienna, Austria

    I propose using the following syntax, and explicitly encouraging (or even requiring) [] over list():

    [$country, $state, $city] = $this->getPlaceInfo();
    [$country] = $this->getPlaceInfo();
    [, , $city] = $this->getPlaceInfo();
    ['city' => $city] = $this->getPlaceInfoAssociative();
    
  • πŸ‡³πŸ‡ΏNew Zealand quietone

    That 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.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    What about using stars?

    $stars = ['blue', 'yellow', 'red'];
    
    [$hottest, $hotter, $coldest] = $stars;
    [$hottest] = $stars;
    [, , $coldest] = $stars;
    
  • πŸ‡³πŸ‡±Netherlands kingdutch

    I'm happy to second quietone's example since it's understandable for people who may have never seen this syntax (you can see $stars is 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.

  • πŸ‡§πŸ‡ͺBelgium borisson_ Mechelen, πŸ‡§πŸ‡ͺ

    I fully agree with #11, I think that example makes sense.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Thanks. 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.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    I did some testing.

    No errors on these lines:

        list(, , $coldest) = $stars;
        [$hottest, $hotter, $coldest] = $stars;
        [, , $coldest] = $stars;

    [ $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(.*)'?

  • πŸ‡¬πŸ‡§United Kingdom catch

    Does this now need to cover the shorthand array destructuring syntax?

  • πŸ‡¦πŸ‡ΉAustria drunken monkey Vienna, Austria

    Thanks, @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?)

    Does this now need to cover the shorthand array destructuring syntax?

    Could you please elaborate, what syntax do you mean?

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Extra 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.

  • πŸ‡¬πŸ‡§United Kingdom catch

    I mean:

    $array = ['foo', 'bar'];
    
    [$foo, $bar] = $array;
    

    See https://stitcher.io/blog/array-destructuring-with-list-in-php

Production build 0.71.5 2024