Skip to content

Commit ba2b475

Browse files
committed
Fix forgetting static property access after impure call
1 parent 67fb7a6 commit ba2b475

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/Analyser/MutatingScope.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4379,6 +4379,14 @@ private function shouldInvalidateExpression(string $exprStringToInvalidate, Expr
43794379
$nodeFinder = new NodeFinder();
43804380
$expressionToInvalidateClass = get_class($exprToInvalidate);
43814381
$found = $nodeFinder->findFirst([$expr], function (Node $node) use ($expressionToInvalidateClass, $exprStringToInvalidate): bool {
4382+
if (
4383+
$exprStringToInvalidate === '$this'
4384+
&& $node instanceof Name
4385+
&& in_array($node->name, ['self', 'static', 'parent'], true)
4386+
) {
4387+
return true;
4388+
}
4389+
43824390
if (!$node instanceof $expressionToInvalidateClass) {
43834391
return false;
43844392
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public function __construct()
7272
assertNativeType('int', self::$i);
7373

7474
$this->impureCall();
75-
assertType('int', self::$i); // should be float|int
76-
assertNativeType('int', self::$i); // should be float|int
75+
assertType('float|int', self::$i);
76+
assertNativeType('float|int', self::$i);
7777
}
7878

7979
public function doFoo(): void {

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public function __construct()
7272
assertNativeType('int', self::$i);
7373

7474
$this->impureCall();
75-
assertType('int', self::$i); // should be float|int
76-
assertNativeType('int', self::$i); // should be float|int
75+
assertType('float|int', self::$i);
76+
assertNativeType('float|int', self::$i);
7777
}
7878

7979
public function doFoo(): void {
@@ -85,6 +85,33 @@ public function doFoo(): void {
8585
public function impureCall(): void {}
8686
}
8787

88+
class BaseClass
89+
{
90+
static protected int|float $i;
91+
}
92+
93+
class UsesBaseClass extends BaseClass
94+
{
95+
public function __construct()
96+
{
97+
parent::$i = getInt();
98+
assertType('int', parent::$i);
99+
assertNativeType('int', parent::$i);
100+
101+
$this->impureCall();
102+
assertType('float|int', parent::$i);
103+
assertNativeType('float|int', parent::$i);
104+
}
105+
106+
public function doFoo(): void {
107+
assertType('float|int', parent::$i);
108+
assertNativeType('float|int', parent::$i);
109+
}
110+
111+
/** @phpstan-impure */
112+
public function impureCall(): void {}
113+
}
114+
88115
function getInt(): int {
89116
return 1;
90117
}

0 commit comments

Comments
 (0)