Skip to content

Commit bfa3be8

Browse files
phpstan-botclaude
andcommitted
Use filtered remaining type instead of new ObjectType for unhandled classes
Preserve generic type information by filtering the original $typeWithMethod to remove handled classes, rather than creating plain ObjectType instances which lose generics and other type details. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4ae3833 commit bfa3be8

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/Analyser/ExprHandler/Helper/MethodCallReturnTypeHelper.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPStan\Type\TypeCombinator;
1515
use function array_unique;
1616
use function count;
17-
use function in_array;
1817

1918
#[AutowiredService]
2019
final class MethodCallReturnTypeHelper
@@ -94,22 +93,19 @@ public function methodCallReturnType(
9493
}
9594

9695
if (count($resolvedTypes) > 0) {
97-
foreach ($allClassNames as $className) {
98-
if (in_array($className, $handledClassNames, true)) {
99-
continue;
100-
}
101-
$classType = new ObjectType($className);
102-
if (!$classType->hasMethod($methodName)->yes()) {
103-
continue;
104-
}
105-
$classMethod = $classType->getMethod($methodName, $scope);
106-
$classParametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
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(
107103
$scope,
108104
$methodCall->getArgs(),
109-
$classMethod->getVariants(),
110-
$classMethod->getNamedArgumentsVariants(),
105+
$remainingMethod->getVariants(),
106+
$remainingMethod->getNamedArgumentsVariants(),
111107
);
112-
$resolvedTypes[] = $classParametersAcceptor->getReturnType();
108+
$resolvedTypes[] = $remainingParametersAcceptor->getReturnType();
113109
}
114110
return VoidToNullTypeTransformer::transform(TypeCombinator::union(...$resolvedTypes), $methodCall);
115111
}

0 commit comments

Comments
 (0)