1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\Php\PhpVersion;
6: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
7: use PHPStan\Reflection\Callables\CallableParametersAcceptor;
8: use PHPStan\Reflection\ClassMemberAccessAnswerer;
9: use PHPStan\Reflection\ClassReflection;
10: use PHPStan\Reflection\ConstantReflection;
11: use PHPStan\Reflection\ExtendedMethodReflection;
12: use PHPStan\Reflection\PropertyReflection;
13: use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection;
14: use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection;
15: use PHPStan\TrinaryLogic;
16: use PHPStan\Type\Constant\ConstantArrayType;
17: use PHPStan\Type\Constant\ConstantStringType;
18: use PHPStan\Type\Enum\EnumCaseObjectType;
19: use PHPStan\Type\Generic\TemplateTypeMap;
20: use PHPStan\Type\Generic\TemplateTypeReference;
21: use PHPStan\Type\Generic\TemplateTypeVariance;
22:
23: /**
24: * @api
25: * @see https://phpstan.org/developing-extensions/type-system
26: */
27: interface Type
28: {
29:
30: /**
31: * @return string[]
32: */
33: public function getReferencedClasses(): array;
34:
35: /** @return list<non-empty-string> */
36: public function getObjectClassNames(): array;
37:
38: /**
39: * @return list<ClassReflection>
40: */
41: public function getObjectClassReflections(): array;
42:
43: /**
44: * Returns object type Foo for class-string<Foo> and 'Foo' (if Foo is a valid class).
45: */
46: public function getClassStringObjectType(): Type;
47:
48: /**
49: * Returns object type Foo for class-string<Foo>, 'Foo' (if Foo is a valid class),
50: * and object type Foo.
51: */
52: public function getObjectTypeOrClassStringObjectType(): Type;
53:
54: public function isObject(): TrinaryLogic;
55:
56: public function isEnum(): TrinaryLogic;
57:
58: /** @return list<ArrayType> */
59: public function getArrays(): array;
60:
61: /** @return list<ConstantArrayType> */
62: public function getConstantArrays(): array;
63:
64: /** @return list<ConstantStringType> */
65: public function getConstantStrings(): array;
66:
67: public function accepts(Type $type, bool $strictTypes): TrinaryLogic;
68:
69: /**
70: * This is like accepts() but gives reasons
71: * why the type was not/might not be accepted in some non-intuitive scenarios.
72: *
73: * In PHPStan 2.0 this method will be removed and the return type of accepts()
74: * will change to AcceptsResult.
75: */
76: public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult;
77:
78: public function isSuperTypeOf(Type $type): TrinaryLogic;
79:
80: public function equals(Type $type): bool;
81:
82: public function describe(VerbosityLevel $level): string;
83:
84: public function canAccessProperties(): TrinaryLogic;
85:
86: public function hasProperty(string $propertyName): TrinaryLogic;
87:
88: public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): PropertyReflection;
89:
90: public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection;
91:
92: public function canCallMethods(): TrinaryLogic;
93:
94: public function hasMethod(string $methodName): TrinaryLogic;
95:
96: public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection;
97:
98: public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection;
99:
100: public function canAccessConstants(): TrinaryLogic;
101:
102: public function hasConstant(string $constantName): TrinaryLogic;
103:
104: public function getConstant(string $constantName): ConstantReflection;
105:
106: public function isIterable(): TrinaryLogic;
107:
108: public function isIterableAtLeastOnce(): TrinaryLogic;
109:
110: public function getArraySize(): Type;
111:
112: public function getIterableKeyType(): Type;
113:
114: public function getFirstIterableKeyType(): Type;
115:
116: public function getLastIterableKeyType(): Type;
117:
118: public function getIterableValueType(): Type;
119:
120: public function getFirstIterableValueType(): Type;
121:
122: public function getLastIterableValueType(): Type;
123:
124: public function isArray(): TrinaryLogic;
125:
126: public function isConstantArray(): TrinaryLogic;
127:
128: public function isOversizedArray(): TrinaryLogic;
129:
130: public function isList(): TrinaryLogic;
131:
132: public function isOffsetAccessible(): TrinaryLogic;
133:
134: public function isOffsetAccessLegal(): TrinaryLogic;
135:
136: public function hasOffsetValueType(Type $offsetType): TrinaryLogic;
137:
138: public function getOffsetValueType(Type $offsetType): Type;
139:
140: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type;
141:
142: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type;
143:
144: public function unsetOffset(Type $offsetType): Type;
145:
146: public function getKeysArray(): Type;
147:
148: public function getValuesArray(): Type;
149:
150: public function fillKeysArray(Type $valueType): Type;
151:
152: public function flipArray(): Type;
153:
154: public function intersectKeyArray(Type $otherArraysType): Type;
155:
156: public function popArray(): Type;
157:
158: public function searchArray(Type $needleType): Type;
159:
160: public function shiftArray(): Type;
161:
162: public function shuffleArray(): Type;
163:
164: /**
165: * @return list<EnumCaseObjectType>
166: */
167: public function getEnumCases(): array;
168:
169: /**
170: * Returns a list of finite values.
171: *
172: * Examples:
173: *
174: * - for bool: [true, false]
175: * - for int<0, 3>: [0, 1, 2, 3]
176: * - for enums: list of enum cases
177: * - for scalars: the scalar itself
178: *
179: * For infinite types it returns an empty array.
180: *
181: * @return list<Type>
182: */
183: public function getFiniteTypes(): array;
184:
185: public function exponentiate(Type $exponent): Type;
186:
187: public function isCallable(): TrinaryLogic;
188:
189: /**
190: * @return CallableParametersAcceptor[]
191: */
192: public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array;
193:
194: public function isCloneable(): TrinaryLogic;
195:
196: public function toBoolean(): BooleanType;
197:
198: public function toNumber(): Type;
199:
200: public function toInteger(): Type;
201:
202: public function toFloat(): Type;
203:
204: public function toString(): Type;
205:
206: public function toArray(): Type;
207:
208: public function toArrayKey(): Type;
209:
210: public function isSmallerThan(Type $otherType): TrinaryLogic;
211:
212: public function isSmallerThanOrEqual(Type $otherType): TrinaryLogic;
213:
214: /**
215: * Is Type of a known constant value? Includes literal strings, integers, floats, true, false, null, and array shapes.
216: */
217: public function isConstantValue(): TrinaryLogic;
218:
219: /**
220: * Is Type of a known constant scalar value? Includes literal strings, integers, floats, true, false, and null.
221: */
222: public function isConstantScalarValue(): TrinaryLogic;
223:
224: /**
225: * @return list<ConstantScalarType>
226: */
227: public function getConstantScalarTypes(): array;
228:
229: /**
230: * @return list<int|float|string|bool|null>
231: */
232: public function getConstantScalarValues(): array;
233:
234: public function isNull(): TrinaryLogic;
235:
236: public function isTrue(): TrinaryLogic;
237:
238: public function isFalse(): TrinaryLogic;
239:
240: public function isBoolean(): TrinaryLogic;
241:
242: public function isFloat(): TrinaryLogic;
243:
244: public function isInteger(): TrinaryLogic;
245:
246: public function isString(): TrinaryLogic;
247:
248: public function isNumericString(): TrinaryLogic;
249:
250: public function isNonEmptyString(): TrinaryLogic;
251:
252: public function isNonFalsyString(): TrinaryLogic;
253:
254: public function isLiteralString(): TrinaryLogic;
255:
256: public function isClassStringType(): TrinaryLogic;
257:
258: public function isVoid(): TrinaryLogic;
259:
260: public function isScalar(): TrinaryLogic;
261:
262: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType;
263:
264: public function getSmallerType(): Type;
265:
266: public function getSmallerOrEqualType(): Type;
267:
268: public function getGreaterType(): Type;
269:
270: public function getGreaterOrEqualType(): Type;
271:
272: /**
273: * Returns actual template type for a given object.
274: *
275: * Example:
276: *
277: * @-template T
278: * class Foo {}
279: *
280: * // $fooType is Foo<int>
281: * $t = $fooType->getTemplateType(Foo::class, 'T');
282: * $t->isInteger(); // yes
283: *
284: * Returns ErrorType in case of a missing type.
285: *
286: * @param class-string $ancestorClassName
287: */
288: public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type;
289:
290: /**
291: * Infers template types
292: *
293: * Infers the real Type of the TemplateTypes found in $this, based on
294: * the received Type.
295: */
296: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap;
297:
298: /**
299: * Returns the template types referenced by this Type, recursively
300: *
301: * The return value is a list of TemplateTypeReferences, who contain the
302: * referenced template type as well as the variance position in which it was
303: * found.
304: *
305: * For example, calling this on array<Foo<T>,Bar> (with T a template type)
306: * will return one TemplateTypeReference for the type T.
307: *
308: * @param TemplateTypeVariance $positionVariance The variance position in
309: * which the receiver type was
310: * found.
311: *
312: * @return TemplateTypeReference[]
313: */
314: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array;
315:
316: /**
317: * Traverses inner types
318: *
319: * Returns a new instance with all inner types mapped through $cb. Might
320: * return the same instance if inner types did not change.
321: *
322: * @param callable(Type):Type $cb
323: */
324: public function traverse(callable $cb): Type;
325:
326: /**
327: * Traverses inner types while keeping the same context in another type.
328: *
329: * @param callable(Type $left, Type $right): Type $cb
330: */
331: public function traverseSimultaneously(Type $right, callable $cb): Type;
332:
333: public function toPhpDocNode(): TypeNode;
334:
335: /**
336: * Return the difference with another type, or null if it cannot be represented.
337: *
338: * @see TypeCombinator::remove()
339: */
340: public function tryRemove(Type $typeToRemove): ?Type;
341:
342: public function generalize(GeneralizePrecision $precision): Type;
343:
344: /**
345: * @param mixed[] $properties
346: */
347: public static function __set_state(array $properties): self;
348:
349: }
350: