forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionCallableVariant.php
More file actions
182 lines (148 loc) · 4.27 KB
/
FunctionCallableVariant.php
File metadata and controls
182 lines (148 loc) · 4.27 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
<?php declare(strict_types = 1);
namespace PHPStan\Reflection\Callables;
use PHPStan\Reflection\Assertions;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ExtendedParameterReflection;
use PHPStan\Reflection\ExtendedParametersAcceptor;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVarianceMap;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Throwable;
use function array_map;
use function count;
final class FunctionCallableVariant implements CallableParametersAcceptor, ExtendedParametersAcceptor
{
/** @var SimpleThrowPoint[]|null */
private ?array $throwPoints = null;
/** @var SimpleImpurePoint[]|null */
private ?array $impurePoints = null;
public function __construct(
private FunctionReflection|ExtendedMethodReflection $function,
private ExtendedParametersAcceptor $variant,
)
{
}
/**
* @param list<ExtendedParametersAcceptor> $variants
* @return list<self>
*/
public static function createFromVariants(FunctionReflection|ExtendedMethodReflection $function, array $variants): array
{
return array_map(static fn (ExtendedParametersAcceptor $variant) => new self($function, $variant), $variants);
}
public function getTemplateTypeMap(): TemplateTypeMap
{
return $this->variant->getTemplateTypeMap();
}
public function getResolvedTemplateTypeMap(): TemplateTypeMap
{
return $this->variant->getResolvedTemplateTypeMap();
}
/**
* @return list<ExtendedParameterReflection>
*/
public function getParameters(): array
{
return $this->variant->getParameters();
}
public function isVariadic(): bool
{
return $this->variant->isVariadic();
}
public function getReturnType(): Type
{
return $this->variant->getReturnType();
}
public function getPhpDocReturnType(): Type
{
return $this->variant->getPhpDocReturnType();
}
public function getNativeReturnType(): Type
{
return $this->variant->getNativeReturnType();
}
public function getCallSiteVarianceMap(): TemplateTypeVarianceMap
{
return $this->variant->getCallSiteVarianceMap();
}
public function getThrowPoints(): array
{
if ($this->throwPoints !== null) {
return $this->throwPoints;
}
if ($this->variant instanceof CallableParametersAcceptor) {
return $this->throwPoints = $this->variant->getThrowPoints();
}
$returnType = $this->variant->getReturnType();
$throwType = $this->function->getThrowType();
if ($throwType === null) {
if ($returnType instanceof NeverType && $returnType->isExplicit()) {
$throwType = new ObjectType(Throwable::class);
}
}
$throwPoints = [];
if ($throwType !== null) {
if (!$throwType->isVoid()->yes()) {
$throwPoints[] = SimpleThrowPoint::createExplicit($throwType, true);
}
} else {
if (!(new ObjectType(Throwable::class))->isSuperTypeOf($returnType)->yes()) {
$throwPoints[] = SimpleThrowPoint::createImplicit();
}
}
return $this->throwPoints = $throwPoints;
}
public function isPure(): TrinaryLogic
{
$impurePoints = $this->getImpurePoints();
if (count($impurePoints) === 0) {
return TrinaryLogic::createYes();
}
$certainCount = 0;
foreach ($impurePoints as $impurePoint) {
if (!$impurePoint->isCertain()) {
continue;
}
$certainCount++;
}
return $certainCount > 0 ? TrinaryLogic::createNo() : TrinaryLogic::createMaybe();
}
public function getImpurePoints(): array
{
if ($this->impurePoints !== null) {
return $this->impurePoints;
}
if ($this->variant instanceof CallableParametersAcceptor) {
return $this->impurePoints = $this->variant->getImpurePoints();
}
$impurePoint = SimpleImpurePoint::createFromVariant($this->function, $this->variant);
if ($impurePoint === null) {
return $this->impurePoints = [];
}
return $this->impurePoints = [$impurePoint];
}
public function getInvalidateExpressions(): array
{
return [];
}
public function getUsedVariables(): array
{
return [];
}
public function acceptsNamedArguments(): TrinaryLogic
{
return $this->function->acceptsNamedArguments();
}
public function mustUseReturnValue(): TrinaryLogic
{
return $this->function->mustUseReturnValue();
}
public function getAsserts(): Assertions
{
return $this->function->getAsserts();
}
}