Skip to content

Commit c7ad25b

Browse files
authored
Do not report do-while-false pseudo-loops that are interrupted by a break statement
1 parent a4b3955 commit c7ad25b

3 files changed

Lines changed: 81 additions & 16 deletions

File tree

src/Rules/Comparison/DoWhileLoopConstantConditionRule.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,20 @@ public function processNode(Node $node, Scope $scope): array
4444
if ($exprType->getValue()) {
4545
foreach ($node->getExitPoints() as $exitPoint) {
4646
$statement = $exitPoint->getStatement();
47-
if ($statement instanceof Break_) {
48-
return [];
49-
}
5047
if (!$statement instanceof Continue_) {
5148
return [];
5249
}
53-
if ($statement->num === null) {
54-
continue;
55-
}
5650
if (!$statement->num instanceof Int_) {
5751
continue;
5852
}
59-
$value = $statement->num->value;
60-
if ($value === 1) {
61-
continue;
53+
if ($statement->num->value > 1) {
54+
return [];
6255
}
63-
64-
if ($value > 1) {
56+
}
57+
} else {
58+
foreach ($node->getExitPoints() as $exitPoint) {
59+
$statement = $exitPoint->getStatement();
60+
if ($statement instanceof Break_) {
6561
return [];
6662
}
6763
}

tests/PHPStan/Rules/Comparison/DoWhileLoopConstantConditionRuleTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ public function testRule(): void
4343
'Do-while loop condition is always false.',
4444
46,
4545
],
46-
[
47-
'Do-while loop condition is always false.',
48-
55,
49-
],
5046
[
5147
'Do-while loop condition is always true.',
5248
64,
@@ -55,6 +51,22 @@ public function testRule(): void
5551
'Do-while loop condition is always false.',
5652
73,
5753
],
54+
[
55+
'Do-while loop condition is always false.',
56+
105,
57+
],
58+
[
59+
'Do-while loop condition is always false.',
60+
115,
61+
],
62+
[
63+
'Do-while loop condition is always false.',
64+
138,
65+
],
66+
[
67+
'Do-while loop condition is always false.',
68+
152,
69+
],
5870
]);
5971
}
6072

tests/PHPStan/Rules/Comparison/data/do-while-loop.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function doBar3()
5252
if (rand(0, 1)) {
5353
break;
5454
}
55-
} while (false); // report
55+
} while (false); // do not report
5656
}
5757

5858
public function doFoo4()
@@ -95,4 +95,61 @@ public function doFoo6(array $a)
9595
}
9696
}
9797

98+
public function doFoo8(array $a)
99+
{
100+
foreach ($a as $v) {
101+
do {
102+
if (rand(0, 1)) {
103+
continue 2;
104+
}
105+
} while (false); // report
106+
}
107+
}
108+
109+
public function doFoo9(array $a)
110+
{
111+
do {
112+
foreach ($a as $v) {
113+
break;
114+
}
115+
} while (false); // report
116+
}
117+
118+
public function doFoo10(array $a)
119+
{
120+
do {
121+
foreach ($a as $v) {
122+
break 2;
123+
}
124+
} while (false); // do not report
125+
}
126+
127+
public function doFoo11(array $a)
128+
{
129+
do {
130+
throw new \Exception;
131+
} while (true); // do not report
132+
}
133+
134+
public function doFoo12(array $a)
135+
{
136+
do {
137+
throw new \Exception;
138+
} while (false); // report
139+
}
140+
141+
public function doFoo13(array $a)
142+
{
143+
do {
144+
exit;
145+
} while (true); // do not report
146+
}
147+
148+
public function doFoo14(array $a)
149+
{
150+
do {
151+
exit;
152+
} while (false); // report
153+
}
154+
98155
}

0 commit comments

Comments
 (0)