Skip to content

Commit d7864cb

Browse files
committed
Fixed bug #3672 : Incorrect ScopeIndent.IncorrectExact report for match inside array literal
1 parent c08491d commit d7864cb

File tree

7 files changed

+52
-13
lines changed

7 files changed

+52
-13
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4848
- Fixed bug #3668 : PSR12.Classes.ClassInstantiation.MissingParentheses false positive when instantiating parent classes
4949
-- Similar issues also fixed in Generic.Functions.FunctionCallArgumentSpacing and Squiz.Formatting.OperatorBracket
5050
-- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
51+
- Fixed bug #3672 : Incorrect ScopeIndent.IncorrectExact report for match inside array literal
5152
</notes>
5253
<contents>
5354
<dir name="/">

src/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,13 @@ public function process(File $phpcsFile, $stackPtr)
142142
}
143143
}
144144

145-
$lastOpenTag = $stackPtr;
146-
$lastCloseTag = null;
147-
$openScopes = [];
148-
$adjustments = [];
149-
$setIndents = [];
150-
$disableExactEnd = 0;
145+
$lastOpenTag = $stackPtr;
146+
$lastCloseTag = null;
147+
$openScopes = [];
148+
$adjustments = [];
149+
$setIndents = [];
150+
$disableExactStack = [];
151+
$disableExactEnd = 0;
151152

152153
$tokens = $phpcsFile->getTokens();
153154
$first = $phpcsFile->findFirstOnLine(T_INLINE_HTML, $stackPtr);
@@ -232,6 +233,7 @@ public function process(File $phpcsFile, $stackPtr)
232233
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS
233234
&& isset($tokens[$i]['parenthesis_closer']) === true
234235
) {
236+
$disableExactStack[$tokens[$i]['parenthesis_closer']] = $tokens[$i]['parenthesis_closer'];
235237
$disableExactEnd = max($disableExactEnd, $tokens[$i]['parenthesis_closer']);
236238
if ($this->debug === true) {
237239
$line = $tokens[$i]['line'];
@@ -802,9 +804,17 @@ public function process(File $phpcsFile, $stackPtr)
802804
&& isset($tokens[$checkToken]['scope_opener']) === true
803805
) {
804806
$exact = true;
807+
805808
if ($disableExactEnd > $checkToken) {
806-
if ($tokens[$checkToken]['conditions'] === $tokens[$disableExactEnd]['conditions']) {
807-
$exact = false;
809+
foreach ($disableExactStack as $disableExactStackEnd) {
810+
if ($disableExactStackEnd < $checkToken) {
811+
continue;
812+
}
813+
814+
if ($tokens[$checkToken]['conditions'] === $tokens[$disableExactStackEnd]['conditions']) {
815+
$exact = false;
816+
break;
817+
}
808818
}
809819
}
810820

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

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

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,13 @@ switch ($foo) {
15721572
return 'default';
15731573
}
15741574

1575+
foo(function ($foo) {
1576+
return [
1577+
match ($foo) {
1578+
}
1579+
];
1580+
});
1581+
15751582
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15761583
?>
15771584

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.1.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,13 @@ switch ($foo) {
15721572
return 'default';
15731573
}
15741574

1575+
foo(function ($foo) {
1576+
return [
1577+
match ($foo) {
1578+
}
1579+
];
1580+
});
1581+
15751582
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15761583
?>
15771584

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,13 @@ switch ($foo) {
15721572
return 'default';
15731573
}
15741574

1575+
foo(function ($foo) {
1576+
return [
1577+
match ($foo) {
1578+
}
1579+
];
1580+
});
1581+
15751582
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15761583
?>
15771584

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.2.inc.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,13 @@ switch ($foo) {
15721572
return 'default';
15731573
}
15741574

1575+
foo(function ($foo) {
1576+
return [
1577+
match ($foo) {
1578+
}
1579+
];
1580+
});
1581+
15751582
/* ADD NEW TESTS ABOVE THIS LINE AND MAKE SURE THAT THE 1 (space-based) AND 2 (tab-based) FILES ARE IN SYNC! */
15761583
?>
15771584

src/Standards/Generic/Tests/WhiteSpace/ScopeIndentUnitTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
187187
1527 => 1,
188188
1529 => 1,
189189
1530 => 1,
190-
1583 => 1,
191-
1584 => 1,
192-
1585 => 1,
193-
1586 => 1,
190+
1590 => 1,
191+
1591 => 1,
192+
1592 => 1,
193+
1593 => 1,
194194
];
195195

196196
}//end getErrorList()

0 commit comments

Comments
 (0)