Skip to content

Commit 4f2ce34

Browse files
committed
Fix assigning properties
1 parent 22c78a9 commit 4f2ce34

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5628,25 +5628,28 @@ static function (): void {
56285628
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
56295629
if ($propertyReflection->canChangeTypeAfterAssignment()) {
56305630
if ($propertyReflection->hasNativeType()) {
5631-
$assignedNativeType = $scope->getNativeType($assignedExpr);
56325631
$propertyNativeType = $propertyReflection->getNativeType();
56335632

5634-
$assignedTypeIsCompatible = false;
5635-
foreach (TypeUtils::flattenTypes($propertyNativeType) as $type) {
5636-
if ($type->isSuperTypeOf($assignedNativeType)->yes()) {
5637-
$assignedTypeIsCompatible = true;
5638-
break;
5633+
$assignedTypeIsCompatible = $propertyNativeType->isSuperTypeOf($assignedExprType)->yes();
5634+
if (!$assignedTypeIsCompatible) {
5635+
foreach (TypeUtils::flattenTypes($propertyNativeType) as $type) {
5636+
if ($type->isSuperTypeOf($assignedExprType)->yes()) {
5637+
$assignedTypeIsCompatible = true;
5638+
break;
5639+
}
56395640
}
56405641
}
56415642

56425643
if ($assignedTypeIsCompatible) {
5643-
$scope = $scope->assignExpression($var, $assignedExprType, $assignedNativeType);
5644+
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
56445645
} elseif ($scope->isDeclareStrictTypes()) {
56455646
$scope = $scope->assignExpression(
56465647
$var,
56475648
TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType),
5648-
TypeCombinator::intersect($assignedNativeType->toCoercedArgumentType(true), $propertyNativeType),
5649+
TypeCombinator::intersect($scope->getNativeType($assignedExpr)->toCoercedArgumentType(true), $propertyNativeType),
56495650
);
5651+
} else {
5652+
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
56505653
}
56515654
} else {
56525655
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
@@ -5716,25 +5719,28 @@ static function (): void {
57165719
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
57175720
if ($propertyReflection !== null && $propertyReflection->canChangeTypeAfterAssignment()) {
57185721
if ($propertyReflection->hasNativeType()) {
5719-
$assignedNativeType = $scope->getNativeType($assignedExpr);
57205722
$propertyNativeType = $propertyReflection->getNativeType();
5723+
$assignedTypeIsCompatible = $propertyNativeType->isSuperTypeOf($assignedExprType)->yes();
57215724

5722-
$assignedTypeIsCompatible = false;
5723-
foreach (TypeUtils::flattenTypes($propertyNativeType) as $type) {
5724-
if ($type->isSuperTypeOf($assignedNativeType)->yes()) {
5725-
$assignedTypeIsCompatible = true;
5726-
break;
5725+
if (!$assignedTypeIsCompatible) {
5726+
foreach (TypeUtils::flattenTypes($propertyNativeType) as $type) {
5727+
if ($type->isSuperTypeOf($assignedExprType)->yes()) {
5728+
$assignedTypeIsCompatible = true;
5729+
break;
5730+
}
57275731
}
57285732
}
57295733

57305734
if ($assignedTypeIsCompatible) {
5731-
$scope = $scope->assignExpression($var, $assignedExprType, $assignedNativeType);
5735+
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
57325736
} elseif ($scope->isDeclareStrictTypes()) {
57335737
$scope = $scope->assignExpression(
57345738
$var,
57355739
TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType),
5736-
TypeCombinator::intersect($assignedNativeType->toCoercedArgumentType(true), $propertyNativeType),
5740+
TypeCombinator::intersect($scope->getNativeType($assignedExpr)->toCoercedArgumentType(true), $propertyNativeType),
57375741
);
5742+
} else {
5743+
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
57385744
}
57395745
} else {
57405746
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));

tests/PHPStan/Analyser/nsrt/bug-12393b.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function doLorem(): void
5353

5454
public function doFloatTricky(){
5555
$this->float = 1;
56-
assertType('1.0', $this->float);
56+
assertType('float', $this->float);
5757
}
5858
}
5959

0 commit comments

Comments
 (0)