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