Fix phpstan/phpstan#14412: Match on ::class reports unhandled values for generic sealed class hierarchy#5369
Merged
ondrejmirtes merged 1 commit intophpstan:2.1.xfrom Mar 31, 2026
Conversation
…rchies - GenericObjectType::changeSubtractedType() now delegates to parent's sealed type logic, returning NeverType when all allowed subtypes are subtracted - This fixes match expressions on $foo::class reporting "unhandled values" for generic @phpstan-sealed class hierarchies - New regression test in tests/PHPStan/Rules/Comparison/data/bug-14412.php Closes phpstan/phpstan#14412
This was referenced Mar 31, 2026
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
Match expressions on
$foo::classagainst@phpstan-sealedclass hierarchies with generic type parameters (@template) falsely reported "Match expression does not handle remaining values" even when all allowed subtypes were covered. The non-generic case was already fixed in #5305.Changes
GenericObjectType::changeSubtractedType()insrc/Type/Generic/GenericObjectType.phpto delegate to the parentObjectType::changeSubtractedType()which contains the sealed type exhaustiveness logicphpstan-baseline.neonto account for the newinstanceof ObjectTypeusagetests/PHPStan/Rules/Comparison/data/bug-14412.phpwith test method inMatchExpressionRuleTest.phpRoot cause
GenericObjectType::changeSubtractedType()was overriding the parentObjectType::changeSubtractedType()without including its sealed type logic. When all allowed subtypes of a sealed hierarchy were subtracted from a generic object type (e.g.,FooCov<string>minusBarCovminusBazCov), the parent's code would correctly returnNeverType(marking the type as exhausted), butGenericObjectTypesimply created a new instance with the subtracted type set — never checking if the sealed hierarchy was fully covered. The fix delegates to the parent first and returns its result when it indicates exhaustiveness (NeverType) or reduces to a single remaining sealed subtype.Test
Added a regression test with both covariant and invariant generic sealed hierarchies. Each hierarchy has two sealed subtypes, and the match expression covers both. The test expects no errors (empty expected-errors array), confirming the match is recognized as exhaustive.
Fixes phpstan/phpstan#14412