Fix phpstan/phpstan#14203: Incorrect return.type recognized from generic callback#5299
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix phpstan/phpstan#14203: Incorrect return.type recognized from generic callback#5299phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- When calling a method with template parameters on a union type (e.g., Collection<int, A>|Collection<int, B>), resolve return types for each union member separately instead of combining variants first - The root cause was that combineAcceptors merged variants from different generic instantiations, losing proper template type resolution for callable return types - Excludes TemplateUnionType from decomposition to preserve template type wrapping - Updated static-late-binding test assertion to match now-correct result - New regression test in tests/PHPStan/Analyser/nsrt/bug-14203.php Closes phpstan/phpstan#14203
Contributor
|
This seems to solve similar issue than #5225... |
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 calling a generic method (like
map()) on a union of generic objects (e.g.,Collection<int, SpecificA>|Collection<int, SpecificB>), the callback's return type was ignored and the original union type was returned instead of properly resolving the template type from the callback.Changes
src/Analyser/ExprHandler/Helper/MethodCallReturnTypeHelper.phpto decompose union types and resolve method return types for each member separately before combining the results, while excludingTemplateTypeinstances to preserve template type wrappingtests/PHPStan/Analyser/nsrt/static-late-binding.phpline 88 assertion to match the now-correct return type for$clUnioned::retStatic()(wasbool|A|X, now correctlybool|AsinceX::retStatic()returnsboolnotstatic)tests/PHPStan/Analyser/nsrt/bug-14203.phpRoot cause
MethodCallReturnTypeHelper::methodCallReturnType()calledgetMethod()on the union type, which produced aUnionTypeMethodReflection. ItsgetVariants()usedcombineAcceptors()to merge variants from different generic instantiations. This merged the return types into a union before template type resolution from the callback argument could happen, so theTMapValuetemplate type could not be properly resolved from the callable.The fix processes each union member independently — resolving template types per member — and then unions the results.
TemplateTypeinstances (which extendUnionTypeviaTemplateUnionType) are excluded from decomposition to preserve template type semantics.Test
Added
tests/PHPStan/Analyser/nsrt/bug-14203.phpwhich tests both the working case (singleCollection<int, SpecificA|SpecificB>) and the previously broken case (unionCollection<int, SpecificA>|Collection<int, SpecificB>) to verify thatmap()with a typed callback correctly resolves toCollection<int, MyDTO>in both cases.Fixes phpstan/phpstan#14203