1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use Closure;
6: use PhpParser\Node\Identifier;
7: use PHPStan\Analyser\OutOfClassScope;
8: use PHPStan\Php\PhpVersion;
9: use PHPStan\PhpDoc\Tag\TemplateTag;
10: use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
11: use PHPStan\PhpDocParser\Ast\Type\CallableTypeNode;
12: use PHPStan\PhpDocParser\Ast\Type\CallableTypeParameterNode;
13: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
14: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
15: use PHPStan\Reflection\Assertions;
16: use PHPStan\Reflection\Callables\CallableParametersAcceptor;
17: use PHPStan\Reflection\Callables\SimpleImpurePoint;
18: use PHPStan\Reflection\Callables\SimpleThrowPoint;
19: use PHPStan\Reflection\ClassMemberAccessAnswerer;
20: use PHPStan\Reflection\ExtendedParameterReflection;
21: use PHPStan\Reflection\Native\NativeParameterReflection;
22: use PHPStan\Reflection\ParameterReflection;
23: use PHPStan\Reflection\ParametersAcceptor;
24: use PHPStan\Reflection\ParametersAcceptorSelector;
25: use PHPStan\Reflection\PassedByReference;
26: use PHPStan\Reflection\Php\DummyParameter;
27: use PHPStan\ShouldNotHappenException;
28: use PHPStan\TrinaryLogic;
29: use PHPStan\Turbo\ShadowedByTurboExtension;
30: use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
31: use PHPStan\Type\Enum\EnumCaseObjectType;
32: use PHPStan\Type\Generic\TemplateType;
33: use PHPStan\Type\Generic\TemplateTypeHelper;
34: use PHPStan\Type\Generic\TemplateTypeMap;
35: use PHPStan\Type\Generic\TemplateTypeVariance;
36: use PHPStan\Type\Generic\TemplateTypeVarianceMap;
37: use PHPStan\Type\Traits\MaybeArrayTypeTrait;
38: use PHPStan\Type\Traits\MaybeIterableTypeTrait;
39: use PHPStan\Type\Traits\MaybeObjectTypeTrait;
40: use PHPStan\Type\Traits\MaybeOffsetAccessibleTypeTrait;
41: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
42: use PHPStan\Type\Traits\NonRemoveableTypeTrait;
43: use PHPStan\Type\Traits\OperatorTypeTrait;
44: use PHPStan\Type\Traits\TruthyBooleanTypeTrait;
45: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
46: use function array_key_exists;
47: use function array_map;
48: use function array_merge;
49: use function count;
50:
51: /** @api */
52: #[InstanceofDeprecated(insteadUse: 'Type::isCallable() and Type::getCallableParametersAcceptors()')]
53: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/CallableType.cpp')]
54: class CallableType implements CompoundType, CallableParametersAcceptor
55: {
56:
57: use MaybeArrayTypeTrait;
58: use MaybeIterableTypeTrait;
59: use MaybeObjectTypeTrait;
60: use MaybeOffsetAccessibleTypeTrait;
61: use TruthyBooleanTypeTrait;
62: use UndecidedComparisonCompoundTypeTrait;
63: use NonRemoveableTypeTrait;
64: use NonGeneralizableTypeTrait;
65: use OperatorTypeTrait;
66:
67: /** @var list<ParameterReflection> */
68: private array $parameters;
69:
70: private Type $returnType;
71:
72: private bool $isCommonCallable;
73:
74: private TemplateTypeMap $templateTypeMap;
75:
76: private TemplateTypeMap $resolvedTemplateTypeMap;
77:
78: private TrinaryLogic $isPure;
79:
80: private Assertions $assertions;
81:
82: /** @var array<int, string> */
83: private array $cachedDescriptions = [];
84:
85: /**
86: * @api
87: * @param list<ParameterReflection>|null $parameters
88: * @param array<non-empty-string, TemplateTag> $templateTags
89: */
90: public function __construct(
91: ?array $parameters = null,
92: ?Type $returnType = null,
93: private bool $variadic = true,
94: ?TemplateTypeMap $templateTypeMap = null,
95: ?TemplateTypeMap $resolvedTemplateTypeMap = null,
96: private array $templateTags = [],
97: ?TrinaryLogic $isPure = null,
98: ?Assertions $assertions = null,
99: )
100: {
101: $this->parameters = $parameters ?? [];
102: $this->returnType = $returnType ?? new MixedType();
103: $this->isCommonCallable = $parameters === null && $returnType === null;
104: $this->templateTypeMap = $templateTypeMap ?? TemplateTypeMap::createEmpty();
105: $this->resolvedTemplateTypeMap = $resolvedTemplateTypeMap ?? TemplateTypeMap::createEmpty();
106: $this->isPure = $isPure ?? TrinaryLogic::createMaybe();
107: $this->assertions = $assertions ?? Assertions::createEmpty();
108: }
109:
110: /**
111: * @return array<non-empty-string, TemplateTag>
112: */
113: public function getTemplateTags(): array
114: {
115: return $this->templateTags;
116: }
117:
118: public function isPure(): TrinaryLogic
119: {
120: return $this->isPure;
121: }
122:
123: public function getReferencedClasses(): array
124: {
125: $classes = [];
126: foreach ($this->parameters as $parameter) {
127: $classes = array_merge($classes, $parameter->getType()->getReferencedClasses());
128: }
129: foreach ($this->assertions->getAll() as $assertTag) {
130: $classes = array_merge($classes, $assertTag->getType()->getReferencedClasses());
131: }
132:
133: return array_merge($classes, $this->returnType->getReferencedClasses());
134: }
135:
136: public function getObjectClassNames(): array
137: {
138: return [];
139: }
140:
141: public function getObjectClassReflections(): array
142: {
143: return [];
144: }
145:
146: public function getConstantStrings(): array
147: {
148: return [];
149: }
150:
151: public function accepts(Type $type, bool $strictTypes): AcceptsResult
152: {
153: if ($type instanceof CompoundType && !$type instanceof self) {
154: return $type->isAcceptedBy($this, $strictTypes);
155: }
156:
157: return $this->isSuperTypeOfInternal($type, true, $strictTypes)->toAcceptsResult();
158: }
159:
160: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
161: {
162: if ($type instanceof CompoundType && !$type instanceof self) {
163: return $type->isSubTypeOf($this);
164: }
165:
166: return $this->isSuperTypeOfInternal($type, false);
167: }
168:
169: private function isSuperTypeOfInternal(Type $type, bool $treatMixedAsAny, bool $strictTypes = true): IsSuperTypeOfResult
170: {
171: $isCallable = IsSuperTypeOfResult::createFromTrinaryLogic($type->isCallable());
172: if ($isCallable->no()) {
173: return $isCallable;
174: }
175:
176: static $scope;
177: if ($scope === null) {
178: $scope = new OutOfClassScope();
179: }
180:
181: if ($this->isCommonCallable) {
182: if ($this->isPure()->yes()) {
183: $typePure = TrinaryLogic::createYes();
184: foreach ($type->getCallableParametersAcceptors($scope) as $variant) {
185: $typePure = $typePure->and($variant->isPure());
186: }
187:
188: return $isCallable->and(IsSuperTypeOfResult::createFromTrinaryLogic($typePure));
189: }
190:
191: return $isCallable;
192: }
193:
194: $parameterTypes = array_map(static fn ($parameter) => $parameter->getType(), $this->getParameters());
195:
196: $variantsResult = null;
197: foreach ($type->getCallableParametersAcceptors($scope) as $variant) {
198: $variant = ParametersAcceptorSelector::selectFromTypes($parameterTypes, [$variant], false);
199: if (!$variant instanceof CallableParametersAcceptor) {
200: return IsSuperTypeOfResult::createNo([]);
201: }
202: $isSuperType = CallableTypeHelper::isParametersAcceptorSuperTypeOf($this, $variant, $treatMixedAsAny, $strictTypes);
203: if ($variantsResult === null) {
204: $variantsResult = $isSuperType;
205: } else {
206: $variantsResult = $variantsResult->or($isSuperType);
207: }
208: }
209:
210: if ($variantsResult === null) {
211: throw new ShouldNotHappenException();
212: }
213:
214: return $isCallable->and($variantsResult);
215: }
216:
217: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
218: {
219: if ($otherType instanceof IntersectionType || $otherType instanceof UnionType) {
220: return $otherType->isSuperTypeOf($this);
221: }
222:
223: return IsSuperTypeOfResult::createFromTrinaryLogic(
224: $otherType->isCallable()->and($otherType instanceof self ? TrinaryLogic::createYes() : TrinaryLogic::createMaybe()),
225: );
226: }
227:
228: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
229: {
230: return $this->isSubTypeOf($acceptingType)->toAcceptsResult();
231: }
232:
233: public function equals(Type $type): bool
234: {
235: if (!$type instanceof self) {
236: return false;
237: }
238:
239: if ($this->isCommonCallable) {
240: if (!$type->isCommonCallable) {
241: return false;
242: }
243: } elseif ($type->isCommonCallable) {
244: return false;
245: }
246:
247: if ($this->variadic !== $type->variadic) {
248: return false;
249: }
250:
251: if ($this->isPure !== $type->isPure) {
252: return false;
253: }
254:
255: if (!$this->returnType->equals($type->returnType)) {
256: return false;
257: }
258:
259: if (!CallableAssertionsHelper::assertionsEqual($this->assertions, $type->assertions)) {
260: return false;
261: }
262:
263: if (count($this->parameters) !== count($type->parameters)) {
264: return false;
265: }
266:
267: foreach ($this->parameters as $i => $parameter) {
268: $otherParameter = $type->parameters[$i];
269: if ($parameter->isOptional() !== $otherParameter->isOptional()) {
270: return false;
271: }
272: if (!$parameter->passedByReference()->equals($otherParameter->passedByReference())) {
273: return false;
274: }
275: if ($parameter->isVariadic() !== $otherParameter->isVariadic()) {
276: return false;
277: }
278: if (!$parameter->getType()->equals($otherParameter->getType())) {
279: return false;
280: }
281: if ($parameter->getDefaultValue() !== null) {
282: if ($otherParameter->getDefaultValue() === null) {
283: return false;
284: }
285:
286: return $parameter->getDefaultValue()->equals($otherParameter->getDefaultValue());
287: } elseif ($otherParameter->getDefaultValue() !== null) {
288: return false;
289: }
290: }
291:
292: foreach ([[$this->templateTypeMap, $type->templateTypeMap], [$this->resolvedTemplateTypeMap, $type->resolvedTemplateTypeMap]] as [$templateTypeMap, $otherTemplateTypeMap]) {
293: if ($templateTypeMap->count() !== $otherTemplateTypeMap->count()) {
294: return false;
295: }
296:
297: foreach ($templateTypeMap->getTypes() as $typeName => $templateType) {
298: $otherTemplateType = $otherTemplateTypeMap->getType($typeName);
299: if ($otherTemplateType === null) {
300: return false;
301: }
302:
303: if (!$templateType->equals($otherTemplateType)) {
304: return false;
305: }
306: }
307: }
308:
309: foreach ($this->templateTags as $tagName => $tag) {
310: if (!array_key_exists($tagName, $type->templateTags)) {
311: return false;
312: }
313:
314: $otherTag = $type->templateTags[$tagName];
315: if ($tag->getName() !== $otherTag->getName()) {
316: return false;
317: }
318:
319: if (!$tag->getBound()->equals($otherTag->getBound())) {
320: return false;
321: }
322: if (!$tag->getVariance()->equals($otherTag->getVariance())) {
323: return false;
324: }
325: if ($tag->getDefault() !== null) {
326: if ($otherTag->getDefault() === null) {
327: return false;
328: }
329:
330: return $tag->getDefault()->equals($otherTag->getDefault());
331: } elseif ($otherTag->getDefault() !== null) {
332: return false;
333: }
334: }
335:
336: return true;
337: }
338:
339: public function describe(VerbosityLevel $level): string
340: {
341: return $this->cachedDescriptions[$level->getLevelValue()] ??= $level->handle(
342: static fn (): string => 'callable',
343: function (): string {
344: $assertedParameterNames = [];
345: foreach ($this->assertions->getAll() as $assertTag) {
346: $assertedParameterNames[$assertTag->getParameter()->getParameterName()] = true;
347: }
348: $selfWithoutParameterNames = new self(
349: array_map(static fn (ParameterReflection $p): ParameterReflection => new DummyParameter(
350: array_key_exists('$' . $p->getName(), $assertedParameterNames) ? $p->getName() : '',
351: $p->getType(),
352: optional: $p->isOptional() && !$p->isVariadic(),
353: passedByReference: PassedByReference::createNo(),
354: variadic: $p->isVariadic(),
355: defaultValue: $p->getDefaultValue(),
356: ), $this->parameters),
357: $this->returnType,
358: $this->variadic,
359: $this->templateTypeMap,
360: $this->resolvedTemplateTypeMap,
361: $this->templateTags,
362: $this->isPure,
363: $this->assertions,
364: );
365:
366: return TypeNodeDescriber::describe($selfWithoutParameterNames->toPhpDocNode());
367: },
368: );
369: }
370:
371: public function isCallable(): TrinaryLogic
372: {
373: return TrinaryLogic::createYes();
374: }
375:
376: public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
377: {
378: return [$this];
379: }
380:
381: public function getThrowPoints(): array
382: {
383: return [
384: SimpleThrowPoint::createImplicit(),
385: ];
386: }
387:
388: public function getImpurePoints(): array
389: {
390: $pure = $this->isPure();
391: if ($pure->yes()) {
392: return [];
393: }
394:
395: return [
396: new SimpleImpurePoint(
397: 'functionCall',
398: 'call to a callable',
399: $pure->no(),
400: ),
401: ];
402: }
403:
404: public function getInvalidateExpressions(): array
405: {
406: return [];
407: }
408:
409: public function getUsedVariables(): array
410: {
411: return [];
412: }
413:
414: public function acceptsNamedArguments(): TrinaryLogic
415: {
416: return TrinaryLogic::createYes();
417: }
418:
419: public function mustUseReturnValue(): TrinaryLogic
420: {
421: return TrinaryLogic::createMaybe();
422: }
423:
424: public function getAsserts(): Assertions
425: {
426: return $this->assertions;
427: }
428:
429: public function isStaticClosure(): TrinaryLogic
430: {
431: return TrinaryLogic::createMaybe();
432: }
433:
434: public function toNumber(): Type
435: {
436: return new ErrorType();
437: }
438:
439: public function toBitwiseNotType(): Type
440: {
441: return new ErrorType();
442: }
443:
444: public function toAbsoluteNumber(): Type
445: {
446: return new ErrorType();
447: }
448:
449: public function toString(): Type
450: {
451: return new ErrorType();
452: }
453:
454: public function toInteger(): Type
455: {
456: return new ErrorType();
457: }
458:
459: public function toFloat(): Type
460: {
461: return new ErrorType();
462: }
463:
464: public function toArray(): Type
465: {
466: return new ArrayType(new MixedType(), new MixedType());
467: }
468:
469: public function toArrayKey(): Type
470: {
471: return new ErrorType();
472: }
473:
474: public function toCoercedArgumentType(bool $strictTypes): Type
475: {
476: return TypeCombinator::union(
477: $this,
478: new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]),
479: new ArrayType(new MixedType(true), new MixedType(true)),
480: new ObjectType(Closure::class),
481: );
482: }
483:
484: public function getTemplateTypeMap(): TemplateTypeMap
485: {
486: return $this->templateTypeMap;
487: }
488:
489: public function getResolvedTemplateTypeMap(): TemplateTypeMap
490: {
491: return $this->resolvedTemplateTypeMap;
492: }
493:
494: public function getCallSiteVarianceMap(): TemplateTypeVarianceMap
495: {
496: return TemplateTypeVarianceMap::createEmpty();
497: }
498:
499: /**
500: * @return list<ParameterReflection>
501: */
502: public function getParameters(): array
503: {
504: return $this->parameters;
505: }
506:
507: public function isVariadic(): bool
508: {
509: return $this->variadic;
510: }
511:
512: public function getReturnType(): Type
513: {
514: return $this->returnType;
515: }
516:
517: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
518: {
519: if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) {
520: return $receivedType->inferTemplateTypesOn($this);
521: }
522:
523: if (! $receivedType->isCallable()->yes()) {
524: return TemplateTypeMap::createEmpty();
525: }
526:
527: $parametersAcceptors = $receivedType->getCallableParametersAcceptors(new OutOfClassScope());
528:
529: $typeMap = TemplateTypeMap::createEmpty();
530:
531: foreach ($parametersAcceptors as $parametersAcceptor) {
532: $typeMap = $typeMap->union($this->inferTemplateTypesOnParametersAcceptor($parametersAcceptor));
533: }
534:
535: return $typeMap;
536: }
537:
538: private function inferTemplateTypesOnParametersAcceptor(ParametersAcceptor $parametersAcceptor): TemplateTypeMap
539: {
540: $parameterTypes = array_map(static fn ($parameter) => $parameter->getType(), $this->getParameters());
541: $parametersAcceptor = ParametersAcceptorSelector::selectFromTypes($parameterTypes, [$parametersAcceptor], false);
542: $args = $parametersAcceptor->getParameters();
543: $returnType = $parametersAcceptor->getReturnType();
544:
545: $typeMap = TemplateTypeMap::createEmpty();
546: foreach ($this->getParameters() as $i => $param) {
547: $paramType = $param->getType();
548: if (isset($args[$i])) {
549: $argType = $args[$i]->getType();
550: } elseif ($paramType instanceof TemplateType) {
551: $argType = TemplateTypeHelper::resolveToBounds($paramType);
552: } else {
553: $argType = new NeverType();
554: }
555:
556: $typeMap = $typeMap->union($paramType->inferTemplateTypes($argType)->convertToLowerBoundTypes());
557: }
558:
559: $typeMap = $typeMap->union(CallableAssertionsHelper::inferTemplateTypesOnAsserts($this, $parametersAcceptor));
560:
561: return $typeMap->union($this->getReturnType()->inferTemplateTypes($returnType));
562: }
563:
564: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
565: {
566: $references = $this->getReturnType()->getReferencedTemplateTypes(
567: $positionVariance->compose(TemplateTypeVariance::createCovariant()),
568: );
569:
570: foreach ($this->assertions->getAll() as $assertTag) {
571: foreach ($assertTag->getType()->getReferencedTemplateTypes($positionVariance->compose(TemplateTypeVariance::createCovariant())) as $reference) {
572: $references[] = $reference;
573: }
574: }
575:
576: $paramVariance = $positionVariance->compose(TemplateTypeVariance::createContravariant());
577:
578: foreach ($this->getParameters() as $param) {
579: foreach ($param->getType()->getReferencedTemplateTypes($paramVariance) as $reference) {
580: $references[] = $reference;
581: }
582: }
583:
584: return $references;
585: }
586:
587: public function traverse(callable $cb): Type
588: {
589: if ($this->isCommonCallable) {
590: return $this;
591: }
592:
593: $parameters = array_map(static function (ParameterReflection $param) use ($cb): NativeParameterReflection {
594: $defaultValue = $param->getDefaultValue();
595: return new NativeParameterReflection(
596: $param->getName(),
597: $param->isOptional(),
598: $cb($param->getType()),
599: $param->passedByReference(),
600: $param->isVariadic(),
601: $defaultValue !== null ? $cb($defaultValue) : null,
602: );
603: }, $this->getParameters());
604:
605: return new self(
606: $parameters,
607: $cb($this->getReturnType()),
608: $this->isVariadic(),
609: $this->templateTypeMap,
610: $this->resolvedTemplateTypeMap,
611: $this->templateTags,
612: $this->isPure,
613: $this->assertions->mapTypes($cb),
614: );
615: }
616:
617: public function traverseSimultaneously(Type $right, callable $cb): Type
618: {
619: if ($this->isCommonCallable) {
620: return $this;
621: }
622:
623: if (!$right->isCallable()->yes()) {
624: return $this;
625: }
626:
627: $rightAcceptors = $right->getCallableParametersAcceptors(new OutOfClassScope());
628: if (count($rightAcceptors) !== 1) {
629: return $this;
630: }
631:
632: $rightParameters = $rightAcceptors[0]->getParameters();
633: if (count($this->getParameters()) !== count($rightParameters)) {
634: return $this;
635: }
636:
637: $parameters = [];
638: foreach ($this->getParameters() as $i => $leftParam) {
639: $rightParam = $rightParameters[$i];
640: $leftDefaultValue = $leftParam->getDefaultValue();
641: $rightDefaultValue = $rightParam->getDefaultValue();
642: $defaultValue = $leftDefaultValue;
643: if ($leftDefaultValue !== null && $rightDefaultValue !== null) {
644: $defaultValue = $cb($leftDefaultValue, $rightDefaultValue);
645: }
646: $parameters[] = new NativeParameterReflection(
647: $leftParam->getName(),
648: $leftParam->isOptional(),
649: $cb($leftParam->getType(), $rightParam->getType()),
650: $leftParam->passedByReference(),
651: $leftParam->isVariadic(),
652: $defaultValue,
653: );
654: }
655:
656: return new self(
657: $parameters,
658: $cb($this->getReturnType(), $rightAcceptors[0]->getReturnType()),
659: $this->isVariadic(),
660: $this->templateTypeMap,
661: $this->resolvedTemplateTypeMap,
662: $this->templateTags,
663: $this->isPure,
664: $this->assertions,
665: );
666: }
667:
668: public function isOversizedArray(): TrinaryLogic
669: {
670: return TrinaryLogic::createNo();
671: }
672:
673: public function isNull(): TrinaryLogic
674: {
675: return TrinaryLogic::createNo();
676: }
677:
678: public function isConstantValue(): TrinaryLogic
679: {
680: return TrinaryLogic::createNo();
681: }
682:
683: public function isConstantScalarValue(): TrinaryLogic
684: {
685: return TrinaryLogic::createNo();
686: }
687:
688: public function getConstantScalarTypes(): array
689: {
690: return [];
691: }
692:
693: public function getConstantScalarValues(): array
694: {
695: return [];
696: }
697:
698: public function isTrue(): TrinaryLogic
699: {
700: return TrinaryLogic::createNo();
701: }
702:
703: public function isFalse(): TrinaryLogic
704: {
705: return TrinaryLogic::createNo();
706: }
707:
708: public function isBoolean(): TrinaryLogic
709: {
710: return TrinaryLogic::createNo();
711: }
712:
713: public function isFloat(): TrinaryLogic
714: {
715: return TrinaryLogic::createNo();
716: }
717:
718: public function isInteger(): TrinaryLogic
719: {
720: return TrinaryLogic::createNo();
721: }
722:
723: public function isString(): TrinaryLogic
724: {
725: return TrinaryLogic::createMaybe();
726: }
727:
728: public function isNumericString(): TrinaryLogic
729: {
730: return TrinaryLogic::createNo();
731: }
732:
733: public function isDecimalIntegerString(): TrinaryLogic
734: {
735: return TrinaryLogic::createNo();
736: }
737:
738: public function isNonEmptyString(): TrinaryLogic
739: {
740: return TrinaryLogic::createMaybe();
741: }
742:
743: public function isNonFalsyString(): TrinaryLogic
744: {
745: return TrinaryLogic::createMaybe();
746: }
747:
748: public function isLiteralString(): TrinaryLogic
749: {
750: return TrinaryLogic::createMaybe();
751: }
752:
753: public function isLowercaseString(): TrinaryLogic
754: {
755: return TrinaryLogic::createMaybe();
756: }
757:
758: public function isClassString(): TrinaryLogic
759: {
760: return TrinaryLogic::createMaybe();
761: }
762:
763: public function isUppercaseString(): TrinaryLogic
764: {
765: return TrinaryLogic::createMaybe();
766: }
767:
768: public function getClassStringObjectType(): Type
769: {
770: return new ObjectWithoutClassType();
771: }
772:
773: public function getObjectTypeOrClassStringObjectType(): Type
774: {
775: return new ObjectWithoutClassType();
776: }
777:
778: public function isVoid(): TrinaryLogic
779: {
780: return TrinaryLogic::createNo();
781: }
782:
783: public function isScalar(): TrinaryLogic
784: {
785: return TrinaryLogic::createMaybe();
786: }
787:
788: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
789: {
790: return new BooleanType();
791: }
792:
793: public function getEnumCases(): array
794: {
795: return [];
796: }
797:
798: public function getEnumCaseObject(): ?EnumCaseObjectType
799: {
800: return null;
801: }
802:
803: public function isCommonCallable(): bool
804: {
805: return $this->isCommonCallable;
806: }
807:
808: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
809: {
810: return new ErrorType();
811: }
812:
813: public function getFiniteTypes(): array
814: {
815: return [];
816: }
817:
818: public function toPhpDocNode(): TypeNode
819: {
820: if ($this->isCommonCallable) {
821: return new IdentifierTypeNode($this->isPure()->yes() ? 'pure-callable' : 'callable');
822: }
823:
824: $parameters = [];
825: foreach ($this->parameters as $parameter) {
826: $parameters[] = new CallableTypeParameterNode(
827: $parameter->getType()->toPhpDocNode(),
828: !$parameter->passedByReference()->no(),
829: $parameter->isVariadic(),
830: $parameter->getName() === '' ? '' : '$' . $parameter->getName(),
831: $parameter->isOptional(),
832: );
833: }
834:
835: $templateTags = [];
836: foreach ($this->templateTags as $templateName => $templateTag) {
837: $templateTags[] = new TemplateTagValueNode(
838: $templateName,
839: $templateTag->getBound()->toPhpDocNode(),
840: '',
841: );
842: }
843:
844: $returnTypeNode = CallableAssertionsHelper::toConditionalReturnTypeNode($this->assertions, $this->parameters, $this->returnType)
845: ?? $this->returnType->toPhpDocNode();
846:
847: return new CallableTypeNode(
848: new IdentifierTypeNode($this->isPure->yes() ? 'pure-callable' : 'callable'),
849: $parameters,
850: $returnTypeNode,
851: $templateTags,
852: );
853: }
854:
855: public function toNativePhpTypeNode(NativeTypePosition $position)
856: {
857: return $position->isCallableAllowed() ? new Identifier('callable') : null;
858: }
859:
860: public function hasTemplateOrLateResolvableType(): bool
861: {
862: foreach ($this->parameters as $parameter) {
863: if ($parameter->getType()->hasTemplateOrLateResolvableType()) {
864: return true;
865: }
866:
867: if (!$parameter instanceof ExtendedParameterReflection) {
868: continue;
869: }
870:
871: if ($parameter->getOutType() !== null && $parameter->getOutType()->hasTemplateOrLateResolvableType()) {
872: return true;
873: }
874: if ($parameter->getClosureThisType() !== null && $parameter->getClosureThisType()->hasTemplateOrLateResolvableType()) {
875: return true;
876: }
877: }
878:
879: foreach ($this->assertions->getAll() as $assertTag) {
880: if ($assertTag->getType()->hasTemplateOrLateResolvableType()) {
881: return true;
882: }
883: }
884:
885: return $this->getReturnType()->hasTemplateOrLateResolvableType();
886: }
887:
888: public function toValueSet(): ValueSet
889: {
890: return ValueSet::createInvalid();
891: }
892:
893: }
894: