Skip to content

Commit 85ff2e2

Browse files
committed
works only on instance methods
1 parent b7477ac commit 85ff2e2

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5711,15 +5711,8 @@ static function (): void {
57115711
$assignedNativeType = $scope->getNativeType($assignedExpr);
57125712
$propertyNativeType = $propertyReflection->getNativeType();
57135713

5714-
$newAssignedType = TypeCombinator::intersect($assignedExprType, $propertyNativeType);
5715-
if ($newAssignedType instanceof NeverType) {
5716-
$newAssignedType = TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType);
5717-
}
5718-
5719-
$newAssignedNativeType = TypeCombinator::intersect($assignedNativeType, $propertyNativeType);
5720-
if ($newAssignedNativeType instanceof NeverType) {
5721-
$newAssignedNativeType = TypeCombinator::intersect($assignedNativeType->toCoercedArgumentType(true), $propertyNativeType);
5722-
}
5714+
$newAssignedType = TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType);
5715+
$newAssignedNativeType = TypeCombinator::intersect($assignedNativeType->toCoercedArgumentType(true), $propertyNativeType);
57235716

57245717
$scope = $scope->assignExpression($var, $newAssignedType, $newAssignedNativeType);
57255718
} else {

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use function PHPStan\Testing\assertNativeType;
66
use function PHPStan\Testing\assertType;
77

8-
class NarrowsNativeUnion {
8+
class NarrowsNativeReadonlyUnion {
99
private readonly int|float $i;
1010

1111
public function __construct()
@@ -21,17 +21,39 @@ public function doFoo(): void {
2121
}
2222
}
2323

24+
class NarrowsNativeUnion {
25+
private int|float $i;
26+
27+
public function __construct()
28+
{
29+
$this->i = getInt();
30+
assertType('int', $this->i);
31+
assertNativeType('int', $this->i);
32+
33+
$this->impureCall();;
34+
assertType('float|int', $this->i);
35+
assertNativeType('float|int', $this->i);
36+
}
37+
38+
public function doFoo(): void {
39+
assertType('float|int', $this->i);
40+
assertNativeType('float|int', $this->i);
41+
}
42+
43+
/** @phpstan-impure */
44+
public function impureCall(): void {}
45+
}
46+
2447
class NarrowsStaticNativeUnion {
2548
private static int|float $i;
2649

2750
public function __construct()
2851
{
2952
self::$i = getInt();
30-
assertType('int', self::$i);
31-
assertNativeType('int', self::$i);
53+
assertType('float|int', self::$i); // could be int
54+
assertNativeType('float|int', self::$i); // could be int
3255

3356
$this->impureCall();
34-
3557
assertType('float|int', self::$i);
3658
assertNativeType('float|int', self::$i);
3759
}

0 commit comments

Comments
 (0)