Skip to content

Commit

Permalink
Fixed bug #3672 : Incorrect ScopeIndent.IncorrectExact report for mat…
Browse files Browse the repository at this point in the history
…ch inside array literal
  • Loading branch information
gsherwood committed Sep 26, 2022
1 parent c08491d commit d7864cb
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
- Fixed bug #3668 : PSR12.Classes.ClassInstantiation.MissingParentheses false positive when instantiating parent classes
-- Similar issues also fixed in Generic.Functions.FunctionCallArgumentSpacing and Squiz.Formatting.OperatorBracket
-- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
- Fixed bug #3672 : Incorrect ScopeIndent.IncorrectExact report for match inside array literal
</notes>
<contents>
<dir name="/">
Expand Down
28 changes: 19 additions & 9 deletions src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,13 @@ public function process(File $phpcsFile, $stackPtr)
}
}

$lastOpenTag = $stackPtr;
$lastCloseTag = null;
$openScopes = [];
$adjustments = [];
$setIndents = [];
$disableExactEnd = 0;
$lastOpenTag = $stackPtr;
$lastCloseTag = null;
$openScopes = [];
$adjustments = [];
$setIndents = [];
$disableExactStack = [];
$disableExactEnd = 0;

$tokens = $phpcsFile->getTokens();
$first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr);
Expand Down Expand Up @@ -232,6 +233,7 @@ public function process(File $phpcsFile, $stackPtr)
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS
&& isset($tokens[$i]['parenthesis_closer']) === true
) {
$disableExactStack[$tokens[$i]['parenthesis_closer']] = $tokens[$i]['parenthesis_closer'];
$disableExactEnd = max($disableExactEnd, $tokens[$i]['parenthesis_closer']);
if ($this->debug === true) {
$line = $tokens[$i]['line'];
Expand Down Expand Up @@ -802,9 +804,17 @@ public function process(File $phpcsFile, $stackPtr)
&& isset($tokens[$checkToken]['scope_opener']) === true
) {
$exact = true;

if ($disableExactEnd > $checkToken) {
if ($tokens[$checkToken]['conditions'] === $tokens[$disableExactEnd]['conditions']) {
$exact = false;
foreach ($disableExactStack as $disableExactStackEnd) {
if ($disableExactStackEnd < $checkToken) {
continue;
}

if ($tokens[$checkToken]['conditions'] === $tokens[$disableExactStackEnd]['conditions']) {
$exact = false;
break;
}
}
}

Expand Down Expand Up @@ -1035,6 +1045,7 @@ public function process(File $phpcsFile, $stackPtr)

// Don't check indents exactly between arrays as they tend to have custom rules.
if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
$disableExactStack[$tokens[$i]['bracket_closer']] = $tokens[$i]['bracket_closer'];
$disableExactEnd = max($disableExactEnd, $tokens[$i]['bracket_closer']);
if ($this->debug === true) {
$line = $tokens[$i]['line'];
Expand All @@ -1056,7 +1067,6 @@ public function process(File $phpcsFile, $stackPtr)
) {
if ($this->debug === true) {
$line = $tokens[$i]['line'];
$type = $tokens[$disableExactEnd]['type'];
echo "Here/nowdoc found on line $line".PHP_EOL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,13 @@ switch ($foo) {
return 'default';
}

foo(function ($foo) {
return [
match ($foo) {
}
];
});

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,13 @@ switch ($foo) {
return 'default';
}

foo(function ($foo) {
return [
match ($foo) {
}
];
});

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,13 @@ switch ($foo) {
return 'default';
}

foo(function ($foo) {
return [
match ($foo) {
}
];
});

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,13 @@ switch ($foo) {
return 'default';
}

foo(function ($foo) {
return [
match ($foo) {
}
];
});

/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
?>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
1527 => 1,
1529 => 1,
1530 => 1,
1583 => 1,
1584 => 1,
1585 => 1,
1586 => 1,
1590 => 1,
1591 => 1,
1592 => 1,
1593 => 1,
];

}//end getErrorList()
Expand Down

0 comments on commit d7864cb

Please sign in to comment.