1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Type; |
4: | |
5: | use PHPStan\Analyser\OutOfClassScope; |
6: | use PHPStan\Php\PhpVersion; |
7: | use PHPStan\PhpDoc\Tag\TemplateTag; |
8: | use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode; |
9: | use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode; |
10: | use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode; |
11: | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
12: | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
13: | use PHPStan\PhpDocParser\Printer\Printer; |
14: | use PHPStan\Reflection\Callables\CallableParametersAcceptor; |
15: | use PHPStan\Reflection\Callables\SimpleImpurePoint; |
16: | use PHPStan\Reflection\Callables\SimpleThrowPoint; |
17: | use PHPStan\Reflection\ClassMemberAccessAnswerer; |
18: | use PHPStan\Reflection\Native\NativeParameterReflection; |
19: | use PHPStan\Reflection\ParameterReflection; |
20: | use PHPStan\Reflection\ParametersAcceptor; |
21: | use PHPStan\Reflection\PassedByReference; |
22: | use PHPStan\Reflection\Php\DummyParameter; |
23: | use PHPStan\ShouldNotHappenException; |
24: | use PHPStan\TrinaryLogic; |
25: | use PHPStan\Type\Generic\TemplateType; |
26: | use PHPStan\Type\Generic\TemplateTypeHelper; |
27: | use PHPStan\Type\Generic\TemplateTypeMap; |
28: | use PHPStan\Type\Generic\TemplateTypeVariance; |
29: | use PHPStan\Type\Generic\TemplateTypeVarianceMap; |
30: | use PHPStan\Type\Traits\MaybeArrayTypeTrait; |
31: | use PHPStan\Type\Traits\MaybeIterableTypeTrait; |
32: | use PHPStan\Type\Traits\MaybeObjectTypeTrait; |
33: | use PHPStan\Type\Traits\MaybeOffsetAccessibleTypeTrait; |
34: | use PHPStan\Type\Traits\NonGeneralizableTypeTrait; |
35: | use PHPStan\Type\Traits\NonRemoveableTypeTrait; |
36: | use PHPStan\Type\Traits\TruthyBooleanTypeTrait; |
37: | use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait; |
38: | use function array_map; |
39: | use function array_merge; |
40: | use function count; |
41: | |
42: | |
43: | class CallableType implements CompoundType, CallableParametersAcceptor |
44: | { |
45: | |
46: | use MaybeArrayTypeTrait; |
47: | use MaybeIterableTypeTrait; |
48: | use MaybeObjectTypeTrait; |
49: | use MaybeOffsetAccessibleTypeTrait; |
50: | use TruthyBooleanTypeTrait; |
51: | use UndecidedComparisonCompoundTypeTrait; |
52: | use NonRemoveableTypeTrait; |
53: | use NonGeneralizableTypeTrait; |
54: | |
55: | |
56: | private array $parameters; |
57: | |
58: | private Type $returnType; |
59: | |
60: | private bool $isCommonCallable; |
61: | |
62: | private TemplateTypeMap $templateTypeMap; |
63: | |
64: | private TemplateTypeMap $resolvedTemplateTypeMap; |
65: | |
66: | private TrinaryLogic $isPure; |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | |
73: | public function __construct( |
74: | ?array $parameters = null, |
75: | ?Type $returnType = null, |
76: | private bool $variadic = true, |
77: | ?TemplateTypeMap $templateTypeMap = null, |
78: | ?TemplateTypeMap $resolvedTemplateTypeMap = null, |
79: | private array $templateTags = [], |
80: | ?TrinaryLogic $isPure = null, |
81: | ) |
82: | { |
83: | $this->parameters = $parameters ?? []; |
84: | $this->returnType = $returnType ?? new MixedType(); |
85: | $this->isCommonCallable = $parameters === null && $returnType === null; |
86: | $this->templateTypeMap = $templateTypeMap ?? TemplateTypeMap::createEmpty(); |
87: | $this->resolvedTemplateTypeMap = $resolvedTemplateTypeMap ?? TemplateTypeMap::createEmpty(); |
88: | $this->isPure = $isPure ?? TrinaryLogic::createMaybe(); |
89: | } |
90: | |
91: | |
92: | |
93: | |
94: | public function getTemplateTags(): array |
95: | { |
96: | return $this->templateTags; |
97: | } |
98: | |
99: | public function isPure(): TrinaryLogic |
100: | { |
101: | return $this->isPure; |
102: | } |
103: | |
104: | |
105: | |
106: | |
107: | public function getReferencedClasses(): array |
108: | { |
109: | $classes = []; |
110: | foreach ($this->parameters as $parameter) { |
111: | $classes = array_merge($classes, $parameter->getType()->getReferencedClasses()); |
112: | } |
113: | |
114: | return array_merge($classes, $this->returnType->getReferencedClasses()); |
115: | } |
116: | |
117: | public function getObjectClassNames(): array |
118: | { |
119: | return []; |
120: | } |
121: | |
122: | public function getObjectClassReflections(): array |
123: | { |
124: | return []; |
125: | } |
126: | |
127: | public function getConstantStrings(): array |
128: | { |
129: | return []; |
130: | } |
131: | |
132: | public function accepts(Type $type, bool $strictTypes): TrinaryLogic |
133: | { |
134: | return $this->acceptsWithReason($type, $strictTypes)->result; |
135: | } |
136: | |
137: | public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult |
138: | { |
139: | if ($type instanceof CompoundType && !$type instanceof self) { |
140: | return $type->isAcceptedWithReasonBy($this, $strictTypes); |
141: | } |
142: | |
143: | return $this->isSuperTypeOfInternal($type, true); |
144: | } |
145: | |
146: | public function isSuperTypeOf(Type $type): TrinaryLogic |
147: | { |
148: | if ($type instanceof CompoundType && !$type instanceof self) { |
149: | return $type->isSubTypeOf($this); |
150: | } |
151: | |
152: | return $this->isSuperTypeOfInternal($type, false)->result; |
153: | } |
154: | |
155: | private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny): AcceptsResult |
156: | { |
157: | $isCallable = new AcceptsResult($type->isCallable(), []); |
158: | if ($isCallable->no()) { |
159: | return $isCallable; |
160: | } |
161: | |
162: | static $scope; |
163: | if ($scope === null) { |
164: | $scope = new OutOfClassScope(); |
165: | } |
166: | |
167: | if ($this->isCommonCallable) { |
168: | if ($this->isPure()->yes()) { |
169: | $typePure = TrinaryLogic::createYes(); |
170: | foreach ($type->getCallableParametersAcceptors($scope) as $variant) { |
171: | $typePure = $typePure->and($variant->isPure()); |
172: | } |
173: | |
174: | return $isCallable->and(new AcceptsResult($typePure, [])); |
175: | } |
176: | |
177: | return $isCallable; |
178: | } |
179: | |
180: | $variantsResult = null; |
181: | foreach ($type->getCallableParametersAcceptors($scope) as $variant) { |
182: | $isSuperType = CallableTypeHelper::isParametersAcceptorSuperTypeOf($this, $variant, $treatMixedAsAny); |
183: | if ($variantsResult === null) { |
184: | $variantsResult = $isSuperType; |
185: | } else { |
186: | $variantsResult = $variantsResult->or($isSuperType); |
187: | } |
188: | } |
189: | |
190: | if ($variantsResult === null) { |
191: | throw new ShouldNotHappenException(); |
192: | } |
193: | |
194: | return $isCallable->and($variantsResult); |
195: | } |
196: | |
197: | public function isSubTypeOf(Type $otherType): TrinaryLogic |
198: | { |
199: | if ($otherType instanceof IntersectionType || $otherType instanceof UnionType) { |
200: | return $otherType->isSuperTypeOf($this); |
201: | } |
202: | |
203: | return $otherType->isCallable() |
204: | ->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()); |
205: | } |
206: | |
207: | public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLogic |
208: | { |
209: | return $this->isAcceptedWithReasonBy($acceptingType, $strictTypes)->result; |
210: | } |
211: | |
212: | public function isAcceptedWithReasonBy(Type $acceptingType, bool $strictTypes): AcceptsResult |
213: | { |
214: | return new AcceptsResult($this->isSubTypeOf($acceptingType), []); |
215: | } |
216: | |
217: | public function equals(Type $type): bool |
218: | { |
219: | if (!$type instanceof self) { |
220: | return false; |
221: | } |
222: | |
223: | return $this->describe(VerbosityLevel::precise()) === $type->describe(VerbosityLevel::precise()); |
224: | } |
225: | |
226: | public function describe(VerbosityLevel $level): string |
227: | { |
228: | return $level->handle( |
229: | static fn (): string => 'callable', |
230: | function (): string { |
231: | $printer = new Printer(); |
232: | $selfWithoutParameterNames = new self( |
233: | array_map(static fn (ParameterReflection $p): ParameterReflection => new DummyParameter( |
234: | '', |
235: | $p->getType(), |
236: | $p->isOptional() && !$p->isVariadic(), |
237: | PassedByReference::createNo(), |
238: | $p->isVariadic(), |
239: | $p->getDefaultValue(), |
240: | ), $this->parameters), |
241: | $this->returnType, |
242: | $this->variadic, |
243: | $this->templateTypeMap, |
244: | $this->resolvedTemplateTypeMap, |
245: | $this->templateTags, |
246: | $this->isPure, |
247: | ); |
248: | |
249: | return $printer->print($selfWithoutParameterNames->toPhpDocNode()); |
250: | }, |
251: | ); |
252: | } |
253: | |
254: | public function isCallable(): TrinaryLogic |
255: | { |
256: | return TrinaryLogic::createYes(); |
257: | } |
258: | |
259: | public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array |
260: | { |
261: | return [$this]; |
262: | } |
263: | |
264: | public function getThrowPoints(): array |
265: | { |
266: | return [ |
267: | SimpleThrowPoint::createImplicit(), |
268: | ]; |
269: | } |
270: | |
271: | public function getImpurePoints(): array |
272: | { |
273: | $pure = $this->isPure(); |
274: | if ($pure->yes()) { |
275: | return []; |
276: | } |
277: | |
278: | return [ |
279: | new SimpleImpurePoint( |
280: | 'functionCall', |
281: | 'call to a callable', |
282: | $pure->no(), |
283: | ), |
284: | ]; |
285: | } |
286: | |
287: | public function getInvalidateExpressions(): array |
288: | { |
289: | return []; |
290: | } |
291: | |
292: | public function getUsedVariables(): array |
293: | { |
294: | return []; |
295: | } |
296: | |
297: | public function toNumber(): Type |
298: | { |
299: | return new ErrorType(); |
300: | } |
301: | |
302: | public function toString(): Type |
303: | { |
304: | return new ErrorType(); |
305: | } |
306: | |
307: | public function toInteger(): Type |
308: | { |
309: | return new ErrorType(); |
310: | } |
311: | |
312: | public function toFloat(): Type |
313: | { |
314: | return new ErrorType(); |
315: | } |
316: | |
317: | public function toArray(): Type |
318: | { |
319: | return new ArrayType(new MixedType(), new MixedType()); |
320: | } |
321: | |
322: | public function toArrayKey(): Type |
323: | { |
324: | return new ErrorType(); |
325: | } |
326: | |
327: | public function isOffsetAccessLegal(): TrinaryLogic |
328: | { |
329: | return TrinaryLogic::createMaybe(); |
330: | } |
331: | |
332: | public function getTemplateTypeMap(): TemplateTypeMap |
333: | { |
334: | return $this->templateTypeMap; |
335: | } |
336: | |
337: | public function getResolvedTemplateTypeMap(): TemplateTypeMap |
338: | { |
339: | return $this->resolvedTemplateTypeMap; |
340: | } |
341: | |
342: | public function getCallSiteVarianceMap(): TemplateTypeVarianceMap |
343: | { |
344: | return TemplateTypeVarianceMap::createEmpty(); |
345: | } |
346: | |
347: | |
348: | |
349: | |
350: | public function getParameters(): array |
351: | { |
352: | return $this->parameters; |
353: | } |
354: | |
355: | public function isVariadic(): bool |
356: | { |
357: | return $this->variadic; |
358: | } |
359: | |
360: | public function getReturnType(): Type |
361: | { |
362: | return $this->returnType; |
363: | } |
364: | |
365: | public function inferTemplateTypes(Type $receivedType): TemplateTypeMap |
366: | { |
367: | if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) { |
368: | return $receivedType->inferTemplateTypesOn($this); |
369: | } |
370: | |
371: | if (! $receivedType->isCallable()->yes()) { |
372: | return TemplateTypeMap::createEmpty(); |
373: | } |
374: | |
375: | $parametersAcceptors = $receivedType->getCallableParametersAcceptors(new OutOfClassScope()); |
376: | |
377: | $typeMap = TemplateTypeMap::createEmpty(); |
378: | |
379: | foreach ($parametersAcceptors as $parametersAcceptor) { |
380: | $typeMap = $typeMap->union($this->inferTemplateTypesOnParametersAcceptor($parametersAcceptor)); |
381: | } |
382: | |
383: | return $typeMap; |
384: | } |
385: | |
386: | private function inferTemplateTypesOnParametersAcceptor(ParametersAcceptor $parametersAcceptor): TemplateTypeMap |
387: | { |
388: | $typeMap = TemplateTypeMap::createEmpty(); |
389: | $args = $parametersAcceptor->getParameters(); |
390: | $returnType = $parametersAcceptor->getReturnType(); |
391: | |
392: | foreach ($this->getParameters() as $i => $param) { |
393: | $paramType = $param->getType(); |
394: | if (isset($args[$i])) { |
395: | $argType = $args[$i]->getType(); |
396: | } elseif ($paramType instanceof TemplateType) { |
397: | $argType = TemplateTypeHelper::resolveToBounds($paramType); |
398: | } else { |
399: | $argType = new NeverType(); |
400: | } |
401: | |
402: | $typeMap = $typeMap->union($paramType->inferTemplateTypes($argType)->convertToLowerBoundTypes()); |
403: | } |
404: | |
405: | return $typeMap->union($this->getReturnType()->inferTemplateTypes($returnType)); |
406: | } |
407: | |
408: | public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array |
409: | { |
410: | $references = $this->getReturnType()->getReferencedTemplateTypes( |
411: | $positionVariance->compose(TemplateTypeVariance::createCovariant()), |
412: | ); |
413: | |
414: | $paramVariance = $positionVariance->compose(TemplateTypeVariance::createContravariant()); |
415: | |
416: | foreach ($this->getParameters() as $param) { |
417: | foreach ($param->getType()->getReferencedTemplateTypes($paramVariance) as $reference) { |
418: | $references[] = $reference; |
419: | } |
420: | } |
421: | |
422: | return $references; |
423: | } |
424: | |
425: | public function traverse(callable $cb): Type |
426: | { |
427: | if ($this->isCommonCallable) { |
428: | return $this; |
429: | } |
430: | |
431: | $parameters = array_map(static function (ParameterReflection $param) use ($cb): NativeParameterReflection { |
432: | $defaultValue = $param->getDefaultValue(); |
433: | return new NativeParameterReflection( |
434: | $param->getName(), |
435: | $param->isOptional(), |
436: | $cb($param->getType()), |
437: | $param->passedByReference(), |
438: | $param->isVariadic(), |
439: | $defaultValue !== null ? $cb($defaultValue) : null, |
440: | ); |
441: | }, $this->getParameters()); |
442: | |
443: | return new self( |
444: | $parameters, |
445: | $cb($this->getReturnType()), |
446: | $this->isVariadic(), |
447: | $this->templateTypeMap, |
448: | $this->resolvedTemplateTypeMap, |
449: | $this->templateTags, |
450: | $this->isPure, |
451: | ); |
452: | } |
453: | |
454: | public function traverseSimultaneously(Type $right, callable $cb): Type |
455: | { |
456: | if ($this->isCommonCallable) { |
457: | return $this; |
458: | } |
459: | |
460: | if (!$right->isCallable()->yes()) { |
461: | return $this; |
462: | } |
463: | |
464: | $rightAcceptors = $right->getCallableParametersAcceptors(new OutOfClassScope()); |
465: | if (count($rightAcceptors) !== 1) { |
466: | return $this; |
467: | } |
468: | |
469: | $rightParameters = $rightAcceptors[0]->getParameters(); |
470: | if (count($this->getParameters()) !== count($rightParameters)) { |
471: | return $this; |
472: | } |
473: | |
474: | $parameters = []; |
475: | foreach ($this->getParameters() as $i => $leftParam) { |
476: | $rightParam = $rightParameters[$i]; |
477: | $leftDefaultValue = $leftParam->getDefaultValue(); |
478: | $rightDefaultValue = $rightParam->getDefaultValue(); |
479: | $defaultValue = $leftDefaultValue; |
480: | if ($leftDefaultValue !== null && $rightDefaultValue !== null) { |
481: | $defaultValue = $cb($leftDefaultValue, $rightDefaultValue); |
482: | } |
483: | $parameters[] = new NativeParameterReflection( |
484: | $leftParam->getName(), |
485: | $leftParam->isOptional(), |
486: | $cb($leftParam->getType(), $rightParam->getType()), |
487: | $leftParam->passedByReference(), |
488: | $leftParam->isVariadic(), |
489: | $defaultValue, |
490: | ); |
491: | } |
492: | |
493: | return new self( |
494: | $parameters, |
495: | $cb($this->getReturnType(), $rightAcceptors[0]->getReturnType()), |
496: | $this->isVariadic(), |
497: | $this->templateTypeMap, |
498: | $this->resolvedTemplateTypeMap, |
499: | $this->templateTags, |
500: | $this->isPure, |
501: | ); |
502: | } |
503: | |
504: | public function isOversizedArray(): TrinaryLogic |
505: | { |
506: | return TrinaryLogic::createNo(); |
507: | } |
508: | |
509: | public function isNull(): TrinaryLogic |
510: | { |
511: | return TrinaryLogic::createNo(); |
512: | } |
513: | |
514: | public function isConstantValue(): TrinaryLogic |
515: | { |
516: | return TrinaryLogic::createNo(); |
517: | } |
518: | |
519: | public function isConstantScalarValue(): TrinaryLogic |
520: | { |
521: | return TrinaryLogic::createNo(); |
522: | } |
523: | |
524: | public function getConstantScalarTypes(): array |
525: | { |
526: | return []; |
527: | } |
528: | |
529: | public function getConstantScalarValues(): array |
530: | { |
531: | return []; |
532: | } |
533: | |
534: | public function isTrue(): TrinaryLogic |
535: | { |
536: | return TrinaryLogic::createNo(); |
537: | } |
538: | |
539: | public function isFalse(): TrinaryLogic |
540: | { |
541: | return TrinaryLogic::createNo(); |
542: | } |
543: | |
544: | public function isBoolean(): TrinaryLogic |
545: | { |
546: | return TrinaryLogic::createNo(); |
547: | } |
548: | |
549: | public function isFloat(): TrinaryLogic |
550: | { |
551: | return TrinaryLogic::createNo(); |
552: | } |
553: | |
554: | public function isInteger(): TrinaryLogic |
555: | { |
556: | return TrinaryLogic::createNo(); |
557: | } |
558: | |
559: | public function isString(): TrinaryLogic |
560: | { |
561: | return TrinaryLogic::createMaybe(); |
562: | } |
563: | |
564: | public function isNumericString(): TrinaryLogic |
565: | { |
566: | return TrinaryLogic::createNo(); |
567: | } |
568: | |
569: | public function isNonEmptyString(): TrinaryLogic |
570: | { |
571: | return TrinaryLogic::createMaybe(); |
572: | } |
573: | |
574: | public function isNonFalsyString(): TrinaryLogic |
575: | { |
576: | return TrinaryLogic::createMaybe(); |
577: | } |
578: | |
579: | public function isLiteralString(): TrinaryLogic |
580: | { |
581: | return TrinaryLogic::createMaybe(); |
582: | } |
583: | |
584: | public function isClassStringType(): TrinaryLogic |
585: | { |
586: | return TrinaryLogic::createMaybe(); |
587: | } |
588: | |
589: | public function getClassStringObjectType(): Type |
590: | { |
591: | return new ObjectWithoutClassType(); |
592: | } |
593: | |
594: | public function getObjectTypeOrClassStringObjectType(): Type |
595: | { |
596: | return new ObjectWithoutClassType(); |
597: | } |
598: | |
599: | public function isVoid(): TrinaryLogic |
600: | { |
601: | return TrinaryLogic::createNo(); |
602: | } |
603: | |
604: | public function isScalar(): TrinaryLogic |
605: | { |
606: | return TrinaryLogic::createMaybe(); |
607: | } |
608: | |
609: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
610: | { |
611: | return new BooleanType(); |
612: | } |
613: | |
614: | public function getEnumCases(): array |
615: | { |
616: | return []; |
617: | } |
618: | |
619: | public function isCommonCallable(): bool |
620: | { |
621: | return $this->isCommonCallable; |
622: | } |
623: | |
624: | public function exponentiate(Type $exponent): Type |
625: | { |
626: | return new ErrorType(); |
627: | } |
628: | |
629: | public function getFiniteTypes(): array |
630: | { |
631: | return []; |
632: | } |
633: | |
634: | public function toPhpDocNode(): TypeNode |
635: | { |
636: | if ($this->isCommonCallable) { |
637: | return new IdentifierTypeNode($this->isPure()->yes() ? 'pure-callable' : 'callable'); |
638: | } |
639: | |
640: | $parameters = []; |
641: | foreach ($this->parameters as $parameter) { |
642: | $parameters[] = new CallableTypeParameterNode( |
643: | $parameter->getType()->toPhpDocNode(), |
644: | !$parameter->passedByReference()->no(), |
645: | $parameter->isVariadic(), |
646: | $parameter->getName() === '' ? '' : '$' . $parameter->getName(), |
647: | $parameter->isOptional(), |
648: | ); |
649: | } |
650: | |
651: | $templateTags = []; |
652: | foreach ($this->templateTags as $templateName => $templateTag) { |
653: | $templateTags[] = new TemplateTagValueNode( |
654: | $templateName, |
655: | $templateTag->getBound()->toPhpDocNode(), |
656: | '', |
657: | ); |
658: | } |
659: | |
660: | return new CallableTypeNode( |
661: | new IdentifierTypeNode($this->isPure->yes() ? 'pure-callable' : 'callable'), |
662: | $parameters, |
663: | $this->returnType->toPhpDocNode(), |
664: | $templateTags, |
665: | ); |
666: | } |
667: | |
668: | |
669: | |
670: | |
671: | public static function __set_state(array $properties): Type |
672: | { |
673: | return new self( |
674: | (bool) $properties['isCommonCallable'] ? null : $properties['parameters'], |
675: | (bool) $properties['isCommonCallable'] ? null : $properties['returnType'], |
676: | $properties['variadic'], |
677: | $properties['templateTypeMap'], |
678: | $properties['resolvedTemplateTypeMap'], |
679: | $properties['templateTags'], |
680: | $properties['isPure'], |
681: | ); |
682: | } |
683: | |
684: | } |
685: | |