Skip to content

Commit 270b546

Browse files
committed
Cache StaticType and ObjectType::getMethod()
1 parent 1f1d382 commit 270b546

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Type/ObjectType.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ class ObjectType implements TypeWithClassName, SubtractableType
118118
/** @var array<string, list<EnumCaseObjectType>> */
119119
private static array $enumCases = [];
120120

121+
/** @var array<string, ExtendedMethodReflection> */
122+
private array $methodCache = [];
123+
121124
/** @api */
122125
public function __construct(
123126
private string $className,
@@ -976,7 +979,11 @@ public function hasMethod(string $methodName): TrinaryLogic
976979

977980
public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection
978981
{
979-
return $this->getUnresolvedMethodPrototype($methodName, $scope)->getTransformedMethod();
982+
$key = $methodName;
983+
if ($scope->isInClass()) {
984+
$key = sprintf('%s-%s', $key, $scope->getClassReflection()->getCacheKey());
985+
}
986+
return $this->methodCache[$key] ??= $this->getUnresolvedMethodPrototype($methodName, $scope)->getTransformedMethod();
980987
}
981988

982989
public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection

src/Type/StaticType.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ class StaticType implements TypeWithClassName, SubtractableType
3838

3939
private string $baseClass;
4040

41+
/** @var array<string, ExtendedMethodReflection> */
42+
private array $methodCache = [];
43+
4144
/**
4245
* @api
4346
*/
@@ -313,7 +316,11 @@ public function hasMethod(string $methodName): TrinaryLogic
313316

314317
public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection
315318
{
316-
return $this->getUnresolvedMethodPrototype($methodName, $scope)->getTransformedMethod();
319+
$key = $methodName;
320+
if ($scope->isInClass()) {
321+
$key = sprintf('%s-%s', $key, $scope->getClassReflection()->getCacheKey());
322+
}
323+
return $this->methodCache[$key] ??= $this->getUnresolvedMethodPrototype($methodName, $scope)->getTransformedMethod();
317324
}
318325

319326
public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection

0 commit comments

Comments
 (0)