Active
Project:
Coder
Version:
8.3.x-dev
Component:
Coder Sniffer
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
24 Jun 2024 at 11:12 UTC
Updated:
24 Jun 2024 at 14:26 UTC
Jump to comment: Most recent
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;
(see https://www.php.net/manual/en/language.types.array.php)
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 Drupal\Core\DependencyInjection\YamlFileLoader
[$provider, ] = explode('.', $basename, 2);
This is used in code outside of Drupal too, e.g. in Composer:
[$response, ] = $spec;
[$defaultUsername, ] = explode(':', $authParts, 2);
However, Coder fails this with two sniffs:
124 | ERROR | [x] Comma not allowed after last value in single-line array
| | declaration (Squiz.Arrays.ArrayDeclaration.CommaAfterLast)
124 | ERROR | [x] There should be no white space before a closing "]"
| | (Drupal.WhiteSpace.CloseBracketSpacing.ClosingWhitespace)
I've created an upstream issue for Squiz.Arrays.ArrayDeclaration.NoCommaAfterLast: https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/528
Comments
Comment #2
joachim commentedComment #3
joachim commentedI got a very quick response to my upstream issue:
> You could consider excluding the Squiz.Arrays.ArrayDeclaration.CommaAfterLast error code and using the PHPCSExtra NormalizedArrays.Arrays.CommaAfterLast sniff to replace it. That sniff does ignore short lists correctly.