-
Notifications
You must be signed in to change notification settings - Fork 568
Fix phpstan/phpstan#10055: Conditional argument type not properly narrowing type #5386
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 18 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-6ci6gsl
Apr 13, 2026
+288
−8
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c80f7bf
Fix conditional parameter type narrowing for union types with 3+ members
VincentLanglet bf4eb5a
Use isSuperTypeOf for condition matching in conditional parameter types
phpstan-bot 89f8a22
Remove useSubtypeForConditionMatching flag, use two-pass matching ins…
phpstan-bot 553962a
Explain why getFiniteTypes() guard is needed in Pass 2 condition matc…
phpstan-bot 819b7fc
Use UnionType guard instead of getFiniteTypes() in Pass 2 condition m…
phpstan-bot 9c66e81
Remove UnionType guard from Pass 2 condition matching
phpstan-bot 996ff3d
Fix lint
VincentLanglet 6d27242
Add non-regression tests for #13591, #12597, and #10422
phpstan-bot 3417651
Merge rule test data files into nsrt files for #10422 and #13591
phpstan-bot 0fb28c7
Add non-regression tests for #6663 and #4090
phpstan-bot 4636a61
Add non-regression tests for #11218
phpstan-bot 3a5557b
Fix
VincentLanglet af79cb5
more readable
staabm f9e6e1f
cs
staabm df3ed28
more readable
staabm aa1f96e
Add comments explaining the two-pass condition matching in filterBySp…
phpstan-bot 219e791
Clarify comments: two-pass matching is about preferring exact matches…
phpstan-bot ffd567d
Update MutatingScope.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,32 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug10055; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @param 'value1'|'value2'|'value3' $param1 | ||
| * @param ($param1 is 'value3' ? bool : int) $param2 | ||
| */ | ||
| function test(string $param1, int|bool $param2): void | ||
| { | ||
| match ($param1) { | ||
| 'value1' => assertType('int', $param2), | ||
| 'value2' => assertType('int', $param2), | ||
| 'value3' => assertType('bool', $param2), | ||
| }; | ||
| } | ||
|
|
||
| function testScopeMerging(mixed $foo): void | ||
| { | ||
| $a = 0; | ||
| if (\is_string($foo) || \is_int($foo)) { | ||
| $a = 1; | ||
| } | ||
|
|
||
| if (\is_int($foo)) { | ||
| assertType('1', $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,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug10422; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class TestClass { | ||
| public function test(): void {} | ||
| public function something(): bool { return true; } | ||
| } | ||
|
|
||
| function test(?TestClass $test): void | ||
| { | ||
| $error = ''; | ||
|
|
||
| if (!$test) { | ||
| $error = 'missing test'; | ||
| } else if ($test->something()) { | ||
| $error = 'another'; | ||
| } | ||
| if ($error) { | ||
| die('Done'); | ||
| } | ||
| assertType('Bug10422\TestClass', $test); | ||
| $test->test(); | ||
| } |
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,24 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug13591; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function processHotel(int $hotelId): void {} | ||
|
|
||
| /** | ||
| * @param 'get_rooms'|'get_hotels' $action | ||
| */ | ||
| function test(string $action, ?int $hotelId): void | ||
| { | ||
| if ($action === 'get_rooms' && $hotelId === null) { | ||
| throw new \InvalidArgumentException('Hotel ID is required'); | ||
| } | ||
|
|
||
| if ($action === 'get_rooms') { | ||
| assertType('int', $hotelId); | ||
| processHotel($hotelId); | ||
| } | ||
| } |
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,46 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug4090; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** @param string[] $a */ | ||
| function foo(array $a): void | ||
| { | ||
| if (count($a) > 1) { | ||
| echo implode(',', $a); | ||
| } elseif (count($a) === 1) { | ||
| assertType('string', current($a)); | ||
| echo trim(current($a)); | ||
| } | ||
| } | ||
|
|
||
| /** @param string[] $a */ | ||
| function bar(array $a): void | ||
| { | ||
| $count = count($a); | ||
| if ($count > 1) { | ||
| echo implode(',', $a); | ||
| } elseif ($count === 1) { | ||
| assertType('string', current($a)); | ||
| echo trim(current($a)); | ||
| } | ||
| } | ||
|
|
||
| /** @param string[] $a */ | ||
| function qux(array $a): void | ||
| { | ||
| switch (count($a)) { | ||
| case 0: | ||
| break; | ||
| case 1: | ||
| assertType('string', current($a)); | ||
| echo trim(current($a)); | ||
| break; | ||
| default: | ||
| echo implode(',', $a); | ||
| break; | ||
| } | ||
| } |
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,24 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug6663; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| class X {} | ||
| class Y {} | ||
|
|
||
| function test(mixed $xy, mixed $ab): void | ||
| { | ||
| if ($xy instanceof X || $ab instanceof X) { | ||
| if ($xy instanceof Y) { | ||
| assertType('Bug6663\Y', $xy); | ||
| assertType('Bug6663\X', $ab); | ||
| } | ||
| if ($ab instanceof Y) { | ||
| assertType('Bug6663\X', $xy); | ||
| assertType('Bug6663\Y', $ab); | ||
| } | ||
| } | ||
| } |
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,18 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug11218Array; | ||
|
|
||
| function test(): void | ||
| { | ||
| $level = 'test'; | ||
|
|
||
| $test = []; | ||
|
|
||
| for ($i = 0 ; $i <= 3 ; $i++) { | ||
| if ($i === 0) { | ||
| $test[$level] = 'this is a'; | ||
| } else { | ||
| $test[$level] .= ' test'; | ||
| } | ||
| } | ||
| } |
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
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,16 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug11218; | ||
|
|
||
| function test(): void | ||
| { | ||
| $level = 'test'; | ||
|
|
||
| for ($i = 0 ; $i <= 3 ; $i++) { | ||
| if ($i === 0) { | ||
| $test[$level] = 'this is a'; | ||
| } else { | ||
| $test[$level] .= ' test'; | ||
| } | ||
| } | ||
| } |
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,24 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug12597; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| private const TYPE_1 = 1; | ||
| private const TYPE_2 = 2; | ||
|
|
||
| public function test(int $type): void | ||
| { | ||
| if (in_array($type, [self::TYPE_1, self::TYPE_2], true)) { | ||
| $message = 'Hello!'; | ||
| } | ||
|
|
||
| if ($type === self::TYPE_1) { | ||
| $this->message($message); | ||
| } | ||
| } | ||
|
|
||
| public function message(string $message): void {} | ||
| } |
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.
my understanding of this change is:
in the first loop (line 3221) we do a speed optimized check with
which does not work good for e.g. MixedType (because its not equal, but just a super-type of everything).
therefore we need the second loop (line 3239) to process only those expressions/types again, which did not match the fast equal-type check, but might still be useful because of "real isSuperTypeOf" typing logic...?
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.
The reviewer's understanding is correct. I've added detailed comments explaining the two-pass condition matching approach:
equals()matching — this is the fast path that handles most cases and takes priority.isSuperTypeOf()matching — this only runs when Pass 1 found no match. It handles cases where the condition type is broader than the narrowed expression type (e.g., a union condition'value1'|'value2'matching a narrowed'value1', orint|stringfrom scope merging matching a narrowedint).isSuperTypeOffor both would cause the broader match to degrade certainty viaextremeIdentity(Yes AND Maybe = Maybe), leading to false "variable might not be defined" warnings.All tests pass (11852 tests, 79360 assertions) and
make phpstanreports no errors.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.
The idea is that you might have conditionalExpression which has matching exactly and we prefer this one other the one which only match with isSuperTypeOf.
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. The comment was updated to clarify VincentLanglet's point: the two-pass approach exists to prefer exact matches over supertype matches (not just for speed). The explanation of why this preference matters (certainty degradation via
extremeIdentity) was moved from Pass 2's comment block to Pass 1's, where the preference is actually enforced.