Skip to content

Commit d0a40f8

Browse files
committed
fix
1 parent c6f0e16 commit d0a40f8

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

src/Analyser/NodeScopeResolver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5637,14 +5637,14 @@ static function (): void {
56375637
}
56385638
}
56395639

5640-
if (!$assignedTypeIsCompatible && $scope->isDeclareStrictTypes()) {
5640+
if ($assignedTypeIsCompatible) {
5641+
$scope = $scope->assignExpression($var, $assignedExprType, $assignedNativeType);
5642+
} elseif ($scope->isDeclareStrictTypes()) {
56415643
$scope = $scope->assignExpression(
56425644
$var,
56435645
TypeCombinator::intersect($assignedExprType->toCoercedArgumentType(true), $propertyNativeType),
56445646
TypeCombinator::intersect($assignedNativeType->toCoercedArgumentType(true), $propertyNativeType),
56455647
);
5646-
} else {
5647-
$scope = $scope->assignExpression($var, $assignedExprType, $assignedNativeType);
56485648
}
56495649
} else {
56505650
$scope = $scope->assignExpression($var, $assignedExprType, $scope->getNativeType($assignedExpr));
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php declare(strict_types = 0); // lint >= 8.1
2+
3+
namespace Bug12902;
4+
5+
use function PHPStan\Testing\assertNativeType;
6+
use function PHPStan\Testing\assertType;
7+
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+
class NarrowsNativeReadonlyUnion {
25+
private readonly 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+
34+
public function doFoo(): void {
35+
assertType('int', $this->i);
36+
assertNativeType('int', $this->i);
37+
}
38+
}
39+
40+
class NarrowsNativeUnion {
41+
private int|float $i;
42+
43+
public function __construct()
44+
{
45+
$this->i = getInt();
46+
assertType('int', $this->i);
47+
assertNativeType('int', $this->i);
48+
49+
$this->impureCall();
50+
assertType('float|int', $this->i);
51+
assertNativeType('float|int', $this->i);
52+
}
53+
54+
public function doFoo(): void {
55+
assertType('float|int', $this->i);
56+
assertNativeType('float|int', $this->i);
57+
}
58+
59+
/** @phpstan-impure */
60+
public function impureCall(): void {}
61+
}
62+
63+
class NarrowsStaticNativeUnion {
64+
private static int|float $i;
65+
66+
public function __construct()
67+
{
68+
self::$i = getInt();
69+
assertType('int', self::$i);
70+
assertNativeType('int', self::$i);
71+
72+
$this->impureCall();
73+
assertType('int', self::$i); // should be float|int
74+
assertNativeType('int', self::$i); // should be float|int
75+
}
76+
77+
public function doFoo(): void {
78+
assertType('float|int', self::$i);
79+
assertNativeType('float|int', self::$i);
80+
}
81+
82+
/** @phpstan-impure */
83+
public function impureCall(): void {}
84+
}
85+
86+
function getInt(): int {
87+
return 1;
88+
}

0 commit comments

Comments
 (0)