Fix phpstan/phpstan#11488: Wrongly assumed undefined variable since 1.11.10#5151
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix phpstan/phpstan#11488: Wrongly assumed undefined variable since 1.11.10#5151phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Fixed specifyTypesForCountFuncCall to use direct type assignment instead
of TypeCombinator::remove for falsey context with fixed-size constant arrays
- The root cause was ConstantArrayType::isSuperTypeOf using structural
subtyping (array{mixed} is supertype of array{mixed, string|null, mixed}),
which made TypeCombinator::remove overly aggressive
- New regression test in tests/PHPStan/Analyser/nsrt/bug-11488.php
Closes phpstan/phpstan#11488
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
When using
count()to narrow a union of constant array types (e.g.,array{mixed}|array{mixed, string|null, mixed}), the falsey branch (e.g.,count($row) !== 1) incorrectly narrowed the type to*NEVER*instead of the expected non-matching array type. This caused false positive "undefined variable" errors when destructuring the narrowed array.Changes
specifyTypesForCountFuncCallinsrc/Analyser/TypeSpecifier.phpto use a direct type assignment (viasetAlwaysOverwriteTypes()) for the falsey context when all arrays in the union have definite (fixed) sizesTypeCombinator::removetests/PHPStan/Analyser/nsrt/bug-11488.phpRoot cause
ConstantArrayType::isSuperTypeOfuses structural subtyping:array{mixed}is considered a supertype ofarray{mixed, string|null, mixed}because every key in the smaller array exists in the larger one with compatible value types. This is intentional for general type checking, but it causedTypeCombinator::removeto be overly aggressive when removing matching count types — it would remove the entire union (producing*NEVER*) instead of just the matching member.The fix avoids
TypeCombinator::removeentirely for this case by collecting non-matching array types and directly assigning them as the narrowed type.Test
Added
tests/PHPStan/Analyser/nsrt/bug-11488.phpwith three test methods covering:count($row) !== 1narrowing witharray{mixed}|array{mixed, string|null, mixed}array{bool}|array{mixed, string|null, mixed}count($row) === 3truthy narrowingFixes phpstan/phpstan#11488