1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Php;
4:
5: use PHPStan\DependencyInjection\AutowiredService;
6: use PHPStan\Turbo\ReferencedByTurboExtension;
7: use function floor;
8:
9: /**
10: * Represents a specific PHP version for version-dependent analysis behavior.
11: *
12: * The version is stored as PHP_VERSION_ID format (e.g. 80100 for PHP 8.1.0).
13: * Extension developers can access it by injecting PhpVersion via constructor injection.
14: *
15: * @api
16: */
17: #[AutowiredService(factory: '@PHPStan\Php\PhpVersionFactory::create')]
18: #[ReferencedByTurboExtension(key: 'phpVersion')]
19: final class PhpVersion
20: {
21:
22: public const SOURCE_RUNTIME = 1;
23: public const SOURCE_CONFIG = 2;
24: public const SOURCE_COMPOSER_PLATFORM_PHP = 3;
25: public const SOURCE_UNKNOWN = 4;
26:
27: /**
28: * @api
29: * @param self::SOURCE_* $source
30: */
31: public function __construct(private int $versionId, private int $source = self::SOURCE_UNKNOWN)
32: {
33: }
34:
35: /**
36: * @return self::SOURCE_*
37: */
38: public function getSource(): int
39: {
40: return $this->source;
41: }
42:
43: public function getSourceLabel(): string
44: {
45: switch ($this->source) {
46: case self::SOURCE_RUNTIME:
47: return 'runtime';
48: case self::SOURCE_CONFIG:
49: return 'config';
50: case self::SOURCE_COMPOSER_PLATFORM_PHP:
51: return 'config.platform.php in composer.json';
52: }
53:
54: return 'unknown';
55: }
56:
57: public function getVersionId(): int
58: {
59: return $this->versionId;
60: }
61:
62: public function getMajorVersionId(): int
63: {
64: return (int) floor($this->versionId / 10000);
65: }
66:
67: public function getMinorVersionId(): int
68: {
69: return (int) floor(($this->versionId % 10000) / 100);
70: }
71:
72: public function getPatchVersionId(): int
73: {
74: return (int) floor($this->versionId % 100);
75: }
76:
77: public function getVersionString(): string
78: {
79: $first = $this->getMajorVersionId();
80: $second = $this->getMinorVersionId();
81: $third = $this->getPatchVersionId();
82:
83: return $first . '.' . $second . ($third !== 0 ? '.' . $third : '');
84: }
85:
86: public function supportsNullCoalesceAssign(): bool
87: {
88: return $this->versionId >= 70400;
89: }
90:
91: public function supportsParameterContravariance(): bool
92: {
93: return $this->versionId >= 70400;
94: }
95:
96: public function supportsReturnCovariance(): bool
97: {
98: return $this->versionId >= 70400;
99: }
100:
101: public function supportsNoncapturingCatches(): bool
102: {
103: return $this->versionId >= 80000;
104: }
105:
106: public function supportsNullsafeOperator(): bool
107: {
108: return $this->versionId >= 80000;
109: }
110:
111: public function supportsNativeUnionTypes(): bool
112: {
113: return $this->versionId >= 80000;
114: }
115:
116: public function supportsNativeMixed(): bool
117: {
118: return $this->versionId >= 80000;
119: }
120:
121: public function supportsTrueFalseNullStandaloneType(): bool
122: {
123: return $this->versionId >= 80200;
124: }
125:
126: public function supportsPipeOperator(): bool
127: {
128: return $this->versionId >= 80500;
129: }
130:
131: public function deprecatesRequiredParameterAfterOptional(): bool
132: {
133: return $this->versionId >= 80000;
134: }
135:
136: public function deprecatesRequiredParameterAfterOptionalNullableAndDefaultNull(): bool
137: {
138: return $this->versionId >= 80100;
139: }
140:
141: public function deprecatesRequiredParameterAfterOptionalUnionOrMixed(): bool
142: {
143: return $this->versionId >= 80300;
144: }
145:
146: public function supportsLessOverridenParametersWithVariadic(): bool
147: {
148: return $this->versionId >= 80000;
149: }
150:
151: public function supportsThrowExpression(): bool
152: {
153: return $this->versionId >= 80000;
154: }
155:
156: public function supportsClassConstantOnExpression(): bool
157: {
158: return $this->versionId >= 80000;
159: }
160:
161: public function supportsLegacyConstructor(): bool
162: {
163: return $this->versionId < 80000;
164: }
165:
166: public function supportsPromotedProperties(): bool
167: {
168: return $this->versionId >= 80000;
169: }
170:
171: public function supportsParameterTypeWidening(): bool
172: {
173: return $this->versionId >= 70200;
174: }
175:
176: public function supportsUnsetCast(): bool
177: {
178: return $this->versionId < 80000;
179: }
180:
181: public function supportsNamedArguments(): bool
182: {
183: return $this->versionId >= 80000;
184: }
185:
186: public function throwsOnDivisionByZero(): bool
187: {
188: return $this->versionId >= 80000;
189: }
190:
191: public function throwsTypeErrorForInternalFunctions(): bool
192: {
193: return $this->versionId >= 80000;
194: }
195:
196: public function throwsValueErrorForInternalFunctions(): bool
197: {
198: return $this->versionId >= 80000;
199: }
200:
201: public function supportsHhPrintfSpecifier(): bool
202: {
203: return $this->versionId >= 80000;
204: }
205:
206: public function isEmptyStringValidAliasForNoneInMbSubstituteCharacter(): bool
207: {
208: return $this->versionId < 80000;
209: }
210:
211: public function supportsAllUnicodeScalarCodePointsInMbSubstituteCharacter(): bool
212: {
213: return $this->versionId >= 70200;
214: }
215:
216: public function isNumericStringValidArgInMbSubstituteCharacter(): bool
217: {
218: return $this->versionId < 80000;
219: }
220:
221: public function isNullValidArgInMbSubstituteCharacter(): bool
222: {
223: return $this->versionId >= 80000;
224: }
225:
226: public function isZeroValidCodePointInMbSubstituteCharacter(): bool
227: {
228: return $this->versionId >= 80000;
229: }
230:
231: public function isInterfaceConstantImplicitlyFinal(): bool
232: {
233: return $this->versionId < 80100;
234: }
235:
236: public function supportsFinalConstants(): bool
237: {
238: return $this->versionId >= 80100;
239: }
240:
241: public function supportsReadOnlyProperties(): bool
242: {
243: return $this->versionId >= 80100;
244: }
245:
246: public function supportsEnums(): bool
247: {
248: return $this->versionId >= 80100;
249: }
250:
251: public function supportsPureIntersectionTypes(): bool
252: {
253: return $this->versionId >= 80100;
254: }
255:
256: public function supportsCaseInsensitiveConstantNames(): bool
257: {
258: return $this->versionId < 80000;
259: }
260:
261: public function hasStricterRoundFunctions(): bool
262: {
263: return $this->versionId >= 80000;
264: }
265:
266: public function throwsValueErrorForInvalidRoundingMode(): bool
267: {
268: return $this->versionId >= 80400;
269: }
270:
271: public function hasTentativeReturnTypes(): bool
272: {
273: return $this->versionId >= 80100;
274: }
275:
276: public function supportsFirstClassCallables(): bool
277: {
278: return $this->versionId >= 80100;
279: }
280:
281: public function supportsPartialFunctionApplication(): bool
282: {
283: return $this->versionId >= 80600;
284: }
285:
286: public function supportsArrayUnpackingWithStringKeys(): bool
287: {
288: return $this->versionId >= 80100;
289: }
290:
291: public function throwsOnInvalidMbStringEncoding(): bool
292: {
293: return $this->versionId >= 80000;
294: }
295:
296: public function supportsPassNoneEncodings(): bool
297: {
298: return $this->versionId < 70300;
299: }
300:
301: public function producesWarningForFinalPrivateMethods(): bool
302: {
303: return $this->versionId >= 80000;
304: }
305:
306: public function deprecatesDynamicProperties(): bool
307: {
308: return $this->versionId >= 80200;
309: }
310:
311: public function strSplitReturnsEmptyArray(): bool
312: {
313: return $this->versionId >= 80200;
314: }
315:
316: public function supportsDisjunctiveNormalForm(): bool
317: {
318: return $this->versionId >= 80200;
319: }
320:
321: public function serializableRequiresMagicMethods(): bool
322: {
323: return $this->versionId >= 80100;
324: }
325:
326: public function arrayFunctionsReturnNullWithNonArray(): bool
327: {
328: return $this->versionId < 80000;
329: }
330:
331: // see https://www.php.net/manual/en/migration80.incompatible.php#migration80.incompatible.core.string-number-comparision
332: public function castsNumbersToStringsOnLooseComparison(): bool
333: {
334: return $this->versionId >= 80000;
335: }
336:
337: public function nonNumericStringAndIntegerIsFalseOnLooseComparison(): bool
338: {
339: return $this->versionId >= 80000;
340: }
341:
342: // see https://wiki.php.net/rfc/saner-numeric-strings
343: public function allowsTrailingWhitespaceInNumericStrings(): bool
344: {
345: return $this->versionId >= 80000;
346: }
347:
348: public function supportsCallableInstanceMethods(): bool
349: {
350: return $this->versionId < 80000;
351: }
352:
353: public function supportsJsonValidate(): bool
354: {
355: return $this->versionId >= 80300;
356: }
357:
358: public function supportsConstantsInTraits(): bool
359: {
360: return $this->versionId >= 80200;
361: }
362:
363: public function supportsNativeTypesInClassConstants(): bool
364: {
365: return $this->versionId >= 80300;
366: }
367:
368: public function supportsAbstractTraitMethods(): bool
369: {
370: return $this->versionId >= 80000;
371: }
372:
373: public function supportsOverrideAttribute(): bool
374: {
375: return $this->versionId >= 80300;
376: }
377:
378: public function supportsDynamicClassConstantFetch(): bool
379: {
380: return $this->versionId >= 80300;
381: }
382:
383: public function supportsReadOnlyClasses(): bool
384: {
385: return $this->versionId >= 80200;
386: }
387:
388: public function supportsReadOnlyAnonymousClasses(): bool
389: {
390: return $this->versionId >= 80300;
391: }
392:
393: public function supportsReadonlyPropertyReinitializationOnClone(): bool
394: {
395: return $this->versionId >= 80300;
396: }
397:
398: public function supportsNeverReturnTypeInArrowFunction(): bool
399: {
400: return $this->versionId >= 80200;
401: }
402:
403: public function supportsPregUnmatchedAsNull(): bool
404: {
405: // while PREG_UNMATCHED_AS_NULL is defined in php-src since 7.2.x it starts working as expected with 7.4.x
406: // https://3v4l.org/v3HE4
407: return $this->versionId >= 70400;
408: }
409:
410: public function supportsPregCaptureOnlyNamedGroups(): bool
411: {
412: // https://php.watch/versions/8.2/preg-n-no-capture-modifier
413: return $this->versionId >= 80200;
414: }
415:
416: public function supportsPropertyHooks(): bool
417: {
418: return $this->versionId >= 80400;
419: }
420:
421: public function supportsFinalProperties(): bool
422: {
423: return $this->versionId >= 80400;
424: }
425:
426: public function supportsAsymmetricVisibility(): bool
427: {
428: return $this->versionId >= 80400;
429: }
430:
431: public function supportsAsymmetricVisibilityForStaticProperties(): bool
432: {
433: return $this->versionId >= 80500;
434: }
435:
436: public function supportsLazyObjects(): bool
437: {
438: return $this->versionId >= 80400;
439: }
440:
441: public function hasDateTimeExceptions(): bool
442: {
443: return $this->versionId >= 80300;
444: }
445:
446: public function isCurloptUrlCheckingFileSchemeWithOpenBasedir(): bool
447: {
448: // Before PHP 8.0, when setting CURLOPT_URL, an unparsable URL or a file:// scheme would fail if open_basedir is used
449: // https://github.com/php/php-src/blob/php-7.4.33/ext/curl/interface.c#L139-L158
450: // https://github.com/php/php-src/blob/php-8.0.0/ext/curl/interface.c#L128-L130
451: return $this->versionId < 80000;
452: }
453:
454: /**
455: * whether curl handles are represented as 'resource' or CurlShareHandle
456: */
457: public function supportsCurlShareHandle(): bool
458: {
459: return $this->versionId >= 80000;
460: }
461:
462: public function supportsCurlSharePersistentHandle(): bool
463: {
464: return $this->versionId >= 80500;
465: }
466:
467: public function highlightStringDoesNotReturnFalse(): bool
468: {
469: return $this->versionId >= 80400;
470: }
471:
472: public function deprecatesImplicitlyNullableParameterTypes(): bool
473: {
474: return $this->versionId >= 80400;
475: }
476:
477: public function substrReturnFalseInsteadOfEmptyString(): bool
478: {
479: return $this->versionId < 80000;
480: }
481:
482: public function supportsBcMathNumberOperatorOverloading(): bool
483: {
484: return $this->versionId >= 80400;
485: }
486:
487: public function hasPDOSubclasses(): bool
488: {
489: return $this->versionId >= 80400;
490: }
491:
492: public function deprecatesImplicitlyFloatConversionToInt(): bool
493: {
494: return $this->versionId >= 80100;
495: }
496:
497: public function deprecatesNullArrayOffset(): bool
498: {
499: return $this->versionId >= 80500;
500: }
501:
502: public function supportsFinalPromotedProperties(): bool
503: {
504: return $this->versionId >= 80500;
505: }
506:
507: public function supportsVoidCast(): bool
508: {
509: return $this->versionId >= 80500;
510: }
511:
512: public function supportsNoDiscardAttribute(): bool
513: {
514: return $this->versionId >= 80500;
515: }
516:
517: public function deprecatesNonStandardCasts(): bool
518: {
519: return $this->versionId >= 80500;
520: }
521:
522: public function deprecatesBacktickOperator(): bool
523: {
524: return $this->versionId >= 80500;
525: }
526:
527: public function supportsAttributesOnGlobalConstants(): bool
528: {
529: return $this->versionId >= 80500;
530: }
531:
532: public function supportsDeprecatedTraits(): bool
533: {
534: return $this->versionId >= 80500;
535: }
536:
537: public function supportsOverrideAttributeOnProperty(): bool
538: {
539: return $this->versionId >= 80500;
540: }
541:
542: public function deprecatesDecOnNonNumericString(): bool
543: {
544: return $this->versionId >= 80300;
545: }
546:
547: public function deprecatesIncOnNonNumericString(): bool
548: {
549: return $this->versionId >= 80500;
550: }
551:
552: public function supportsObjectsInArraySumProduct(): bool
553: {
554: return $this->versionId >= 80300;
555: }
556:
557: public function hasFilterThrowOnFailureConstant(): bool
558: {
559: return $this->versionId >= 80500;
560: }
561:
562: public function throwsOnStringCast(): bool
563: {
564: return $this->versionId >= 70400;
565: }
566:
567: }
568: