forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionSignatureMapProvider.php
More file actions
352 lines (295 loc) · 11.2 KB
/
Copy pathFunctionSignatureMapProvider.php
File metadata and controls
352 lines (295 loc) · 11.2 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\SignatureMap;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionFunction;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
use PHPStan\Cache\ArenaCache;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\InitializerExprContext;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\MixedType;
use PHPStan\Type\TypehintHelper;
use ReflectionFunctionAbstract;
use function array_change_key_case;
use function array_key_exists;
use function array_keys;
use function is_array;
use function sprintf;
use function strtolower;
use const CASE_LOWER;
#[AutowiredService(as: FunctionSignatureMapProvider::class)]
final class FunctionSignatureMapProvider implements SignatureMapProvider
{
/** @var array<string, mixed[]> */
private static array $signatureMaps = [];
/**
* Rows already fetched from the shared arena (false = authoritatively
* absent there), so steady-state lookups cost one local array access —
* the same as reading the full map would.
*
* @var array<string, mixed[]|false>
*/
private static array $arenaSignatureRows = [];
/** @var array<string, array{hasSideEffects: bool}>|null */
private static ?array $functionMetadata = null;
/** @var array<string, array{hasSideEffects: bool}|false> */
private static array $arenaMetadataRows = [];
public function __construct(
private SignatureMapParser $parser,
private InitializerExprTypeResolver $initializerExprTypeResolver,
private PhpVersion $phpVersion,
#[AutowiredParameter(ref: '%featureToggles.stricterFunctionMap%')]
private bool $stricterFunctionMap,
)
{
}
public function hasMethodSignature(string $className, string $methodName): bool
{
return $this->hasFunctionSignature(sprintf('%s::%s', $className, $methodName));
}
public function hasFunctionSignature(string $name): bool
{
return $this->getSignatureMapEntry(strtolower($name)) !== null;
}
/**
* A row of the merged signature map, preferring the run's shared arena:
* the first process to need the map builds and publishes it, workers
* after that materialize only the rows they touch instead of the whole
* multi-megabyte map.
*
* @return mixed[]|null
*/
private function getSignatureMapEntry(string $lowerName): ?array
{
$cacheKey = sprintf('%d-%d', $this->phpVersion->getVersionId(), $this->stricterFunctionMap ? 1 : 0);
if (array_key_exists($cacheKey, self::$signatureMaps)) {
return self::$signatureMaps[$cacheKey][$lowerName] ?? null;
}
$memoKey = $cacheKey . '/' . $lowerName;
if (array_key_exists($memoKey, self::$arenaSignatureRows)) {
$row = self::$arenaSignatureRows[$memoKey];
return $row === false ? null : $row;
}
$recordKey = 'sigmap-' . $cacheKey;
if (ArenaCache::hasRecord($recordKey)) {
$row = ArenaCache::lookupHash($recordKey, $lowerName);
if (!is_array($row)) {
$row = null;
}
self::$arenaSignatureRows[$memoKey] = $row ?? false;
return $row;
}
return $this->getSignatureMap()[$lowerName] ?? null;
}
public function getMethodSignatures(string $className, string $methodName, ?ReflectionMethod $reflectionMethod): array
{
return $this->getFunctionSignatures(sprintf('%s::%s', $className, $methodName), $className, $reflectionMethod);
}
public function getFunctionSignatures(string $functionName, ?string $className, ?ReflectionFunctionAbstract $reflectionFunction): array
{
$functionName = strtolower($functionName);
$signatures = [$this->createSignature($functionName, $className, $reflectionFunction)];
$i = 1;
$variantFunctionName = $functionName . '\'' . $i;
while ($this->hasFunctionSignature($variantFunctionName)) {
$signatures[] = $this->createSignature($variantFunctionName, $className, $reflectionFunction);
$i++;
$variantFunctionName = $functionName . '\'' . $i;
}
return ['positional' => $signatures, 'named' => null];
}
private function createSignature(string $functionName, ?string $className, ?ReflectionFunctionAbstract $reflectionFunction): FunctionSignature
{
if (!$reflectionFunction instanceof ReflectionMethod && !$reflectionFunction instanceof ReflectionFunction && $reflectionFunction !== null) {
throw new ShouldNotHappenException();
}
$signatureRow = $this->getSignatureMapEntry($functionName);
if ($signatureRow === null) {
throw new ShouldNotHappenException(sprintf('Function %s is not in the signature map.', $functionName));
}
$signature = $this->parser->getFunctionSignature(
$signatureRow,
$className,
);
$parameters = [];
foreach ($signature->getParameters() as $i => $parameter) {
if ($reflectionFunction === null) {
$parameters[] = $parameter;
continue;
}
$nativeParameters = $reflectionFunction->getParameters();
if (!array_key_exists($i, $nativeParameters)) {
$parameters[] = $parameter;
continue;
}
$parameters[] = new ParameterSignature(
$parameter->getName(),
$parameter->isOptional(),
$parameter->getType(),
TypehintHelper::decideTypeFromReflection($nativeParameters[$i]->getType(), isBuiltin: true),
$parameter->passedByReference(),
$parameter->isVariadic(),
$nativeParameters[$i]->isDefaultValueAvailable() ? $this->initializerExprTypeResolver->getType(
$nativeParameters[$i]->getDefaultValueExpression(),
InitializerExprContext::fromReflectionParameter($nativeParameters[$i]),
) : null,
$parameter->getOutType(),
);
}
if ($reflectionFunction === null) {
$nativeReturnType = new MixedType();
} else {
$nativeReturnType = TypehintHelper::decideTypeFromReflection($reflectionFunction->getReturnType(), isBuiltin: true);
}
return new FunctionSignature(
$parameters,
$signature->getReturnType(),
$nativeReturnType,
$signature->isVariadic(),
);
}
public function hasMethodMetadata(string $className, string $methodName): bool
{
return $this->hasFunctionMetadata(sprintf('%s::%s', $className, $methodName));
}
public function hasFunctionMetadata(string $name): bool
{
return $this->getFunctionMetadataEntry(strtolower($name)) !== null;
}
/**
* @return array{hasSideEffects: bool}|null
*/
private function getFunctionMetadataEntry(string $lowerName): ?array
{
if (self::$functionMetadata !== null) {
return self::$functionMetadata[$lowerName] ?? null;
}
if (array_key_exists($lowerName, self::$arenaMetadataRows)) {
$row = self::$arenaMetadataRows[$lowerName];
return $row === false ? null : $row;
}
if (ArenaCache::hasRecord('funcmeta')) {
$row = ArenaCache::lookupHash('funcmeta', $lowerName);
if (!is_array($row)) {
$row = null;
}
/** @var array{hasSideEffects: bool}|null $row */
self::$arenaMetadataRows[$lowerName] = $row ?? false;
return $row;
}
return self::getFunctionMetadataMap()[$lowerName] ?? null;
}
/**
* @return array{hasSideEffects: bool}
*/
public function getMethodMetadata(string $className, string $methodName): array
{
return $this->getFunctionMetadata(sprintf('%s::%s', $className, $methodName));
}
/**
* @return array{hasSideEffects: bool}
*/
public function getFunctionMetadata(string $functionName): array
{
$entry = $this->getFunctionMetadataEntry(strtolower($functionName));
if ($entry === null) {
throw new ShouldNotHappenException();
}
return $entry;
}
/**
* @return array<string, array{hasSideEffects: bool}>
*/
private static function getFunctionMetadataMap(): array
{
if (self::$functionMetadata === null) {
/** @var array<string, array{hasSideEffects: bool}> $metadata */
$metadata = require __DIR__ . '/../../../resources/functionMetadata.php';
self::$functionMetadata = array_change_key_case($metadata, CASE_LOWER);
ArenaCache::publishHash('funcmeta', self::$functionMetadata);
}
return self::$functionMetadata;
}
/**
* @return mixed[]
*/
public function getSignatureMap(): array
{
$cacheKey = sprintf('%d-%d', $this->phpVersion->getVersionId(), $this->stricterFunctionMap ? 1 : 0);
if (array_key_exists($cacheKey, self::$signatureMaps)) {
return self::$signatureMaps[$cacheKey];
}
$signatureMap = require __DIR__ . '/../../../resources/functionMap.php';
if (!is_array($signatureMap)) {
throw new ShouldNotHappenException('Signature map could not be loaded.');
}
$signatureMap = array_change_key_case($signatureMap, CASE_LOWER);
if ($this->stricterFunctionMap) {
$signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_bleedingEdge.php');
}
if ($this->phpVersion->getVersionId() >= 70400) {
$signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php74delta.php');
}
if ($this->phpVersion->getVersionId() >= 80000) {
$signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php80delta.php');
if ($this->stricterFunctionMap) {
$signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php80delta_bleedingEdge.php');
}
}
if ($this->phpVersion->getVersionId() >= 80100) {
$signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php81delta.php');
}
if ($this->phpVersion->getVersionId() >= 80200) {
$signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php82delta.php');
}
if ($this->phpVersion->getVersionId() >= 80300) {
$signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php83delta.php');
}
if ($this->phpVersion->getVersionId() >= 80400) {
$signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php84delta.php');
}
if ($this->phpVersion->getVersionId() >= 80500) {
$signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php85delta.php');
}
self::$signatureMaps[$cacheKey] = $signatureMap;
ArenaCache::publishHash('sigmap-' . $cacheKey, $signatureMap);
return $signatureMap;
}
/**
* @param array<string, mixed> $signatureMap
* @return array<string, mixed>
*/
private function computeSignatureMapFile(array $signatureMap, string $file): array
{
$signatureMapDelta = include $file;
if (!is_array($signatureMapDelta)) {
throw new ShouldNotHappenException(sprintf('Signature map file "%s" could not be loaded.', $file));
}
return $this->computeSignatureMap($signatureMap, $signatureMapDelta);
}
/**
* @param array<string, mixed> $signatureMap
* @param array<string, array<string, mixed>> $delta
* @return array<string, mixed>
*/
private function computeSignatureMap(array $signatureMap, array $delta): array
{
foreach (array_keys($delta['old']) as $key) {
unset($signatureMap[strtolower($key)]);
}
foreach ($delta['new'] as $key => $signature) {
$signatureMap[strtolower($key)] = $signature;
}
return $signatureMap;
}
public function hasClassConstantMetadata(string $className, string $constantName): bool
{
return false;
}
public function getClassConstantMetadata(string $className, string $constantName): array
{
throw new ShouldNotHappenException();
}
}