forked from phpstan/phpstan-src
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhpFunctionReflection.php
314 lines (268 loc) · 7.92 KB
/
PhpFunctionReflection.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\Php;
use PhpParser\Node;
use PhpParser\Node\Stmt\Function_;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionFunction;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter;
use PHPStan\Cache\Cache;
use PHPStan\Parser\FunctionCallStatementFinder;
use PHPStan\Parser\Parser;
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\ExtendedFunctionVariant;
use PHPStan\Reflection\ExtendedParameterReflection;
use PHPStan\Reflection\ExtendedParametersAcceptor;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypehintHelper;
use function array_key_exists;
use function array_map;
use function filemtime;
use function is_array;
use function is_file;
use function sprintf;
use function time;
final class PhpFunctionReflection implements FunctionReflection
{
/** @var list<ExtendedFunctionVariant>|null */
private ?array $variants = null;
/**
* @param array<string, Type> $phpDocParameterTypes
* @param array<string, Type> $phpDocParameterOutTypes
* @param array<string, bool> $phpDocParameterImmediatelyInvokedCallable
* @param array<string, Type> $phpDocParameterClosureThisTypes
*/
public function __construct(
private InitializerExprTypeResolver $initializerExprTypeResolver,
private ReflectionFunction $reflection,
private Parser $parser,
private FunctionCallStatementFinder $functionCallStatementFinder,
private Cache $cache,
private TemplateTypeMap $templateTypeMap,
private array $phpDocParameterTypes,
private ?Type $phpDocReturnType,
private ?Type $phpDocThrowType,
private ?string $deprecatedDescription,
private bool $isDeprecated,
private bool $isInternal,
private ?string $filename,
private ?bool $isPure,
private Assertions $asserts,
private bool $acceptsNamedArguments,
private ?string $phpDocComment,
private array $phpDocParameterOutTypes,
private array $phpDocParameterImmediatelyInvokedCallable,
private array $phpDocParameterClosureThisTypes,
)
{
}
public function getName(): string
{
return $this->reflection->getName();
}
public function getFileName(): ?string
{
if ($this->filename === null) {
return null;
}
if (!is_file($this->filename)) {
return null;
}
return $this->filename;
}
public function getVariants(): array
{
if ($this->variants === null) {
$this->variants = [
new ExtendedFunctionVariant(
$this->templateTypeMap,
null,
$this->getParameters(),
$this->isVariadic(),
$this->getReturnType(),
$this->getPhpDocReturnType(),
$this->getNativeReturnType(),
),
];
}
return $this->variants;
}
public function getOnlyVariant(): ExtendedParametersAcceptor
{
return $this->getVariants()[0];
}
public function getNamedArgumentsVariants(): ?array
{
return null;
}
/**
* @return list<ExtendedParameterReflection>
*/
private function getParameters(): array
{
return array_map(function (ReflectionParameter $reflection): PhpParameterReflection {
if (array_key_exists($reflection->getName(), $this->phpDocParameterImmediatelyInvokedCallable)) {
$immediatelyInvokedCallable = TrinaryLogic::createFromBoolean($this->phpDocParameterImmediatelyInvokedCallable[$reflection->getName()]);
} else {
$immediatelyInvokedCallable = TrinaryLogic::createMaybe();
}
return new PhpParameterReflection(
$this->initializerExprTypeResolver,
$reflection,
$this->phpDocParameterTypes[$reflection->getName()] ?? null,
null,
$this->phpDocParameterOutTypes[$reflection->getName()] ?? null,
$immediatelyInvokedCallable,
$this->phpDocParameterClosureThisTypes[$reflection->getName()] ?? null,
);
}, $this->reflection->getParameters());
}
private function isVariadic(): bool
{
$isNativelyVariadic = $this->reflection->isVariadic();
if (!$isNativelyVariadic && $this->reflection
->getFileName() !== false) {
$fileName = $this->reflection->getFileName();
if (is_file($fileName)) {
$functionName = $this->reflection->getName();
$modifiedTime = filemtime($fileName);
if ($modifiedTime === false) {
$modifiedTime = time();
}
$variableCacheKey = sprintf('%d-v4', $modifiedTime);
$key = sprintf('variadic-function-%s-%s', $functionName, $fileName);
$cachedResult = $this->cache->load($key, $variableCacheKey);
if ($cachedResult === null) {
$nodes = $this->parser->parseFile($fileName);
$result = !$this->containsVariadicFunction($nodes)->no();
$this->cache->save($key, $variableCacheKey, $result);
return $result;
}
return $cachedResult;
}
}
return $isNativelyVariadic;
}
/**
* @param Node[]|scalar[]|Node $node
*/
private function containsVariadicFunction(array|Node $node): TrinaryLogic
{
$result = TrinaryLogic::createMaybe();
if ($node instanceof Node) {
if ($node instanceof Function_) {
$functionName = (string) $node->namespacedName;
if ($functionName === $this->reflection->getName()) {
return TrinaryLogic::createFromBoolean($this->isFunctionNodeVariadic($node));
}
}
foreach ($node->getSubNodeNames() as $subNodeName) {
$innerNode = $node->{$subNodeName};
if (!$innerNode instanceof Node && !is_array($innerNode)) {
continue;
}
$result = $result->and($this->containsVariadicFunction($innerNode));
}
} elseif (is_array($node)) {
foreach ($node as $subNode) {
if (!$subNode instanceof Node) {
continue;
}
$result = $result->and($this->containsVariadicFunction($subNode));
}
}
return $result;
}
private function getReturnType(): Type
{
return TypehintHelper::decideTypeFromReflection(
$this->reflection->getReturnType(),
$this->phpDocReturnType,
);
}
private function getPhpDocReturnType(): Type
{
if ($this->phpDocReturnType !== null) {
return $this->phpDocReturnType;
}
return new MixedType();
}
private function getNativeReturnType(): Type
{
return TypehintHelper::decideTypeFromReflection($this->reflection->getReturnType());
}
public function getDeprecatedDescription(): ?string
{
if ($this->isDeprecated) {
return $this->deprecatedDescription;
}
return null;
}
public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean(
$this->isDeprecated || $this->reflection->isDeprecated(),
);
}
public function isInternal(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($this->isInternal);
}
public function getThrowType(): ?Type
{
return $this->phpDocThrowType;
}
public function hasSideEffects(): TrinaryLogic
{
if ($this->getReturnType()->isVoid()->yes()) {
return TrinaryLogic::createYes();
}
if ($this->isPure !== null) {
return TrinaryLogic::createFromBoolean(!$this->isPure);
}
return TrinaryLogic::createMaybe();
}
public function isPure(): TrinaryLogic
{
if ($this->isPure === null) {
return TrinaryLogic::createMaybe();
}
return TrinaryLogic::createFromBoolean($this->isPure);
}
public function isBuiltin(): bool
{
return $this->reflection->isInternal();
}
public function getAsserts(): Assertions
{
return $this->asserts;
}
public function getDocComment(): ?string
{
return $this->phpDocComment;
}
public function returnsByReference(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($this->reflection->returnsReference());
}
public function acceptsNamedArguments(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($this->acceptsNamedArguments);
}
private function isFunctionNodeVariadic(Function_ $node): bool
{
foreach ($node->params as $parameter) {
if ($parameter->variadic) {
return true;
}
}
if ($this->functionCallStatementFinder->findFunctionCallInStatements(ParametersAcceptor::VARIADIC_FUNCTIONS, $node->getStmts()) !== null) {
return true;
}
return false;
}
}