Skip to content

Commit

Permalink
feat: cache the result of ClassReflection::hasMethod method
Browse files Browse the repository at this point in the history
  • Loading branch information
canvural authored and ondrejmirtes committed Feb 13, 2025
1 parent 14faee1 commit fde29a5
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ class ClassReflection
/** @var array<string, true> */
private static array $resolvingTypeAliasImports = [];

/** @var array<string, bool> */
private array $hasMethodCache = [];

/**
* @param PropertiesClassReflectionExtension[] $propertiesClassReflectionExtensions
* @param MethodsClassReflectionExtension[] $methodsClassReflectionExtensions
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit fde29a5

Please sign in to comment.