Skip to content

Commit 97e48d3

Browse files
committed
simplify logic for the do-while(true) case; additional tests
1 parent fca23a9 commit 97e48d3

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

src/Rules/Comparison/DoWhileLoopConstantConditionRule.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,13 @@ 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;
62-
}
63-
64-
if ($value > 1) {
53+
if ($statement->num->value > 1) {
6554
return [];
6655
}
6756
}

tests/PHPStan/Rules/Comparison/DoWhileLoopConstantConditionRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ public function testRule(): void
5959
'Do-while loop condition is always false.',
6060
115,
6161
],
62+
[
63+
'Do-while loop condition is always false.',
64+
138,
65+
],
66+
[
67+
'Do-while loop condition is always false.',
68+
152,
69+
],
6270
]);
6371
}
6472

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,32 @@ public function doFoo10(array $a)
124124
} while (false); // do not report
125125
}
126126

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 doFoo12(array $a)
149+
{
150+
do {
151+
exit;
152+
} while (false); // report
153+
}
154+
127155
}

0 commit comments

Comments
 (0)