Skip to content

Generic/ScopeIndent: false positive with nested closures #1312

@jrfnl

Description

@jrfnl

Describe the bug

The Generic.WhiteSpace.ScopeIndent sniff incorrectly flags code within a closure, and directly following a nested closure as having the wrong indent, while the indent is correct.

Code sample

Anonymized minimal reproduction sample:

$obj->callA($foo)
    ->callB(function (string $bar) use ($baz) {
        $joined = function (array $newParts) {
            return implode('/', $newParts);
        };

        foreach ($something as $foobar) {
            return $foobar;
        }
    })
;

To reproduce

Steps to reproduce the behavior:

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

☝🏻 Line 9 is the foreach line, line 11, the line with the closing brace for the foreach.

Expected behavior

No errors from this sniff.

Versions (please complete the following information)

Operating System not relevant
PHP version not relevant
PHP_CodeSniffer version 4.x
Standard Generic
Install type git clone

Please confirm

  • I have searched the issue list and am not opening a duplicate issue.
  • I have read the Contribution Guidelines and this is not a support question.
  • 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 4.x 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