-
Notifications
You must be signed in to change notification settings - Fork 574
Resolve ConditionalType when subject-target relationship is deterministic despite containing template types
#5631
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 7 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-vd4m3b6
May 10, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fd142b7
Resolve `ConditionalType` when subject-target relationship is determi…
VincentLanglet c30702d
Exclude NSRT test for bug-11894 from PHP 7.4
phpstan-bot 32f9c23
Add non-regression test for bug 8048
phpstan-bot 092cba3
Use actual reproducer from issue #8048 and add NSRT test
phpstan-bot bed3414
Add tests for `maybe` isSuperTypeOf path in ConditionalType::isResolv…
phpstan-bot a56defe
Update bug-8048.php
staabm 790643f
Update bug-8048.php
staabm 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,70 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug11894Nsrt; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @template T | ||
| * @param T $a | ||
| * @return (T is string ? string : T) | ||
| */ | ||
| function conditionalReturn(mixed $a): mixed | ||
| { | ||
| if (!is_string($a)) { | ||
| return $a; | ||
| } | ||
| return trim($a); | ||
| } | ||
|
|
||
| /** | ||
| * @template T of string|null | ||
| * @param T $a | ||
| */ | ||
| function testNarrowedToString(mixed $a): void | ||
| { | ||
| if (!is_string($a)) { | ||
| return; | ||
| } | ||
| assertType('string', conditionalReturn($a)); | ||
| } | ||
|
|
||
| /** | ||
| * @template T of int|null | ||
| * @param T $a | ||
| */ | ||
| function testNarrowedToNonMatchingType(mixed $a): void | ||
| { | ||
| if (!is_int($a)) { | ||
| return; | ||
| } | ||
| assertType('T of int (function Bug11894Nsrt\testNarrowedToNonMatchingType(), argument)', conditionalReturn($a)); | ||
| } | ||
|
|
||
| /** | ||
| * @template T of string|int | ||
| * @param T $a | ||
| */ | ||
| function testNotFullyNarrowable(mixed $a): void | ||
| { | ||
| assertType('string|T of int (function Bug11894Nsrt\testNotFullyNarrowable(), argument)', conditionalReturn($a)); | ||
| } | ||
|
|
||
| abstract class ConditionalArrayKeys | ||
| { | ||
| /** | ||
| * @template TKey of array-key | ||
| * @template TArray of array<TKey, mixed> | ||
| * @param TArray $array | ||
| * @return (TArray is non-empty-array ? non-empty-list<TKey> : list<TKey>) | ||
| */ | ||
| abstract public function arrayKeys(array $array): array; | ||
|
|
||
| /** @param non-empty-array<int, int> $nonEmpty */ | ||
| public function testMaybeStaysUnresolved(array $nonEmpty): void | ||
| { | ||
| assertType('non-empty-list<int>', $this->arrayKeys($nonEmpty)); | ||
| } | ||
| } |
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,35 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Bug8048Nsrt; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| interface CustomResponseInterface {} | ||
|
|
||
| class CustomResponse implements CustomResponseInterface {} | ||
|
|
||
| class ApiService | ||
| { | ||
| /** | ||
| * @template T of CustomResponseInterface | ||
| * | ||
| * @param class-string<T>|null $responseType | ||
| * | ||
| * @return ($responseType is class-string<T> ? T : null) | ||
| */ | ||
| public function request(?string $responseType = null): ?CustomResponseInterface | ||
| { | ||
| if ($responseType === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return new CustomResponse(); | ||
| } | ||
| } | ||
|
|
||
| function (): void { | ||
| assertType('null', (new ApiService())->request(null)); | ||
| assertType('Bug8048Nsrt\CustomResponse', (new ApiService())->request(CustomResponse::class)); | ||
| $x = rand(0, 1) ? CustomResponse::class : null; | ||
| assertType('Bug8048Nsrt\CustomResponse|null', (new ApiService())->request($x)); | ||
| }; |
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,79 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug11894; | ||
|
|
||
| /** | ||
| * @template T of string|null | ||
| * @param T $a | ||
| */ | ||
| function test(mixed $a): mixed | ||
| { | ||
| if (!is_string($a)) { | ||
| return $a; | ||
| } | ||
|
|
||
| return conditionalReturn($a); | ||
| } | ||
|
|
||
| /** | ||
| * @template T | ||
| * @param T $a | ||
| * @return (T is string ? string : T) | ||
| */ | ||
| function conditionalReturn(mixed $a): mixed | ||
| { | ||
| if (!is_string($a)) { | ||
| return $a; | ||
| } | ||
|
|
||
| return trim($a); | ||
| } | ||
|
|
||
| /** | ||
| * @template T of string|null | ||
| * @param T $a | ||
| */ | ||
| function testNegated(mixed $a): mixed | ||
| { | ||
| if (!is_string($a)) { | ||
| return $a; | ||
| } | ||
|
|
||
| return conditionalReturnNegated($a); | ||
| } | ||
|
|
||
| /** | ||
| * @template T | ||
| * @param T $a | ||
| * @return (T is not string ? T : string) | ||
| */ | ||
| function conditionalReturnNegated(mixed $a): mixed | ||
| { | ||
| if (!is_string($a)) { | ||
| return $a; | ||
| } | ||
|
|
||
| return trim($a); | ||
| } | ||
|
|
||
| /** | ||
| * @template T of int|null | ||
| * @param T $a | ||
| */ | ||
| function testNoRelation(mixed $a): mixed | ||
| { | ||
| if (!is_int($a)) { | ||
| return $a; | ||
| } | ||
|
|
||
| return conditionalReturn($a); | ||
| } | ||
|
|
||
| /** | ||
| * @template T of string|int | ||
| * @param T $a | ||
| */ | ||
| function testMaybeRelation(mixed $a): mixed | ||
| { | ||
| return conditionalReturn($a); | ||
| } |
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,81 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug11894Methods; | ||
|
|
||
| class Converter | ||
| { | ||
| /** | ||
| * @template T | ||
| * @param T $a | ||
| * @return (T is string ? string : T) | ||
| */ | ||
| public function conditionalReturn(mixed $a): mixed | ||
| { | ||
| if (!is_string($a)) { | ||
| return $a; | ||
| } | ||
| return trim($a); | ||
| } | ||
|
|
||
| /** | ||
| * @template T | ||
| * @param T $a | ||
| * @return (T is string ? string : T) | ||
| */ | ||
| public static function conditionalReturnStatic(mixed $a): mixed | ||
| { | ||
| if (!is_string($a)) { | ||
| return $a; | ||
| } | ||
| return trim($a); | ||
| } | ||
| } | ||
|
|
||
| class Consumer | ||
| { | ||
| /** | ||
| * @template T of string|null | ||
| * @param T $a | ||
| */ | ||
| public function testMethod(mixed $a): mixed | ||
| { | ||
| if (!is_string($a)) { | ||
| return $a; | ||
| } | ||
|
|
||
| $c = new Converter(); | ||
| return $c->conditionalReturn($a); | ||
| } | ||
|
|
||
| /** | ||
| * @template T of string|null | ||
| * @param T $a | ||
| */ | ||
| public function testStaticMethod(mixed $a): mixed | ||
| { | ||
| if (!is_string($a)) { | ||
| return $a; | ||
| } | ||
|
|
||
| return Converter::conditionalReturnStatic($a); | ||
| } | ||
|
|
||
| /** | ||
| * @template T of string|int | ||
| * @param T $a | ||
| */ | ||
| public function testMaybeMethod(mixed $a): mixed | ||
| { | ||
| $c = new Converter(); | ||
| return $c->conditionalReturn($a); | ||
| } | ||
|
|
||
| /** | ||
| * @template T of string|int | ||
| * @param T $a | ||
| */ | ||
| public function testMaybeStaticMethod(mixed $a): mixed | ||
| { | ||
| return Converter::conditionalReturnStatic($a); | ||
| } | ||
| } |
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,33 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Bug8048; | ||
|
|
||
| interface CustomResponseInterface {} | ||
|
|
||
| class CustomResponse implements CustomResponseInterface {} | ||
|
|
||
| class ApiService | ||
| { | ||
| /** | ||
| * @template T of CustomResponseInterface | ||
| * | ||
| * @param class-string<T>|null $responseType | ||
| * | ||
| * @return ($responseType is class-string<T> ? T : null) | ||
| */ | ||
| public function request(?string $responseType = null): ?CustomResponseInterface | ||
| { | ||
| if ($responseType === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return new CustomResponse(); | ||
| } | ||
| } | ||
|
|
||
| function (): void { | ||
| (new ApiService())->request(null); | ||
| (new ApiService())->request(CustomResponse::class); | ||
| $x = rand(0, 1) ? CustomResponse::class : null; | ||
| (new ApiService())->request($x); | ||
| }; |
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.
mutation testing suggests we miss tests for this line when $isSuperType is maybe
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.
Pushed. Here's what I did to address staabm's review comment:
Added NSRT test for
maybepath (tests/PHPStan/Analyser/nsrt/bug-11894.php): A multi-template class method with@return (TArray is non-empty-array ? non-empty-list<TKey> : list<TKey>)called with anon-empty-array<int, int>argument. This verifies the correct typenon-empty-list<int>is inferred. IfisResolvable()incorrectly returnedtruefor themaybecase, the conditional would resolve prematurely to the union of both branches (list<int>) — this test catches that mutation.Added rule test data for
maybecases: AddedtestMaybeRelation(functions) andtestMaybeMethod/testMaybeStaticMethod(methods) that call the conditional return functions withT of string|int. These verify no false "unresolvable type" errors are reported for themaybepath, sinceTypeCombinator::remove(T of string|int, string)producesint(notnever).