-
Notifications
You must be signed in to change notification settings - Fork 564
Fix method call on unioned class type analysis is broken #5225
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
VincentLanglet
merged 5 commits into
phpstan:2.1.x
from
VincentLanglet:create-pull-request/patch-8y7mxaj
Mar 25, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7c9013a
Fix static return type resolution on union class types
phpstan-bot 030fe22
Add regression test for #12562
phpstan-bot 9526da6
Fix CI failures [claude-ci-fix]
phpstan-bot eae15bb
Fix CI failures [claude-ci-fix]
phpstan-bot 8943feb
Add test
VincentLanglet 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,60 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug11687; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class A | ||
| { | ||
| public static function retStaticConst(): int | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| /** | ||
| * @return static | ||
| */ | ||
| public static function retStatic() | ||
| { | ||
| return new static(); // @phpstan-ignore new.static | ||
| } | ||
| } | ||
|
|
||
| class B extends A | ||
| { | ||
| /** | ||
| * @return 2 | ||
| */ | ||
| public static function retStaticConst(): int | ||
| { | ||
| return 2; | ||
| } | ||
|
|
||
| public function foo(): void | ||
| { | ||
| $clUnioned = mt_rand() === 0 | ||
| ? A::class | ||
| : X::class; | ||
|
|
||
| assertType('int', A::retStaticConst()); | ||
| assertType('bool', X::retStaticConst()); | ||
| assertType('bool|int', $clUnioned::retStaticConst()); | ||
|
|
||
| assertType('Bug11687\A', A::retStatic()); | ||
| assertType('bool', X::retStatic()); | ||
| assertType('bool|Bug11687\A', $clUnioned::retStatic()); | ||
| } | ||
| } | ||
|
|
||
| class X | ||
| { | ||
| public static function retStaticConst(): bool | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| public static function retStatic(): bool | ||
| { | ||
| return false; | ||
| } | ||
| } |
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,38 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| namespace Bug12562; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class UserV1 | ||
| { | ||
| public function toV2(): UserV2 | ||
| { | ||
| return new UserV2(); | ||
| } | ||
| } | ||
|
|
||
| class UserV2 | ||
| { | ||
| public function toV2(): self | ||
| { | ||
| return $this; | ||
| } | ||
| } | ||
|
|
||
| class UserV2a extends UserV2 | ||
| { | ||
| /** | ||
| * @return $this | ||
| */ | ||
| public function toV2(): self | ||
| { | ||
| return $this; | ||
| } | ||
| } | ||
|
|
||
| function doSomething(UserV1|UserV2 $user, UserV1|UserV2a $user2): void | ||
| { | ||
| assertType('Bug12562\UserV2', $user->toV2()); | ||
| assertType('Bug12562\UserV2', $user2->toV2()); | ||
| } |
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,90 @@ | ||
| <?php // lint >= 8.1 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug14203; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @template TKey of array-key | ||
| * @template TValue | ||
| */ | ||
| class Collection | ||
| { | ||
| /** | ||
| * Create a new collection. | ||
| * | ||
| * @param array<TKey, TValue> $items | ||
| */ | ||
| final public function __construct(protected $items = []) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @template TMapValue | ||
| * | ||
| * @param callable(TValue, TKey): TMapValue $callback | ||
| * @return static<TKey, TMapValue> | ||
| */ | ||
| public function map(callable $callback) | ||
| { | ||
| $newItems = []; | ||
|
|
||
| foreach ($this->items as $key => $value) { | ||
| $newItems[$key] = $callback($value, $key); | ||
| } | ||
|
|
||
| return new static($newItems); | ||
| } | ||
| } | ||
|
|
||
| class SpecificA { | ||
| public function __construct( | ||
| public readonly int $valueA, | ||
| public readonly string $someSharedValue, | ||
| ) {} | ||
| } | ||
|
|
||
| class SpecificB { | ||
| public function __construct( | ||
| public readonly int $valueB, | ||
| public readonly string $someSharedValue, | ||
| ) {} | ||
| } | ||
|
|
||
| class MyDTO { | ||
| public function __construct( | ||
| public readonly string $thatSharedValue, | ||
| ) {} | ||
| } | ||
|
|
||
| function works(): void { | ||
| $myCollection = new Collection([new SpecificA(1, 'A'), new SpecificB(2, 'B')]); | ||
|
|
||
| $result = $myCollection->map(static fn (SpecificA|SpecificB $specific): MyDTO => new MyDTO($specific->someSharedValue)); | ||
| assertType('Bug14203\Collection<int, Bug14203\MyDTO>', $result); | ||
| } | ||
|
|
||
| /** | ||
| * @return Collection<int, SpecificA> | ||
| */ | ||
| function getA(): Collection { | ||
| return new Collection([new SpecificA(1, 'A')]); | ||
| } | ||
|
|
||
| /** | ||
| * @return Collection<int, SpecificB> | ||
| */ | ||
| function getB(): Collection { | ||
| return new Collection([new SpecificB(2, 'B')]); | ||
| } | ||
|
|
||
| function breaks(): void { | ||
| $myCollection = random_int(0, 1) === 0 | ||
| ? getA() | ||
| : getB(); | ||
|
|
||
| $result = $myCollection->map(static fn (SpecificA|SpecificB $specific): MyDTO => new MyDTO($specific->someSharedValue)); | ||
| assertType('Bug14203\Collection<int, Bug14203\MyDTO>', $result); | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file mixes tabs and spaces for indent