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: /** The type of the first key; the union of the keys when their order is not known. */
192: public function getFirstIterableKeyType(): Type;
193:
194: /** The type of the last key; the union of the keys when their order is not known. */
195: public function getLastIterableKeyType(): Type;
196:
197: public function getIterableValueType(): Type;
198:
199: /** The type of the first value; the union of the values when the order of the keys is not known. */
200: public function getFirstIterableValueType(): Type;
201:
202: /** The type of the last value; the union of the values when the order of the keys is not known. */
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: * @param bool $unionValues Ignored, the written value is always unioned with the existing values. Will be removed in 3.0.
238: */
239: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type;
240:
241: /**
242: * Unlike setOffsetValueType(), assumes the key already exists.
243: * Preserves the array shape and list type.
244: */
245: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type;
246:
247: public function unsetOffset(Type $offsetType): Type;
248:
249: /** Models array_keys($array, $searchValue, $strict). */
250: public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type;
251:
252: /** Models array_keys($array). */
253: public function getKeysArray(): Type;
254:
255: /** Models array_values($array). */
256: public function getValuesArray(): Type;
257:
258: /** Models array_chunk($array, $length, $preserveKeys). */
259: public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type;
260:
261: /** Models array_fill_keys($keys, $value). */
262: public function fillKeysArray(Type $valueType): Type;
263:
264: /** Models array_flip($array). */
265: public function flipArray(): Type;
266:
267: /** Models array_intersect_key($array, ...$otherArrays). */
268: public function intersectKeyArray(Type $otherArraysType): Type;
269:
270: /** Models array_pop() effect on the array. */
271: public function popArray(): Type;
272:
273: /** Models array_reverse($array, $preserveKeys). */
274: public function reverseArray(TrinaryLogic $preserveKeys): Type;
275:
276: /** Models array_search($needle, $array, $strict). */
277: public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type;
278:
279: /** Models array_shift() effect on the array. */
280: public function shiftArray(): Type;
281:
282: /**
283: * Models shuffle() effect on the array. Result is always a list.
284: *
285: * It's also used to model array after `sort` / `rsort` / `usort` calls.
286: */
287: public function shuffleArray(): Type;
288:
289: /** Models array_slice($array, $offset, $length, $preserveKeys). */
290: public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type;
291:
292: /** Models array_splice() effect on the array (the modified array, not the removed portion). */
293: public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type;
294:
295: /**
296: * Narrows a list-shaped array type to "size lies in $sizeType" — the
297: * type-system equivalent of `count($list) === N` / `count($list) >= N`
298: * / `count($list) in [N, M]`. Used by `TypeSpecifier` when specifying
299: * types for `count()` comparisons; the call site is responsible for
300: * gating on outer list-ness, so each implementation may assume it's
301: * narrowing a list shape.
302: *
303: * `$sizeType` is expected to be a `ConstantIntegerType` (exact size) or
304: * an `IntegerRangeType` (a min/max bound). Concrete implementations
305: * (`ConstantArrayType`, `ArrayType`) rebuild the array with a required
306: * prefix `[0, min)` and an optional middle `[min, max)` (when `max` is
307: * set), or extend with `HasOffsetValueType` accessories when the upper
308: * bound is unbounded. Non-array types return `ErrorType`.
309: */
310: public function truncateListToSize(Type $sizeType): Type;
311:
312: /**
313: * Downgrades the list-ness of the array from `Yes` to `Maybe` (e.g. for
314: * `asort`/`uksort`/etc. which preserve keys but break list ordering).
315: * Other shape information (keys, values, accessories like NonEmpty) is
316: * preserved.
317: */
318: public function makeListMaybe(): Type;
319:
320: /**
321: * Models "same keys, every value transformed" (e.g. `array_walk`,
322: * `array_map($cb, $a)`, `preg_replace*` over an array subject). Keys
323: * and accessories like list-ness / non-emptiness are preserved.
324: *
325: * @param callable(Type): Type $cb
326: */
327: public function mapValueType(callable $cb): Type;
328:
329: /**
330: * Replaces the iterable key type via `$cb($currentKeyType)`. For
331: * `ArrayType` rewrites the key type wholesale; for `ConstantArrayType`
332: * the explicit keys (which are already precise constants) are preserved
333: * — pass-through, matching the prior `TypeTraverser`-based callers.
334: * Used to widen / narrow the key type after a foreach narrowed `$key`
335: * via `is_int($key)` / `is_string($key)` checks.
336: *
337: * @param callable(Type): Type $cb
338: */
339: public function mapKeyType(callable $cb): Type;
340:
341: /**
342: * Marks every explicit key in a `ConstantArrayType` as optional (the
343: * shape can have any subset of the original keys). For non-`CAT` arrays
344: * this is a no-op — they already model arbitrary subsets. Used by
345: * `preg_replace*` over array subjects, where the callback can drop
346: * entries.
347: */
348: public function makeAllArrayKeysOptional(): Type;
349:
350: /**
351: * Models `array_change_key_case($a, $case)`. String keys are case-folded
352: * (constant ones to a specific value, general ones via accessories);
353: * non-string keys, values, accessories and list-ness are preserved.
354: * `$case` matches PHP's `CASE_LOWER` / `CASE_UPPER`; `null` means the
355: * case is non-constant and the result is the union of both folds.
356: */
357: public function changeKeyCaseArray(?int $case): Type;
358:
359: /**
360: * Models `array_filter($a)` (no callback): drops entries whose value is
361: * definitely falsey, marks possibly-falsey entries optional, keeps
362: * definitely-truthy entries unchanged. Keys are preserved; list-ness
363: * is downgraded since gaps may appear.
364: */
365: public function filterArrayRemovingFalsey(): Type;
366:
367: /** @return list<EnumCaseObjectType> */
368: public function getEnumCases(): array;
369:
370: /**
371: * Returns the single enum case this type represents, or null if not exactly one case.
372: */
373: public function getEnumCaseObject(): ?EnumCaseObjectType;
374:
375: /**
376: * Returns a list of finite values this type can take.
377: *
378: * Examples:
379: *
380: * - for bool: [true, false]
381: * - for int<0, 3>: [0, 1, 2, 3]
382: * - for enums: list of enum cases
383: * - for scalars: the scalar itself
384: *
385: * For infinite types it returns an empty array.
386: *
387: * @return list<Type>
388: */
389: public function getFiniteTypes(): array;
390:
391: /**
392: * Models the ** operator.
393: *
394: * @deprecated Use arithmeticPower() instead.
395: */
396: public function exponentiate(Type $exponent): Type;
397:
398: /**
399: * Models the + operator on numbers. The operators of these methods return ErrorType
400: * for an invalid operation, they do not call OperatorTypeSpecifyingExtensions.
401: */
402: public function arithmeticAdd(Type $right, PhpVersion $phpVersion): Type;
403:
404: /** Models the - operator. */
405: public function arithmeticSubtract(Type $right, PhpVersion $phpVersion): Type;
406:
407: /** Models the * operator. */
408: public function arithmeticMultiply(Type $right, PhpVersion $phpVersion): Type;
409:
410: /** Models the / operator. */
411: public function arithmeticDivide(Type $right, PhpVersion $phpVersion): Type;
412:
413: /** Models intdiv(). */
414: public function arithmeticIntDivide(Type $right, PhpVersion $phpVersion): Type;
415:
416: /** Models the % operator. */
417: public function arithmeticModulo(Type $right, PhpVersion $phpVersion): Type;
418:
419: /** Models the ** operator. */
420: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type;
421:
422: /** Models the unary - operator. */
423: public function arithmeticNegate(PhpVersion $phpVersion): Type;
424:
425: /** Models the & operator. */
426: public function bitwiseAnd(Type $right, PhpVersion $phpVersion): Type;
427:
428: /** Models the | operator. */
429: public function bitwiseOr(Type $right, PhpVersion $phpVersion): Type;
430:
431: /** Models the ^ operator. */
432: public function bitwiseXor(Type $right, PhpVersion $phpVersion): Type;
433:
434: /** Models the << operator. */
435: public function bitwiseShiftLeft(Type $right, PhpVersion $phpVersion): Type;
436:
437: /** Models the >> operator. */
438: public function bitwiseShiftRight(Type $right, PhpVersion $phpVersion): Type;
439:
440: /** Models the . operator. */
441: public function stringConcatenate(Type $right, PhpVersion $phpVersion): Type;
442:
443: /** Models the + operator on arrays. */
444: public function arrayUnion(Type $right, PhpVersion $phpVersion): Type;
445:
446: public function isCallable(): TrinaryLogic;
447:
448: /** @return list<CallableParametersAcceptor> */
449: public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array;
450:
451: public function isCloneable(): TrinaryLogic;
452:
453: /** Models the (bool) cast. */
454: public function toBoolean(): BooleanType;
455:
456: /** Models numeric coercion for arithmetic operators. */
457: public function toNumber(): Type;
458:
459: /**
460: * The intervals of the numbers this type holds, or null when it can hold
461: * anything but an int or a float. Unlike toNumber() it converts nothing:
462: * call it on toNumber() for the numbers an operand stands for.
463: */
464: public function toNumberSet(): ?NumberSet;
465:
466: /**
467: * The numbers this type holds when it is exactly the union of them: int,
468: * float and their ranges and constants. Null for any other type, a template
469: * of a number type included, which stands for a type and not its values.
470: */
471: public function toExactNumberSet(): ?NumberSet;
472:
473: /**
474: * The values this type holds, by the kinds of values PHP's operators tell apart.
475: * A union of types is the union of their sets, an intersection their intersection.
476: */
477: public function toValueSet(): ValueSet;
478:
479: /**
480: * The kinds of values this type may hold, as TypeKind bits: a bit can be
481: * set for a kind the type holds no value of, never the other way round.
482: * Unions OR the masks of their members, intersections AND them.
483: *
484: * @return int-mask-of<TypeKind::*>
485: */
486: public function getKindMask(): int;
487:
488: /** Models the bitwise-not (`~$x`) operator. Returns `ErrorType` for types where `~` is undefined. */
489: public function toBitwiseNotType(): Type;
490:
491: /**
492: * Models `get_class($x)`'s return type per leaf: definite objects yield
493: * their `class-string` projection, definite non-objects yield `false`,
494: * and possibly-objects yield the union of both.
495: */
496: public function toGetClassResultType(): Type;
497:
498: /**
499: * Models the type of `$x::class`. For known final classes the literal
500: * class name is returned; for everything else an
501: * `IntersectionType[ClassString<X>, AccessoryLiteralStringType]`.
502: * `NullType` passes through (mirrors PHP's nullsafe `::class` semantics).
503: * `ReflectionProvider` is needed for the final-class lookup.
504: */
505: public function toClassConstantType(ReflectionProvider $reflectionProvider): Type;
506:
507: /**
508: * Projects a class-name-or-object `Type` (the right-hand side of
509: * `$x instanceof <expr>`) to the `ObjectType` it should be compared
510: * against. Constant class strings collapse to their `ObjectType`
511: * exactly; everything kept symbolically (object class names,
512: * `class-string<X>`) carries an uncertainty flag so the caller can
513: * fall back to `BooleanType` instead of a definite yes/no.
514: */
515: public function toObjectTypeForInstanceofCheck(): ClassNameToObjectTypeResult;
516:
517: /**
518: * Projects a class-name-or-object `Type` (the second argument of
519: * `is_a($x, $class, $allow_string)`) to the `ObjectType` to narrow
520: * `$x` against. When `$allowString` is true, the `is_a()` result also
521: * keeps the original class-string accepted alongside the object.
522: * `$allowSameClass` controls whether matching the input's own class
523: * collapses to `NeverType` for final classes (the call site's
524: * "always-true" suppression).
525: */
526: public function toObjectTypeForIsACheck(Type $objectOrClassType, bool $allowString, bool $allowSameClass): ClassNameToObjectTypeResult;
527:
528: /** Models the (int) cast. */
529: public function toInteger(): Type;
530:
531: /** Models the (float) cast. */
532: public function toFloat(): Type;
533:
534: /** Models the (string) cast. */
535: public function toString(): Type;
536:
537: /** Models the (array) cast. */
538: public function toArray(): Type;
539:
540: /**
541: * Models PHP's implicit array key coercion: floats truncated to int,
542: * booleans become 0/1, null becomes '', numeric strings become int.
543: */
544: public function toArrayKey(): Type;
545:
546: /**
547: * Returns how this type might change when passed to a typed parameter
548: * or assigned to a typed property.
549: *
550: * With $strictTypes = true: int widens to int|float (since int is accepted
551: * by float parameters in strict mode).
552: * With $strictTypes = false: additional coercions apply, e.g. Stringable
553: * objects are accepted by string parameters.
554: *
555: * Used internally to determine what types a value might be coerced to
556: * when checking parameter acceptance.
557: */
558: public function toCoercedArgumentType(bool $strictTypes): self;
559:
560: public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic;
561:
562: public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic;
563:
564: /**
565: * Is Type of a known constant value? Includes literal strings, integers, floats, true, false, null, and array shapes.
566: *
567: * Unlike isConstantScalarValue(), this also returns yes for constant array types (array shapes
568: * with known keys and values). Use this when you need to detect any constant value including arrays.
569: */
570: public function isConstantValue(): TrinaryLogic;
571:
572: /**
573: * Is Type of a known constant scalar value? Includes literal strings, integers, floats, true, false, and null.
574: *
575: * Unlike isConstantValue(), this does NOT return yes for array shapes.
576: * Use this when you specifically need scalar constants only.
577: */
578: public function isConstantScalarValue(): TrinaryLogic;
579:
580: /** @return list<ConstantScalarType> */
581: public function getConstantScalarTypes(): array;
582:
583: /** @return list<int|float|string|bool|null> */
584: public function getConstantScalarValues(): array;
585:
586: public function isNull(): TrinaryLogic;
587:
588: public function isTrue(): TrinaryLogic;
589:
590: public function isFalse(): TrinaryLogic;
591:
592: public function isBoolean(): TrinaryLogic;
593:
594: public function isFloat(): TrinaryLogic;
595:
596: public function isInteger(): TrinaryLogic;
597:
598: public function isString(): TrinaryLogic;
599:
600: public function isNumericString(): TrinaryLogic;
601:
602: /**
603: * When isDecimalIntegerString() returns yes(), the type
604: * is guaranteed to be cast to an integer in an array key.
605: * Examples of constant values covered by this type: "0", "1", "1234", "-1"
606: *
607: * When isDecimalIntegerString() returns no(), the type represents strings containing non-decimal integers and other text.
608: * These are guaranteed to stay as string in an array key.
609: * Examples of constant values covered by this type: "+1", "00", "18E+3", "1.2", "1,3", "foo"
610: */
611: public function isDecimalIntegerString(): TrinaryLogic;
612:
613: public function isNonEmptyString(): TrinaryLogic;
614:
615: /**
616: * Non-falsy string is a non-empty string that is also not '0'.
617: * Stricter subset of non-empty-string.
618: */
619: public function isNonFalsyString(): TrinaryLogic;
620:
621: /**
622: * A literal-string is a string composed entirely from string literals
623: * in the source code (not from user input). Used for SQL injection prevention.
624: */
625: public function isLiteralString(): TrinaryLogic;
626:
627: /**
628: * The strings this type holds as StringRefinement cells, when they are
629: * exactly the strings of those cells: string and the conjunctions of its
630: * accessory types. Null for any other type, a constant string included.
631: *
632: * @return int-mask-of<StringRefinement::*>|null
633: */
634: public function getStringRefinement(): ?int;
635:
636: public function isLowercaseString(): TrinaryLogic;
637:
638: public function isUppercaseString(): TrinaryLogic;
639:
640: public function isClassString(): TrinaryLogic;
641:
642: public function isVoid(): TrinaryLogic;
643:
644: public function isScalar(): TrinaryLogic;
645:
646: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType;
647:
648: /**
649: * The values this type can be loosely equal to: `$this == $x` is false for any $x outside of it.
650: * The true branch of a `==` narrows the other side by intersecting with it.
651: */
652: public function getPossiblyLooselyEqualType(PhpVersion $phpVersion): Type;
653:
654: /**
655: * The values this type is always loosely equal to: `$this == $x` is true for any $x of it.
656: * The false branch of a `==` narrows the other side by removing it.
657: */
658: public function getAlwaysLooselyEqualType(PhpVersion $phpVersion): Type;
659:
660: /**
661: * Type narrowing methods for comparison operators.
662: * For example, for ConstantIntegerType(5), getSmallerType() returns int<min, 4>.
663: */
664: public function getSmallerType(PhpVersion $phpVersion): Type;
665:
666: public function getSmallerOrEqualType(PhpVersion $phpVersion): Type;
667:
668: public function getGreaterType(PhpVersion $phpVersion): Type;
669:
670: public function getGreaterOrEqualType(PhpVersion $phpVersion): Type;
671:
672: /**
673: * Returns actual template type for a given object.
674: *
675: * For example, with a class `Foo` that declares `@template T`:
676: *
677: * ```php
678: * // $fooType is Foo<int>
679: * $t = $fooType->getTemplateType(Foo::class, 'T');
680: * $t->isInteger(); // yes
681: * ```
682: *
683: * Returns ErrorType in case of a missing type.
684: *
685: * @param class-string $ancestorClassName
686: */
687: public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type;
688:
689: /**
690: * Infers the real types of TemplateTypes found in $this, based on
691: * the received Type. E.g. if $this is array<T> and $receivedType
692: * is array<int>, infers T = int.
693: */
694: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap;
695:
696: /**
697: * Returns the template types referenced by this Type, recursively.
698: *
699: * The return value is a list of TemplateTypeReferences, who contain the
700: * referenced template type as well as the variance position in which it was
701: * found.
702: *
703: * For example, calling this on array<Foo<T>,Bar> (with T a template type)
704: * will return one TemplateTypeReference for the type T.
705: *
706: * @param TemplateTypeVariance $positionVariance The variance position in
707: * which the receiver type was
708: * found.
709: *
710: * @return list<TemplateTypeReference>
711: */
712: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array;
713:
714: /** Models abs(). */
715: public function toAbsoluteNumber(): Type;
716:
717: /**
718: * Returns a new instance with all inner types mapped through $cb.
719: * Returns the same instance if inner types did not change.
720: *
721: * Not used directly — use TypeTraverser::map() instead.
722: *
723: * @param callable(Type):Type $cb
724: */
725: public function traverse(callable $cb): Type;
726:
727: /**
728: * Like traverse(), but walks two types simultaneously.
729: *
730: * Not used directly — use SimultaneousTypeTraverser::map() instead.
731: *
732: * @param callable(Type $left, Type $right): Type $cb
733: */
734: public function traverseSimultaneously(Type $right, callable $cb): Type;
735:
736: public function toPhpDocNode(): TypeNode;
737:
738: /**
739: * The native PHP type declaration for this type at the given position,
740: * widened to what the position allows, e.g. non-empty-string to string.
741: * Returns null when the type cannot be declared natively there.
742: *
743: * @return Identifier|Name|ComplexType|null
744: */
745: public function toNativePhpTypeNode(NativeTypePosition $position);
746:
747: /** @see TypeCombinator::remove() */
748: public function tryRemove(Type $typeToRemove): ?Type;
749:
750: /**
751: * The union with the other type as this type alone, when this type absorbs
752: * it by more than being its supertype, such as `mixed~int|1` being
753: * `mixed~(int<min, 0>|int<2, max>)`. Null when it does not; TypeCombinator::union()
754: * asks the other type the same.
755: */
756: public function tryUnion(Type $otherType): ?Type;
757:
758: /**
759: * The intersection with the other type as one type, when this type knows it
760: * better than as the subtype of the two, such as `Foo & object~Bar` being
761: * `Foo~Bar`. Null when it does not; TypeCombinator::intersect() asks the
762: * other type the same, and asks both before it drops supertypes. A type that
763: * compares the two to find out may return the one the other holds instead.
764: */
765: public function tryIntersect(Type $otherType): ?Type;
766:
767: /**
768: * Removes constant value information. E.g. 'foo' -> string, 1 -> int.
769: * Used when types become too complex to track precisely (e.g. loop iterations).
770: */
771: public function generalize(GeneralizePrecision $precision): Type;
772:
773: /**
774: * Performance optimization to skip template resolution when no templates are present.
775: */
776: public function hasTemplateOrLateResolvableType(): bool;
777:
778: /**
779: * Whether an UnresolvedTemplateArgumentType occurs anywhere in the type:
780: * a template argument that is still being inferred.
781: */
782: public function hasUnresolvedTemplateArgumentType(): bool;
783:
784: }
785: