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

Generic.WhiteSpace.ScopeIndent.Incorrect issue after NOWDOC #2883

Closed JeroenBakker closed 4 years ago

JeroenBakker commented 4 years ago

Hi there,

I'm having an issue where phpcs is complaining about my indentation level in a nested array. It seems to be caused by a NOWDOC or HEREDOC and then the issue crops up somewhere after a multi-line array

This is using phpcs version 3.5.4

The following array reproduces this issue:

return [
    'lor' =>
        // Without this NOWDOC everything works fine
        <<<'INTRO'
        lorem ipsum
        INTRO,
    'em' => [
        // Without this multiline array everything works fine
        [
            '',
        ],
    ],
    // Every toplevel array key from here is being flagged as being incorrectly indented
    'abc' => [
        'a' => 'wop wop',
        'b' => 'ola ola.',
    ],
];

The full error is:

 Line indented incorrectly; expected at least 8 spaces, found 4 (Generic.WhiteSpace.ScopeIndent.Incorrect)

If I were to attempt to fix this with phpcbf it adds the indentation and throws the next error:

Array key not indented correctly; expected 4 spaces but found 8 (Generic.Arrays.ArrayIndent.KeyIncorrect)

Any further attempts to fix this just go back and forth between the two.

If you need any further details please let me know!

BackEndTea commented 4 years ago

Could you check what happends when the , after the nowdoc is on another line (or completely missing)?

JeroenBakker commented 4 years ago

Good find, if the trailing comma after the NOWDOC closing tag is moved to the next line the issue goes away.

That's ugly though 😛

gsherwood commented 4 years ago

I've committed a fix for this problem. The core issue is the the end of the nowdoc was seen as the end of that statement, whereas the comma should really be the end. The whitespace sniff specifically tries to ignore commas to cover cases like this, but the nowdoc issue was stopping that from working.

Thanks for the report.