Fix phpstan/phpstan#11488: Wrongly assumed undefined variable since 1.11.10#5150
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#5150phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Added targeted handling in resolveNormalizedIdentical for falsey context
with unions of multiple constant arrays to avoid TypeCombinator::remove
issues caused by supertype relationships between constant array types
- New regression test in tests/PHPStan/Analyser/nsrt/bug-11488.php
- The root cause was that array{mixed} is a supertype of array{mixed,
string|null, mixed}, so TypeCombinator::remove incorrectly eliminated
all union members when negating count() === N comparisons
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($row) !== Non a union of constant arrays likearray{mixed}|array{mixed, string|null, mixed}, PHPStan incorrectly narrowed the type to*NEVER*instead of filtering out the array member that matches the count. This caused false positives about undefined variables from destructured assignments.Changes
src/Analyser/TypeSpecifier.phpinresolveNormalizedIdentical()for the falsey context when the argument is a union of multiple constant arrayssetAlwaysOverwriteTypes()to bypass the problematicTypeCombinator::removebehaviortests/PHPStan/Analyser/nsrt/bug-11488.phpRoot cause
The bug was introduced when
specifyTypesForCountFuncCall()was added toresolveNormalizedIdentical(). In the falsey context (e.g.,count($row) !== 1), the method builds a type representing arrays that match the count, then usesTypeCombinator::removeto subtract it from the original union. However,array{mixed}(a 1-element constant array with a mixed value) is considered a supertype ofarray{mixed, string|null, mixed}(a 3-element array) because mixed accepts all values and extra keys don't prevent the supertype relationship. This causedTypeCombinator::removeto eliminate ALL union members, producing*NEVER*.The fix adds a pre-check specifically for the falsey context with unions of multiple constant arrays. It directly filters out union members whose sizes exactly match the comparison value and uses
setAlwaysOverwriteTypes()to set the result type directly, avoiding the supertype-relatedTypeCombinator::removeissue.Test
Added
tests/PHPStan/Analyser/nsrt/bug-11488.phpwhich tests thatcount($row) !== 1onarray{mixed}|array{mixed, string|null, mixed}correctly narrows toarray{mixed, string|null, mixed}, and that destructured variables from the narrowed array have correct types.Fixes phpstan/phpstan#11488