Skip to content

Commit

Permalink
Fixed incorrect while loop logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 4, 2023
1 parent 28c2c79 commit 091fcaf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -934,8 +934,8 @@ private function processStmtNode(
$beforeCondBooleanType = $scope->getType($stmt->cond)->toBoolean();
$condBooleanType = $bodyScopeMaybeRan->getType($stmt->cond)->toBoolean();
$isIterableAtLeastOnce = $beforeCondBooleanType->isTrue()->yes();
$alwaysIterates = $condBooleanType->isTrue()->yes();
$neverIterates = $condBooleanType->isFalse()->yes();
$alwaysIterates = $condBooleanType->isTrue()->yes() && $context->isTopLevel();
$neverIterates = $condBooleanType->isFalse()->yes() && $context->isTopLevel();
$nodeCallback(new BreaklessWhileLoopNode($stmt, $finalScopeResult->getExitPoints()), $bodyScopeMaybeRan);

if ($alwaysIterates) {
Expand Down Expand Up @@ -1008,7 +1008,7 @@ private function processStmtNode(
$bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope());
}
$condBooleanType = $bodyScope->getType($stmt->cond)->toBoolean();
$alwaysIterates = $condBooleanType->isTrue()->yes();
$alwaysIterates = $condBooleanType->isTrue()->yes() && $context->isTopLevel();

$nodeCallback(new DoWhileLoopConditionNode($stmt->cond, $bodyScopeResult->getExitPoints()), $bodyScope);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,9 @@ public function testBug2851(): void
$this->analyse([__DIR__ . '/data/bug-2851.php'], []);
}

public function testBug8643(): void
{
$this->analyse([__DIR__ . '/data/bug-8643.php'], []);
}

}
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-8643.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Bug8643;

function (): void {
$x = 0;

while ($x < 4) {
$y = 0;

while ($y < 6) {
$y += 1;
}

$x += 1;
}

echo 'done';
};

0 comments on commit 091fcaf

Please sign in to comment.