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