PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Generic/ScopeIndent: false positive when match is nested in control structure condition

Open jrfnl opened this issue 2 months ago • 1 comments

Describe the bug

Given a control structure which contains a match expression within its condition... ... and within the body of said control structure, there is a nested control structure.... ... the Generic.WhiteSpace.ScopeIndent sniff incorrectly flags the lines of the nested control structure as having the wrong indent, while the indent is correct.

Code sample

if (match ($name[0]) {
    'g' => true,
    default => false,
} && !do_something($name[$i])) {
    // If there is no control structure in if body, it's fine.
    return $foo;
}

if (match ($name[0]) {
    'g' => true,
    default => false,
} && !do_something($name[$i])) {
	// Control structure in body of other control structure with match in condition results in false positives.
    if ($obj->doSomething($name)) {
        return $foo;
    }
}

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 3 ERRORS AFFECTING 3 LINES
------------------------------------------------------------------------------------------------------------------------
 16 | ERROR | [x] Line indented incorrectly; expected 0 spaces, found 4 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
 18 | ERROR | [x] Line indented incorrectly; expected 0 spaces, found 4 (Generic.WhiteSpace.ScopeIndent.IncorrectExact)
------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------

☝🏻 Line 16 is the if ($obj->doSomething($name)) { line, line 18, the line with the closing brace for the if.

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

Previous, loosely related issue:

  • https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/110

jrfnl avatar Nov 08 '25 03:11 jrfnl