Narrow array type based on array_all() and array_any() callback type assertions#5624
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Narrow array type based on array_all() and array_any() callback type assertions#5624phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
array_all() and array_any() callback type assertions#5624phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…ype assertions - Add ArrayAllFunctionTypeSpecifyingExtension that narrows array key/value types when array_all() is used in a truthy context or array_any() in a falsey context - Support all callback forms: closures, arrow functions, first-class callables, string callables, and @phpstan-assert-if-true annotations - Narrow both value types and key types when the callback inspects both - Preserve constant array structure and optional key status during narrowing - Preserve list and non-empty-array accessory types through intersection
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
array_all($array, $callback)returnstrue, every element in$arraysatisfies the callback. If the callback narrows types (e.g.is_int(...), a closure withinstanceof, or a function with@phpstan-assert-if-true), the array's value and/or key types should be narrowed accordingly. Similarly, whenarray_any($array, $callback)returnsfalse, no element satisfies the callback, so the falsey branch of the callback applies to all elements.Changes
src/Type/Php/ArrayAllFunctionTypeSpecifyingExtension.php— a newFunctionTypeSpecifyingExtensionthat handles:array_all()in truthy context: narrows array types based on the callback's truthy brancharray_any()in falsey context: narrows array types based on the callback's falsey branchfn ($v) => is_int($v))function ($v) { return is_int($v); })is_int(...),isInt(...))'is_int')filterByTruthyValue/filterByFalseyValueto determine the narrowed typesRoot cause
There was no
FunctionTypeSpecifyingExtensionregistered forarray_all()orarray_any(). The functions returnedboolbut PHPStan had no mechanism to narrow the array argument's type based on the callback's type assertions. The fix adds an extension following the same pattern used by other type-specifying extensions (ArraySearchFunctionTypeSpecifyingExtension,InArrayFunctionTypeSpecifyingExtension, etc.) and reuses the scope filtering mechanism already used byArrayFilterFunctionReturnTypeHelper.Analogous cases probed
array_allfalsey context: Correctly does NOT narrow (we only know at least one element doesn't match) — verified with testarray_anytruthy context: Correctly does NOT narrow (we only know at least one matches) — verified with testarray_allandarray_anywhen the callback inspects the second parameter — testedassert(array_all(...))): Works via the existing assert() type specifying infrastructure — testedarray_find/array_find_key: These return the found element/key, not a boolean — they don't benefit from this type of narrowingTest
Added
tests/PHPStan/Analyser/nsrt/array-all-type-narrowing.phpwith 20 test functions covering:@phpstan-assert-if-true)is_int,is_string,instanceof)array_anyfalsey narrowingarray_allfalsey,array_anytruthy)assert(array_all(...))usageFixes phpstan/phpstan#12939