Skip to content

Commit f819a4a

Browse files
committed
feat: cache the result of ClassReflection::hasMethod method
1 parent 924a7a2 commit f819a4a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Reflection/ClassReflection.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ final class ClassReflection
141141
/** @var array<string, true> */
142142
private static array $resolvingTypeAliasImports = [];
143143

144+
/** @var array<string, bool> */
145+
private array $hasMethodCache = [];
146+
144147
/**
145148
* @param PropertiesClassReflectionExtension[] $propertiesClassReflectionExtensions
146149
* @param MethodsClassReflectionExtension[] $methodsClassReflectionExtensions
@@ -473,16 +476,28 @@ public function hasProperty(string $propertyName): bool
473476

474477
public function hasMethod(string $methodName): bool
475478
{
479+
$key = sprintf('%s-%s', $methodName, $this->getCacheKey());
480+
481+
if (array_key_exists($key, $this->hasMethodCache)) {
482+
return $this->hasMethodCache[$key];
483+
}
484+
476485
foreach ($this->methodsClassReflectionExtensions as $extension) {
477486
if ($extension->hasMethod($this, $methodName)) {
487+
$this->hasMethodCache[$key] = true;
488+
478489
return true;
479490
}
480491
}
481492

482493
if ($this->requireExtendsMethodsClassReflectionExtension->hasMethod($this, $methodName)) {
494+
$this->hasMethodCache[$key] = true;
495+
483496
return true;
484497
}
485498

499+
$this->hasMethodCache[$key] = false;
500+
486501
return false;
487502
}
488503

0 commit comments

Comments
 (0)