1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Php;
4:
5: use PHPStan\Analyser\ConstantResolver;
6: use PHPStan\TrinaryLogic;
7: use PHPStan\Turbo\ReferencedByTurboExtension;
8: use PHPStan\Type\IntegerRangeType;
9: use PHPStan\Type\Type;
10: use function count;
11: use function is_int;
12:
13: /**
14: * Range-aware PHP version check that handles version uncertainty.
15: *
16: * Unlike PhpVersion (which represents a single known version), PhpVersions wraps
17: * a Type representing the possible PHP versions. When the exact version is known,
18: * queries return Yes/No. When a range of versions is possible, queries return Maybe.
19: *
20: * This is the return type of Scope::getPhpVersion().
21: *
22: * @api
23: */
24: #[ReferencedByTurboExtension(key: 'phpVersions')]
25: final class PhpVersions
26: {
27:
28: // PHP 8.0 compares a number with a non-numeric string as strings, PHP 8.4 adds BcMath\Number
29: private const LOOSE_COMPARISON_PHP_VERSION_BOUNDARIES = [80000, 80400];
30:
31: public function __construct(
32: private Type $phpVersions,
33: )
34: {
35: }
36:
37: public function getType(): Type
38: {
39: return $this->phpVersions;
40: }
41:
42: /**
43: * One PHP version for each range of the versions that loosely compares (`==`) the same way.
44: *
45: * @return non-empty-list<PhpVersion>
46: */
47: public function getLooseComparisonVersions(): array
48: {
49: $versionIds = $this->phpVersions->getConstantScalarValues();
50: if (count($versionIds) === 1 && is_int($versionIds[0])) {
51: return [new PhpVersion($versionIds[0])];
52: }
53:
54: $mins = [ConstantResolver::PHP_MIN_ANALYZABLE_VERSION_ID, ...self::LOOSE_COMPARISON_PHP_VERSION_BOUNDARIES];
55: $versions = [];
56: foreach ($mins as $i => $min) {
57: $segment = IntegerRangeType::fromInterval($min, isset($mins[$i + 1]) ? $mins[$i + 1] - 1 : null);
58: if ($segment->isSuperTypeOf($this->phpVersions)->no()) {
59: continue;
60: }
61:
62: $versions[] = new PhpVersion($min);
63: }
64: if ($versions === []) {
65: // no PHP version reaches this code, any of them will do
66: return [new PhpVersion($mins[0])];
67: }
68:
69: return $versions;
70: }
71:
72: public function supportsNoncapturingCatches(): TrinaryLogic
73: {
74: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
75: }
76:
77: public function producesWarningForFinalPrivateMethods(): TrinaryLogic
78: {
79: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
80: }
81:
82: public function supportsNamedArguments(): TrinaryLogic
83: {
84: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
85: }
86:
87: public function supportsNamedArgumentAfterUnpackedArgument(): TrinaryLogic
88: {
89: return IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($this->phpVersions)->result;
90: }
91:
92: public function supportsNativeTypesInClassConstants(): TrinaryLogic
93: {
94: return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
95: }
96:
97: public function supportsConstantsInTraits(): TrinaryLogic
98: {
99: return IntegerRangeType::fromInterval(80200, null)->isSuperTypeOf($this->phpVersions)->result;
100: }
101:
102: public function supportsNeverReturnTypeInArrowFunction(): TrinaryLogic
103: {
104: return IntegerRangeType::fromInterval(80200, null)->isSuperTypeOf($this->phpVersions)->result;
105: }
106:
107: public function supportsArrayUnpackingWithStringKeys(): TrinaryLogic
108: {
109: return IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($this->phpVersions)->result;
110: }
111:
112: public function supportsPropertyHooks(): TrinaryLogic
113: {
114: return IntegerRangeType::fromInterval(80400, null)->isSuperTypeOf($this->phpVersions)->result;
115: }
116:
117: public function supportsFinalProperties(): TrinaryLogic
118: {
119: return IntegerRangeType::fromInterval(80400, null)->isSuperTypeOf($this->phpVersions)->result;
120: }
121:
122: public function supportsAsymmetricVisibility(): TrinaryLogic
123: {
124: return IntegerRangeType::fromInterval(80400, null)->isSuperTypeOf($this->phpVersions)->result;
125: }
126:
127: public function supportsAsymmetricVisibilityForStaticProperties(): TrinaryLogic
128: {
129: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
130: }
131:
132: public function supportsOverrideAttributeOnProperty(): TrinaryLogic
133: {
134: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
135: }
136:
137: public function supportsOverrideAttributeOnClassConstant(): TrinaryLogic
138: {
139: return IntegerRangeType::fromInterval(80600, null)->isSuperTypeOf($this->phpVersions)->result;
140: }
141:
142: public function supportsAttributesOnGlobalConstants(): TrinaryLogic
143: {
144: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
145: }
146:
147: public function supportsUnsetCast(): TrinaryLogic
148: {
149: return IntegerRangeType::fromInterval(null, 79999)->isSuperTypeOf($this->phpVersions)->result;
150: }
151:
152: public function supportsTrueAndFalseStandaloneType(): TrinaryLogic
153: {
154: return IntegerRangeType::fromInterval(80200, null)->isSuperTypeOf($this->phpVersions)->result;
155: }
156:
157: public function throwsTypeErrorForInternalFunctions(): TrinaryLogic
158: {
159: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
160: }
161:
162: public function throwsValueErrorForInternalFunctions(): TrinaryLogic
163: {
164: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
165: }
166:
167: public function supportsMaxMemoryLimit(): TrinaryLogic
168: {
169: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
170: }
171:
172: public function supportsThrowExpression(): TrinaryLogic
173: {
174: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
175: }
176:
177: public function supportsClassConstantOnExpression(): TrinaryLogic
178: {
179: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
180: }
181:
182: public function supportsPromotedProperties(): TrinaryLogic
183: {
184: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
185: }
186:
187: public function supportsNativeUnionTypes(): TrinaryLogic
188: {
189: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
190: }
191:
192: public function supportsFinalConstants(): TrinaryLogic
193: {
194: return IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($this->phpVersions)->result;
195: }
196:
197: public function throwsOnStringCast(): TrinaryLogic
198: {
199: return IntegerRangeType::fromInterval(70400, null)->isSuperTypeOf($this->phpVersions)->result;
200: }
201:
202: public function supportsReadOnlyProperties(): TrinaryLogic
203: {
204: return IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($this->phpVersions)->result;
205: }
206:
207: public function supportsReadonlyPropertyReinitializationOnClone(): TrinaryLogic
208: {
209: return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
210: }
211:
212: public function supportsReadonlyPropertyDefaultValues(): TrinaryLogic
213: {
214: return IntegerRangeType::fromInterval(80600, null)->isSuperTypeOf($this->phpVersions)->result;
215: }
216:
217: public function supportsFirstClassCallables(): TrinaryLogic
218: {
219: return IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($this->phpVersions)->result;
220: }
221:
222: public function supportsPartialFunctionApplication(): TrinaryLogic
223: {
224: return IntegerRangeType::fromInterval(80600, null)->isSuperTypeOf($this->phpVersions)->result;
225: }
226:
227: public function supportsReadOnlyClasses(): TrinaryLogic
228: {
229: return IntegerRangeType::fromInterval(80200, null)->isSuperTypeOf($this->phpVersions)->result;
230: }
231:
232: public function supportsDynamicClassConstantFetch(): TrinaryLogic
233: {
234: return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
235: }
236:
237: public function supportsReadOnlyAnonymousClasses(): TrinaryLogic
238: {
239: return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
240: }
241:
242: public function supportsFinalPromotedProperties(): TrinaryLogic
243: {
244: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
245: }
246:
247: public function supportsVoidCast(): TrinaryLogic
248: {
249: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
250: }
251:
252: public function deprecatesNonStandardCasts(): TrinaryLogic
253: {
254: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
255: }
256:
257: public function deprecatesBacktickOperator(): TrinaryLogic
258: {
259: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
260: }
261:
262: public function deprecatesReturnInFinally(): TrinaryLogic
263: {
264: return IntegerRangeType::fromInterval(80600, null)->isSuperTypeOf($this->phpVersions)->result;
265: }
266:
267: public function deprecatesDecOnNonNumericString(): TrinaryLogic
268: {
269: return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
270: }
271:
272: public function deprecatesIncOnNonNumericString(): TrinaryLogic
273: {
274: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
275: }
276:
277: public function deprecatesImplicitlyFloatConversionToInt(): TrinaryLogic
278: {
279: return IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($this->phpVersions)->result;
280: }
281:
282: public function deprecatesNullArrayOffset(): TrinaryLogic
283: {
284: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
285: }
286:
287: public function deprecatesRequiredParameterAfterOptional(): TrinaryLogic
288: {
289: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
290: }
291:
292: public function deprecatesRequiredParameterAfterOptionalNullableAndDefaultNull(): TrinaryLogic
293: {
294: return IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($this->phpVersions)->result;
295: }
296:
297: public function deprecatesRequiredParameterAfterOptionalUnionOrMixed(): TrinaryLogic
298: {
299: return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
300: }
301:
302: public function deprecatesImplicitlyNullableParameterTypes(): TrinaryLogic
303: {
304: return IntegerRangeType::fromInterval(80400, null)->isSuperTypeOf($this->phpVersions)->result;
305: }
306:
307: public function hasTentativeReturnTypes(): TrinaryLogic
308: {
309: return IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($this->phpVersions)->result;
310: }
311:
312: public function serializableRequiresMagicMethods(): TrinaryLogic
313: {
314: return IntegerRangeType::fromInterval(80100, null)->isSuperTypeOf($this->phpVersions)->result;
315: }
316:
317: public function supportsAbstractTraitMethods(): TrinaryLogic
318: {
319: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
320: }
321:
322: public function supportsCaseInsensitiveConstantNames(): TrinaryLogic
323: {
324: return IntegerRangeType::fromInterval(null, 79999)->isSuperTypeOf($this->phpVersions)->result;
325: }
326:
327: public function supportsDeprecatedTraits(): TrinaryLogic
328: {
329: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
330: }
331:
332: public function supportsDebugInfoOnEnums(): TrinaryLogic
333: {
334: return IntegerRangeType::fromInterval(80600, null)->isSuperTypeOf($this->phpVersions)->result;
335: }
336:
337: public function arrayFunctionsReturnNullWithNonArray(): TrinaryLogic
338: {
339: return IntegerRangeType::fromInterval(null, 79999)->isSuperTypeOf($this->phpVersions)->result;
340: }
341:
342: public function hasDateTimeExceptions(): TrinaryLogic
343: {
344: return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
345: }
346:
347: public function hasFilterThrowOnFailureConstant(): TrinaryLogic
348: {
349: return IntegerRangeType::fromInterval(80500, null)->isSuperTypeOf($this->phpVersions)->result;
350: }
351:
352: public function hasPDOSubclasses(): TrinaryLogic
353: {
354: return IntegerRangeType::fromInterval(80400, null)->isSuperTypeOf($this->phpVersions)->result;
355: }
356:
357: public function hasStricterRoundFunctions(): TrinaryLogic
358: {
359: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
360: }
361:
362: public function highlightStringDoesNotReturnFalse(): TrinaryLogic
363: {
364: return IntegerRangeType::fromInterval(80400, null)->isSuperTypeOf($this->phpVersions)->result;
365: }
366:
367: public function isEmptyStringValidAliasForNoneInMbSubstituteCharacter(): TrinaryLogic
368: {
369: return IntegerRangeType::fromInterval(null, 79999)->isSuperTypeOf($this->phpVersions)->result;
370: }
371:
372: public function isNullValidArgInMbSubstituteCharacter(): TrinaryLogic
373: {
374: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
375: }
376:
377: public function isZeroValidCodePointInMbSubstituteCharacter(): TrinaryLogic
378: {
379: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
380: }
381:
382: /** On PHP 8.0+ the Reflection* classes extended by the BetterReflection enum adapters declare the correct return types. */
383: public function hasCorrectReflectionEnumAdapterReturnTypes(): TrinaryLogic
384: {
385: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
386: }
387:
388: public function isNumericStringValidArgInMbSubstituteCharacter(): TrinaryLogic
389: {
390: return IntegerRangeType::fromInterval(null, 79999)->isSuperTypeOf($this->phpVersions)->result;
391: }
392:
393: public function strSplitReturnsEmptyArray(): TrinaryLogic
394: {
395: return IntegerRangeType::fromInterval(80200, null)->isSuperTypeOf($this->phpVersions)->result;
396: }
397:
398: public function substrReturnFalseInsteadOfEmptyString(): TrinaryLogic
399: {
400: return IntegerRangeType::fromInterval(null, 79999)->isSuperTypeOf($this->phpVersions)->result;
401: }
402:
403: public function supportsAllUnicodeScalarCodePointsInMbSubstituteCharacter(): TrinaryLogic
404: {
405: return IntegerRangeType::fromInterval(70200, null)->isSuperTypeOf($this->phpVersions)->result;
406: }
407:
408: public function supportsHhPrintfSpecifier(): TrinaryLogic
409: {
410: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
411: }
412:
413: public function supportsPassNoneEncodings(): TrinaryLogic
414: {
415: return IntegerRangeType::fromInterval(null, 70299)->isSuperTypeOf($this->phpVersions)->result;
416: }
417:
418: public function throwsOnInvalidMbStringEncoding(): TrinaryLogic
419: {
420: return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
421: }
422:
423: public function throwsValueErrorForInvalidRoundingMode(): TrinaryLogic
424: {
425: return IntegerRangeType::fromInterval(80400, null)->isSuperTypeOf($this->phpVersions)->result;
426: }
427:
428: /**
429: * PHP 8.3 rejects a negative step on an increasing range and a NAN step, builds a character
430: * range from two single bytes and no longer turns an integral float step into floats.
431: */
432: public function hasStricterRangeFunction(): TrinaryLogic
433: {
434: return IntegerRangeType::fromInterval(80300, null)->isSuperTypeOf($this->phpVersions)->result;
435: }
436:
437: }
438: