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.68k stars 1.48k forks source link

Generic.WhiteSpace.ScopeIndent.IncorrectExact false positive in foreach with heredoc #3943

Open puggan opened 8 months ago

puggan commented 8 months ago

Describe the bug

I get false positive Generic.WhiteSpace.ScopeIndent.IncorrectExact when an foreach has an heredoc in it.

Code sample

<?php

(static function () {
    foreach ([] as $a) {
        echo '.';
        if ($a) {
            echo '.';
        }
    }

    foreach (
        [
            <<<EOL
            TEST
            EOL,
        ] as $b
    ) {
        echo '.';
        if ($b) {
            echo '.';
        }
    }
})();

Custom ruleset

using a psr12 ruleset, but get the same error but for PEAR.WhiteSpace.ScopeIndent.IncorrectExact when running with a fresh clone of this repo.

To reproduce

Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs -s --sniffs="PEAR.WhiteSpace.ScopeIndent" test.php ...
  3. See error message displayed
    ---------------------------------------------------------------------------------------------------------------------
    FOUND 2 ERRORS AFFECTING 2 LINES
    ---------------------------------------------------------------------------------------------------------------------
    19 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 8
    |       |     (PEAR.WhiteSpace.ScopeIndent.IncorrectExact)
    21 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 8
    |       |     (PEAR.WhiteSpace.ScopeIndent.IncorrectExact)
    ---------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ---------------------------------------------------------------------------------------------------------------------

    or with my config, i get Generic instead of PEAR:

    ------------------------------------------------------------------------------------------------------------------------
    FOUND 2 ERRORS AFFECTING 2 LINES
    ------------------------------------------------------------------------------------------------------------------------
    19 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 8
    |       |     (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
    21 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 8
    |       |     (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
    ------------------------------------------------------------------------------------------------------------------------
    PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ------------------------------------------------------------------------------------------------------------------------

Expected behavior

I expect both row 18 and 19 if ($b) { should have the same indentation, 8 spaces. So instead of the warning, it should expect 8 and found 8, and therefor not show the warning.

Versions (please complete the following information)

PHP_CodeSniffer version 3.8.0 (stable) by Squiz and PHPCSStandards

Operating System Ubuntu 23.10 mantic
PHP version 8.3.2-1+ubuntu22.04.1+deb.sury.org+1
PHP_CodeSniffer version 3.8.0
Standard Generic
Install type Composer local
Operating System Ubuntu 23.10 mantic
PHP version 8.3.2-1+ubuntu22.04.1+deb.sury.org+1
PHP_CodeSniffer version branch master, 3.8.0, c6c65ca0dc8608ba87631523b97b2f8d5351a854
Standard PEAR
Install type git clone

Additional context

Workaround: move the heredoc before the foreach.

Please confirm:

jrfnl commented 8 months ago

This sounds like a duplicate of known issue https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/149

Also, please don't open issues in this repo anymore. This repo is abandoned and https://github.com/PHPCSStandards/PHP_CodeSniffer is its successor. See: #3932