Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
puggan opened this issue Mar 15, 2024 · 1 comment
Open
3 tasks done

Comments

@puggan
Copy link

puggan commented Mar 15, 2024

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.
@jrfnl
Copy link
Contributor

jrfnl commented Mar 20, 2024

This sounds like a duplicate of known issue PHPCSStandards/PHP_CodeSniffer#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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants