Skip to content

Commit 12b8772

Browse files
committed
Fixed bug #2504 : Generic.WhiteSpace.ScopeIndent false positives with nested arrays and nowdoc string
Also removed some duplicated code as it was always run twice and no tests needed it
1 parent 802e6ef commit 12b8772

File tree

7 files changed

+73
-18
lines changed

7 files changed

+73
-18
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
5151
- Fixed bug #2479 : Generic.WhiteSpace.ScopeIndent error when using array destructing with exact indent checking
5252
- Fixed bug #2498 : Squiz.Arrays.ArrayDeclaration.MultiLineNotAllowed autofix breaks heredoc
5353
- Fixed bug #2502 : Generic.WhiteSpace.ScopeIndent false positives with nested switch indentation and case fall-through
54+
- Fixed bug #2504 : Generic.WhiteSpace.ScopeIndent false positives with nested arrays and nowdoc string
5455
</notes>
5556
<contents>
5657
<dir name="/">

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,6 @@ public function process(File $phpcsFile, $stackPtr)
229229

230230
$exact = $this->exact;
231231

232-
if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
233-
$disableExactEnd = max($disableExactEnd, $tokens[$i]['bracket_closer']);
234-
if ($this->debug === true) {
235-
$line = $tokens[$i]['line'];
236-
$type = $tokens[$disableExactEnd]['type'];
237-
echo "Opening short array bracket found on line $line".PHP_EOL;
238-
echo "\t=> disabling exact indent checking until $disableExactEnd ($type)".PHP_EOL;
239-
}
240-
}
241-
242232
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS
243233
&& isset($tokens[$i]['parenthesis_closer']) === true
244234
) {
@@ -1001,7 +991,11 @@ public function process(File $phpcsFile, $stackPtr)
1001991
$line = $tokens[$i]['line'];
1002992
$type = $tokens[$disableExactEnd]['type'];
1003993
echo "Opening short array bracket found on line $line".PHP_EOL;
1004-
echo "\t=> disabling exact indent checking until $disableExactEnd ($type)".PHP_EOL;
994+
if ($disableExactEnd === $tokens[$i]['bracket_closer']) {
995+
echo "\t=> disabling exact indent checking until $disableExactEnd ($type)".PHP_EOL;
996+
} else {
997+
echo "\t=> continuing to disable exact indent checking until $disableExactEnd ($type)".PHP_EOL;
998+
}
1005999
}
10061000
}
10071001

@@ -1010,10 +1004,26 @@ public function process(File $phpcsFile, $stackPtr)
10101004
if ($tokens[$i]['code'] === T_START_HEREDOC
10111005
|| $tokens[$i]['code'] === T_START_NOWDOC
10121006
) {
1013-
$i = $phpcsFile->findNext([T_END_HEREDOC, T_END_NOWDOC], ($i + 1));
1014-
$i = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
1007+
if ($this->debug === true) {
1008+
$line = $tokens[$i]['line'];
1009+
$type = $tokens[$disableExactEnd]['type'];
1010+
echo "Here/nowdoc found on line $line".PHP_EOL;
1011+
}
1012+
1013+
$i = $phpcsFile->findNext([T_END_HEREDOC, T_END_NOWDOC], ($i + 1));
1014+
$next = $phpcsFile->findNext(Tokens::$emptyTokens, ($i + 1), null, true);
1015+
if ($tokens[$next]['code'] === T_COMMA) {
1016+
$i = $next;
1017+
}
1018+
1019+
if ($this->debug === true) {
1020+
$line = $tokens[$i]['line'];
1021+
$type = $tokens[$i]['type'];
1022+
echo "\t* skipping to token $i ($type) on line $line *".PHP_EOL;
1023+
}
1024+
10151025
continue;
1016-
}
1026+
}//end if
10171027

10181028
// Completely skip multi-line strings as the indent is a part of the
10191029
// content itself.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,17 @@ switch ($x) {
14181418
}
14191419
}
14201420

1421+
$foo = [
1422+
[
1423+
'a' => [
1424+
],
1425+
'b' => <<<'FOO'
1426+
foo;
1427+
FOO
1428+
],
1429+
$a,
1430+
];
1431+
14211432
?>
14221433

14231434
<?php

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,17 @@ switch ($x) {
14181418
}
14191419
}
14201420

1421+
$foo = [
1422+
[
1423+
'a' => [
1424+
],
1425+
'b' => <<<'FOO'
1426+
foo;
1427+
FOO
1428+
],
1429+
$a,
1430+
];
1431+
14211432
?>
14221433

14231434
<?php

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,17 @@ switch ($x) {
14181418
}
14191419
}
14201420

1421+
$foo = [
1422+
[
1423+
'a' => [
1424+
],
1425+
'b' => <<<'FOO'
1426+
foo;
1427+
FOO
1428+
],
1429+
$a,
1430+
];
1431+
14211432
?>
14221433

14231434
<?php

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,17 @@ switch ($x) {
14181418
}
14191419
}
14201420

1421+
$foo = [
1422+
[
1423+
'a' => [
1424+
],
1425+
'b' => <<<'FOO'
1426+
foo;
1427+
FOO
1428+
],
1429+
$a,
1430+
];
1431+
14211432
?>
14221433

14231434
<?php

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ public function getErrorList($testFile='ScopeIndentUnitTest.inc')
178178
1340 => 1,
179179
1342 => 1,
180180
1345 => 1,
181-
1424 => 1,
182-
1425 => 1,
183-
1426 => 1,
184-
1427 => 1,
181+
1435 => 1,
182+
1436 => 1,
183+
1437 => 1,
184+
1438 => 1,
185185
];
186186

187187
}//end getErrorList()

0 commit comments

Comments
 (0)