Skip to content

Commit 091fcaf

Browse files
committed
Fixed incorrect while loop logic
1 parent 28c2c79 commit 091fcaf

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,8 @@ private function processStmtNode(
934934
$beforeCondBooleanType = $scope->getType($stmt->cond)->toBoolean();
935935
$condBooleanType = $bodyScopeMaybeRan->getType($stmt->cond)->toBoolean();
936936
$isIterableAtLeastOnce = $beforeCondBooleanType->isTrue()->yes();
937-
$alwaysIterates = $condBooleanType->isTrue()->yes();
938-
$neverIterates = $condBooleanType->isFalse()->yes();
937+
$alwaysIterates = $condBooleanType->isTrue()->yes() && $context->isTopLevel();
938+
$neverIterates = $condBooleanType->isFalse()->yes() && $context->isTopLevel();
939939
$nodeCallback(new BreaklessWhileLoopNode($stmt, $finalScopeResult->getExitPoints()), $bodyScopeMaybeRan);
940940

941941
if ($alwaysIterates) {
@@ -1008,7 +1008,7 @@ private function processStmtNode(
10081008
$bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope());
10091009
}
10101010
$condBooleanType = $bodyScope->getType($stmt->cond)->toBoolean();
1011-
$alwaysIterates = $condBooleanType->isTrue()->yes();
1011+
$alwaysIterates = $condBooleanType->isTrue()->yes() && $context->isTopLevel();
10121012

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

tests/PHPStan/Rules/Comparison/NumberComparisonOperatorsConstantConditionRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,9 @@ public function testBug2851(): void
155155
$this->analyse([__DIR__ . '/data/bug-2851.php'], []);
156156
}
157157

158+
public function testBug8643(): void
159+
{
160+
$this->analyse([__DIR__ . '/data/bug-8643.php'], []);
161+
}
162+
158163
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Bug8643;
4+
5+
function (): void {
6+
$x = 0;
7+
8+
while ($x < 4) {
9+
$y = 0;
10+
11+
while ($y < 6) {
12+
$y += 1;
13+
}
14+
15+
$x += 1;
16+
}
17+
18+
echo 'done';
19+
};

0 commit comments

Comments
 (0)