Skip to content

Commit 812d947

Browse files
committed
implement feedback
1 parent 189f5c6 commit 812d947

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5625,18 +5625,27 @@ static function (): void {
56255625
$assignedExprType = $scope->getType($assignedExpr);
56265626
$nodeCallback(new PropertyAssignNode($var, $assignedExpr, $isAssignOp), $scope);
56275627
if ($propertyReflection->canChangeTypeAfterAssignment()) {
5628-
if ($propertyReflection->hasNativeType() && $scope->isDeclareStrictTypes()) {
5628+
if ($propertyReflection->hasNativeType()) {
56295629
$assignedNativeType = $scope->getNativeType($assignedExpr);
56305630
$propertyNativeType = $propertyReflection->getNativeType();
56315631

5632-
$newAssignedType = TypeCombinator::intersect($assignedExprType, $propertyNativeType);
5633-
$newAssignedNativeType = TypeCombinator::intersect($assignedNativeType, $propertyNativeType);
5634-
if ($newAssignedType instanceof NeverType || $newAssignedNativeType instanceof NeverType) {
5635-
$newAssignedType = TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType);
5636-
$newAssignedNativeType = TypeCombinator::intersect($assignedNativeType->toCoercedArgumentType(true), $propertyNativeType);
5632+
$assignedTypeIsCompatible = false;
5633+
foreach(TypeUtils::flattenTypes($propertyNativeType) as $type) {
5634+
if ($type->isSuperTypeOf($assignedNativeType)->yes()) {
5635+
$assignedTypeIsCompatible = true;
5636+
break;
5637+
}
56375638
}
56385639

5639-
$scope = $scope->assignExpression($var, $newAssignedType, $newAssignedNativeType);
5640+
if (!$assignedTypeIsCompatible && $scope->isDeclareStrictTypes()) {
5641+
$scope = $scope->assignExpression(
5642+
$var,
5643+
TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType),
5644+
TypeCombinator::intersect($assignedNativeType->toCoercedArgumentType(true), $propertyNativeType)
5645+
);
5646+
} else {
5647+
$scope = $scope->assignExpression($var, $assignedExprType, $assignedNativeType);
5648+
}
56405649
} else {
56415650
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
56425651
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@
55
use function PHPStan\Testing\assertNativeType;
66
use function PHPStan\Testing\assertType;
77

8+
class NarrowsNativeConstantValue
9+
{
10+
private readonly int|float $i;
11+
12+
public function __construct()
13+
{
14+
$this->i = 1;
15+
}
16+
17+
public function doFoo(): void
18+
{
19+
assertType('1', $this->i);
20+
assertNativeType('1', $this->i);
21+
}
22+
}
23+
24+
function getInt(): int
25+
{
26+
return 1;
27+
}
28+
29+
830
class NarrowsNativeReadonlyUnion {
931
private readonly int|float $i;
1032

0 commit comments

Comments
 (0)