Fix phpstan/phpstan#14396: UnhandledMatchError since 2.1.41#5333
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix phpstan/phpstan#14396: UnhandledMatchError since 2.1.41#5333phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…treatPhpDocTypesAsCertain=false - Fixed TypeSpecifier to specify types for both AlwaysRememberedExpr and its unwrapped inner expression when handling always-true/false identity comparisons - Previously, only the unwrapped expression was specified, leaving the AlwaysRememberedExpr type un-narrowed in the match scope - New regression test in tests/PHPStan/Rules/Exceptions/Bug14396Test.php
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 where an exhaustive match expression inside a
foreachloop was incorrectly reported as throwingUnhandledMatchErrorwhentreatPhpDocTypesAsCertainwas set tofalse.Changes
src/Analyser/TypeSpecifier.phpinresolveNormalizedIdentical(): when an identity comparison is always true/false (constant boolean) and involves anAlwaysRememberedExpr, the type specification is now created for both theAlwaysRememberedExprand its unwrapped inner expression, not just the unwrapped onetests/PHPStan/Rules/Exceptions/Bug14396Test.phpwith configtests/PHPStan/Rules/Exceptions/bug-14396.neonand test datatests/PHPStan/Rules/Exceptions/data/bug-14396.phpRoot cause
When
treatPhpDocTypesAsCertainisfalse, theMatchHandleruses native types to determine if an arm condition is always true. For match subjects with native typemixed(e.g., from iterating a PHPDoc-typedlist<Item>over a natively-typedarray), no arm condition evaluates as always-true natively, so$hasAlwaysTrueCondstaysfalse.The handler then falls back to checking if the remaining PHPDoc type is
never. During per-arm type narrowing viafilterByFalseyValue, when the match subject type has been narrowed to exactly match the last arm condition (e.g., both arenull), theTypeSpecifiertakes a fast path for constant boolean identity comparisons. In this fast path, it only specified the type for the unwrapped expression (e.g.,$item->status) but NOT for theAlwaysRememberedExprwrapper that the match handler tracks. This left theAlwaysRememberedExprtype asnullinstead of narrowing it tonever, making the match appear non-exhaustive.The fix ensures both the wrapper and the inner expression are specified, consistent with how other code paths in the same function handle
AlwaysRememberedExpr.Test
The regression test uses
MissingCheckedExceptionInFunctionThrowsRulewithtreatPhpDocTypesAsCertain: falseconfigured via NEON. It verifies that an exhaustive match expression covering all enum cases plusnullinside aforeachloop does not produce a falseUnhandledMatchErrorexception warning.Fixes phpstan/phpstan#14396