PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

`Squiz.Arrays.ArrayDeclaration.NoComma` adds invalid comma to certain `match` statements since `3.10.1`

Open janedbal opened this issue 1 year ago • 3 comments

Describe the bug

Mentioned fixable sniff breaks the PHP code as follows:

Code sample

match ($foo) {
    'a' => [
        'x' => 0,
        'y' => 0,
    ],
    default => 0
};

Custom ruleset

<?xml version="1.0"?>
<ruleset>
    <rule ref="Squiz.Arrays.ArrayDeclaration.NoComma"/>
</ruleset>

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above
  2. Create phpcs.xml with the custom ruleset above
  3. Run phpcbf test.php
  4. See fix applied
match ($foo) {
    'a' => [
        'x' => 0,
        'y' => 0,
    ,],
    default => 0
};

Expected behavior

No file changes.

Versions

Operating System ubuntu
PHP version PHP 8.3.13
PHP_CodeSniffer version 3.10.1
Standard Squiz
Install type composer

Additional context

Version 3.10.0 does not contains this issue.

Please confirm

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

janedbal avatar Nov 28 '24 15:11 janedbal

@janedbal Thanks for reporting this.

The Squiz.Arrays.ArrayDeclaration sniff is known to be problematic and has been for years.

The problem is largely that as soon as something is excluded from that sniff, the sniff is broken. This is a design flaw in the sniff and applies to your situation.

I've been building up a (highly configurable) NormalizedArrays standard in PHPCSExtra to replace it, but that's not complete yet. Would be lovely if I could find some time to finish that at some point.

With that in mind, I will not be working on this issue. If someone would submit a PR with a small/simple fix for this issue, I will accept it, but other than that, this sniff is out of bounds for large fixes/rewrites as it is just not worth the time.

jrfnl avatar Nov 28 '24 15:11 jrfnl

@janedbal I've just checked, but if all you are using of the sniff is the Squiz.Arrays.ArrayDeclaration.NoComma error code, I suggest switching to the PHPCSExtra NormalizedArrays.Arrays.CommaAfterLast sniff, which already handles this correctly.

jrfnl avatar Nov 30 '24 13:11 jrfnl

We were actually using more from that, this was just the most simple example to show the bug. But since it is buggy and wont be maintained, we will disable it completely. Thanks

janedbal avatar Dec 01 '24 10:12 janedbal