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\ExtendedPropertyReflection;
13: use PHPStan\Reflection\PropertyReflection;
14: use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection;
15: use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection;
16: use PHPStan\TrinaryLogic;
17: use PHPStan\Type\Constant\ConstantArrayType;
18: use PHPStan\Type\Constant\ConstantStringType;
19: use PHPStan\Type\Enum\EnumCaseObjectType;
20: use PHPStan\Type\Generic\TemplateTypeMap;
21: use PHPStan\Type\Generic\TemplateTypeReference;
22: use PHPStan\Type\Generic\TemplateTypeVariance;
23:
24: /**
25: * @api
26: * @see https://phpstan.org/developing-extensions/type-system
27: */
28: interface Type
29: {
30:
31: /**
32: * @return string[]
33: */
34: public function getReferencedClasses(): array;
35:
36: /** @return list<non-empty-string> */
37: public function getObjectClassNames(): array;
38:
39: /**
40: * @return list<ClassReflection>
41: */
42: public function getObjectClassReflections(): array;
43:
44: /**
45: * Returns object type Foo for class-string<Foo> and 'Foo' (if Foo is a valid class).
46: */
47: public function getClassStringObjectType(): Type;
48:
49: /**
50: * Returns object type Foo for class-string<Foo>, 'Foo' (if Foo is a valid class),
51: * and object type Foo.
52: */
53: public function getObjectTypeOrClassStringObjectType(): Type;
54:
55: public function isObject(): TrinaryLogic;
56:
57: public function isEnum(): TrinaryLogic;
58:
59: /** @return list<ArrayType> */
60: public function getArrays(): array;
61:
62: /** @return list<ConstantArrayType> */
63: public function getConstantArrays(): array;
64:
65: /** @return list<ConstantStringType> */
66: public function getConstantStrings(): array;
67:
68: public function accepts(Type $type, bool $strictTypes): TrinaryLogic;
69:
70: /**
71: * This is like accepts() but gives reasons
72: * why the type was not/might not be accepted in some non-intuitive scenarios.
73: *
74: * In PHPStan 2.0 this method will be removed and the return type of accepts()
75: * will change to AcceptsResult.
76: */
77: public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult;
78:
79: public function isSuperTypeOf(Type $type): TrinaryLogic;
80:
81: public function equals(Type $type): bool;
82:
83: public function describe(VerbosityLevel $level): string;
84:
85: public function canAccessProperties(): TrinaryLogic;
86:
87: public function hasProperty(string $propertyName): TrinaryLogic;
88:
89: /**
90: * @return ExtendedPropertyReflection
91: */
92: public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): PropertyReflection;
93:
94: public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection;
95:
96: public function canCallMethods(): TrinaryLogic;
97:
98: public function hasMethod(string $methodName): TrinaryLogic;
99:
100: public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection;
101:
102: public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection;
103:
104: public function canAccessConstants(): TrinaryLogic;
105:
106: public function hasConstant(string $constantName): TrinaryLogic;
107:
108: public function getConstant(string $constantName): ConstantReflection;
109:
110: public function isIterable(): TrinaryLogic;
111:
112: public function isIterableAtLeastOnce(): TrinaryLogic;
113:
114: public function getArraySize(): Type;
115:
116: public function getIterableKeyType(): Type;
117:
118: public function getFirstIterableKeyType(): Type;
119:
120: public function getLastIterableKeyType(): Type;
121:
122: public function getIterableValueType(): Type;
123:
124: public function getFirstIterableValueType(): Type;
125:
126: public function getLastIterableValueType(): Type;
127:
128: public function isArray(): TrinaryLogic;
129:
130: public function isConstantArray(): TrinaryLogic;
131:
132: public function isOversizedArray(): TrinaryLogic;
133:
134: public function isList(): TrinaryLogic;
135:
136: public function isOffsetAccessible(): TrinaryLogic;
137:
138: public function isOffsetAccessLegal(): TrinaryLogic;
139:
140: public function hasOffsetValueType(Type $offsetType): TrinaryLogic;
141:
142: public function getOffsetValueType(Type $offsetType): Type;
143:
144: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type;
145:
146: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type;
147:
148: public function unsetOffset(Type $offsetType): Type;
149:
150: public function getKeysArray(): Type;
151:
152: public function getValuesArray(): Type;
153:
154: public function fillKeysArray(Type $valueType): Type;
155:
156: public function flipArray(): Type;
157:
158: public function intersectKeyArray(Type $otherArraysType): Type;
159:
160: public function popArray(): Type;
161:
162: public function searchArray(Type $needleType): Type;
163:
164: public function shiftArray(): Type;
165:
166: public function shuffleArray(): 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): TrinaryLogic;
215:
216: public function isSmallerThanOrEqual(Type $otherType): 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 isClassStringType(): TrinaryLogic;
261:
262: public function isVoid(): TrinaryLogic;
263:
264: public function isScalar(): TrinaryLogic;
265:
266: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType;
267:
268: public function getSmallerType(): Type;
269:
270: public function getSmallerOrEqualType(): Type;
271:
272: public function getGreaterType(): Type;
273:
274: public function getGreaterOrEqualType(): Type;
275:
276: /**
277: * Returns actual template type for a given object.
278: *
279: * Example:
280: *
281: * @-template T
282: * class Foo {}
283: *
284: * // $fooType is Foo<int>
285: * $t = $fooType->getTemplateType(Foo::class, 'T');
286: * $t->isInteger(); // yes
287: *
288: * Returns ErrorType in case of a missing type.
289: *
290: * @param class-string $ancestorClassName
291: */
292: public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type;
293:
294: /**
295: * Infers template types
296: *
297: * Infers the real Type of the TemplateTypes found in $this, based on
298: * the received Type.
299: */
300: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap;
301:
302: /**
303: * Returns the template types referenced by this Type, recursively
304: *
305: * The return value is a list of TemplateTypeReferences, who contain the
306: * referenced template type as well as the variance position in which it was
307: * found.
308: *
309: * For example, calling this on array<Foo<T>,Bar> (with T a template type)
310: * will return one TemplateTypeReference for the type T.
311: *
312: * @param TemplateTypeVariance $positionVariance The variance position in
313: * which the receiver type was
314: * found.
315: *
316: * @return TemplateTypeReference[]
317: */
318: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array;
319:
320: public function toAbsoluteNumber(): Type;
321:
322: /**
323: * Traverses inner types
324: *
325: * Returns a new instance with all inner types mapped through $cb. Might
326: * return the same instance if inner types did not change.
327: *
328: * @param callable(Type):Type $cb
329: */
330: public function traverse(callable $cb): Type;
331:
332: /**
333: * Traverses inner types while keeping the same context in another type.
334: *
335: * @param callable(Type $left, Type $right): Type $cb
336: */
337: public function traverseSimultaneously(Type $right, callable $cb): Type;
338:
339: public function toPhpDocNode(): TypeNode;
340:
341: /**
342: * Return the difference with another type, or null if it cannot be represented.
343: *
344: * @see TypeCombinator::remove()
345: */
346: public function tryRemove(Type $typeToRemove): ?Type;
347:
348: public function generalize(GeneralizePrecision $precision): Type;
349:
350: /**
351: * @param mixed[] $properties
352: */
353: public static function __set_state(array $properties): self;
354:
355: }
356: