Skip to content
Merged
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
15 changes: 9 additions & 6 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,10 @@ public function processStmtNode(
// the narrowed value-var type in place of the broader dim fetch type so
// the loop's final array rewrite below picks up the sharper element type.
if ($originalValueExpr !== null && $scopeWithIterableValueType->hasExpressionType($originalValueExpr)->yes()) {
$valueVarType = $scopeWithIterableValueType->getType($stmt->valueVar);
// read the loop value variable's narrowed type directly by name -
// it is an assigned (not processExprNode-processed) variable
// ($originalValueExpr !== null implies a string-named Variable)
$valueVarType = $scopeWithIterableValueType->getVariableType($stmt->valueVar->name);
if ($dimFetchType->isSuperTypeOf($valueVarType)->yes()) {
$dimFetchType = $valueVarType;
}
Expand All @@ -1629,7 +1632,7 @@ public function processStmtNode(
$keyLoopNativeTypes[] = $scopeWithIterableValueType->getType($keyVarExpr);
} else {
// No key variable: the narrowed value var is the array element type directly.
$dimFetchType = $scopeWithIterableValueType->getType($stmt->valueVar);
$dimFetchType = $scopeWithIterableValueType->getVariableType($stmt->valueVar->name);
$dimFetchNativeType = $scopeWithIterableValueType->getNativeType($stmt->valueVar);
}
$arrayDimFetchLoopTypes[] = $dimFetchType;
Expand Down Expand Up @@ -1946,7 +1949,7 @@ public function processStmtNode(
// only the last condition expression is relevant whether the loop continues
// see https://www.php.net/manual/en/control-structures.for.php
if ($condExpr === $lastCondExpr) {
$condTruthiness = ($this->treatPhpDocTypesAsCertain ? $condResultScope->getType($condExpr) : $condResultScope->getNativeType($condExpr))->toBoolean();
$condTruthiness = ($this->treatPhpDocTypesAsCertain ? $condResult->getType() : $condResult->getNativeType())->toBoolean();
$isIterableAtLeastOnce = $isIterableAtLeastOnce->and($condTruthiness->isTrue());
}

Expand Down Expand Up @@ -2504,7 +2507,7 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch
} else {
$constantName = new Name\FullyQualified($const->name->toString());
}
$scope = $scope->assignExpression(new ConstFetch($constantName), $scope->getType($const->value), $scope->getNativeType($const->value));
$scope = $scope->assignExpression(new ConstFetch($constantName), $constResult->getType(), $constResult->getNativeType());
}
} elseif ($stmt instanceof Node\Stmt\ClassConst) {
$hasYield = false;
Expand All @@ -2520,8 +2523,8 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch
}
$scope = $scope->assignExpression(
new Expr\ClassConstFetch(new Name\FullyQualified($scope->getClassReflection()->getName()), $const->name),
$scope->getType($const->value),
$scope->getNativeType($const->value),
$constResult->getType(),
$constResult->getNativeType(),
);
}
} elseif ($stmt instanceof Node\Stmt\EnumCase) {
Expand Down
Loading