Skip to content
Merged
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
21 changes: 13 additions & 8 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1314,13 +1314,18 @@ private function processStmtNode(

$bodyScope = $initScope;
$isIterableAtLeastOnce = TrinaryLogic::createYes();
$lastCondExpr = $stmt->cond[count($stmt->cond) - 1] ?? null;
foreach ($stmt->cond as $condExpr) {
$condResult = $this->processExprNode($stmt, $condExpr, $bodyScope, static function (): void {
}, ExpressionContext::createDeep());
$initScope = $condResult->getScope();
$condResultScope = $condResult->getScope();
$condTruthiness = ($this->treatPhpDocTypesAsCertain ? $condResultScope->getType($condExpr) : $condResultScope->getNativeType($condExpr))->toBoolean();
$isIterableAtLeastOnce = $isIterableAtLeastOnce->and($condTruthiness->isTrue());

if ($condExpr === $lastCondExpr) {
$condTruthiness = ($this->treatPhpDocTypesAsCertain ? $condResultScope->getType($condExpr) : $condResultScope->getNativeType($condExpr))->toBoolean();
$isIterableAtLeastOnce = $isIterableAtLeastOnce->and($condTruthiness->isTrue());
}

$hasYield = $hasYield || $condResult->hasYield();
$throwPoints = array_merge($throwPoints, $condResult->getThrowPoints());
$impurePoints = array_merge($impurePoints, $condResult->getImpurePoints());
Expand All @@ -1332,8 +1337,8 @@ private function processStmtNode(
do {
$prevScope = $bodyScope;
$bodyScope = $bodyScope->mergeWith($initScope);
foreach ($stmt->cond as $condExpr) {
$bodyScope = $this->processExprNode($stmt, $condExpr, $bodyScope, static function (): void {
if ($lastCondExpr !== null) {
$bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, static function (): void {
}, ExpressionContext::createDeep())->getTruthyScope();
}
$bodyScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, static function (): void {
Expand Down Expand Up @@ -1363,8 +1368,8 @@ private function processStmtNode(
}

$bodyScope = $bodyScope->mergeWith($initScope);
foreach ($stmt->cond as $condExpr) {
$bodyScope = $this->processExprNode($stmt, $condExpr, $bodyScope, $nodeCallback, ExpressionContext::createDeep())->getTruthyScope();
if ($lastCondExpr !== null) {
$bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, $nodeCallback, ExpressionContext::createDeep())->getTruthyScope();
}

$finalScopeResult = $this->processStmtNodes($stmt, $stmt->stmts, $bodyScope, $nodeCallback, $context)->filterOutLoopExitPoints();
Expand All @@ -1378,8 +1383,8 @@ private function processStmtNode(
$loopScope = $this->processExprNode($stmt, $loopExpr, $loopScope, $nodeCallback, ExpressionContext::createTopLevel())->getScope();
}
$finalScope = $finalScope->generalizeWith($loopScope);
foreach ($stmt->cond as $condExpr) {
$finalScope = $finalScope->filterByFalseyValue($condExpr);
if ($lastCondExpr !== null) {
$finalScope = $finalScope->filterByFalseyValue($lastCondExpr);
}

foreach ($finalScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) {
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/nsrt/for-loop-i-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,12 @@ public static function groupCapacities(array $startTimes): array

return $capacities;
}

public function lastConditionResult(): void
{
for ($i = 0, $j = 5; $i < 10, $j > 0; $i++, $j--) {
assertType('int<0, max>', $i); // int<0,4> would be more precise, see https://github.com/phpstan/phpstan/issues/11872
assertType('int<1, 5>', $j);
}
}
}
Loading