Skip to content

Commit

Permalink
While loop's condition with a return is not reported as always true
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 22, 2021
1 parent 43c192b commit ac7a60a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -900,13 +900,12 @@ private function processStmtNode(
$isIterableAtLeastOnce = $beforeCondBooleanType instanceof ConstantBooleanType && $beforeCondBooleanType->getValue();
$alwaysIterates = $condBooleanType instanceof ConstantBooleanType && $condBooleanType->getValue();
$neverIterates = $condBooleanType instanceof ConstantBooleanType && !$condBooleanType->getValue();
$breakCount = count($finalScopeResult->getExitPointsByType(Break_::class));
if ($breakCount === 0) {
if (count($finalScopeResult->getExitPoints()) === 0) {
$nodeCallback(new BreaklessWhileLoopNode($stmt), $bodyScopeMaybeRan);
}

if ($alwaysIterates) {
$isAlwaysTerminating = $breakCount === 0;
$isAlwaysTerminating = count($finalScopeResult->getExitPointsByType(Break_::class)) === 0;
} elseif ($isIterableAtLeastOnce) {
$isAlwaysTerminating = $finalScopeResult->isAlwaysTerminating();
} else {
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/while-loop-true.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ public function doBar4(): void
}
}

public function doBar5(): void
{
while (true) {
if (rand(0, 1)) {
return;
}
}
}

}

0 comments on commit ac7a60a

Please sign in to comment.