Skip to content

Commit 8d3f5de

Browse files
committed
Do not resolve a late-static-bound get_parent_class() argument inside a trait
The in-trait guard already bails out for $this. Apply it to static::class as well, so a trait does not resolve a late-static-bound argument against one particular using class. Outside a trait static::class keeps the narrowed type.
1 parent 3b0a343 commit 8d3f5de

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎src/Type/Php/GetParentClassDynamicFunctionReturnTypeExtension.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ public function getTypeFromFunctionCall(
7676
}
7777
}
7878

79+
// Inside a trait a late-static-bound value cannot be resolved to a concrete parent,
80+
// same as `$this` above.
81+
if ($scope->isInTrait() && $valueType instanceof StaticType) {
82+
return null;
83+
}
84+
7985
$classNames = $valueType->getObjectClassNames();
8086
if (count($classNames) > 0) {
8187
// A `$this`/`static` value can be an instance of a subclass through late static

‎tests/PHPStan/Analyser/nsrt/get-parent-class.php‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ trait FooTrait
4545

4646
public function doBaz()
4747
{
48-
// Inside a trait the using class is unknown, so $this defers to the general type.
48+
// Inside a trait a late-static-bound argument is not resolved to a concrete parent.
4949
assertType('class-string|false', get_parent_class());
5050
assertType('class-string|false', get_parent_class($this));
51-
// self::class / static::class resolve against the using class (Bar).
52-
assertType('\'ParentClass\\\\Foo\'', get_parent_class(self::class));
53-
assertType('\'ParentClass\\\\Foo\'|class-string<ParentClass\Bar>', get_parent_class(static::class));
51+
assertType('class-string|false', get_parent_class(static::class));
5452
}
5553

5654
}

0 commit comments

Comments
 (0)