Skip to content

Terminal comma to indicate discarded items in array destructuring fails Squiz.Arrays.ArrayDeclaration.CommaAfterLast #528

Open
@joachim-n

Description

@joachim-n

Describe the bug

Array destructuring is a syntax in PHP where items of an array are assigned to single variables, using an array construct on the left of the assignment, e.g.

[$foo, $bar, $baz] = $source_array;

It's a common pattern to include a terminal comma to indicate that we know not all of the array's elements are being assigned.

E.g. in Composer:

[$response, ] = $spec;
                    [$defaultUsername, ] = explode(':', $authParts, 2);

However, this fails the Squiz.Arrays.ArrayDeclaration.CommaAfterLast sniff:

124 | ERROR | [x] Comma not allowed after last value in single-line array
     |       |     declaration (Squiz.Arrays.ArrayDeclaration.CommaAfterLast)

Code sample

<?php

[$alpha, ] = explode('/', 'alpha/beta');

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs --standard=Squiz --sniffs=Squiz.Arrays.ArrayDeclaration test.php
  3. See error message displayed
 3 | ERROR | [x] Comma not allowed after last value in single-line array declaration

Expected behavior

A clear and concise description of what you expected to happen.

Versions (please complete the following information)

Operating System MacOS 10.15
PHP version 8.3
PHP_CodeSniffer version 3.10.1
Standard Squiz
Install type Composer

Additional context

Add any other context about the problem here.

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • I have verified the issue still exists in the master branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions