-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathFileCachedSourceLocator.php
More file actions
184 lines (156 loc) · 5.51 KB
/
FileCachedSourceLocator.php
File metadata and controls
184 lines (156 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\BetterReflection\SourceLocator;
use Override;
use PHPStan\BetterReflection\Identifier\Identifier;
use PHPStan\BetterReflection\Identifier\IdentifierType;
use PHPStan\BetterReflection\Reflection\Reflection;
use PHPStan\BetterReflection\Reflection\ReflectionClass;
use PHPStan\BetterReflection\Reflection\ReflectionConstant;
use PHPStan\BetterReflection\Reflection\ReflectionEnum;
use PHPStan\BetterReflection\Reflection\ReflectionFunction;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;
use PHPStan\Cache\Cache;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ConstantNameHelper;
use PHPStan\ShouldNotHappenException;
use function array_key_exists;
use function hash;
use function sprintf;
use function strtolower;
final class FileCachedSourceLocator implements SourceLocator
{
private const NOT_FOUND = null;
private const NULL_CACHED = true;
/** @var array{classes: array<string, ?Reflection>, functions: array<string, ?Reflection>, constants: array<string, ?Reflection>} */
private array $cachedSymbols = ['classes' => [], 'functions' => [], 'constants' => []];
/**
* @param non-empty-string $cacheKey
*/
public function __construct(
private SourceLocator $locator,
private Cache $cache,
private PhpVersion $phpVersion,
private string $cacheKey,
)
{
}
#[Override]
public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
{
if ($identifier->isClass()) {
$className = strtolower($identifier->getName());
if (!array_key_exists($className, $this->cachedSymbols['classes'])) {
$result = $this->loadCache($identifier, $reflector);
if ($result === self::NOT_FOUND) {
$result = $this->locator->locateIdentifier($reflector, $identifier);
$this->storeCache($identifier, $result);
} elseif ($result === self::NULL_CACHED) {
$result = null;
}
$this->cachedSymbols['classes'][$className] = $result;
}
return $this->cachedSymbols['classes'][$className];
}
if ($identifier->isFunction()) {
$className = strtolower($identifier->getName());
if (!array_key_exists($className, $this->cachedSymbols['functions'])) {
$result = $this->loadCache($identifier, $reflector);
if ($result === self::NOT_FOUND) {
$result = $this->locator->locateIdentifier($reflector, $identifier);
$this->storeCache($identifier, $result);
} elseif ($result === self::NULL_CACHED) {
$result = null;
}
$this->cachedSymbols['functions'][$className] = $result;
}
return $this->cachedSymbols['functions'][$className];
}
if ($identifier->isConstant()) {
$constantName = ConstantNameHelper::normalize($identifier->getName());
if (!array_key_exists($constantName, $this->cachedSymbols['constants'])) {
$result = $this->loadCache($identifier, $reflector);
if ($result === self::NOT_FOUND) {
$result = $this->locator->locateIdentifier($reflector, $identifier);
$this->storeCache($identifier, $result);
} elseif ($result === self::NULL_CACHED) {
$result = null;
}
$this->cachedSymbols['constants'][$constantName] = $result;
}
return $this->cachedSymbols['constants'][$constantName];
}
return null;
}
#[Override]
public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
{
return $this->locator->locateIdentifiersByType($reflector, $identifierType);
}
private function loadCache(
Identifier $identifier,
Reflector $reflector,
): ReflectionClass|ReflectionFunction|ReflectionConstant|true|null
{
[$cacheKey, $variableCacheKey] = $this->getCacheKeys($identifier);
$cachedReflection = $this->cache->load(
$cacheKey,
$variableCacheKey,
);
if ($cachedReflection === self::NOT_FOUND) {
return self::NOT_FOUND;
}
if ($cachedReflection === self::NULL_CACHED) {
return self::NULL_CACHED;
}
if ($identifier->isClass()) {
if (array_key_exists('backingType', $cachedReflection)) {
return ReflectionEnum::importFromCache($reflector, $cachedReflection);
}
return ReflectionClass::importFromCache($reflector, $cachedReflection);
}
if ($identifier->isFunction()) {
return ReflectionFunction::importFromCache($reflector, $cachedReflection);
}
return ReflectionConstant::importFromCache($reflector, $cachedReflection);
}
private function storeCache(
Identifier $identifier,
Reflection|null $reflection,
): void
{
[$cacheKey, $variableCacheKey] = $this->getCacheKeys($identifier);
$exported = self::NULL_CACHED;
if ($reflection !== null) {
if (
!$reflection instanceof ReflectionClass
&& !$reflection instanceof ReflectionFunction
&& !$reflection instanceof ReflectionConstant
) {
throw new ShouldNotHappenException();
}
$exported = $reflection->exportToCache();
}
$this->cache->save(
$cacheKey,
$variableCacheKey,
$exported,
);
}
/** @return array{non-empty-string, non-empty-string} */
private function getCacheKeys(Identifier $identifier): array
{
$suffix = $this->getCacheKeySuffix($identifier);
return [
sprintf('%s-%s', $this->cacheKey, $suffix),
sprintf('v3-%s-%s-%s', ComposerHelper::getBetterReflectionVersion(), $this->phpVersion->getVersionString(), $suffix),
];
}
/** @return non-empty-string */
private function getCacheKeySuffix(Identifier $identifier): string
{
$identifierHash = hash('sha256', $identifier->getName());
return sprintf('%s-%s', $identifier->getType()->getName(), $identifierHash);
}
}