Skip to content

Commit

Permalink
Cache getConstructor()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 1, 2021
1 parent 79c0ffb commit fb85182
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ class ReflectionClass implements Reflection
/** @var string|null */
private $cachedName;

/** @var ReflectionMethod|null */
private $cachedConstructor;

private function __construct()
{
}
Expand Down Expand Up @@ -651,6 +654,10 @@ static function (ReflectionClass $interface) : array {
*/
public function getConstructor() : ReflectionMethod
{
if ($this->cachedConstructor !== null) {
return $this->cachedConstructor;
}

$constructors = array_values(array_filter($this->getMethods(), static function (ReflectionMethod $method) : bool {
return $method->isConstructor();
}));
Expand All @@ -659,7 +666,7 @@ public function getConstructor() : ReflectionMethod
throw new OutOfBoundsException('Could not find method: __construct');
}

return $constructors[0];
return $this->cachedConstructor = $constructors[0];
}

/**
Expand Down

0 comments on commit fb85182

Please sign in to comment.