Skip to content

Commit 09ee265

Browse files
committed
cache reflection
1 parent 3d5248f commit 09ee265

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/Reflection/ReflectionResolver.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,19 @@
3737
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
3838
use Rector\ValueObject\MethodName;
3939

40-
final readonly class ReflectionResolver
40+
final class ReflectionResolver
4141
{
42+
/**
43+
* @var array<string, MethodReflection|FunctionReflection|null>
44+
*/
45+
private array $reflectionByHash = [];
46+
4247
public function __construct(
43-
private ReflectionProvider $reflectionProvider,
44-
private NodeTypeResolver $nodeTypeResolver,
45-
private NodeNameResolver $nodeNameResolver,
46-
private ClassAnalyzer $classAnalyzer,
47-
private MethodReflectionResolver $methodReflectionResolver
48+
private readonly ReflectionProvider $reflectionProvider,
49+
private readonly NodeTypeResolver $nodeTypeResolver,
50+
private readonly NodeNameResolver $nodeNameResolver,
51+
private readonly ClassAnalyzer $classAnalyzer,
52+
private readonly MethodReflectionResolver $methodReflectionResolver
4853
) {
4954
}
5055

@@ -196,23 +201,36 @@ public function resolveMethodReflectionFromMethodCall(MethodCall $methodCall): ?
196201
public function resolveFunctionLikeReflectionFromCall(
197202
CallLike $callLike
198203
): MethodReflection | FunctionReflection | null {
204+
// cache here
205+
$callLikeHash = spl_object_hash($callLike);
206+
207+
if (isset($this->reflectionByHash[$callLikeHash])) {
208+
return $this->reflectionByHash[$callLikeHash];
209+
}
210+
199211
if ($callLike instanceof MethodCall) {
200-
return $this->resolveMethodReflectionFromMethodCall($callLike);
212+
$reflection = $this->resolveMethodReflectionFromMethodCall($callLike);
213+
$this->reflectionByHash[$callLikeHash] = $reflection;
214+
215+
return $reflection;
201216
}
202217

203218
if ($callLike instanceof StaticCall) {
204-
return $this->resolveMethodReflectionFromStaticCall($callLike);
219+
$reflection = $this->resolveMethodReflectionFromStaticCall($callLike);
220+
$this->reflectionByHash[$callLikeHash] = $reflection;
221+
return $reflection;
205222
}
206223

207224
if ($callLike instanceof New_) {
208-
return $this->resolveMethodReflectionFromNew($callLike);
225+
$reflection = $this->resolveMethodReflectionFromNew($callLike);
226+
$this->reflectionByHash[$callLikeHash] = $reflection;
227+
return $reflection;
209228
}
210229

211230
if ($callLike instanceof FuncCall) {
212231
return $this->resolveFunctionReflectionFromFuncCall($callLike);
213232
}
214233

215-
// todo: support NullsafeMethodCall
216234
return null;
217235
}
218236

src/StaticTypeMapper/Resolver/ClassNameFromObjectTypeResolver.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ final class ClassNameFromObjectTypeResolver
1111
public static function resolve(Type $type): ?string
1212
{
1313
$objectClassNames = $type->getObjectClassNames();
14-
1514
if (count($objectClassNames) !== 1) {
1615
return null;
1716
}

0 commit comments

Comments
 (0)