PHPCSStandards / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
919 stars 55 forks source link

Squiz.Arrays.ArrayDeclaration.ValueNoNewline false positive with `static fn` #559

Open simPod opened 3 months ago

simPod commented 3 months ago

Describe the bug

Squiz.Arrays.ArrayDeclaration.ValueNoNewline triggers false positive at static fn.

Code sample

return [
    static fn (string $value): AAA => new AAA($value),
    BBB::fromString(...),
];

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
    PHPCS output here

Expected behavior

No error

Versions (please complete the following information)

Operating System macos 14
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

jrfnl commented 3 months ago

Confirmed. Thanks for reporting this @simPod.

This looks like a similar issue to #368 which was fixed by #369 and #379, which were included in the 3.9.1 release. I've checked and the fixes in the 3.9.1 release are not directly related to this issue. This issue can be reproduced both with PHPCS 3.9.0 as well as master.

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.

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.

LastDragon-ru commented 3 months ago

Seems also related to this issue?

<?php declare(strict_types = 1);

class Bug {
    public static function bug(): mixed {
        return [
            'paginator' => [
                new class() {
                    public function toArray(): mixed {
                        return [];
                    }
                },
            ],
        ];
    }
}
 9 | ERROR | [x] The first value in a multi-value array must be on a new line
 9 | ERROR | [x] Each value in a multi-line array must be on a new line
jrfnl commented 3 months ago

@LastDragon-ru To be honest, that looks like a completely different issue to me.