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