Skip to content

Commit dee1a72

Browse files
phpstan-botclaude
authored andcommitted
Extract getMethodWithMostParameters() into cached private method
Replace all occurrences of $this->methods[0] with a call to the new getMethodWithMostParameters() private method, which caches its result. This addresses the review feedback to avoid duplicating the method selection logic and to add caching. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 98af204 commit dee1a72

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/Reflection/Type/IntersectionTypeMethodReflection.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
final class IntersectionTypeMethodReflection implements ExtendedMethodReflection
2323
{
2424

25+
private ?ExtendedMethodReflection $methodWithMostParameters = null;
26+
2527
/**
2628
* @param ExtendedMethodReflection[] $methods
2729
*/
@@ -31,7 +33,7 @@ public function __construct(private string $methodName, private array $methods)
3133

3234
public function getDeclaringClass(): ClassReflection
3335
{
34-
return $this->methods[0]->getDeclaringClass();
36+
return $this->getMethodWithMostParameters()->getDeclaringClass();
3537
}
3638

3739
public function isStatic(): bool
@@ -95,19 +97,6 @@ public function getVariants(): array
9597
$returnType = TypeCombinator::intersect(...$returnTypes);
9698
$phpDocReturnType = TypeCombinator::intersect(...$phpDocReturnTypes);
9799
$nativeReturnType = TypeCombinator::intersect(...$nativeReturnTypes);
98-
$methodWithMostParameters = $this->methods[0];
99-
$maxParameters = 0;
100-
foreach ($this->methods as $method) {
101-
foreach ($method->getVariants() as $variant) {
102-
if (count($variant->getParameters()) <= $maxParameters) {
103-
continue;
104-
}
105-
106-
$maxParameters = count($variant->getParameters());
107-
$methodWithMostParameters = $method;
108-
}
109-
}
110-
111100
return array_map(static fn (ExtendedParametersAcceptor $acceptor): ExtendedParametersAcceptor => new ExtendedFunctionVariant(
112101
$acceptor->getTemplateTypeMap(),
113102
$acceptor->getResolvedTemplateTypeMap(),
@@ -117,7 +106,7 @@ public function getVariants(): array
117106
$phpDocReturnType,
118107
$nativeReturnType,
119108
$acceptor->getCallSiteVarianceMap(),
120-
), $methodWithMostParameters->getVariants());
109+
), $this->getMethodWithMostParameters()->getVariants());
121110
}
122111

123112
public function getOnlyVariant(): ExtendedParametersAcceptor
@@ -250,7 +239,7 @@ public function isAbstract(): TrinaryLogic
250239

251240
public function getAttributes(): array
252241
{
253-
return $this->methods[0]->getAttributes();
242+
return $this->getMethodWithMostParameters()->getAttributes();
254243
}
255244

256245
public function mustUseReturnValue(): TrinaryLogic
@@ -260,7 +249,31 @@ public function mustUseReturnValue(): TrinaryLogic
260249

261250
public function getResolvedPhpDoc(): ?ResolvedPhpDocBlock
262251
{
263-
return $this->methods[0]->getResolvedPhpDoc();
252+
return $this->getMethodWithMostParameters()->getResolvedPhpDoc();
253+
}
254+
255+
private function getMethodWithMostParameters(): ExtendedMethodReflection
256+
{
257+
if ($this->methodWithMostParameters !== null) {
258+
return $this->methodWithMostParameters;
259+
}
260+
261+
$methodWithMostParameters = $this->methods[0];
262+
$maxParameters = 0;
263+
foreach ($this->methods as $method) {
264+
foreach ($method->getVariants() as $variant) {
265+
if (count($variant->getParameters()) <= $maxParameters) {
266+
continue;
267+
}
268+
269+
$maxParameters = count($variant->getParameters());
270+
$methodWithMostParameters = $method;
271+
}
272+
}
273+
274+
$this->methodWithMostParameters = $methodWithMostParameters;
275+
276+
return $methodWithMostParameters;
264277
}
265278

266279
}

0 commit comments

Comments
 (0)