PHPCSStandards/PHP_CodeSniffer

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

simPod opened this issue · 3 comments

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

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

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.

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

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