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