squizlabs / PHP_CodeSniffer

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

Can't satisfy Squiz.Arrays.ArrayDeclaration.ValueNoNewline with callable value #3112

Closed simPod closed 3 years ago

simPod commented 4 years ago

Describe the bug Can't satisfy sniff when callable is within multiline array

Code sample

$a = [
    static function (): void {
    },
];

Each value in a multi-line array must be on a new line

The value is on a new line.

This can't be done either because of Squiz.WhiteSpace.ScopeClosingBrace.ContentBefore

$a = [
    static function (): void {},
];

Closing brace must be on a line by itself

Versions (please complete the following information):

morozov commented 4 years ago

This should be fixed by https://github.com/squizlabs/PHP_CodeSniffer/pull/3061.

simPod commented 3 years ago

Tried with dev-master but the issue seems to still exist.

gsherwood commented 3 years ago

Tried with master and can confirm there error no longer appears.

Test file:

$ cat temp.php
<?php
$a = [
    static function (): void {
    },
];

PHPCS 3.5.6:

$ phpcs --version
PHP_CodeSniffer version 3.5.6 (stable) by Squiz (http://www.squiz.net)

$ phpcs temp.php --standard=Squiz

FILE: /Users/gsherwood/Projects/PHP_CodeSniffer/temp.php
------------------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 3 LINES
------------------------------------------------------------------------------
 1 | ERROR | [ ] Missing file doc comment
 3 | ERROR | [x] The first value in a multi-value array must be on a new line
 4 | ERROR | [x] Expected 1 blank line before closing function brace; 0 found
------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------

Time: 61ms; Memory: 8MB

Master branch:

$ php bin/phpcs --version
PHP_CodeSniffer version 3.5.7 (stable) by Squiz (http://www.squiz.net)

$ php bin/phpcs temp.php --standard=Squiz

FILE: /Users/gsherwood/Projects/PHP_CodeSniffer/temp.php
------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
------------------------------------------------------------------------------
 1 | ERROR | [ ] Missing file doc comment
 4 | ERROR | [x] Expected 1 blank line before closing function brace; 0 found
------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------

Time: 142ms; Memory: 8MB

@simPod What are you seeing?

simPod commented 3 years ago

I see, just checked. Try this:

yield [
    static fn () : string => '',
];

yield [
    static function () : string {
        return '';
    },
];

arrow function fails

gsherwood commented 3 years ago

@simPod I've pushed an additional fix for the arrow function support. Are you able to try again?

simPod commented 3 years ago

Wrks, thanks a lot!