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