Skip to content

Commit 1b42b47

Browse files
phpstan-botclaude
authored andcommitted
Rename resolveReflectionAttributeClassNames to resolveReflectionAttributeType and return Type directly
Instead of returning class name strings that get wrapped in GenericObjectType by the caller, the helper method now accepts the classType parameter and returns the final GenericObjectType or UnionType directly. This avoids the unnecessary TypeCombinator::union call in the single-match case. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent dde7229 commit 1b42b47

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/Type/Php/ReflectionGetAttributesMethodReturnTypeExtension.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,32 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4545
$argType = $scope->getType($methodCall->getArgs()[0]->value);
4646
$classType = $argType->getClassStringObjectType();
4747

48-
$reflectionAttributeClassNames = $this->resolveReflectionAttributeClassNames($methodReflection);
48+
$valueType = $this->resolveReflectionAttributeType($methodReflection, $classType);
4949

50-
$valueTypes = [];
51-
foreach ($reflectionAttributeClassNames as $className) {
52-
$valueTypes[] = new GenericObjectType($className, [$classType]);
53-
}
54-
55-
return new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), TypeCombinator::union(...$valueTypes)), new AccessoryArrayListType()]);
50+
return new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), $valueType), new AccessoryArrayListType()]);
5651
}
5752

58-
/**
59-
* @return non-empty-list<string>
60-
*/
61-
private function resolveReflectionAttributeClassNames(MethodReflection $methodReflection): array
53+
private function resolveReflectionAttributeType(MethodReflection $methodReflection, Type $classType): Type
6254
{
6355
$returnType = $methodReflection->getVariants()[0]->getReturnType();
6456
$nativeReflectionAttributeType = new ObjectType(ReflectionAttribute::class);
6557

66-
$matchedClassNames = [];
58+
$valueTypes = [];
6759
foreach ($returnType->getIterableValueType()->getObjectClassNames() as $className) {
6860
if ($nativeReflectionAttributeType->isSuperTypeOf(new ObjectType($className))->yes()) {
69-
$matchedClassNames[] = $className;
61+
$valueTypes[] = new GenericObjectType($className, [$classType]);
7062
}
7163
}
7264

73-
if (count($matchedClassNames) === 0) {
74-
return [ReflectionAttribute::class];
65+
if (count($valueTypes) === 0) {
66+
return new GenericObjectType(ReflectionAttribute::class, [$classType]);
67+
}
68+
69+
if (count($valueTypes) === 1) {
70+
return $valueTypes[0];
7571
}
7672

77-
return $matchedClassNames;
73+
return TypeCombinator::union(...$valueTypes);
7874
}
7975

8076
}

0 commit comments

Comments
 (0)