Skip to content

Commit ea2cf3f

Browse files
committed
Fixed bug #2994 : Generic.Formatting.DisallowMultipleStatements false positive for FOR loop with no body
1 parent e3eecbb commit ea2cf3f

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
4343
- Fixed bug #2943 : Redundant semicolon added to a file when fixing PSR2.Files.ClosingTag.NotAllowed
4444
- Fixed bug #2977 : File::isReference() does not detect return by reference for closures
4545
-- Thanks to Juliette Reinders Folmer for the patch
46+
- Fixed bug #2994 : Generic.Formatting.DisallowMultipleStatements false positive for FOR loop with no body
4647
</notes>
4748
<contents>
4849
<dir name="/">

src/Standards/Generic/Sniffs/Formatting/DisallowMultipleStatementsSniff.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,18 @@ public function process(File $phpcsFile, $stackPtr)
5959
} while ($tokens[$prev]['code'] === T_PHPCS_IGNORE);
6060

6161
// Ignore multiple statements in a FOR condition.
62-
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
63-
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $bracket) {
64-
if (isset($tokens[$bracket]['parenthesis_owner']) === false) {
65-
// Probably a closure sitting inside a function call.
66-
continue;
67-
}
68-
69-
$owner = $tokens[$bracket]['parenthesis_owner'];
70-
if ($tokens[$owner]['code'] === T_FOR) {
71-
return;
62+
foreach ([$stackPtr, $prev] as $checkToken) {
63+
if (isset($tokens[$checkToken]['nested_parenthesis']) === true) {
64+
foreach ($tokens[$checkToken]['nested_parenthesis'] as $bracket) {
65+
if (isset($tokens[$bracket]['parenthesis_owner']) === false) {
66+
// Probably a closure sitting inside a function call.
67+
continue;
68+
}
69+
70+
$owner = $tokens[$bracket]['parenthesis_owner'];
71+
if ($tokens[$owner]['code'] === T_FOR) {
72+
return;
73+
}
7274
}
7375
}
7476
}

src/Standards/Generic/Tests/Formatting/DisallowMultipleStatementsUnitTest.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ $this->wizardid = 10; $this->paint(); echo 'x';
1414

1515
<?php
1616
echo 'x'; /* phpcs:ignore Standard */ echo $y; /* phpcs:ignore OtherStandard */;
17+
18+
for ($i = 0 ; $i < 10; $i++);
19+
{
20+
}

src/Standards/Generic/Tests/Formatting/DisallowMultipleStatementsUnitTest.inc.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ echo 'x';
1919

2020
<?php
2121
echo 'x'; /* phpcs:ignore Standard */ echo $y; /* phpcs:ignore OtherStandard */;
22+
23+
for ($i = 0 ; $i < 10; $i++);
24+
{
25+
}

0 commit comments

Comments
 (0)