Repository navigation
Expand file tree
/
Copy pathPhpFunctionReflection.php
More file actions
278 lines (240 loc) · 7.83 KB
/
Copy pathPhpFunctionReflection.php
File metadata and controls
278 lines (240 loc) · 7.83 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
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\Php;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionFunction;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionParameter;
use PHPStan\DependencyInjection\GenerateFactory;
use PHPStan\Internal\DeprecatedAttributeHelper;
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\AttributeReflection;
use PHPStan\Reflection\AttributeReflectionFactory;
use PHPStan\Reflection\ExtendedFunctionVariant;
use PHPStan\Reflection\ExtendedParameterReflection;
use PHPStan\Reflection\ExtendedParametersAcceptor;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\FunctionReflectionFactory;
use PHPStan\Reflection\InitializerExprContext;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Reflection\ParameterAllowedConstantsMapProvider;
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 is_file;
use function strtolower;
#[GenerateFactory(interface: FunctionReflectionFactory::class)]
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
* @param list<AttributeReflection> $attributes
* @param array<string, bool> $phpDocParameterPureUnlessCallableIsImpure
* @param array<string, bool> $phpDocParameterPureUnlessParameterPassed
*/
public function __construct(
private InitializerExprTypeResolver $initializerExprTypeResolver,
private ReflectionFunction $reflection,
private AttributeReflectionFactory $attributeReflectionFactory,
private ParameterAllowedConstantsMapProvider $allowedConstantsMapProvider,
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,
private array $attributes,
private array $phpDocParameterPureUnlessCallableIsImpure,
private array $phpDocParameterPureUnlessParameterPassed,
)
{
}
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
{
return $this->variants ??= [
new ExtendedFunctionVariant(
$this->templateTypeMap,
null,
$this->getParameters(),
$this->isVariadic(),
null,
$this->getPhpDocReturnType(),
$this->getNativeReturnType(),
),
];
}
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->attributeReflectionFactory->fromNativeReflection($reflection->getAttributes(), InitializerExprContext::fromReflectionParameter($reflection)),
$this->allowedConstantsMapProvider->getForFunctionParameter(strtolower($this->reflection->getName()), $reflection->getName()),
TrinaryLogic::createFromBoolean($this->phpDocParameterPureUnlessCallableIsImpure[$reflection->getName()] ?? false),
TrinaryLogic::createFromBoolean($this->phpDocParameterPureUnlessParameterPassed[$reflection->getName()] ?? false),
);
}, $this->reflection->getParameters());
}
private function isVariadic(): bool
{
return $this->reflection->isVariadic();
}
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;
}
if ($this->reflection->isDeprecated()) {
$attributes = $this->reflection->getBetterReflection()->getAttributes();
return DeprecatedAttributeHelper::getDeprecatedDescription($attributes);
}
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);
}
/**
* @return array<string, TrinaryLogic>
*/
public function getPureUnlessCallableIsImpureParameters(): array
{
return array_map(static fn (bool $value): TrinaryLogic => TrinaryLogic::createFromBoolean($value), $this->phpDocParameterPureUnlessCallableIsImpure);
}
/**
* @return array<string, TrinaryLogic>
*/
public function getPureUnlessParameterPassedParameters(): array
{
return array_map(static fn (bool $value): TrinaryLogic => TrinaryLogic::createFromBoolean($value), $this->phpDocParameterPureUnlessParameterPassed);
}
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);
}
public function getAttributes(): array
{
return $this->attributes;
}
public function mustUseReturnValue(): TrinaryLogic
{
foreach ($this->attributes as $attrib) {
if (strtolower($attrib->getName()) === 'nodiscard') {
return TrinaryLogic::createYes();
}
}
return TrinaryLogic::createNo();
}
}