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