PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Generic/ScopeIndent: false positive with nested closures

Open jrfnl opened this issue 2 months ago • 0 comments

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

  • [x] I have searched the issue list and am not opening a duplicate issue.
  • [x] I have read the Contribution Guidelines and this is not a support question.
  • [x] I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
  • [x] I have verified the issue still exists in the 4.x branch of PHP_CodeSniffer.

jrfnl avatar Nov 08 '25 03:11 jrfnl