Skip to content

Commit 6cdbbef

Browse files
lookymanondrejmirtes
authored andcommitted
Introduce MemoizingReflectionProvider
1 parent 9be18df commit 6cdbbef

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Reflection\ReflectionProvider;
4+
5+
use PHPStan\Analyser\Scope;
6+
use PHPStan\Reflection\ClassReflection;
7+
use PHPStan\Reflection\FunctionReflection;
8+
use PHPStan\Reflection\GlobalConstantReflection;
9+
use PHPStan\Reflection\ReflectionProvider;
10+
11+
class MemoizingReflectionProvider implements ReflectionProvider
12+
{
13+
14+
/** @var \PHPStan\Reflection\ReflectionProvider */
15+
private $provider;
16+
17+
/** @var array<string, bool> */
18+
private $hasClasses = [];
19+
20+
/** @var array<string, \PHPStan\Reflection\ClassReflection> */
21+
private $classes = [];
22+
23+
/** @var array<string, string> */
24+
private $classNames = [];
25+
26+
/**
27+
* @param \PHPStan\Reflection\ReflectionProvider $provider
28+
*/
29+
public function __construct(ReflectionProvider $provider)
30+
{
31+
$this->provider = $provider;
32+
}
33+
34+
public function hasClass(string $className): bool
35+
{
36+
if (isset($this->hasClasses[$className])) {
37+
return $this->hasClasses[$className];
38+
}
39+
40+
return $this->hasClasses[$className] = $this->provider->hasClass($className);
41+
}
42+
43+
public function getClass(string $className): ClassReflection
44+
{
45+
if (isset($this->classes[$className])) {
46+
return $this->classes[$className];
47+
}
48+
49+
return $this->classes[$className] = $this->provider->getClass($className);
50+
}
51+
52+
public function getClassName(string $className): string
53+
{
54+
if (isset($this->classNames[$className])) {
55+
return $this->classNames[$className];
56+
}
57+
58+
return $this->classNames[$className] = $this->provider->getClassName($className);
59+
}
60+
61+
public function supportsAnonymousClasses(): bool
62+
{
63+
return $this->provider->supportsAnonymousClasses();
64+
}
65+
66+
public function getAnonymousClassReflection(\PhpParser\Node\Stmt\Class_ $classNode, Scope $scope): ClassReflection
67+
{
68+
return $this->provider->getAnonymousClassReflection($classNode, $scope);
69+
}
70+
71+
public function hasFunction(\PhpParser\Node\Name $nameNode, ?Scope $scope): bool
72+
{
73+
return $this->provider->hasFunction($nameNode, $scope);
74+
}
75+
76+
public function getFunction(\PhpParser\Node\Name $nameNode, ?Scope $scope): FunctionReflection
77+
{
78+
return $this->provider->getFunction($nameNode, $scope);
79+
}
80+
81+
public function resolveFunctionName(\PhpParser\Node\Name $nameNode, ?Scope $scope): ?string
82+
{
83+
return $this->provider->resolveFunctionName($nameNode, $scope);
84+
}
85+
86+
public function hasConstant(\PhpParser\Node\Name $nameNode, ?Scope $scope): bool
87+
{
88+
return $this->provider->hasConstant($nameNode, $scope);
89+
}
90+
91+
public function getConstant(\PhpParser\Node\Name $nameNode, ?Scope $scope): GlobalConstantReflection
92+
{
93+
return $this->provider->getConstant($nameNode, $scope);
94+
}
95+
96+
public function resolveConstantName(\PhpParser\Node\Name $nameNode, ?Scope $scope): ?string
97+
{
98+
return $this->provider->resolveConstantName($nameNode, $scope);
99+
}
100+
101+
}

src/Reflection/ReflectionProvider/ReflectionProviderFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function create(): ReflectionProvider
7272

7373
$providers[] = $this->createPhpStormStubsReflectionProvider();
7474

75-
return new ChainReflectionProvider($providers);
75+
return new MemoizingReflectionProvider(new ChainReflectionProvider($providers));
7676
}
7777

7878
private function createPhpStormStubsReflectionProvider(): ReflectionProvider

0 commit comments

Comments
 (0)