Skip to content

Commit 7fdb85b

Browse files
committed
static property fetch
1 parent d0a40f8 commit 7fdb85b

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5713,10 +5713,27 @@ static function (): void {
57135713
$assignedExprType = $scope->getType($assignedExpr);
57145714
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
57155715
if ($propertyReflection !== null && $propertyReflection->canChangeTypeAfterAssignment()) {
5716-
if ($propertyReflection->hasNativeType() && $scope->isDeclareStrictTypes()) {
5716+
if ($propertyReflection->hasNativeType()) {
5717+
$assignedNativeType = $scope->getNativeType($assignedExpr);
57175718
$propertyNativeType = $propertyReflection->getNativeType();
57185719

5719-
$scope = $scope->assignExpression($var, TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType), TypeCombinator::intersect($scope->getNativeType($assignedExpr)->toCoercedArgumentType(true), $propertyNativeType));
5720+
$assignedTypeIsCompatible = false;
5721+
foreach (TypeUtils::flattenTypes($propertyNativeType) as $type) {
5722+
if ($type->isSuperTypeOf($assignedNativeType)->yes()) {
5723+
$assignedTypeIsCompatible = true;
5724+
break;
5725+
}
5726+
}
5727+
5728+
if ($assignedTypeIsCompatible) {
5729+
$scope = $scope->assignExpression($var, $assignedExprType, $assignedNativeType);
5730+
} elseif ($scope->isDeclareStrictTypes()) {
5731+
$scope = $scope->assignExpression(
5732+
$var,
5733+
TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType),
5734+
TypeCombinator::intersect($assignedNativeType->toCoercedArgumentType(true), $propertyNativeType),
5735+
);
5736+
}
57205737
} else {
57215738
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
57225739
}

tests/PHPStan/Analyser/nsrt/bug-12902.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ class NarrowsStaticNativeUnion {
6666
public function __construct()
6767
{
6868
self::$i = getInt();
69-
assertType('float|int', self::$i); // could be int
70-
assertNativeType('float|int', self::$i); // could be int
69+
assertType('int', self::$i);
70+
assertNativeType('int', self::$i);
7171

7272
$this->impureCall();
73-
assertType('float|int', self::$i);
74-
assertNativeType('float|int', self::$i);
73+
assertType('int', self::$i); // should be float|int
74+
assertNativeType('int', self::$i); // should be float|int
7575
}
7676

7777
public function doFoo(): void {

0 commit comments

Comments
 (0)