Squiz.Arrays.ArrayDeclaration error for empty comma in shorthand list declaration
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
[
$a,
,
$b,
] = [
1,
2,
3,
];To reproduce Steps to reproduce the behavior:
- Create a file called
test.phpwith the code sample above... - Run
phpcs --standard=Squiz --sniffs=Squiz.Arrays.ArrayDeclaration test.php - See error message displayed
FOUND 1 ERROR AFFECTING 1 LINE ---------------------------------------------------------------------- 4 | ERROR | [x] Expected 0 spaces before comma; 4 found - Run
phpcbf --standard=Squiz --sniffs=Squiz.Arrays.ArrayDeclaration test.php - See formatting result
<?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