Squiz.Arrays.ArrayDeclaration error for empty comma in shorthand list declaration

Author: tscniCreated Jun 9, 2021Updated Mar 5, 2026

Describe the bug Squiz.Arrays.ArrayDeclaration also handles shorthand list declarations. (maybe that's an issue by itself?) As part of that it considers an empty comma on its own line (meaning a skipped value) an error.

Code sample

php
<?php
[
    $a,
    ,
    $b,
] = [
    1,
    2,
    3,
];

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
    FOUND 1 ERROR AFFECTING 1 LINE
    ----------------------------------------------------------------------
     4 | ERROR | [x] Expected 0 spaces before comma; 4 found
  4. Run phpcbf --standard=Squiz --sniffs=Squiz.Arrays.ArrayDeclaration test.php
  5. See formatting result
    php
    <?php
    [
        $a,,
        $b,
    ] = [
        1,
        2,
        3,
    ];

Expected behavior No error, no formatting change

Versions (please complete the following information):

  • PHP: 7.4
  • PHPCS: 3.6.0
  • Standard: Squiz

Possible fix As an empty comma is only possible in keyless list declarations, a simple way to fix this might be to skip SpaceBeforeComma, when the previous token is T_COMMA or T_OPEN_SHORT_ARRAY, here: https://github.com/squizlabs/PHP_CodeSniffer/blob/d2574b9185cdde7185132647d0c9121da3e59f90/src/Standards/Squiz/Sniffs/Arrays/ArrayDeclarationSniff.php#L439-L443

Source: squizlabs/PHP_CodeSniffer