1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PhpParser\Node\ComplexType;
6: use PhpParser\Node\Identifier;
7: use PhpParser\Node\Name;
8: use PHPStan\Php\PhpVersion;
9: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
10: use PHPStan\Reflection\Callables\CallableParametersAcceptor;
11: use PHPStan\Reflection\ClassConstantReflection;
12: use PHPStan\Reflection\ClassMemberAccessAnswerer;
13: use PHPStan\Reflection\ClassReflection;
14: use PHPStan\Reflection\ExtendedMethodReflection;
15: use PHPStan\Reflection\ExtendedPropertyReflection;
16: use PHPStan\Reflection\ReflectionProvider;
17: use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection;
18: use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection;
19: use PHPStan\TrinaryLogic;
20: use PHPStan\Turbo\ReferencedByTurboExtension;
21: use PHPStan\Type\Constant\ConstantArrayType;
22: use PHPStan\Type\Constant\ConstantStringType;
23: use PHPStan\Type\Enum\EnumCaseObjectType;
24: use PHPStan\Type\Generic\TemplateTypeMap;
25: use PHPStan\Type\Generic\TemplateTypeReference;
26: use PHPStan\Type\Generic\TemplateTypeVariance;
27:
28: /**
29: * Represents a PHPStan type in the type system.
30: *
31: * This is the central interface of PHPStan's type system. Every type that PHPStan
32: * can reason about implements this interface — from simple scalars like StringType
33: * to complex generics like GenericObjectType.
34: *
35: * Each Type knows what it accepts, what is a supertype of it, what properties/methods/constants
36: * it has, what operations it supports, and how to describe itself for error messages.
37: *
38: * Important: Never use `instanceof` to check types. For example, `$type instanceof StringType`
39: * will miss union types, intersection types with accessory types, and other composite forms.
40: * Always use the `is*()` methods or `isSuperTypeOf()` instead:
41: *
42: * ```php
43: * // Wrong:
44: * if ($type instanceof StringType) { ... }
45: *
46: * // Correct:
47: * if ($type->isString()->yes()) { ... }
48: * ```
49: *
50: * @api
51: * @api-do-not-implement
52: * @see https://phpstan.org/developing-extensions/type-system
53: */
54: #[ReferencedByTurboExtension(key: 'type')]
55: interface Type
56: {
57:
58: /**
59: * Returns all class names referenced anywhere in this type, recursively
60: * (including generic arguments, callable signatures, etc.).
61: *
62: * @see Type::getObjectClassNames() for only direct object type class names
63: *
64: * @return list<non-empty-string>
65: */
66: public function getReferencedClasses(): array;
67:
68: /**
69: * Returns class names of the object types this type directly represents.
70: * Unlike getReferencedClasses(), excludes classes in generic arguments, etc.
71: *
72: * @return list<non-empty-string>
73: */
74: public function getObjectClassNames(): array;
75:
76: /** @return list<ClassReflection> */
77: public function getObjectClassReflections(): array;
78:
79: /**
80: * Return class-string<Foo> for object type Foo.
81: */
82: public function getClassStringType(): Type;
83:
84: /**
85: * Returns the object type for a class-string or literal class name string.
86: * For non-class-string types, returns ErrorType.
87: */
88: public function getClassStringObjectType(): Type;
89:
90: /**
91: * Like getClassStringObjectType(), but also returns object types as-is.
92: * Used for `$classOrObject::method()` where the left side can be either.
93: */
94: public function getObjectTypeOrClassStringObjectType(): Type;
95:
96: public function isObject(): TrinaryLogic;
97:
98: public function isEnum(): TrinaryLogic;
99:
100: /** @return list<ArrayType|ConstantArrayType> */
101: public function getArrays(): array;
102:
103: /**
104: * Only ConstantArrayType instances (array shapes with known keys).
105: *
106: * @return list<ConstantArrayType>
107: */
108: public function getConstantArrays(): array;
109:
110: /** @return list<ConstantStringType> */
111: public function getConstantStrings(): array;
112:
113: /**
114: * Unlike isSuperTypeOf(), accepts() takes into account PHP's implicit type coercion.
115: * With $strictTypes = false, int is accepted by float, and Stringable objects are
116: * accepted by string.
117: */
118: public function accepts(Type $type, bool $strictTypes): AcceptsResult;
119:
120: /**
121: * "Does every value of $type belong to $this type?"
122: *
123: * Preferable to instanceof checks because it correctly handles
124: * union types, intersection types, and all other composite types.
125: */
126: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult;
127:
128: public function equals(Type $type): bool;
129:
130: public function describe(VerbosityLevel $level): string;
131:
132: public function canAccessProperties(): TrinaryLogic;
133:
134: /** @deprecated Use hasInstanceProperty or hasStaticProperty instead */
135: public function hasProperty(string $propertyName): TrinaryLogic;
136:
137: /** @deprecated Use getInstanceProperty or getStaticProperty instead */
138: public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection;
139:
140: /** @deprecated Use getUnresolvedInstancePropertyPrototype or getUnresolvedStaticPropertyPrototype instead */
141: public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection;
142:
143: public function hasInstanceProperty(string $propertyName): TrinaryLogic;
144:
145: public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection;
146:
147: /**
148: * Unlike getInstanceProperty(), this defers template type resolution.
149: * Use getInstanceProperty() in most rule implementations.
150: */
151: public function getUnresolvedInstancePropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection;
152:
153: public function hasStaticProperty(string $propertyName): TrinaryLogic;
154:
155: public function getStaticProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection;
156:
157: public function getUnresolvedStaticPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection;
158:
159: public function canCallMethods(): TrinaryLogic;
160:
161: public function hasMethod(string $methodName): TrinaryLogic;
162:
163: public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection;
164:
165: /**
166: * Unlike getMethod(), this defers template type and static type resolution.
167: * Use getMethod() in most rule implementations.
168: */
169: public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection;
170:
171: public function canAccessConstants(): TrinaryLogic;
172:
173: public function hasConstant(string $constantName): TrinaryLogic;
174:
175: public function getConstant(string $constantName): ClassConstantReflection;
176:
177: public function isIterable(): TrinaryLogic;
178:
179: public function isIterableAtLeastOnce(): TrinaryLogic;
180:
181: /**
182: * Returns the count of elements as a Type (typically IntegerRangeType).
183: */
184: public function getArraySize(): Type;
185:
186: /**
187: * Works for both arrays and Traversable objects.
188: */
189: public function getIterableKeyType(): Type;
190:
191: /** @deprecated use getIterableKeyType */
192: public function getFirstIterableKeyType(): Type;
193:
194: /** @deprecated use getIterableKeyType */
195: public function getLastIterableKeyType(): Type;
196:
197: public function getIterableValueType(): Type;
198:
199: /** @deprecated use getIterableValueType */
200: public function getFirstIterableValueType(): Type;
201:
202: /** @deprecated use getIterableValueType */
203: public function getLastIterableValueType(): Type;
204:
205: public function isArray(): TrinaryLogic;
206:
207: public function isConstantArray(): TrinaryLogic;
208:
209: /**
210: * An oversized array is a constant array shape that grew too large to track
211: * precisely and was degraded to a generic array type.
212: */
213: public function isOversizedArray(): TrinaryLogic;
214:
215: /**
216: * A list is an array with sequential integer keys starting from 0 with no gaps.
217: */
218: public function isList(): TrinaryLogic;
219:
220: public function isOffsetAccessible(): TrinaryLogic;
221:
222: /**
223: * Whether accessing a non-existent offset is safe (won't cause errors).
224: * Unlike isOffsetAccessible() which checks if offset access is supported at all.
225: */
226: public function isOffsetAccessLegal(): TrinaryLogic;
227:
228: public function hasOffsetValueType(Type $offsetType): TrinaryLogic;
229:
230: public function getOffsetValueType(Type $offsetType): Type;
231:
232: /**
233: * May add a new key. When $offsetType is null, appends (like $a[] = $value).
234: *
235: * @see Type::setExistingOffsetValueType() for modifying an existing key without widening
236: */
237: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type;
238:
239: /**
240: * Unlike setOffsetValueType(), assumes the key already exists.
241: * Preserves the array shape and list type.
242: */
243: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type;
244:
245: public function unsetOffset(Type $offsetType): Type;
246:
247: /** Models array_keys($array, $searchValue, $strict). */
248: public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type;
249:
250: /** Models array_keys($array). */
251: public function getKeysArray(): Type;
252:
253: /** Models array_values($array). */
254: public function getValuesArray(): Type;
255:
256: /** Models array_chunk($array, $length, $preserveKeys). */
257: public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type;
258:
259: /** Models array_fill_keys($keys, $value). */
260: public function fillKeysArray(Type $valueType): Type;
261:
262: /** Models array_flip($array). */
263: public function flipArray(): Type;
264:
265: /** Models array_intersect_key($array, ...$otherArrays). */
266: public function intersectKeyArray(Type $otherArraysType): Type;
267:
268: /** Models array_pop() effect on the array. */
269: public function popArray(): Type;
270:
271: /** Models array_reverse($array, $preserveKeys). */
272: public function reverseArray(TrinaryLogic $preserveKeys): Type;
273:
274: /** Models array_search($needle, $array, $strict). */
275: public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type;
276:
277: /** Models array_shift() effect on the array. */
278: public function shiftArray(): Type;
279:
280: /**
281: * Models shuffle() effect on the array. Result is always a list.
282: *
283: * It's also used to model array after `sort` / `rsort` / `usort` calls.
284: */
285: public function shuffleArray(): Type;
286:
287: /** Models array_slice($array, $offset, $length, $preserveKeys). */
288: public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type;
289:
290: /** Models array_splice() effect on the array (the modified array, not the removed portion). */
291: public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type;
292:
293: /**
294: * Narrows a list-shaped array type to "size lies in $sizeType" — the
295: * type-system equivalent of `count($list) === N` / `count($list) >= N`
296: * / `count($list) in [N, M]`. Used by `TypeSpecifier` when specifying
297: * types for `count()` comparisons; the call site is responsible for
298: * gating on outer list-ness, so each implementation may assume it's
299: * narrowing a list shape.
300: *
301: * `$sizeType` is expected to be a `ConstantIntegerType` (exact size) or
302: * an `IntegerRangeType` (a min/max bound). Concrete implementations
303: * (`ConstantArrayType`, `ArrayType`) rebuild the array with a required
304: * prefix `[0, min)` and an optional middle `[min, max)` (when `max` is
305: * set), or extend with `HasOffsetValueType` accessories when the upper
306: * bound is unbounded. Non-array types return `ErrorType`.
307: */
308: public function truncateListToSize(Type $sizeType): Type;
309:
310: /**
311: * Downgrades the list-ness of the array from `Yes` to `Maybe` (e.g. for
312: * `asort`/`uksort`/etc. which preserve keys but break list ordering).
313: * Other shape information (keys, values, accessories like NonEmpty) is
314: * preserved.
315: */
316: public function makeListMaybe(): Type;
317:
318: /**
319: * Models "same keys, every value transformed" (e.g. `array_walk`,
320: * `array_map($cb, $a)`, `preg_replace*` over an array subject). Keys
321: * and accessories like list-ness / non-emptiness are preserved.
322: *
323: * @param callable(Type): Type $cb
324: */
325: public function mapValueType(callable $cb): Type;
326:
327: /**
328: * Replaces the iterable key type via `$cb($currentKeyType)`. For
329: * `ArrayType` rewrites the key type wholesale; for `ConstantArrayType`
330: * the explicit keys (which are already precise constants) are preserved
331: * — pass-through, matching the prior `TypeTraverser`-based callers.
332: * Used to widen / narrow the key type after a foreach narrowed `$key`
333: * via `is_int($key)` / `is_string($key)` checks.
334: *
335: * @param callable(Type): Type $cb
336: */
337: public function mapKeyType(callable $cb): Type;
338:
339: /**
340: * Marks every explicit key in a `ConstantArrayType` as optional (the
341: * shape can have any subset of the original keys). For non-`CAT` arrays
342: * this is a no-op — they already model arbitrary subsets. Used by
343: * `preg_replace*` over array subjects, where the callback can drop
344: * entries.
345: */
346: public function makeAllArrayKeysOptional(): Type;
347:
348: /**
349: * Models `array_change_key_case($a, $case)`. String keys are case-folded
350: * (constant ones to a specific value, general ones via accessories);
351: * non-string keys, values, accessories and list-ness are preserved.
352: * `$case` matches PHP's `CASE_LOWER` / `CASE_UPPER`; `null` means the
353: * case is non-constant and the result is the union of both folds.
354: */
355: public function changeKeyCaseArray(?int $case): Type;
356:
357: /**
358: * Models `array_filter($a)` (no callback): drops entries whose value is
359: * definitely falsey, marks possibly-falsey entries optional, keeps
360: * definitely-truthy entries unchanged. Keys are preserved; list-ness
361: * is downgraded since gaps may appear.
362: */
363: public function filterArrayRemovingFalsey(): Type;
364:
365: /** @return list<EnumCaseObjectType> */
366: public function getEnumCases(): array;
367:
368: /**
369: * Returns the single enum case this type represents, or null if not exactly one case.
370: */
371: public function getEnumCaseObject(): ?EnumCaseObjectType;
372:
373: /**
374: * Returns a list of finite values this type can take.
375: *
376: * Examples:
377: *
378: * - for bool: [true, false]
379: * - for int<0, 3>: [0, 1, 2, 3]
380: * - for enums: list of enum cases
381: * - for scalars: the scalar itself
382: *
383: * For infinite types it returns an empty array.
384: *
385: * @return list<Type>
386: */
387: public function getFiniteTypes(): array;
388:
389: /**
390: * Models the ** operator.
391: *
392: * @deprecated Use arithmeticPower() instead.
393: */
394: public function exponentiate(Type $exponent): Type;
395:
396: /**
397: * Models the + operator on numbers. The operators of these methods return ErrorType
398: * for an invalid operation, they do not call OperatorTypeSpecifyingExtensions.
399: */
400: public function arithmeticAdd(Type $right, PhpVersion $phpVersion): Type;
401:
402: /** Models the - operator. */
403: public function arithmeticSubtract(Type $right, PhpVersion $phpVersion): Type;
404:
405: /** Models the * operator. */
406: public function arithmeticMultiply(Type $right, PhpVersion $phpVersion): Type;
407:
408: /** Models the / operator. */
409: public function arithmeticDivide(Type $right, PhpVersion $phpVersion): Type;
410:
411: /** Models intdiv(). */
412: public function arithmeticIntDivide(Type $right, PhpVersion $phpVersion): Type;
413:
414: /** Models the % operator. */
415: public function arithmeticModulo(Type $right, PhpVersion $phpVersion): Type;
416:
417: /** Models the ** operator. */
418: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type;
419:
420: /** Models the unary - operator. */
421: public function arithmeticNegate(PhpVersion $phpVersion): Type;
422:
423: /** Models the & operator. */
424: public function bitwiseAnd(Type $right, PhpVersion $phpVersion): Type;
425:
426: /** Models the | operator. */
427: public function bitwiseOr(Type $right, PhpVersion $phpVersion): Type;
428:
429: /** Models the ^ operator. */
430: public function bitwiseXor(Type $right, PhpVersion $phpVersion): Type;
431:
432: /** Models the << operator. */
433: public function bitwiseShiftLeft(Type $right, PhpVersion $phpVersion): Type;
434:
435: /** Models the >> operator. */
436: public function bitwiseShiftRight(Type $right, PhpVersion $phpVersion): Type;
437:
438: /** Models the . operator. */
439: public function stringConcatenate(Type $right, PhpVersion $phpVersion): Type;
440:
441: /** Models the + operator on arrays. */
442: public function arrayUnion(Type $right, PhpVersion $phpVersion): Type;
443:
444: public function isCallable(): TrinaryLogic;
445:
446: /** @return list<CallableParametersAcceptor> */
447: public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array;
448:
449: public function isCloneable(): TrinaryLogic;
450:
451: /** Models the (bool) cast. */
452: public function toBoolean(): BooleanType;
453:
454: /** Models numeric coercion for arithmetic operators. */
455: public function toNumber(): Type;
456:
457: /**
458: * The intervals of the numbers this type holds, or null when it can hold
459: * anything but an int or a float. Unlike toNumber() it converts nothing:
460: * call it on toNumber() for the numbers an operand stands for.
461: */
462: public function toNumberSet(): ?NumberSet;
463:
464: /**
465: * The values this type holds, by the kinds of values PHP's operators tell apart.
466: * A union of types is the union of their sets, an intersection their intersection.
467: */
468: public function toValueSet(): ValueSet;
469:
470: /** Models the bitwise-not (`~$x`) operator. Returns `ErrorType` for types where `~` is undefined. */
471: public function toBitwiseNotType(): Type;
472:
473: /**
474: * Models `get_class($x)`'s return type per leaf: definite objects yield
475: * their `class-string` projection, definite non-objects yield `false`,
476: * and possibly-objects yield the union of both.
477: */
478: public function toGetClassResultType(): Type;
479:
480: /**
481: * Models the type of `$x::class`. For known final classes the literal
482: * class name is returned; for everything else an
483: * `IntersectionType[ClassString<X>, AccessoryLiteralStringType]`.
484: * `NullType` passes through (mirrors PHP's nullsafe `::class` semantics).
485: * `ReflectionProvider` is needed for the final-class lookup.
486: */
487: public function toClassConstantType(ReflectionProvider $reflectionProvider): Type;
488:
489: /**
490: * Projects a class-name-or-object `Type` (the right-hand side of
491: * `$x instanceof <expr>`) to the `ObjectType` it should be compared
492: * against. Constant class strings collapse to their `ObjectType`
493: * exactly; everything kept symbolically (object class names,
494: * `class-string<X>`) carries an uncertainty flag so the caller can
495: * fall back to `BooleanType` instead of a definite yes/no.
496: */
497: public function toObjectTypeForInstanceofCheck(): ClassNameToObjectTypeResult;
498:
499: /**
500: * Projects a class-name-or-object `Type` (the second argument of
501: * `is_a($x, $class, $allow_string)`) to the `ObjectType` to narrow
502: * `$x` against. When `$allowString` is true, the `is_a()` result also
503: * keeps the original class-string accepted alongside the object.
504: * `$allowSameClass` controls whether matching the input's own class
505: * collapses to `NeverType` for final classes (the call site's
506: * "always-true" suppression).
507: */
508: public function toObjectTypeForIsACheck(Type $objectOrClassType, bool $allowString, bool $allowSameClass): ClassNameToObjectTypeResult;
509:
510: /** Models the (int) cast. */
511: public function toInteger(): Type;
512:
513: /** Models the (float) cast. */
514: public function toFloat(): Type;
515:
516: /** Models the (string) cast. */
517: public function toString(): Type;
518:
519: /** Models the (array) cast. */
520: public function toArray(): Type;
521:
522: /**
523: * Models PHP's implicit array key coercion: floats truncated to int,
524: * booleans become 0/1, null becomes '', numeric strings become int.
525: */
526: public function toArrayKey(): Type;
527:
528: /**
529: * Returns how this type might change when passed to a typed parameter
530: * or assigned to a typed property.
531: *
532: * With $strictTypes = true: int widens to int|float (since int is accepted
533: * by float parameters in strict mode).
534: * With $strictTypes = false: additional coercions apply, e.g. Stringable
535: * objects are accepted by string parameters.
536: *
537: * Used internally to determine what types a value might be coerced to
538: * when checking parameter acceptance.
539: */
540: public function toCoercedArgumentType(bool $strictTypes): self;
541:
542: public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic;
543:
544: public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic;
545:
546: /**
547: * Is Type of a known constant value? Includes literal strings, integers, floats, true, false, null, and array shapes.
548: *
549: * Unlike isConstantScalarValue(), this also returns yes for constant array types (array shapes
550: * with known keys and values). Use this when you need to detect any constant value including arrays.
551: */
552: public function isConstantValue(): TrinaryLogic;
553:
554: /**
555: * Is Type of a known constant scalar value? Includes literal strings, integers, floats, true, false, and null.
556: *
557: * Unlike isConstantValue(), this does NOT return yes for array shapes.
558: * Use this when you specifically need scalar constants only.
559: */
560: public function isConstantScalarValue(): TrinaryLogic;
561:
562: /** @return list<ConstantScalarType> */
563: public function getConstantScalarTypes(): array;
564:
565: /** @return list<int|float|string|bool|null> */
566: public function getConstantScalarValues(): array;
567:
568: public function isNull(): TrinaryLogic;
569:
570: public function isTrue(): TrinaryLogic;
571:
572: public function isFalse(): TrinaryLogic;
573:
574: public function isBoolean(): TrinaryLogic;
575:
576: public function isFloat(): TrinaryLogic;
577:
578: public function isInteger(): TrinaryLogic;
579:
580: public function isString(): TrinaryLogic;
581:
582: public function isNumericString(): TrinaryLogic;
583:
584: /**
585: * When isDecimalIntegerString() returns yes(), the type
586: * is guaranteed to be cast to an integer in an array key.
587: * Examples of constant values covered by this type: "0", "1", "1234", "-1"
588: *
589: * When isDecimalIntegerString() returns no(), the type represents strings containing non-decimal integers and other text.
590: * These are guaranteed to stay as string in an array key.
591: * Examples of constant values covered by this type: "+1", "00", "18E+3", "1.2", "1,3", "foo"
592: */
593: public function isDecimalIntegerString(): TrinaryLogic;
594:
595: public function isNonEmptyString(): TrinaryLogic;
596:
597: /**
598: * Non-falsy string is a non-empty string that is also not '0'.
599: * Stricter subset of non-empty-string.
600: */
601: public function isNonFalsyString(): TrinaryLogic;
602:
603: /**
604: * A literal-string is a string composed entirely from string literals
605: * in the source code (not from user input). Used for SQL injection prevention.
606: */
607: public function isLiteralString(): TrinaryLogic;
608:
609: public function isLowercaseString(): TrinaryLogic;
610:
611: public function isUppercaseString(): TrinaryLogic;
612:
613: public function isClassString(): TrinaryLogic;
614:
615: public function isVoid(): TrinaryLogic;
616:
617: public function isScalar(): TrinaryLogic;
618:
619: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType;
620:
621: /**
622: * The values this type can be loosely equal to: `$this == $x` is false for any $x outside of it.
623: * The true branch of a `==` narrows the other side by intersecting with it.
624: */
625: public function getPossiblyLooselyEqualType(PhpVersion $phpVersion): Type;
626:
627: /**
628: * The values this type is always loosely equal to: `$this == $x` is true for any $x of it.
629: * The false branch of a `==` narrows the other side by removing it.
630: */
631: public function getAlwaysLooselyEqualType(PhpVersion $phpVersion): Type;
632:
633: /**
634: * Type narrowing methods for comparison operators.
635: * For example, for ConstantIntegerType(5), getSmallerType() returns int<min, 4>.
636: */
637: public function getSmallerType(PhpVersion $phpVersion): Type;
638:
639: public function getSmallerOrEqualType(PhpVersion $phpVersion): Type;
640:
641: public function getGreaterType(PhpVersion $phpVersion): Type;
642:
643: public function getGreaterOrEqualType(PhpVersion $phpVersion): Type;
644:
645: /**
646: * Returns actual template type for a given object.
647: *
648: * For example, with a class `Foo` that declares `@template T`:
649: *
650: * ```php
651: * // $fooType is Foo<int>
652: * $t = $fooType->getTemplateType(Foo::class, 'T');
653: * $t->isInteger(); // yes
654: * ```
655: *
656: * Returns ErrorType in case of a missing type.
657: *
658: * @param class-string $ancestorClassName
659: */
660: public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type;
661:
662: /**
663: * Infers the real types of TemplateTypes found in $this, based on
664: * the received Type. E.g. if $this is array<T> and $receivedType
665: * is array<int>, infers T = int.
666: */
667: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap;
668:
669: /**
670: * Returns the template types referenced by this Type, recursively.
671: *
672: * The return value is a list of TemplateTypeReferences, who contain the
673: * referenced template type as well as the variance position in which it was
674: * found.
675: *
676: * For example, calling this on array<Foo<T>,Bar> (with T a template type)
677: * will return one TemplateTypeReference for the type T.
678: *
679: * @param TemplateTypeVariance $positionVariance The variance position in
680: * which the receiver type was
681: * found.
682: *
683: * @return list<TemplateTypeReference>
684: */
685: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array;
686:
687: /** Models abs(). */
688: public function toAbsoluteNumber(): Type;
689:
690: /**
691: * Returns a new instance with all inner types mapped through $cb.
692: * Returns the same instance if inner types did not change.
693: *
694: * Not used directly — use TypeTraverser::map() instead.
695: *
696: * @param callable(Type):Type $cb
697: */
698: public function traverse(callable $cb): Type;
699:
700: /**
701: * Like traverse(), but walks two types simultaneously.
702: *
703: * Not used directly — use SimultaneousTypeTraverser::map() instead.
704: *
705: * @param callable(Type $left, Type $right): Type $cb
706: */
707: public function traverseSimultaneously(Type $right, callable $cb): Type;
708:
709: public function toPhpDocNode(): TypeNode;
710:
711: /**
712: * The native PHP type declaration for this type at the given position,
713: * widened to what the position allows, e.g. non-empty-string to string.
714: * Returns null when the type cannot be declared natively there.
715: *
716: * @return Identifier|Name|ComplexType|null
717: */
718: public function toNativePhpTypeNode(NativeTypePosition $position);
719:
720: /** @see TypeCombinator::remove() */
721: public function tryRemove(Type $typeToRemove): ?Type;
722:
723: /**
724: * Removes constant value information. E.g. 'foo' -> string, 1 -> int.
725: * Used when types become too complex to track precisely (e.g. loop iterations).
726: */
727: public function generalize(GeneralizePrecision $precision): Type;
728:
729: /**
730: * Performance optimization to skip template resolution when no templates are present.
731: */
732: public function hasTemplateOrLateResolvableType(): bool;
733:
734: }
735: