Skip to content

Commit 939aa1f

Browse files
committed
Account for subclasses in get_parent_class() of a $this/static value
get_parent_class($this) inside a non-final class returned the declared parent (or false when there was none), treating $this as exactly the current class. Through late static binding $this can be an instance of a subclass, so for a direct child the parent is the class itself and for a deeper descendant it is a subclass. The return type now also includes class-string<Class> in that case, which removes a false-positive identical.alwaysFalse on get_parent_class($this) === self::class. Final classes and non-$this/static arguments (typed parameters, new X()) keep the exact parent type. Closes phpstan/phpstan#14951
1 parent 4eee31c commit 939aa1f

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

‎src/Type/Php/GetParentClassDynamicFunctionReturnTypeExtension.php‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use PHPStan\Type\Constant\ConstantBooleanType;
1313
use PHPStan\Type\Constant\ConstantStringType;
1414
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
15+
use PHPStan\Type\Generic\GenericClassStringType;
16+
use PHPStan\Type\ObjectType;
17+
use PHPStan\Type\StaticType;
1518
use PHPStan\Type\Type;
1619
use PHPStan\Type\TypeCombinator;
1720
use PHPStan\Type\TypeUtils;
@@ -65,7 +68,24 @@ public function getTypeFromFunctionCall(
6568

6669
$classNames = $argType->getObjectClassNames();
6770
if (count($classNames) > 0) {
68-
return TypeCombinator::union(...array_map(fn (string $classNames): Type => $this->findParentClassNameType($classNames), $classNames));
71+
// A `$this`/`static` value can be an instance of a subclass through late static
72+
// binding. For a non-final class the parent class is then not pinned to the declared
73+
// parent: a direct child's parent is the class itself, a deeper descendant's parent is
74+
// some subclass. So the result also includes `class-string<Class>`.
75+
$isLateStaticBound = $argType instanceof StaticType;
76+
77+
return TypeCombinator::union(...array_map(function (string $className) use ($isLateStaticBound): Type {
78+
$parentType = $this->findParentClassNameType($className);
79+
if (!$isLateStaticBound || !$this->reflectionProvider->hasClass($className)) {
80+
return $parentType;
81+
}
82+
83+
if ($this->reflectionProvider->getClass($className)->isFinal()) {
84+
return $parentType;
85+
}
86+
87+
return TypeCombinator::union($parentType, new GenericClassStringType(new ObjectType($className)));
88+
}, $classNames));
6989
}
7090

7191
return null;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14951;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class A
8+
{
9+
10+
public function isDirectChildOfA(): bool
11+
{
12+
// $this can be an instance of a subclass, so get_parent_class($this)
13+
// may return A::class (for a direct child) — not just false.
14+
assertType('class-string<Bug14951\A>|false', get_parent_class($this));
15+
16+
return get_parent_class($this) === self::class;
17+
}
18+
19+
}
20+
21+
class B extends A
22+
{
23+
24+
public function parentOfThis(): void
25+
{
26+
assertType('\'Bug14951\\\\A\'|class-string<Bug14951\B>', get_parent_class($this));
27+
}
28+
29+
}
30+
31+
final class FinalNoParent
32+
{
33+
34+
public function parentOfThis(): void
35+
{
36+
// Final class cannot be subclassed, so the parent is exactly false.
37+
assertType('false', get_parent_class($this));
38+
}
39+
40+
}
41+
42+
final class FinalWithParent extends A
43+
{
44+
45+
public function parentOfThis(): void
46+
{
47+
assertType("'Bug14951\\\\A'", get_parent_class($this));
48+
}
49+
50+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Foo
1010
public function doFoo()
1111
{
1212
assertType('false', get_parent_class());
13-
assertType('false', get_parent_class($this));
13+
assertType('class-string<ParentClass\Foo>|false', get_parent_class($this));
1414
assertType('class-string<$this(ParentClass\Foo)>', get_class($this));
1515
assertType('\'ParentClass\\\\Foo\'', get_class());
1616
}
@@ -25,7 +25,7 @@ class Bar extends Foo
2525
public function doBar()
2626
{
2727
assertType('\'ParentClass\\\\Foo\'', get_parent_class());
28-
assertType('\'ParentClass\\\\Foo\'', get_parent_class($this));
28+
assertType('\'ParentClass\\\\Foo\'|class-string<ParentClass\Bar>', get_parent_class($this));
2929
}
3030

3131
}

0 commit comments

Comments
 (0)