Fix phpstan/phpstan#2572: Optional templated args not provided cause unbound template errors#5175
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
…rams - When a template type is only used on a variadic parameter and no arguments are passed, it was incorrectly reported as unresolvable - Fixed by inferring NeverType for variadic parameters with no arguments in GenericParametersAcceptorResolver, since an empty variadic means the element type is never - Added regression tests in both rule test and NSRT format Closes phpstan/phpstan#2572
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 a function has a template type bound to a variadic parameter (e.g.
@param TR ...$elts) and the function is called without providing any variadic arguments, PHPStan incorrectly reports "Unable to resolve the template type TR". Since variadic parameters are optional, not providing them should not trigger this error.Changes
src/Reflection/GenericParametersAcceptorResolver.phpto inferNeverTypefor variadic parameters that receive no arguments, instead of leaving the template type asErrorTypetests/PHPStan/Rules/Functions/data/bug-2572.phpandtests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.phptests/PHPStan/Analyser/nsrt/bug-2572.phpRoot cause
In
GenericParametersAcceptorResolver::resolve(), all template types are initially mapped toErrorType, then overwritten by types inferred from actual arguments. When a variadic parameter receives no arguments, it was simply skipped (continue), leaving its template type asErrorType. TheFunctionCallParametersCheckthen saw thisErrorTypeand reported the template as unresolvable.The fix adds an
elseif ($param->isVariadic())branch that infers the template type fromNeverTypeinstead of skipping. This is semantically correct: an empty variadic parameter means an empty array, and the element type of an empty array isnever. This causesTE|TRto resolve toTE|never=TE, which is the expected behavior.Test
collect("a")(previously a false positive) andcollect("a", "b", "c")(always worked)collect("a")infers type'a'andcollect("a", "b", "c")infers type'a'|'b'|'c'Fixes phpstan/phpstan#2572