Skip to content

Commit 79db625

Browse files
committed
cache reflection
1 parent 3d5248f commit 79db625

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/Reflection/ReflectionResolver.php

Lines changed: 30 additions & 11 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,37 @@ 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+
if ($this->reflectionByHash[$callLikeHash] ?? null) {
207+
return $this->reflectionByHash[$callLikeHash];
208+
}
209+
199210
if ($callLike instanceof MethodCall) {
200-
return $this->resolveMethodReflectionFromMethodCall($callLike);
211+
$reflection = $this->resolveMethodReflectionFromMethodCall($callLike);
212+
$this->reflectionByHash[$callLikeHash] = $reflection;
213+
214+
return $reflection;
201215
}
202216

203217
if ($callLike instanceof StaticCall) {
204-
return $this->resolveMethodReflectionFromStaticCall($callLike);
218+
$reflection = $this->resolveMethodReflectionFromStaticCall($callLike);
219+
$this->reflectionByHash[$callLikeHash] = $reflection;
220+
return $reflection;
205221
}
206222

207223
if ($callLike instanceof New_) {
208-
return $this->resolveMethodReflectionFromNew($callLike);
224+
$reflection = $this->resolveMethodReflectionFromNew($callLike);
225+
$this->reflectionByHash[$callLikeHash] = $reflection;
226+
return $reflection;
209227
}
210228

211229
if ($callLike instanceof FuncCall) {
212-
return $this->resolveFunctionReflectionFromFuncCall($callLike);
230+
$reflection = $this->resolveFunctionReflectionFromFuncCall($callLike);
231+
$this->reflectionByHash[$callLikeHash] = $reflection;
232+
return $reflection;
213233
}
214234

215-
// todo: support NullsafeMethodCall
216235
return null;
217236
}
218237

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)