Skip to content

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

@jrfnl

Description

@jrfnl

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

  • 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