|
12 | 12 | use PHPStan\Type\ObjectType; |
13 | 13 | use PHPStan\Type\Type; |
14 | 14 | use PHPStan\Type\TypeCombinator; |
| 15 | +use PHPStan\Type\UnionType; |
15 | 16 | use function count; |
16 | 17 |
|
17 | 18 | #[AutowiredService] |
@@ -55,43 +56,46 @@ public function methodCallReturnType( |
55 | 56 | $resolvedTypes = []; |
56 | 57 | $allClassNames = $typeWithMethod->getObjectClassNames(); |
57 | 58 | $handledClassNames = []; |
58 | | - foreach ($allClassNames as $className) { |
59 | | - $classType = new ObjectType($className); |
60 | | - if (!$classType->hasMethod($methodName)->yes()) { |
| 59 | + $innerTypes = $typeWithMethod instanceof UnionType ? $typeWithMethod->getTypes() : [$typeWithMethod]; |
| 60 | + foreach ($innerTypes as $innerType) { |
| 61 | + $classNames = $innerType->getObjectClassNames(); |
| 62 | + if ($classNames === [] || !$innerType->hasMethod($methodName)->yes()) { |
61 | 63 | continue; |
62 | 64 | } |
63 | | - $classMethodReflection = $classType->getMethod($methodName, $scope); |
64 | | - if ($normalizedMethodCall instanceof MethodCall) { |
65 | | - foreach ($this->dynamicReturnTypeExtensionRegistryProvider->getRegistry()->getDynamicMethodReturnTypeExtensionsForClass($className) as $dynamicMethodReturnTypeExtension) { |
66 | | - if (!$dynamicMethodReturnTypeExtension->isMethodSupported($classMethodReflection)) { |
67 | | - continue; |
68 | | - } |
| 65 | + $classMethodReflection = $innerType->getMethod($methodName, $scope); |
| 66 | + foreach ($classNames as $className) { |
| 67 | + if ($normalizedMethodCall instanceof MethodCall) { |
| 68 | + foreach ($this->dynamicReturnTypeExtensionRegistryProvider->getRegistry()->getDynamicMethodReturnTypeExtensionsForClass($className) as $dynamicMethodReturnTypeExtension) { |
| 69 | + if (!$dynamicMethodReturnTypeExtension->isMethodSupported($classMethodReflection)) { |
| 70 | + continue; |
| 71 | + } |
69 | 72 |
|
70 | | - $resolvedType = $dynamicMethodReturnTypeExtension->getTypeFromMethodCall($classMethodReflection, $normalizedMethodCall, $scope); |
71 | | - if ($resolvedType === null) { |
72 | | - continue; |
73 | | - } |
| 73 | + $resolvedType = $dynamicMethodReturnTypeExtension->getTypeFromMethodCall($classMethodReflection, $normalizedMethodCall, $scope); |
| 74 | + if ($resolvedType === null) { |
| 75 | + continue; |
| 76 | + } |
74 | 77 |
|
75 | | - $resolvedTypes[] = $resolvedType; |
76 | | - $handledClassNames[] = $className; |
77 | | - } |
78 | | - } else { |
79 | | - foreach ($this->dynamicReturnTypeExtensionRegistryProvider->getRegistry()->getDynamicStaticMethodReturnTypeExtensionsForClass($className) as $dynamicStaticMethodReturnTypeExtension) { |
80 | | - if (!$dynamicStaticMethodReturnTypeExtension->isStaticMethodSupported($classMethodReflection)) { |
81 | | - continue; |
| 78 | + $resolvedTypes[] = $resolvedType; |
| 79 | + $handledClassNames[] = $className; |
82 | 80 | } |
| 81 | + } else { |
| 82 | + foreach ($this->dynamicReturnTypeExtensionRegistryProvider->getRegistry()->getDynamicStaticMethodReturnTypeExtensionsForClass($className) as $dynamicStaticMethodReturnTypeExtension) { |
| 83 | + if (!$dynamicStaticMethodReturnTypeExtension->isStaticMethodSupported($classMethodReflection)) { |
| 84 | + continue; |
| 85 | + } |
83 | 86 |
|
84 | | - $resolvedType = $dynamicStaticMethodReturnTypeExtension->getTypeFromStaticMethodCall( |
85 | | - $classMethodReflection, |
86 | | - $normalizedMethodCall, |
87 | | - $scope, |
88 | | - ); |
89 | | - if ($resolvedType === null) { |
90 | | - continue; |
91 | | - } |
| 87 | + $resolvedType = $dynamicStaticMethodReturnTypeExtension->getTypeFromStaticMethodCall( |
| 88 | + $classMethodReflection, |
| 89 | + $normalizedMethodCall, |
| 90 | + $scope, |
| 91 | + ); |
| 92 | + if ($resolvedType === null) { |
| 93 | + continue; |
| 94 | + } |
92 | 95 |
|
93 | | - $resolvedTypes[] = $resolvedType; |
94 | | - $handledClassNames[] = $className; |
| 96 | + $resolvedTypes[] = $resolvedType; |
| 97 | + $handledClassNames[] = $className; |
| 98 | + } |
95 | 99 | } |
96 | 100 | } |
97 | 101 | } |
|
0 commit comments