From 10a1e9d21ad3b0a1d0aabbc44e2d5c6aa1dd1726 Mon Sep 17 00:00:00 2001 From: Can Vural Date: Thu, 13 Feb 2025 14:48:30 +0100 Subject: [PATCH] feat: cache the result of ClassReflection::hasMethod method --- src/Reflection/ClassReflection.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index 02705f32cd..d031cb8816 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -142,6 +142,9 @@ class ClassReflection /** @var array */ private static array $resolvingTypeAliasImports = []; + /** @var array */ + private array $hasMethodCache = []; + /** * @param PropertiesClassReflectionExtension[] $propertiesClassReflectionExtensions * @param MethodsClassReflectionExtension[] $methodsClassReflectionExtensions @@ -482,16 +485,26 @@ public function hasProperty(string $propertyName): bool public function hasMethod(string $methodName): bool { + if (array_key_exists($methodName, $this->hasMethodCache)) { + return $this->hasMethodCache[$methodName]; + } + foreach ($this->methodsClassReflectionExtensions as $extension) { if ($extension->hasMethod($this, $methodName)) { + $this->hasMethodCache[$methodName] = true; + return true; } } if ($this->requireExtendsMethodsClassReflectionExtension->hasMethod($this, $methodName)) { + $this->hasMethodCache[$methodName] = true; + return true; } + $this->hasMethodCache[$methodName] = false; + return false; }