Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/Rules/Comparison/DoWhileLoopConstantConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,20 @@ public function processNode(Node $node, Scope $scope): array
if ($exprType->getValue()) {
foreach ($node->getExitPoints() as $exitPoint) {
$statement = $exitPoint->getStatement();
if ($statement instanceof Break_) {
return [];
}
if (!$statement instanceof Continue_) {
return [];
}
if ($statement->num === null) {
continue;
}
if (!$statement->num instanceof Int_) {
continue;
}
$value = $statement->num->value;
if ($value === 1) {
continue;
if ($statement->num->value > 1) {
return [];
}

if ($value > 1) {
}
} else {
foreach ($node->getExitPoints() as $exitPoint) {
$statement = $exitPoint->getStatement();
if ($statement instanceof Break_) {
return [];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public function testRule(): void
'Do-while loop condition is always false.',
46,
],
[
'Do-while loop condition is always false.',
55,
],
[
'Do-while loop condition is always true.',
64,
Expand All @@ -55,6 +51,22 @@ public function testRule(): void
'Do-while loop condition is always false.',
73,
],
[
'Do-while loop condition is always false.',
105,
],
[
'Do-while loop condition is always false.',
115,
],
[
'Do-while loop condition is always false.',
138,
],
[
'Do-while loop condition is always false.',
152,
],
]);
}

Expand Down
59 changes: 58 additions & 1 deletion tests/PHPStan/Rules/Comparison/data/do-while-loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function doBar3()
if (rand(0, 1)) {
break;
}
} while (false); // report
} while (false); // do not report
}

public function doFoo4()
Expand Down Expand Up @@ -95,4 +95,61 @@ public function doFoo6(array $a)
}
}

public function doFoo8(array $a)
{
foreach ($a as $v) {
do {
if (rand(0, 1)) {
continue 2;
}
} while (false); // report
}
}

public function doFoo9(array $a)
{
do {
foreach ($a as $v) {
break;
}
} while (false); // report
}

public function doFoo10(array $a)
{
do {
foreach ($a as $v) {
break 2;
}
} while (false); // do not report
}

public function doFoo11(array $a)
{
do {
throw new \Exception;
} while (true); // do not report
}

public function doFoo12(array $a)
{
do {
throw new \Exception;
} while (false); // report
}

public function doFoo13(array $a)
{
do {
exit;
} while (true); // do not report
}

public function doFoo12(array $a)
{
do {
exit;
} while (false); // report
}

}
Loading