-
Notifications
You must be signed in to change notification settings - Fork 556
Fix phpstan/phpstan#9732: BenevolentUnionType with generics produce confusing errors #5070
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 3 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-wq026g0
Mar 2, 2026
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| use PHPStan\Type\TypeUtils; | ||
| use PHPStan\Type\UnionType; | ||
| use PHPStan\Type\VerbosityLevel; | ||
| use function count; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
|
|
@@ -287,6 +288,23 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap | |
| ]))->union($map); | ||
| } | ||
|
|
||
| if ($receivedType instanceof UnionType) { | ||
| $matchingTypes = []; | ||
| foreach ($receivedType->getTypes() as $innerType) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just had the idea that we only need todo this work when the bound maybe matches |
||
| if (!$resolvedBound->isSuperTypeOf($innerType)->yes()) { | ||
| continue; | ||
| } | ||
|
|
||
| $matchingTypes[] = $innerType; | ||
| } | ||
| if (count($matchingTypes) > 0) { | ||
| $filteredType = TypeCombinator::union(...$matchingTypes); | ||
| return (new TemplateTypeMap([ | ||
| $this->name => $filteredType, | ||
| ]))->union($map); | ||
| } | ||
| } | ||
|
|
||
| return $map; | ||
| } | ||
|
|
||
|
|
||
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,48 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug12558; | ||
|
|
||
| class Foo | ||
| { | ||
| /** | ||
| * @template T of object | ||
| * | ||
| * @param T $object | ||
| * | ||
| * @return (T is static ? T : static) | ||
| */ | ||
| public static function assertStatic(object $object) | ||
| { | ||
| if (!$object instanceof static) { | ||
| throw new \Error('Object is not an instance of static class'); | ||
| } | ||
|
|
||
| return $object; | ||
| } | ||
|
|
||
| protected function createFoo(): self | ||
| { | ||
| return new Foo(); | ||
| } | ||
|
|
||
| protected function createFooNullable(): ?self | ||
| { | ||
| return new Foo(); | ||
| } | ||
|
|
||
| protected function createFooUnionedWithBool(): self|bool | ||
| { | ||
| return new Foo(); | ||
| } | ||
|
|
||
| protected function foo(): void | ||
| { | ||
| } | ||
|
|
||
| public function testAssertInstanceOf(): void | ||
| { | ||
| (static::class)::assertStatic($this->createFoo())->foo(); | ||
| (static::class)::assertStatic($this->createFooNullable())->foo(); | ||
| (static::class)::assertStatic($this->createFooUnionedWithBool())->foo(); | ||
| } | ||
| } |
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 Bug9732; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** | ||
| * @phpstan-template TKeyType of string | ||
| * @phpstan-template TValueType | ||
| * @phpstan-param array<TKeyType, TValueType> $array | ||
| * @phpstan-return \Generator<TKeyType, TValueType, void, void> | ||
| */ | ||
| public static function stringifyKeys(array $array) : \Generator{ | ||
| foreach($array as $key => $value){ | ||
| yield (string) $key => $value; | ||
| } | ||
| } | ||
|
|
||
| public function sayHello(): void | ||
| { | ||
| self::stringifyKeys($GLOBALS); | ||
| } | ||
| } |
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.
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.
can we drop the previous if on line 285 than? seems like its redundant now?
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.
It's not redundant, the previous
ifwill works