/
ConstantResolver.php
268 lines (256 loc) · 8.07 KB
/
ConstantResolver.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
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use PhpParser\Node\Name;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Reflection\ReflectionProvider\ReflectionProviderProvider;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use function in_array;
use const PHP_INT_SIZE;
class ConstantResolver
{
/**
* @param string[] $dynamicConstantNames
*/
public function __construct(private ReflectionProviderProvider $reflectionProviderProvider, private array $dynamicConstantNames)
{
}
public function resolveConstant(Name $name, ?Scope $scope): ?Type
{
if (!$this->getReflectionProvider()->hasConstant($name, $scope)) {
return null;
}
/** @var string $resolvedConstantName */
$resolvedConstantName = $this->getReflectionProvider()->resolveConstantName($name, $scope);
// core, https://www.php.net/manual/en/reserved.constants.php
if ($resolvedConstantName === 'PHP_VERSION') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_MAJOR_VERSION') {
return IntegerRangeType::fromInterval(5, null);
}
if ($resolvedConstantName === 'PHP_MINOR_VERSION') {
return IntegerRangeType::fromInterval(0, null);
}
if ($resolvedConstantName === 'PHP_RELEASE_VERSION') {
return IntegerRangeType::fromInterval(0, null);
}
if ($resolvedConstantName === 'PHP_VERSION_ID') {
return IntegerRangeType::fromInterval(50207, null);
}
if ($resolvedConstantName === 'PHP_ZTS') {
return new UnionType([
new ConstantIntegerType(0),
new ConstantIntegerType(1),
]);
}
if ($resolvedConstantName === 'PHP_DEBUG') {
return new UnionType([
new ConstantIntegerType(0),
new ConstantIntegerType(1),
]);
}
if ($resolvedConstantName === 'PHP_MAXPATHLEN') {
return IntegerRangeType::fromInterval(1, null);
}
if ($resolvedConstantName === 'PHP_OS') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_OS_FAMILY') {
return new UnionType([
new ConstantStringType('Windows'),
new ConstantStringType('BSD'),
new ConstantStringType('Darwin'),
new ConstantStringType('Solaris'),
new ConstantStringType('Linux'),
new ConstantStringType('Unknown'),
]);
}
if ($resolvedConstantName === 'PHP_SAPI') {
return new UnionType([
new ConstantStringType('apache'),
new ConstantStringType('apache2handler'),
new ConstantStringType('cgi'),
new ConstantStringType('cli'),
new ConstantStringType('cli-server'),
new ConstantStringType('embed'),
new ConstantStringType('fpm-fcgi'),
new ConstantStringType('litespeed'),
new ConstantStringType('phpdbg'),
new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]),
]);
}
if ($resolvedConstantName === 'PHP_EOL') {
return new UnionType([
new ConstantStringType("\n"),
new ConstantStringType("\r\n"),
]);
}
if ($resolvedConstantName === 'PHP_INT_MAX') {
return PHP_INT_SIZE === 8
? new UnionType([new ConstantIntegerType(2147483647), new ConstantIntegerType(9223372036854775807)])
: new ConstantIntegerType(2147483647);
}
if ($resolvedConstantName === 'PHP_INT_MIN') {
// Why the -1 you might wonder, the answer is to fit it into an int :/ see https://3v4l.org/4SHIQ
return PHP_INT_SIZE === 8
? new UnionType([new ConstantIntegerType(-9223372036854775807 - 1), new ConstantIntegerType(-2147483647 - 1)])
: new ConstantIntegerType(-2147483647 - 1);
}
if ($resolvedConstantName === 'PHP_INT_SIZE') {
return new UnionType([
new ConstantIntegerType(4),
new ConstantIntegerType(8),
]);
}
if ($resolvedConstantName === 'PHP_FLOAT_DIG') {
return IntegerRangeType::fromInterval(1, null);
}
if ($resolvedConstantName === 'PHP_EXTENSION_DIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_PREFIX') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_BINDIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_BINARY') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_MANDIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_LIBDIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_DATADIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_SYSCONFDIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_LOCALSTATEDIR') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_CONFIG_FILE_PATH') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
if ($resolvedConstantName === 'PHP_SHLIB_SUFFIX') {
return new UnionType([
new ConstantStringType('so'),
new ConstantStringType('dll'),
]);
}
if ($resolvedConstantName === 'PHP_FD_SETSIZE') {
return IntegerRangeType::fromInterval(1, null);
}
if ($resolvedConstantName === '__COMPILER_HALT_OFFSET__') {
return IntegerRangeType::fromInterval(0, null);
}
// core other, https://www.php.net/manual/en/info.constants.php
if ($resolvedConstantName === 'PHP_WINDOWS_VERSION_MAJOR') {
return IntegerRangeType::fromInterval(4, null);
}
if ($resolvedConstantName === 'PHP_WINDOWS_VERSION_MINOR') {
return IntegerRangeType::fromInterval(0, null);
}
if ($resolvedConstantName === 'PHP_WINDOWS_VERSION_BUILD') {
return IntegerRangeType::fromInterval(1, null);
}
// dir, https://www.php.net/manual/en/dir.constants.php
if ($resolvedConstantName === 'DIRECTORY_SEPARATOR') {
return new UnionType([
new ConstantStringType('/'),
new ConstantStringType('\\'),
]);
}
if ($resolvedConstantName === 'PATH_SEPARATOR') {
return new UnionType([
new ConstantStringType(':'),
new ConstantStringType(';'),
]);
}
// iconv, https://www.php.net/manual/en/iconv.constants.php
if ($resolvedConstantName === 'ICONV_IMPL') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
// libxml, https://www.php.net/manual/en/libxml.constants.php
if ($resolvedConstantName === 'LIBXML_VERSION') {
return IntegerRangeType::fromInterval(1, null);
}
if ($resolvedConstantName === 'LIBXML_DOTTED_VERSION') {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
}
// openssl, https://www.php.net/manual/en/openssl.constants.php
if ($resolvedConstantName === 'OPENSSL_VERSION_NUMBER') {
return IntegerRangeType::fromInterval(1, null);
}
$constantType = $this->getReflectionProvider()->getConstant($name, $scope)->getValueType();
return $this->resolveConstantType($resolvedConstantName, $constantType);
}
public function resolveConstantType(string $constantName, Type $constantType): Type
{
if ($constantType instanceof ConstantType && in_array($constantName, $this->dynamicConstantNames, true)) {
return $constantType->generalize(GeneralizePrecision::lessSpecific());
}
return $constantType;
}
private function getReflectionProvider(): ReflectionProvider
{
return $this->reflectionProviderProvider->getReflectionProvider();
}
}