forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhpVersion.php
More file actions
509 lines (411 loc) · 10.5 KB
/
PhpVersion.php
File metadata and controls
509 lines (411 loc) · 10.5 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<?php declare(strict_types = 1);
namespace PHPStan\Php;
use PHPStan\DependencyInjection\AutowiredService;
use function floor;
/**
* Represents a specific PHP version for version-dependent analysis behavior.
*
* The version is stored as PHP_VERSION_ID format (e.g. 80100 for PHP 8.1.0).
* Extension developers can access it by injecting PhpVersion via constructor injection.
*
* @api
*/
#[AutowiredService(factory: '@PHPStan\Php\PhpVersionFactory::create')]
final class PhpVersion
{
public const SOURCE_RUNTIME = 1;
public const SOURCE_CONFIG = 2;
public const SOURCE_COMPOSER_PLATFORM_PHP = 3;
public const SOURCE_UNKNOWN = 4;
/**
* @api
* @param self::SOURCE_* $source
*/
public function __construct(private int $versionId, private int $source = self::SOURCE_UNKNOWN)
{
}
/**
* @return self::SOURCE_*
*/
public function getSource(): int
{
return $this->source;
}
public function getSourceLabel(): string
{
switch ($this->source) {
case self::SOURCE_RUNTIME:
return 'runtime';
case self::SOURCE_CONFIG:
return 'config';
case self::SOURCE_COMPOSER_PLATFORM_PHP:
return 'config.platform.php in composer.json';
}
return 'unknown';
}
public function getVersionId(): int
{
return $this->versionId;
}
public function getMajorVersionId(): int
{
return (int) floor($this->versionId / 10000);
}
public function getMinorVersionId(): int
{
return (int) floor(($this->versionId % 10000) / 100);
}
public function getPatchVersionId(): int
{
return (int) floor($this->versionId % 100);
}
public function getVersionString(): string
{
$first = $this->getMajorVersionId();
$second = $this->getMinorVersionId();
$third = $this->getPatchVersionId();
return $first . '.' . $second . ($third !== 0 ? '.' . $third : '');
}
public function supportsNullCoalesceAssign(): bool
{
return $this->versionId >= 70400;
}
public function supportsParameterContravariance(): bool
{
return $this->versionId >= 70400;
}
public function supportsReturnCovariance(): bool
{
return $this->versionId >= 70400;
}
public function supportsNoncapturingCatches(): bool
{
return $this->versionId >= 80000;
}
public function supportsNativeUnionTypes(): bool
{
return $this->versionId >= 80000;
}
public function deprecatesRequiredParameterAfterOptional(): bool
{
return $this->versionId >= 80000;
}
public function deprecatesRequiredParameterAfterOptionalNullableAndDefaultNull(): bool
{
return $this->versionId >= 80100;
}
public function deprecatesRequiredParameterAfterOptionalUnionOrMixed(): bool
{
return $this->versionId >= 80300;
}
public function supportsLessOverridenParametersWithVariadic(): bool
{
return $this->versionId >= 80000;
}
public function supportsThrowExpression(): bool
{
return $this->versionId >= 80000;
}
public function supportsClassConstantOnExpression(): bool
{
return $this->versionId >= 80000;
}
public function supportsLegacyConstructor(): bool
{
return $this->versionId < 80000;
}
public function supportsPromotedProperties(): bool
{
return $this->versionId >= 80000;
}
public function supportsParameterTypeWidening(): bool
{
return $this->versionId >= 70200;
}
public function supportsUnsetCast(): bool
{
return $this->versionId < 80000;
}
public function supportsNamedArguments(): bool
{
return $this->versionId >= 80000;
}
public function throwsTypeErrorForInternalFunctions(): bool
{
return $this->versionId >= 80000;
}
public function throwsValueErrorForInternalFunctions(): bool
{
return $this->versionId >= 80000;
}
public function supportsHhPrintfSpecifier(): bool
{
return $this->versionId >= 80000;
}
public function isEmptyStringValidAliasForNoneInMbSubstituteCharacter(): bool
{
return $this->versionId < 80000;
}
public function supportsAllUnicodeScalarCodePointsInMbSubstituteCharacter(): bool
{
return $this->versionId >= 70200;
}
public function isNumericStringValidArgInMbSubstituteCharacter(): bool
{
return $this->versionId < 80000;
}
public function isNullValidArgInMbSubstituteCharacter(): bool
{
return $this->versionId >= 80000;
}
public function isInterfaceConstantImplicitlyFinal(): bool
{
return $this->versionId < 80100;
}
public function supportsFinalConstants(): bool
{
return $this->versionId >= 80100;
}
public function supportsReadOnlyProperties(): bool
{
return $this->versionId >= 80100;
}
public function supportsEnums(): bool
{
return $this->versionId >= 80100;
}
public function supportsPureIntersectionTypes(): bool
{
return $this->versionId >= 80100;
}
public function supportsCaseInsensitiveConstantNames(): bool
{
return $this->versionId < 80000;
}
public function hasStricterRoundFunctions(): bool
{
return $this->versionId >= 80000;
}
public function hasTentativeReturnTypes(): bool
{
return $this->versionId >= 80100;
}
public function supportsFirstClassCallables(): bool
{
return $this->versionId >= 80100;
}
public function supportsArrayUnpackingWithStringKeys(): bool
{
return $this->versionId >= 80100;
}
public function throwsOnInvalidMbStringEncoding(): bool
{
return $this->versionId >= 80000;
}
public function supportsPassNoneEncodings(): bool
{
return $this->versionId < 70300;
}
public function producesWarningForFinalPrivateMethods(): bool
{
return $this->versionId >= 80000;
}
public function deprecatesDynamicProperties(): bool
{
return $this->versionId >= 80200;
}
public function strSplitReturnsEmptyArray(): bool
{
return $this->versionId >= 80200;
}
public function supportsDisjunctiveNormalForm(): bool
{
return $this->versionId >= 80200;
}
public function serializableRequiresMagicMethods(): bool
{
return $this->versionId >= 80100;
}
public function arrayFunctionsReturnNullWithNonArray(): bool
{
return $this->versionId < 80000;
}
// see https://www.php.net/manual/en/migration80.incompatible.php#migration80.incompatible.core.string-number-comparision
public function castsNumbersToStringsOnLooseComparison(): bool
{
return $this->versionId >= 80000;
}
public function nonNumericStringAndIntegerIsFalseOnLooseComparison(): bool
{
return $this->versionId >= 80000;
}
public function supportsCallableInstanceMethods(): bool
{
return $this->versionId < 80000;
}
public function supportsJsonValidate(): bool
{
return $this->versionId >= 80300;
}
public function supportsConstantsInTraits(): bool
{
return $this->versionId >= 80200;
}
public function supportsNativeTypesInClassConstants(): bool
{
return $this->versionId >= 80300;
}
public function supportsAbstractTraitMethods(): bool
{
return $this->versionId >= 80000;
}
public function supportsOverrideAttribute(): bool
{
return $this->versionId >= 80300;
}
public function supportsDynamicClassConstantFetch(): bool
{
return $this->versionId >= 80300;
}
public function supportsReadOnlyClasses(): bool
{
return $this->versionId >= 80200;
}
public function supportsReadOnlyAnonymousClasses(): bool
{
return $this->versionId >= 80300;
}
public function supportsNeverReturnTypeInArrowFunction(): bool
{
return $this->versionId >= 80200;
}
public function supportsPregUnmatchedAsNull(): bool
{
// while PREG_UNMATCHED_AS_NULL is defined in php-src since 7.2.x it starts working as expected with 7.4.x
// https://3v4l.org/v3HE4
return $this->versionId >= 70400;
}
public function supportsPregCaptureOnlyNamedGroups(): bool
{
// https://php.watch/versions/8.2/preg-n-no-capture-modifier
return $this->versionId >= 80200;
}
public function supportsPropertyHooks(): bool
{
return $this->versionId >= 80400;
}
public function supportsFinalProperties(): bool
{
return $this->versionId >= 80400;
}
public function supportsAsymmetricVisibility(): bool
{
return $this->versionId >= 80400;
}
public function supportsAsymmetricVisibilityForStaticProperties(): bool
{
return $this->versionId >= 80500;
}
public function supportsLazyObjects(): bool
{
return $this->versionId >= 80400;
}
public function hasDateTimeExceptions(): bool
{
return $this->versionId >= 80300;
}
public function isCurloptUrlCheckingFileSchemeWithOpenBasedir(): bool
{
// Before PHP 8.0, when setting CURLOPT_URL, an unparsable URL or a file:// scheme would fail if open_basedir is used
// https://github.com/php/php-src/blob/php-7.4.33/ext/curl/interface.c#L139-L158
// https://github.com/php/php-src/blob/php-8.0.0/ext/curl/interface.c#L128-L130
return $this->versionId < 80000;
}
/**
* whether curl handles are represented as 'resource' or CurlShareHandle
*/
public function supportsCurlShareHandle(): bool
{
return $this->versionId >= 80000;
}
public function supportsCurlSharePersistentHandle(): bool
{
return $this->versionId >= 80500;
}
public function highlightStringDoesNotReturnFalse(): bool
{
return $this->versionId >= 80400;
}
public function deprecatesImplicitlyNullableParameterTypes(): bool
{
return $this->versionId >= 80400;
}
public function substrReturnFalseInsteadOfEmptyString(): bool
{
return $this->versionId < 80000;
}
public function supportsBcMathNumberOperatorOverloading(): bool
{
return $this->versionId >= 80400;
}
public function hasPDOSubclasses(): bool
{
return $this->versionId >= 80400;
}
public function deprecatesImplicitlyFloatConversionToInt(): bool
{
return $this->versionId >= 80100;
}
public function deprecatesNullArrayOffset(): bool
{
return $this->versionId >= 80500;
}
public function supportsFinalPromotedProperties(): bool
{
return $this->versionId >= 80500;
}
public function supportsVoidCast(): bool
{
return $this->versionId >= 80500;
}
public function supportsNoDiscardAttribute(): bool
{
return $this->versionId >= 80500;
}
public function deprecatesNonStandardCasts(): bool
{
return $this->versionId >= 80500;
}
public function deprecatesBacktickOperator(): bool
{
return $this->versionId >= 80500;
}
public function supportsAttributesOnGlobalConstants(): bool
{
return $this->versionId >= 80500;
}
public function supportsDeprecatedTraits(): bool
{
return $this->versionId >= 80500;
}
public function supportsOverrideAttributeOnProperty(): bool
{
return $this->versionId >= 80500;
}
public function deprecatesDecOnNonNumericString(): bool
{
return $this->versionId >= 80300;
}
public function deprecatesIncOnNonNumericString(): bool
{
return $this->versionId >= 80500;
}
public function supportsObjectsInArraySumProduct(): bool
{
return $this->versionId >= 80300;
}
public function hasFilterThrowOnFailureConstant(): bool
{
return $this->versionId >= 80500;
}
}