1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Accessory;
4:
5: use PHPStan\Php\PhpVersion;
6: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
7: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
8: use PHPStan\TrinaryLogic;
9: use PHPStan\Turbo\ShadowedByTurboExtension;
10: use PHPStan\Type\AcceptsResult;
11: use PHPStan\Type\BooleanType;
12: use PHPStan\Type\CompoundType;
13: use PHPStan\Type\Enum\EnumCaseObjectType;
14: use PHPStan\Type\ErrorType;
15: use PHPStan\Type\Generic\GenericClassStringType;
16: use PHPStan\Type\InstanceofDeprecated;
17: use PHPStan\Type\IntersectionType;
18: use PHPStan\Type\IsSuperTypeOfResult;
19: use PHPStan\Type\MixedType;
20: use PHPStan\Type\NativeTypePosition;
21: use PHPStan\Type\ObjectWithoutClassType;
22: use PHPStan\Type\Traits\MaybeCallableTypeTrait;
23: use PHPStan\Type\Traits\MaybeIterableTypeTrait;
24: use PHPStan\Type\Traits\MaybeObjectTypeTrait;
25: use PHPStan\Type\Traits\MaybeOffsetAccessibleTypeTrait;
26: use PHPStan\Type\Traits\MaybeStringTypeTrait;
27: use PHPStan\Type\Traits\NonArrayTypeTrait;
28: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
29: use PHPStan\Type\Traits\NonGenericTypeTrait;
30: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
31: use PHPStan\Type\Traits\OperatorTypeTrait;
32: use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
33: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
34: use PHPStan\Type\Type;
35: use PHPStan\Type\TypeCombinator;
36: use PHPStan\Type\TypeKind;
37: use PHPStan\Type\UnionType;
38: use PHPStan\Type\VerbosityLevel;
39: use function sprintf;
40:
41: #[InstanceofDeprecated(insteadUse: 'Type::hasProperty()')]
42: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/HasPropertyType.cpp')]
43: class HasPropertyType implements AccessoryType, CompoundType
44: {
45:
46: use MaybeCallableTypeTrait;
47: use MaybeIterableTypeTrait;
48: use MaybeObjectTypeTrait;
49: use MaybeOffsetAccessibleTypeTrait;
50: use MaybeStringTypeTrait;
51: use NonArrayTypeTrait;
52: use TruthyBooleanTypeTrait;
53: use NonGenericTypeTrait;
54: use UndecidedComparisonCompoundTypeTrait;
55: use NonRemoveableTypeTrait;
56: use NonGeneralizableTypeTrait;
57: use OperatorTypeTrait;
58:
59: /** @api */
60: public function __construct(private string $propertyName)
61: {
62: }
63:
64: public function getReferencedClasses(): array
65: {
66: return [];
67: }
68:
69: public function getObjectClassNames(): array
70: {
71: return [];
72: }
73:
74: public function getObjectClassReflections(): array
75: {
76: return [];
77: }
78:
79: public function getClassStringType(): Type
80: {
81: return new GenericClassStringType($this);
82: }
83:
84: public function getPropertyName(): string
85: {
86: return $this->propertyName;
87: }
88:
89: public function accepts(Type $type, bool $strictTypes): AcceptsResult
90: {
91: if ($type instanceof CompoundType) {
92: return $type->isAcceptedBy($this, $strictTypes);
93: }
94:
95: return AcceptsResult::createFromBoolean($this->equals($type));
96: }
97:
98: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
99: {
100: if ($type instanceof CompoundType) {
101: return $type->isSubTypeOf($this);
102: }
103:
104: return IsSuperTypeOfResult::createFromTrinaryLogic(
105: $type->hasInstanceProperty($this->propertyName)->or($type->hasStaticProperty($this->propertyName)),
106: );
107: }
108:
109: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
110: {
111: if ($otherType instanceof UnionType || $otherType instanceof IntersectionType) {
112: return $otherType->isSuperTypeOf($this);
113: }
114:
115: if ($otherType instanceof self) {
116: $limit = TrinaryLogic::createYes();
117: } else {
118: $limit = TrinaryLogic::createMaybe();
119: }
120:
121: return IsSuperTypeOfResult::createFromTrinaryLogic(
122: $limit->and($otherType->hasInstanceProperty($this->propertyName)->or($otherType->hasStaticProperty($this->propertyName))),
123: );
124: }
125:
126: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
127: {
128: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
129: }
130:
131: public function equals(Type $type): bool
132: {
133: return $type instanceof self
134: && $this->propertyName === $type->propertyName;
135: }
136:
137: public function describe(VerbosityLevel $level): string
138: {
139: return sprintf('hasProperty(%s)', $this->propertyName);
140: }
141:
142: public function hasProperty(string $propertyName): TrinaryLogic
143: {
144: if ($this->propertyName === $propertyName) {
145: return TrinaryLogic::createYes();
146: }
147:
148: return TrinaryLogic::createMaybe();
149: }
150:
151: public function hasInstanceProperty(string $propertyName): TrinaryLogic
152: {
153: if ($this->propertyName === $propertyName) {
154: return TrinaryLogic::createYes();
155: }
156:
157: return TrinaryLogic::createMaybe();
158: }
159:
160: public function hasStaticProperty(string $propertyName): TrinaryLogic
161: {
162: if ($this->propertyName === $propertyName) {
163: return TrinaryLogic::createYes();
164: }
165:
166: return TrinaryLogic::createMaybe();
167: }
168:
169: public function getKindMask(): int
170: {
171: return TypeKind::STRING | TypeKind::OBJECT;
172: }
173:
174: public function isNull(): TrinaryLogic
175: {
176: return TrinaryLogic::createNo();
177: }
178:
179: public function isConstantValue(): TrinaryLogic
180: {
181: return TrinaryLogic::createNo();
182: }
183:
184: public function isConstantScalarValue(): TrinaryLogic
185: {
186: return TrinaryLogic::createNo();
187: }
188:
189: public function getConstantScalarTypes(): array
190: {
191: return [];
192: }
193:
194: public function getConstantScalarValues(): array
195: {
196: return [];
197: }
198:
199: public function isTrue(): TrinaryLogic
200: {
201: return TrinaryLogic::createNo();
202: }
203:
204: public function isFalse(): TrinaryLogic
205: {
206: return TrinaryLogic::createNo();
207: }
208:
209: public function isBoolean(): TrinaryLogic
210: {
211: return TrinaryLogic::createNo();
212: }
213:
214: public function isFloat(): TrinaryLogic
215: {
216: return TrinaryLogic::createNo();
217: }
218:
219: public function isInteger(): TrinaryLogic
220: {
221: return TrinaryLogic::createNo();
222: }
223:
224: public function getClassStringObjectType(): Type
225: {
226: return $this;
227: }
228:
229: public function getObjectTypeOrClassStringObjectType(): Type
230: {
231: return $this;
232: }
233:
234: public function isVoid(): TrinaryLogic
235: {
236: return TrinaryLogic::createNo();
237: }
238:
239: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
240: {
241: return new BooleanType();
242: }
243:
244: public function toNumber(): Type
245: {
246: return new ErrorType();
247: }
248:
249: public function toBitwiseNotType(): Type
250: {
251: return new ErrorType();
252: }
253:
254: public function toAbsoluteNumber(): Type
255: {
256: return new ErrorType();
257: }
258:
259: public function toString(): Type
260: {
261: return new ErrorType();
262: }
263:
264: public function toInteger(): Type
265: {
266: return new ErrorType();
267: }
268:
269: public function toFloat(): Type
270: {
271: return new ErrorType();
272: }
273:
274: public function toArray(): Type
275: {
276: return new MixedType();
277: }
278:
279: public function toArrayKey(): Type
280: {
281: return new ErrorType();
282: }
283:
284: public function toCoercedArgumentType(bool $strictTypes): Type
285: {
286: if (!$strictTypes) {
287: return TypeCombinator::union($this, $this->toString());
288: }
289:
290: return $this;
291: }
292:
293: public function getEnumCases(): array
294: {
295: return [];
296: }
297:
298: public function getEnumCaseObject(): ?EnumCaseObjectType
299: {
300: return null;
301: }
302:
303: public function traverse(callable $cb): Type
304: {
305: return $this;
306: }
307:
308: public function traverseSimultaneously(Type $right, callable $cb): Type
309: {
310: return $this;
311: }
312:
313: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
314: {
315: return new ErrorType();
316: }
317:
318: public function getFiniteTypes(): array
319: {
320: return [];
321: }
322:
323: public function getDefaultBaseType(): Type
324: {
325: return new ObjectWithoutClassType();
326: }
327:
328: public function toPhpDocNode(): TypeNode
329: {
330: return new IdentifierTypeNode(''); // no PHPDoc representation
331: }
332:
333: public function toNativePhpTypeNode(NativeTypePosition $position)
334: {
335: // not certainly an object on its own, object&hasMethod(foo) maps to object through the intersection
336: return null;
337: }
338:
339: public function hasTemplateOrLateResolvableType(): bool
340: {
341: return false;
342: }
343:
344: public function hasUnresolvedTemplateArgumentType(): bool
345: {
346: return false;
347: }
348:
349: public function tryUnion(Type $otherType): ?Type
350: {
351: return null;
352: }
353:
354: public function tryIntersect(Type $otherType): ?Type
355: {
356: return null;
357: }
358:
359: }
360: