|
| 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 | + // The sibling functions already model late static binding, so they do not have the |
| 17 | + // same problem: they keep the subclass possibility instead of pinning to this class. |
| 18 | + assertType('class-string<static(Bug14951\A)>', get_called_class()); |
| 19 | + assertType('class-string<$this(Bug14951\A)>', get_class($this)); |
| 20 | + |
| 21 | + // A class-string argument (e.g. self::class) names an exact class, not a runtime |
| 22 | + // value, so it keeps the exact parent — no subclass widening. |
| 23 | + assertType('false', get_parent_class(self::class)); |
| 24 | + |
| 25 | + // static::class is late static bound just like $this, so it is widened the same way. |
| 26 | + assertType('class-string<Bug14951\A>|false', get_parent_class(static::class)); |
| 27 | + |
| 28 | + return get_parent_class($this) === self::class; |
| 29 | + } |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +class B extends A |
| 34 | +{ |
| 35 | + |
| 36 | + public function parentOfThis(): void |
| 37 | + { |
| 38 | + assertType('\'Bug14951\\\\A\'|class-string<Bug14951\B>', get_parent_class($this)); |
| 39 | + // self::class is the exact class B, so its parent is exactly A. |
| 40 | + assertType("'Bug14951\\\\A'", get_parent_class(self::class)); |
| 41 | + // static::class is late static bound, matching get_parent_class($this). |
| 42 | + assertType('\'Bug14951\\\\A\'|class-string<Bug14951\B>', get_parent_class(static::class)); |
| 43 | + } |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +final class FinalNoParent |
| 48 | +{ |
| 49 | + |
| 50 | + public function parentOfThis(): void |
| 51 | + { |
| 52 | + // Final class cannot be subclassed, so the parent is exactly false. |
| 53 | + assertType('false', get_parent_class($this)); |
| 54 | + assertType('false', get_parent_class(static::class)); |
| 55 | + // ... and get_called_class() is pinned to the final class. |
| 56 | + assertType("'Bug14951\\\\FinalNoParent'", get_called_class()); |
| 57 | + } |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +final class FinalWithParent extends A |
| 62 | +{ |
| 63 | + |
| 64 | + public function parentOfThis(): void |
| 65 | + { |
| 66 | + assertType("'Bug14951\\\\A'", get_parent_class($this)); |
| 67 | + assertType("'Bug14951\\\\A'", get_parent_class(static::class)); |
| 68 | + } |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +/** @final */ |
| 73 | +class PhpDocFinalNoParent |
| 74 | +{ |
| 75 | + |
| 76 | + public function parentOfThis(): void |
| 77 | + { |
| 78 | + // A @final class cannot be subclassed either, so there is no subclass widening. |
| 79 | + assertType('false', get_parent_class($this)); |
| 80 | + assertType('false', get_parent_class(static::class)); |
| 81 | + } |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +/** @final */ |
| 86 | +class PhpDocFinalWithParent extends A |
| 87 | +{ |
| 88 | + |
| 89 | + public function parentOfThis(): void |
| 90 | + { |
| 91 | + assertType("'Bug14951\\\\A'", get_parent_class($this)); |
| 92 | + assertType("'Bug14951\\\\A'", get_parent_class(static::class)); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments