-
Notifications
You must be signed in to change notification settings - Fork 579
Account for subclasses in get_parent_class() of a $this/static value #6048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
staabm
merged 11 commits into
phpstan:2.2.x
from
zonuexe:fix/get-parent-class-non-final-this
Jul 14, 2026
+174
−4
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
050504e
Account for subclasses in get_parent_class() of a $this/static value
zonuexe 15e73fc
Add rule test for get_parent_class($this) === self::class not being a…
zonuexe fabdaf6
Build the get_parent_class() return type with a foreach and a single …
zonuexe 2903e1e
Assert get_called_class()/get_class() are unaffected by the same problem
zonuexe 8d0091a
Cover get_parent_class(self::class) keeping the exact parent
zonuexe 8f1f338
Narrow get_parent_class(static::class) like get_parent_class($this)
zonuexe b342212
Cover get_parent_class() with self::class/static::class inside a trai…
zonuexe 3ac3184
Do not resolve a late-static-bound get_parent_class() argument inside…
zonuexe fc781b9
Cover get_parent_class(self::class) inside a trait
zonuexe 2e4c4af
Cover @final classes in get_parent_class()
zonuexe 035e89f
Resolve the class-string object type without a TrinaryLogic check
zonuexe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14951; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class A | ||
| { | ||
|
|
||
| public function isDirectChildOfA(): bool | ||
| { | ||
| // $this can be an instance of a subclass, so get_parent_class($this) | ||
| // may return A::class (for a direct child) — not just false. | ||
| assertType('class-string<Bug14951\A>|false', get_parent_class($this)); | ||
|
|
||
| // The sibling functions already model late static binding, so they do not have the | ||
| // same problem: they keep the subclass possibility instead of pinning to this class. | ||
| assertType('class-string<static(Bug14951\A)>', get_called_class()); | ||
| assertType('class-string<$this(Bug14951\A)>', get_class($this)); | ||
|
|
||
| // A class-string argument (e.g. self::class) names an exact class, not a runtime | ||
| // value, so it keeps the exact parent — no subclass widening. | ||
| assertType('false', get_parent_class(self::class)); | ||
|
|
||
| // static::class is late static bound just like $this, so it is widened the same way. | ||
| assertType('class-string<Bug14951\A>|false', get_parent_class(static::class)); | ||
|
|
||
| return get_parent_class($this) === self::class; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class B extends A | ||
| { | ||
|
|
||
| public function parentOfThis(): void | ||
| { | ||
| assertType('\'Bug14951\\\\A\'|class-string<Bug14951\B>', get_parent_class($this)); | ||
| // self::class is the exact class B, so its parent is exactly A. | ||
| assertType("'Bug14951\\\\A'", get_parent_class(self::class)); | ||
| // static::class is late static bound, matching get_parent_class($this). | ||
| assertType('\'Bug14951\\\\A\'|class-string<Bug14951\B>', get_parent_class(static::class)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| final class FinalNoParent | ||
| { | ||
|
|
||
| public function parentOfThis(): void | ||
| { | ||
| // Final class cannot be subclassed, so the parent is exactly false. | ||
| assertType('false', get_parent_class($this)); | ||
| assertType('false', get_parent_class(static::class)); | ||
| // ... and get_called_class() is pinned to the final class. | ||
| assertType("'Bug14951\\\\FinalNoParent'", get_called_class()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| final class FinalWithParent extends A | ||
| { | ||
|
|
||
| public function parentOfThis(): void | ||
| { | ||
| assertType("'Bug14951\\\\A'", get_parent_class($this)); | ||
| assertType("'Bug14951\\\\A'", get_parent_class(static::class)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** @final */ | ||
| class PhpDocFinalNoParent | ||
| { | ||
|
|
||
| public function parentOfThis(): void | ||
| { | ||
| // A @final class cannot be subclassed either, so there is no subclass widening. | ||
| assertType('false', get_parent_class($this)); | ||
| assertType('false', get_parent_class(static::class)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** @final */ | ||
| class PhpDocFinalWithParent extends A | ||
| { | ||
|
|
||
| public function parentOfThis(): void | ||
| { | ||
| assertType("'Bug14951\\\\A'", get_parent_class($this)); | ||
| assertType("'Bug14951\\\\A'", get_parent_class(static::class)); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14951StrictComparison; | ||
|
|
||
| class A | ||
| { | ||
|
|
||
| public function isDirectChildOfA(): bool | ||
| { | ||
| // $this can be an instance of a subclass, so get_parent_class($this) | ||
| // can equal self::class for a direct child — this comparison is not always false. | ||
| return get_parent_class($this) === self::class; | ||
| } | ||
|
|
||
| } | ||
|
staabm marked this conversation as resolved.
|
||
|
|
||
| class B extends A | ||
| { | ||
| } | ||
|
|
||
| class C extends B | ||
| { | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.