Fix #13029: False match.unhandled on array with multiple booleans#5107
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix #13029: False match.unhandled on array with multiple booleans#5107phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Fixed TypeCombinator::remove() to handle removal of types with multiple finite types - Previously only removed types with exactly 1 finite type, now handles any count - This caused match expressions with array conditions and multi-condition arms to not narrow types properly - New regression test in tests/PHPStan/Rules/Comparison/data/bug-13029.php Closes phpstan/phpstan#13029
VincentLanglet
approved these changes
Feb 28, 2026
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
Seems fine to me ; but this seems to not be enough for the related issue phpstan/phpstan#10128
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes a false positive
match.unhandlederror when using a match expression with an array subject containing multiple booleans and multi-condition arms (comma-separated conditions).For example, this valid code was incorrectly reported as having an unhandled
array{true, bool}:Changes
TypeCombinator::remove()insrc/Type/TypeCombinator.phpto handle removal of types with multiple finite typestests/PHPStan/Rules/Comparison/data/bug-13029.phptestBug13029intests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.phpRoot cause
When a match arm has multiple conditions (e.g.,
[true, false], [true, true]), PHPStan builds anin_array()expression for type narrowing. TheSpecifiedTypes::unionWith()correctly combines the sureNotTypes from each condition into a union type (e.g.,array{true, false} | array{true, true}simplifies toarray{true, bool}).However,
TypeCombinator::remove()had a restriction at line 93 that only performed finite-type-based removal when the type to remove had exactly 1 finite type (count($finiteTypesToRemove) === 1). The unionarray{true, bool}has 2 finite types (array{true, true}andarray{true, false}), so the removal was silently skipped, leaving the match condition type un-narrowed.The fix changes the condition to
count($finiteTypesToRemove) > 0and compares against all finite types to remove, not just the first one.Test
Added a regression test that reproduces the exact scenario from the issue: a match expression with
[$bool1, $bool2]as the subject where the first arm uses comma-separated array conditions[true, false], [true, true]. The test expects no errors (empty expected errors array).Fixes phpstan/phpstan#13029