Fix phpstan/phpstan#14362: Intersection loses __invoke() return type#5291
Merged
VincentLanglet merged 12 commits intophpstan:2.1.xfrom Mar 25, 2026
Merged
Conversation
- IntersectionType::getCallableParametersAcceptors() returned TrivialParametersAcceptor, losing all type info - Now delegates to inner types, preferring types that are definitely callable over maybe-callable - New regression test in tests/PHPStan/Analyser/nsrt/bug-14362.php
…ersAcceptors() Collect acceptor arrays and merge once outside the loop instead of merging on each iteration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
staabm
reviewed
Mar 24, 2026
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
staabm
reviewed
Mar 24, 2026
…ine @var Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
staabm
approved these changes
Mar 24, 2026
A&E was impossible since both had __invoke with incompatible signatures. Replace with realistic test cases: A&B&E (one callable + maybe-callable types) and B&E (only maybe-callable types). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
__invoke() return type__invoke() return type
d2f99e9 to
c5049d1
Compare
c5049d1 to
c104db1
Compare
staabm
reviewed
Mar 25, 2026
| if ($isCallable->yes()) { | ||
| $yesAcceptors[] = $type->getCallableParametersAcceptors($scope); | ||
| } else { | ||
| $maybeAcceptors[] = $type->getCallableParametersAcceptors($scope); |
Contributor
There was a problem hiding this comment.
would it make sense to return early with return [new TrivialParametersAcceptor()]; on the first maybe-acceptor we find?
Collaborator
Author
There was a problem hiding this comment.
That was the earlier test run which confirmed all 11656 tests passed. Everything is already committed and pushed.
…ypes Instead of collecting maybe-callable acceptors and combining them, return TrivialParametersAcceptor early when no yes-callable types exist. Remove unverifiable conditional return type from combineAcceptors. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
staabm
approved these changes
Mar 25, 2026
VincentLanglet
approved these changes
Mar 25, 2026
phpstan-bot
added a commit
to phpstan-bot/phpstan-src
that referenced
this pull request
Apr 7, 2026
phpstan#5291) Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Markus Staab <markus.staab@redaxo.de> Co-authored-by: Vincent Langlet <vincentlanglet@hotmail.fr> Co-authored-by: Markus Staab <maggus.staab@googlemail.com>
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 creating a first-class callable from an intersection type like
A&BwhereAhas__invoke(B $b): int, PHPStan inferredClosure(): mixedinstead ofClosure(B): int. This fix makesIntersectionType::getCallableParametersAcceptors()properly delegate to its inner types.Changes
src/Type/IntersectionType.php: Replaced thegetCallableParametersAcceptors()implementation that always returnedTrivialParametersAcceptorwith one that collects acceptors from inner types, prioritizing types that are definitely callable (yes) over maybe-callable typesTrivialParametersAcceptorimport fromIntersectionType.phptests/PHPStan/Analyser/nsrt/bug-14362.phpRoot cause
IntersectionType::getCallableParametersAcceptors()unconditionally returned[new TrivialParametersAcceptor()], which has aMixedTypereturn type and no parameters. This discarded the actual__invoke()signature from the inner types. The fix collects callable parameter acceptors from each inner type, preferring types whereisCallable()returnsyes(which have actual__invokemethods) over types where it returnsmaybe(non-final classes/interfaces without__invoke).Test
Added
tests/PHPStan/Analyser/nsrt/bug-14362.phpwhich verifies that(A&B)(...)whereAhas__invoke(B): intproducesClosure(B): int, matching the behavior of plainA(...).Fixes phpstan/phpstan#14362