Skip to content

Destructuring syntax (list() and []) #119

Description

@j-willette

I want to report a case that does not seem to be explicitly covered by PSR-12.
It concerns variable destructuring using the list() and [] syntaxes.

I have this code:

[$val1,$val2] = explode(',', $string);
list($val1,$val2) = explode(',', $string);

And I do not get any error when running the command:

phpcs --standard=PSR12 my-file.php

In my opinion, there should be a space required after the comma following $val1, as in function calls or arrays.

Correction:

[$val1, $val2] = explode(',', $string);
list($val1, $val2) = explode(',', $string);

Activity

  1. Crell commented on Jul 1, 2025

    @Crell
    Collaborator

    I'm pretty sure a space is required after any comma. Sounds like something you should take up with php-cs.

  2. jrfnl commented on Jul 1, 2025

    @jrfnl
    Contributor

    PHPCS does exactly what is expected: follow PSR12. PSR12 has no rules about array or list formatting, so PHPCS doesn't enforce any.

    And even PER in the current master has no rules about list formatting and has no explicit rule about spacing after a comma for arrays (other than single-line, multi-line).

  3. Jean85 commented on Jul 1, 2025

    @Jean85
    Member

    Surely, it would make sense to have spaces, since 4.7 says:

    In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma.

    Arguably, these are not function calls, it's arrays and language constructs, so we may need an amendment to explicitly say that the same rule applies there too.

  4. KorvinSzanto commented on Jul 1, 2025

    @KorvinSzanto
    Contributor

    While we don't have specific cases for destructuring, it stands to reason that language constructs like list(...) = would follow the function call rules and [...] = would follow the array declaration rules.

    Easy enough to add clarification in a future version but to me there isn't really much ambiguity here and we all know exactly the syntax that would be codified.

  5. Crell commented on Jul 1, 2025

    @Crell
    Collaborator

    Since we're about to tag 3.0, if we want to clarify this further, it sounds like a reasonable 3.1 target.

  6. jrfnl commented on Jul 1, 2025

    @jrfnl
    Contributor

    Happy to see this addressed/made explicit in PER 3.1.

  7. j-willette commented on Jul 2, 2025

    @j-willette
    Author

    but to me there isn't really much ambiguity here and we all know exactly the syntax that would be codified.

    Agreed, but tools like PHPCS also need to be aware of it.

    Similarly, this code doesn't raise any PHPCS error:

    $array = ['val1','val2','val3'];

  8. added this to the PER-CS 3.1 milestone on Nov 10, 2025
  9. Crell commented on May 2, 2026

    @Crell
    Collaborator

    Does anyone want to make a PR for this before we close out 3.1? (Which I am late on doing, I know.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions