99use PHPStan \DependencyInjection \AutowiredService ;
1010use PHPStan \DependencyInjection \Type \DynamicReturnTypeExtensionRegistryProvider ;
1111use PHPStan \Reflection \ParametersAcceptorSelector ;
12+ use PHPStan \Type \ObjectType ;
1213use PHPStan \Type \Type ;
1314use PHPStan \Type \TypeCombinator ;
1415use function count ;
@@ -52,7 +53,9 @@ public function methodCallReturnType(
5253 }
5354
5455 $ resolvedTypes = [];
55- foreach ($ typeWithMethod ->getObjectClassNames () as $ className ) {
56+ $ allClassNames = $ typeWithMethod ->getObjectClassNames ();
57+ $ handledClassNames = [];
58+ foreach ($ allClassNames as $ className ) {
5659 if ($ normalizedMethodCall instanceof MethodCall) {
5760 foreach ($ this ->dynamicReturnTypeExtensionRegistryProvider ->getRegistry ()->getDynamicMethodReturnTypeExtensionsForClass ($ className ) as $ dynamicMethodReturnTypeExtension ) {
5861 if (!$ dynamicMethodReturnTypeExtension ->isMethodSupported ($ methodReflection )) {
@@ -65,6 +68,7 @@ public function methodCallReturnType(
6568 }
6669
6770 $ resolvedTypes [] = $ resolvedType ;
71+ $ handledClassNames [] = $ className ;
6872 }
6973 } else {
7074 foreach ($ this ->dynamicReturnTypeExtensionRegistryProvider ->getRegistry ()->getDynamicStaticMethodReturnTypeExtensionsForClass ($ className ) as $ dynamicStaticMethodReturnTypeExtension ) {
@@ -82,11 +86,29 @@ public function methodCallReturnType(
8286 }
8387
8488 $ resolvedTypes [] = $ resolvedType ;
89+ $ handledClassNames [] = $ className ;
8590 }
8691 }
8792 }
8893
8994 if (count ($ resolvedTypes ) > 0 ) {
95+ if (count ($ allClassNames ) !== count ($ handledClassNames )) {
96+ $ remainingType = $ typeWithMethod ;
97+ foreach ($ handledClassNames as $ handledClassName ) {
98+ $ remainingType = TypeCombinator::remove ($ remainingType , new ObjectType ($ handledClassName ));
99+ }
100+ if ($ remainingType ->hasMethod ($ methodName )->yes ()) {
101+ $ remainingMethod = $ remainingType ->getMethod ($ methodName , $ scope );
102+ $ remainingParametersAcceptor = ParametersAcceptorSelector::selectFromArgs (
103+ $ scope ,
104+ $ methodCall ->getArgs (),
105+ $ remainingMethod ->getVariants (),
106+ $ remainingMethod ->getNamedArgumentsVariants (),
107+ );
108+ $ resolvedTypes [] = $ remainingParametersAcceptor ->getReturnType ();
109+ }
110+ }
111+
90112 return VoidToNullTypeTransformer::transform (TypeCombinator::union (...$ resolvedTypes ), $ methodCall );
91113 }
92114
0 commit comments