Skip to content

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

Open
@puggan

Description

@puggan

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, c6c65ca
Standard PEAR
Install type git clone

Additional context

Workaround: move the heredoc before the foreach.

Please confirm:

  • I have searched the issue list and am not opening a duplicate issue.
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions