1: | <?php declare(strict_types = 1); |
2: | |
3: | namespace PHPStan\Type; |
4: | |
5: | use PHPStan\Php\PhpVersion; |
6: | use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode; |
7: | use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode; |
8: | use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
9: | use PHPStan\PhpDocParser\Ast\Type\IntersectionTypeNode; |
10: | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
11: | use PHPStan\Reflection\ClassConstantReflection; |
12: | use PHPStan\Reflection\ClassMemberAccessAnswerer; |
13: | use PHPStan\Reflection\ExtendedMethodReflection; |
14: | use PHPStan\Reflection\ExtendedPropertyReflection; |
15: | use PHPStan\Reflection\InitializerExprTypeResolver; |
16: | use PHPStan\Reflection\TrivialParametersAcceptor; |
17: | use PHPStan\Reflection\Type\IntersectionTypeUnresolvedMethodPrototypeReflection; |
18: | use PHPStan\Reflection\Type\IntersectionTypeUnresolvedPropertyPrototypeReflection; |
19: | use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection; |
20: | use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection; |
21: | use PHPStan\ShouldNotHappenException; |
22: | use PHPStan\TrinaryLogic; |
23: | use PHPStan\Type\Accessory\AccessoryArrayListType; |
24: | use PHPStan\Type\Accessory\AccessoryLiteralStringType; |
25: | use PHPStan\Type\Accessory\AccessoryLowercaseStringType; |
26: | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
27: | use PHPStan\Type\Accessory\AccessoryNonFalsyStringType; |
28: | use PHPStan\Type\Accessory\AccessoryNumericStringType; |
29: | use PHPStan\Type\Accessory\AccessoryType; |
30: | use PHPStan\Type\Accessory\AccessoryUppercaseStringType; |
31: | use PHPStan\Type\Accessory\HasOffsetType; |
32: | use PHPStan\Type\Accessory\HasOffsetValueType; |
33: | use PHPStan\Type\Accessory\NonEmptyArrayType; |
34: | use PHPStan\Type\Constant\ConstantArrayType; |
35: | use PHPStan\Type\Constant\ConstantIntegerType; |
36: | use PHPStan\Type\Constant\ConstantStringType; |
37: | use PHPStan\Type\Generic\TemplateType; |
38: | use PHPStan\Type\Generic\TemplateTypeMap; |
39: | use PHPStan\Type\Generic\TemplateTypeVariance; |
40: | use PHPStan\Type\Traits\NonGeneralizableTypeTrait; |
41: | use PHPStan\Type\Traits\NonRemoveableTypeTrait; |
42: | use function array_intersect_key; |
43: | use function array_map; |
44: | use function array_shift; |
45: | use function array_unique; |
46: | use function array_values; |
47: | use function count; |
48: | use function implode; |
49: | use function in_array; |
50: | use function is_int; |
51: | use function ksort; |
52: | use function md5; |
53: | use function sprintf; |
54: | use function strcasecmp; |
55: | use function strlen; |
56: | use function substr; |
57: | use function usort; |
58: | |
59: | |
60: | class IntersectionType implements CompoundType |
61: | { |
62: | |
63: | use NonRemoveableTypeTrait; |
64: | use NonGeneralizableTypeTrait; |
65: | |
66: | private bool $sortedTypes = false; |
67: | |
68: | |
69: | |
70: | |
71: | |
72: | public function __construct(private array $types) |
73: | { |
74: | if (count($types) < 2) { |
75: | throw new ShouldNotHappenException(sprintf( |
76: | 'Cannot create %s with: %s', |
77: | self::class, |
78: | implode(', ', array_map(static fn (Type $type): string => $type->describe(VerbosityLevel::value()), $types)), |
79: | )); |
80: | } |
81: | } |
82: | |
83: | |
84: | |
85: | |
86: | public function getTypes(): array |
87: | { |
88: | return $this->types; |
89: | } |
90: | |
91: | |
92: | |
93: | |
94: | private function getSortedTypes(): array |
95: | { |
96: | if ($this->sortedTypes) { |
97: | return $this->types; |
98: | } |
99: | |
100: | $this->types = UnionTypeHelper::sortTypes($this->types); |
101: | $this->sortedTypes = true; |
102: | |
103: | return $this->types; |
104: | } |
105: | |
106: | public function inferTemplateTypesOn(Type $templateType): TemplateTypeMap |
107: | { |
108: | $types = TemplateTypeMap::createEmpty(); |
109: | |
110: | foreach ($this->types as $type) { |
111: | $types = $types->intersect($templateType->inferTemplateTypes($type)); |
112: | } |
113: | |
114: | return $types; |
115: | } |
116: | |
117: | public function getReferencedClasses(): array |
118: | { |
119: | $classes = []; |
120: | foreach ($this->types as $type) { |
121: | foreach ($type->getReferencedClasses() as $className) { |
122: | $classes[] = $className; |
123: | } |
124: | } |
125: | |
126: | return $classes; |
127: | } |
128: | |
129: | public function getObjectClassNames(): array |
130: | { |
131: | $objectClassNames = []; |
132: | foreach ($this->types as $type) { |
133: | $innerObjectClassNames = $type->getObjectClassNames(); |
134: | foreach ($innerObjectClassNames as $innerObjectClassName) { |
135: | $objectClassNames[] = $innerObjectClassName; |
136: | } |
137: | } |
138: | |
139: | return array_values(array_unique($objectClassNames)); |
140: | } |
141: | |
142: | public function getObjectClassReflections(): array |
143: | { |
144: | $reflections = []; |
145: | foreach ($this->types as $type) { |
146: | foreach ($type->getObjectClassReflections() as $reflection) { |
147: | $reflections[] = $reflection; |
148: | } |
149: | } |
150: | |
151: | return $reflections; |
152: | } |
153: | |
154: | public function getArrays(): array |
155: | { |
156: | $arrays = []; |
157: | foreach ($this->types as $type) { |
158: | foreach ($type->getArrays() as $array) { |
159: | $arrays[] = $array; |
160: | } |
161: | } |
162: | |
163: | return $arrays; |
164: | } |
165: | |
166: | public function getConstantArrays(): array |
167: | { |
168: | $constantArrays = []; |
169: | foreach ($this->types as $type) { |
170: | foreach ($type->getConstantArrays() as $constantArray) { |
171: | $constantArrays[] = $constantArray; |
172: | } |
173: | } |
174: | |
175: | return $constantArrays; |
176: | } |
177: | |
178: | public function getConstantStrings(): array |
179: | { |
180: | $strings = []; |
181: | foreach ($this->types as $type) { |
182: | foreach ($type->getConstantStrings() as $string) { |
183: | $strings[] = $string; |
184: | } |
185: | } |
186: | |
187: | return $strings; |
188: | } |
189: | |
190: | public function accepts(Type $otherType, bool $strictTypes): AcceptsResult |
191: | { |
192: | $result = AcceptsResult::createYes(); |
193: | foreach ($this->types as $type) { |
194: | $result = $result->and($type->accepts($otherType, $strictTypes)); |
195: | } |
196: | |
197: | if (!$result->yes()) { |
198: | $isList = $otherType->isList(); |
199: | $reasons = $result->reasons; |
200: | $verbosity = VerbosityLevel::getRecommendedLevelByType($this, $otherType); |
201: | if ($this->isList()->yes() && !$isList->yes()) { |
202: | $reasons[] = sprintf( |
203: | '%s %s a list.', |
204: | $otherType->describe($verbosity), |
205: | $isList->no() ? 'is not' : 'might not be', |
206: | ); |
207: | } |
208: | |
209: | $isNonEmpty = $otherType->isIterableAtLeastOnce(); |
210: | if ($this->isIterableAtLeastOnce()->yes() && !$isNonEmpty->yes()) { |
211: | $reasons[] = sprintf( |
212: | '%s %s empty.', |
213: | $otherType->describe($verbosity), |
214: | $isNonEmpty->no() ? 'is' : 'might be', |
215: | ); |
216: | } |
217: | |
218: | if (count($reasons) > 0) { |
219: | return new AcceptsResult($result->result, $reasons); |
220: | } |
221: | } |
222: | |
223: | return $result; |
224: | } |
225: | |
226: | public function isSuperTypeOf(Type $otherType): IsSuperTypeOfResult |
227: | { |
228: | if ($otherType instanceof IntersectionType && $this->equals($otherType)) { |
229: | return IsSuperTypeOfResult::createYes(); |
230: | } |
231: | |
232: | if ($otherType instanceof NeverType) { |
233: | return IsSuperTypeOfResult::createYes(); |
234: | } |
235: | |
236: | return IsSuperTypeOfResult::createYes()->and(...array_map(static fn (Type $innerType) => $innerType->isSuperTypeOf($otherType), $this->types)); |
237: | } |
238: | |
239: | public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult |
240: | { |
241: | if (($otherType instanceof self || $otherType instanceof UnionType) && !$otherType instanceof TemplateType) { |
242: | return $otherType->isSuperTypeOf($this); |
243: | } |
244: | |
245: | $result = IsSuperTypeOfResult::maxMin(...array_map(static fn (Type $innerType) => $otherType->isSuperTypeOf($innerType), $this->types)); |
246: | if ($this->isOversizedArray()->yes()) { |
247: | if (!$result->no()) { |
248: | return IsSuperTypeOfResult::createYes(); |
249: | } |
250: | } |
251: | |
252: | return $result; |
253: | } |
254: | |
255: | public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult |
256: | { |
257: | $result = AcceptsResult::maxMin(...array_map(static fn (Type $innerType) => $acceptingType->accepts($innerType, $strictTypes), $this->types)); |
258: | if ($this->isOversizedArray()->yes()) { |
259: | if (!$result->no()) { |
260: | return AcceptsResult::createYes(); |
261: | } |
262: | } |
263: | |
264: | return $result; |
265: | } |
266: | |
267: | public function equals(Type $type): bool |
268: | { |
269: | if (!$type instanceof static) { |
270: | return false; |
271: | } |
272: | |
273: | if (count($this->types) !== count($type->types)) { |
274: | return false; |
275: | } |
276: | |
277: | $otherTypes = $type->types; |
278: | foreach ($this->types as $innerType) { |
279: | $match = false; |
280: | foreach ($otherTypes as $i => $otherType) { |
281: | if (!$innerType->equals($otherType)) { |
282: | continue; |
283: | } |
284: | |
285: | $match = true; |
286: | unset($otherTypes[$i]); |
287: | break; |
288: | } |
289: | |
290: | if (!$match) { |
291: | return false; |
292: | } |
293: | } |
294: | |
295: | return count($otherTypes) === 0; |
296: | } |
297: | |
298: | public function describe(VerbosityLevel $level): string |
299: | { |
300: | return $level->handle( |
301: | function () use ($level): string { |
302: | $typeNames = []; |
303: | $isList = $this->isList()->yes(); |
304: | $valueType = null; |
305: | foreach ($this->getSortedTypes() as $type) { |
306: | if ($isList) { |
307: | if ($type instanceof ArrayType || $type instanceof ConstantArrayType) { |
308: | $valueType = $type->getIterableValueType(); |
309: | continue; |
310: | } |
311: | if ($type instanceof NonEmptyArrayType) { |
312: | continue; |
313: | } |
314: | } |
315: | if ($type instanceof AccessoryType) { |
316: | continue; |
317: | } |
318: | $typeNames[] = $type->generalize(GeneralizePrecision::lessSpecific())->describe($level); |
319: | } |
320: | |
321: | if ($isList) { |
322: | $isMixedValueType = $valueType instanceof MixedType && $valueType->describe(VerbosityLevel::precise()) === 'mixed' && !$valueType->isExplicitMixed(); |
323: | $innerType = ''; |
324: | if ($valueType !== null && !$isMixedValueType) { |
325: | $innerType = sprintf('<%s>', $valueType->describe($level)); |
326: | } |
327: | |
328: | $typeNames[] = 'list' . $innerType; |
329: | } |
330: | |
331: | usort($typeNames, static function ($a, $b) { |
332: | $cmp = strcasecmp($a, $b); |
333: | if ($cmp !== 0) { |
334: | return $cmp; |
335: | } |
336: | |
337: | return $a <=> $b; |
338: | }); |
339: | |
340: | return implode('&', $typeNames); |
341: | }, |
342: | fn (): string => $this->describeItself($level, true), |
343: | fn (): string => $this->describeItself($level, false), |
344: | ); |
345: | } |
346: | |
347: | private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes): string |
348: | { |
349: | $baseTypes = []; |
350: | $typesToDescribe = []; |
351: | $skipTypeNames = []; |
352: | |
353: | $nonEmptyStr = false; |
354: | $nonFalsyStr = false; |
355: | $isList = $this->isList()->yes(); |
356: | $isArray = $this->isArray()->yes(); |
357: | $isNonEmptyArray = $this->isIterableAtLeastOnce()->yes(); |
358: | $describedTypes = []; |
359: | foreach ($this->getSortedTypes() as $i => $type) { |
360: | if ($type instanceof AccessoryNonEmptyStringType |
361: | || $type instanceof AccessoryLiteralStringType |
362: | || $type instanceof AccessoryNumericStringType |
363: | || $type instanceof AccessoryNonFalsyStringType |
364: | || $type instanceof AccessoryLowercaseStringType |
365: | || $type instanceof AccessoryUppercaseStringType |
366: | ) { |
367: | if ( |
368: | ($type instanceof AccessoryLowercaseStringType || $type instanceof AccessoryUppercaseStringType) |
369: | && !$level->isPrecise() |
370: | ) { |
371: | continue; |
372: | } |
373: | if ($type instanceof AccessoryNonFalsyStringType) { |
374: | $nonFalsyStr = true; |
375: | } |
376: | if ($type instanceof AccessoryNonEmptyStringType) { |
377: | $nonEmptyStr = true; |
378: | } |
379: | if ($nonEmptyStr && $nonFalsyStr) { |
380: | |
381: | foreach ($typesToDescribe as $key => $typeToDescribe) { |
382: | if (!($typeToDescribe instanceof AccessoryNonEmptyStringType)) { |
383: | continue; |
384: | } |
385: | |
386: | unset($typesToDescribe[$key]); |
387: | } |
388: | } |
389: | |
390: | $typesToDescribe[$i] = $type; |
391: | $skipTypeNames[] = 'string'; |
392: | continue; |
393: | } |
394: | if ($isList || $isArray) { |
395: | if ($type instanceof ArrayType) { |
396: | $keyType = $type->getKeyType(); |
397: | $valueType = $type->getItemType(); |
398: | if ($isList) { |
399: | $isMixedValueType = $valueType instanceof MixedType && $valueType->describe(VerbosityLevel::precise()) === 'mixed' && !$valueType->isExplicitMixed(); |
400: | $valueTypeDescription = ''; |
401: | if (!$isMixedValueType) { |
402: | $valueTypeDescription = sprintf('<%s>', $valueType->describe($level)); |
403: | } |
404: | |
405: | $describedTypes[$i] = ($isNonEmptyArray ? 'non-empty-list' : 'list') . $valueTypeDescription; |
406: | } else { |
407: | $isMixedKeyType = $keyType instanceof MixedType && $keyType->describe(VerbosityLevel::precise()) === 'mixed' && !$keyType->isExplicitMixed(); |
408: | $isMixedValueType = $valueType instanceof MixedType && $valueType->describe(VerbosityLevel::precise()) === 'mixed' && !$valueType->isExplicitMixed(); |
409: | $typeDescription = ''; |
410: | if (!$isMixedKeyType) { |
411: | $typeDescription = sprintf('<%s, %s>', $keyType->describe($level), $valueType->describe($level)); |
412: | } elseif (!$isMixedValueType) { |
413: | $typeDescription = sprintf('<%s>', $valueType->describe($level)); |
414: | } |
415: | |
416: | $describedTypes[$i] = ($isNonEmptyArray ? 'non-empty-array' : 'array') . $typeDescription; |
417: | } |
418: | continue; |
419: | } elseif ($type instanceof ConstantArrayType) { |
420: | $description = $type->describe($level); |
421: | $descriptionWithoutKind = substr($description, strlen('array')); |
422: | $begin = $isList ? 'list' : 'array'; |
423: | if ($isNonEmptyArray && !$type->isIterableAtLeastOnce()->yes()) { |
424: | $begin = 'non-empty-' . $begin; |
425: | } |
426: | |
427: | $describedTypes[$i] = $begin . $descriptionWithoutKind; |
428: | continue; |
429: | } |
430: | if ($type instanceof NonEmptyArrayType || $type instanceof AccessoryArrayListType) { |
431: | continue; |
432: | } |
433: | } |
434: | |
435: | if ($type instanceof CallableType && $type->isCommonCallable()) { |
436: | $typesToDescribe[$i] = $type; |
437: | $skipTypeNames[] = 'object'; |
438: | $skipTypeNames[] = 'string'; |
439: | continue; |
440: | } |
441: | |
442: | if (!$type instanceof AccessoryType) { |
443: | $baseTypes[$i] = $type; |
444: | continue; |
445: | } |
446: | |
447: | if ($skipAccessoryTypes) { |
448: | continue; |
449: | } |
450: | |
451: | $typesToDescribe[$i] = $type; |
452: | } |
453: | |
454: | foreach ($baseTypes as $i => $type) { |
455: | $typeDescription = $type->describe($level); |
456: | |
457: | if (in_array($typeDescription, ['object', 'string'], true) && in_array($typeDescription, $skipTypeNames, true)) { |
458: | foreach ($typesToDescribe as $j => $typeToDescribe) { |
459: | if ($typeToDescribe instanceof CallableType && $typeToDescribe->isCommonCallable()) { |
460: | $describedTypes[$i] = 'callable-' . $typeDescription; |
461: | unset($typesToDescribe[$j]); |
462: | continue 2; |
463: | } |
464: | } |
465: | } |
466: | |
467: | if (in_array($typeDescription, $skipTypeNames, true)) { |
468: | continue; |
469: | } |
470: | |
471: | $describedTypes[$i] = $type->describe($level); |
472: | } |
473: | |
474: | foreach ($typesToDescribe as $i => $typeToDescribe) { |
475: | $describedTypes[$i] = $typeToDescribe->describe($level); |
476: | } |
477: | |
478: | ksort($describedTypes); |
479: | |
480: | return implode('&', $describedTypes); |
481: | } |
482: | |
483: | public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type |
484: | { |
485: | return $this->intersectTypes(static fn (Type $type): Type => $type->getTemplateType($ancestorClassName, $templateTypeName)); |
486: | } |
487: | |
488: | public function isObject(): TrinaryLogic |
489: | { |
490: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isObject()); |
491: | } |
492: | |
493: | public function isEnum(): TrinaryLogic |
494: | { |
495: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isEnum()); |
496: | } |
497: | |
498: | public function canAccessProperties(): TrinaryLogic |
499: | { |
500: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->canAccessProperties()); |
501: | } |
502: | |
503: | public function hasProperty(string $propertyName): TrinaryLogic |
504: | { |
505: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->hasProperty($propertyName)); |
506: | } |
507: | |
508: | public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection |
509: | { |
510: | return $this->getUnresolvedPropertyPrototype($propertyName, $scope)->getTransformedProperty(); |
511: | } |
512: | |
513: | public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection |
514: | { |
515: | $propertyPrototypes = []; |
516: | foreach ($this->types as $type) { |
517: | if (!$type->hasProperty($propertyName)->yes()) { |
518: | continue; |
519: | } |
520: | |
521: | $propertyPrototypes[] = $type->getUnresolvedPropertyPrototype($propertyName, $scope)->withFechedOnType($this); |
522: | } |
523: | |
524: | $propertiesCount = count($propertyPrototypes); |
525: | if ($propertiesCount === 0) { |
526: | throw new ShouldNotHappenException(); |
527: | } |
528: | |
529: | if ($propertiesCount === 1) { |
530: | return $propertyPrototypes[0]; |
531: | } |
532: | |
533: | return new IntersectionTypeUnresolvedPropertyPrototypeReflection($propertyName, $propertyPrototypes); |
534: | } |
535: | |
536: | public function canCallMethods(): TrinaryLogic |
537: | { |
538: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->canCallMethods()); |
539: | } |
540: | |
541: | public function hasMethod(string $methodName): TrinaryLogic |
542: | { |
543: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->hasMethod($methodName)); |
544: | } |
545: | |
546: | public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection |
547: | { |
548: | return $this->getUnresolvedMethodPrototype($methodName, $scope)->getTransformedMethod(); |
549: | } |
550: | |
551: | public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection |
552: | { |
553: | $methodPrototypes = []; |
554: | foreach ($this->types as $type) { |
555: | if (!$type->hasMethod($methodName)->yes()) { |
556: | continue; |
557: | } |
558: | |
559: | $methodPrototypes[] = $type->getUnresolvedMethodPrototype($methodName, $scope)->withCalledOnType($this); |
560: | } |
561: | |
562: | $methodsCount = count($methodPrototypes); |
563: | if ($methodsCount === 0) { |
564: | throw new ShouldNotHappenException(); |
565: | } |
566: | |
567: | if ($methodsCount === 1) { |
568: | return $methodPrototypes[0]; |
569: | } |
570: | |
571: | return new IntersectionTypeUnresolvedMethodPrototypeReflection($methodName, $methodPrototypes); |
572: | } |
573: | |
574: | public function canAccessConstants(): TrinaryLogic |
575: | { |
576: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->canAccessConstants()); |
577: | } |
578: | |
579: | public function hasConstant(string $constantName): TrinaryLogic |
580: | { |
581: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->hasConstant($constantName)); |
582: | } |
583: | |
584: | public function getConstant(string $constantName): ClassConstantReflection |
585: | { |
586: | foreach ($this->types as $type) { |
587: | if ($type->hasConstant($constantName)->yes()) { |
588: | return $type->getConstant($constantName); |
589: | } |
590: | } |
591: | |
592: | throw new ShouldNotHappenException(); |
593: | } |
594: | |
595: | public function isIterable(): TrinaryLogic |
596: | { |
597: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isIterable()); |
598: | } |
599: | |
600: | public function isIterableAtLeastOnce(): TrinaryLogic |
601: | { |
602: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isIterableAtLeastOnce()); |
603: | } |
604: | |
605: | public function getArraySize(): Type |
606: | { |
607: | return $this->intersectTypes(static fn (Type $type): Type => $type->getArraySize()); |
608: | } |
609: | |
610: | public function getIterableKeyType(): Type |
611: | { |
612: | return $this->intersectTypes(static fn (Type $type): Type => $type->getIterableKeyType()); |
613: | } |
614: | |
615: | public function getFirstIterableKeyType(): Type |
616: | { |
617: | return $this->intersectTypes(static fn (Type $type): Type => $type->getFirstIterableKeyType()); |
618: | } |
619: | |
620: | public function getLastIterableKeyType(): Type |
621: | { |
622: | return $this->intersectTypes(static fn (Type $type): Type => $type->getLastIterableKeyType()); |
623: | } |
624: | |
625: | public function getIterableValueType(): Type |
626: | { |
627: | return $this->intersectTypes(static fn (Type $type): Type => $type->getIterableValueType()); |
628: | } |
629: | |
630: | public function getFirstIterableValueType(): Type |
631: | { |
632: | return $this->intersectTypes(static fn (Type $type): Type => $type->getFirstIterableValueType()); |
633: | } |
634: | |
635: | public function getLastIterableValueType(): Type |
636: | { |
637: | return $this->intersectTypes(static fn (Type $type): Type => $type->getLastIterableValueType()); |
638: | } |
639: | |
640: | public function isArray(): TrinaryLogic |
641: | { |
642: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isArray()); |
643: | } |
644: | |
645: | public function isConstantArray(): TrinaryLogic |
646: | { |
647: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isConstantArray()); |
648: | } |
649: | |
650: | public function isOversizedArray(): TrinaryLogic |
651: | { |
652: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isOversizedArray()); |
653: | } |
654: | |
655: | public function isList(): TrinaryLogic |
656: | { |
657: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isList()); |
658: | } |
659: | |
660: | public function isString(): TrinaryLogic |
661: | { |
662: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isString()); |
663: | } |
664: | |
665: | public function isNumericString(): TrinaryLogic |
666: | { |
667: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isNumericString()); |
668: | } |
669: | |
670: | public function isNonEmptyString(): TrinaryLogic |
671: | { |
672: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isNonEmptyString()); |
673: | } |
674: | |
675: | public function isNonFalsyString(): TrinaryLogic |
676: | { |
677: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isNonFalsyString()); |
678: | } |
679: | |
680: | public function isLiteralString(): TrinaryLogic |
681: | { |
682: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isLiteralString()); |
683: | } |
684: | |
685: | public function isLowercaseString(): TrinaryLogic |
686: | { |
687: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isLowercaseString()); |
688: | } |
689: | |
690: | public function isUppercaseString(): TrinaryLogic |
691: | { |
692: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isUppercaseString()); |
693: | } |
694: | |
695: | public function isClassString(): TrinaryLogic |
696: | { |
697: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isClassString()); |
698: | } |
699: | |
700: | public function getClassStringObjectType(): Type |
701: | { |
702: | return $this->intersectTypes(static fn (Type $type): Type => $type->getClassStringObjectType()); |
703: | } |
704: | |
705: | public function getObjectTypeOrClassStringObjectType(): Type |
706: | { |
707: | return $this->intersectTypes(static fn (Type $type): Type => $type->getObjectTypeOrClassStringObjectType()); |
708: | } |
709: | |
710: | public function isVoid(): TrinaryLogic |
711: | { |
712: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isVoid()); |
713: | } |
714: | |
715: | public function isScalar(): TrinaryLogic |
716: | { |
717: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isScalar()); |
718: | } |
719: | |
720: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
721: | { |
722: | return $this->intersectResults( |
723: | static fn (Type $innerType): TrinaryLogic => $innerType->looseCompare($type, $phpVersion)->toTrinaryLogic() |
724: | )->toBooleanType(); |
725: | } |
726: | |
727: | public function isOffsetAccessible(): TrinaryLogic |
728: | { |
729: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isOffsetAccessible()); |
730: | } |
731: | |
732: | public function isOffsetAccessLegal(): TrinaryLogic |
733: | { |
734: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isOffsetAccessLegal()); |
735: | } |
736: | |
737: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
738: | { |
739: | if ($this->isList()->yes() && $this->isIterableAtLeastOnce()->yes()) { |
740: | $arrayKeyOffsetType = $offsetType->toArrayKey(); |
741: | if ((new ConstantIntegerType(0))->isSuperTypeOf($arrayKeyOffsetType)->yes()) { |
742: | return TrinaryLogic::createYes(); |
743: | } |
744: | |
745: | foreach ($this->types as $type) { |
746: | if (!$type instanceof HasOffsetValueType && !$type instanceof HasOffsetType) { |
747: | continue; |
748: | } |
749: | |
750: | foreach ($type->getOffsetType()->getConstantScalarValues() as $constantScalarValue) { |
751: | if (!is_int($constantScalarValue)) { |
752: | continue; |
753: | } |
754: | if (IntegerRangeType::fromInterval(0, $constantScalarValue)->isSuperTypeOf($arrayKeyOffsetType)->yes()) { |
755: | return TrinaryLogic::createYes(); |
756: | } |
757: | } |
758: | } |
759: | } |
760: | |
761: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->hasOffsetValueType($offsetType)); |
762: | } |
763: | |
764: | public function getOffsetValueType(Type $offsetType): Type |
765: | { |
766: | $result = $this->intersectTypes(static fn (Type $type): Type => $type->getOffsetValueType($offsetType)); |
767: | if ($this->isOversizedArray()->yes()) { |
768: | return TypeUtils::toBenevolentUnion($result); |
769: | } |
770: | |
771: | return $result; |
772: | } |
773: | |
774: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
775: | { |
776: | if ($this->isOversizedArray()->yes()) { |
777: | return $this->intersectTypes(static function (Type $type) use ($offsetType, $valueType, $unionValues): Type { |
778: | |
779: | if (!$type instanceof ArrayType) { |
780: | return $type->setOffsetValueType($offsetType, $valueType, $unionValues); |
781: | } |
782: | |
783: | if (!$offsetType instanceof ConstantStringType && !$offsetType instanceof ConstantIntegerType) { |
784: | return $type->setOffsetValueType($offsetType, $valueType, $unionValues); |
785: | } |
786: | |
787: | if (!$offsetType->isSuperTypeOf($type->getKeyType())->yes()) { |
788: | return $type->setOffsetValueType($offsetType, $valueType, $unionValues); |
789: | } |
790: | |
791: | return TypeCombinator::intersect( |
792: | new ArrayType( |
793: | TypeCombinator::union($type->getKeyType(), $offsetType), |
794: | TypeCombinator::union($type->getItemType(), $valueType), |
795: | ), |
796: | new NonEmptyArrayType(), |
797: | ); |
798: | }); |
799: | } |
800: | |
801: | $result = $this->intersectTypes(static fn (Type $type): Type => $type->setOffsetValueType($offsetType, $valueType, $unionValues)); |
802: | |
803: | if ( |
804: | $offsetType !== null |
805: | && $this->isList()->yes() |
806: | && !$result->isList()->yes() |
807: | ) { |
808: | if ($this->isIterableAtLeastOnce()->yes() && (new ConstantIntegerType(1))->isSuperTypeOf($offsetType)->yes()) { |
809: | $result = TypeCombinator::intersect($result, new AccessoryArrayListType()); |
810: | } else { |
811: | foreach ($this->types as $type) { |
812: | if (!$type instanceof HasOffsetValueType && !$type instanceof HasOffsetType) { |
813: | continue; |
814: | } |
815: | |
816: | foreach ($type->getOffsetType()->getConstantScalarValues() as $constantScalarValue) { |
817: | if (!is_int($constantScalarValue)) { |
818: | continue; |
819: | } |
820: | if (IntegerRangeType::fromInterval(0, $constantScalarValue + 1)->isSuperTypeOf($offsetType)->yes()) { |
821: | $result = TypeCombinator::intersect($result, new AccessoryArrayListType()); |
822: | break 2; |
823: | } |
824: | } |
825: | } |
826: | } |
827: | } |
828: | |
829: | return $result; |
830: | } |
831: | |
832: | public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type |
833: | { |
834: | return $this->intersectTypes(static fn (Type $type): Type => $type->setExistingOffsetValueType($offsetType, $valueType)); |
835: | } |
836: | |
837: | public function unsetOffset(Type $offsetType): Type |
838: | { |
839: | return $this->intersectTypes(static fn (Type $type): Type => $type->unsetOffset($offsetType)); |
840: | } |
841: | |
842: | public function getKeysArray(): Type |
843: | { |
844: | return $this->intersectTypes(static fn (Type $type): Type => $type->getKeysArray()); |
845: | } |
846: | |
847: | public function getValuesArray(): Type |
848: | { |
849: | return $this->intersectTypes(static fn (Type $type): Type => $type->getValuesArray()); |
850: | } |
851: | |
852: | public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type |
853: | { |
854: | return $this->intersectTypes(static fn (Type $type): Type => $type->chunkArray($lengthType, $preserveKeys)); |
855: | } |
856: | |
857: | public function fillKeysArray(Type $valueType): Type |
858: | { |
859: | return $this->intersectTypes(static fn (Type $type): Type => $type->fillKeysArray($valueType)); |
860: | } |
861: | |
862: | public function flipArray(): Type |
863: | { |
864: | return $this->intersectTypes(static fn (Type $type): Type => $type->flipArray()); |
865: | } |
866: | |
867: | public function intersectKeyArray(Type $otherArraysType): Type |
868: | { |
869: | return $this->intersectTypes(static fn (Type $type): Type => $type->intersectKeyArray($otherArraysType)); |
870: | } |
871: | |
872: | public function popArray(): Type |
873: | { |
874: | return $this->intersectTypes(static fn (Type $type): Type => $type->popArray()); |
875: | } |
876: | |
877: | public function reverseArray(TrinaryLogic $preserveKeys): Type |
878: | { |
879: | return $this->intersectTypes(static fn (Type $type): Type => $type->reverseArray($preserveKeys)); |
880: | } |
881: | |
882: | public function searchArray(Type $needleType): Type |
883: | { |
884: | return $this->intersectTypes(static fn (Type $type): Type => $type->searchArray($needleType)); |
885: | } |
886: | |
887: | public function shiftArray(): Type |
888: | { |
889: | return $this->intersectTypes(static fn (Type $type): Type => $type->shiftArray()); |
890: | } |
891: | |
892: | public function shuffleArray(): Type |
893: | { |
894: | return $this->intersectTypes(static fn (Type $type): Type => $type->shuffleArray()); |
895: | } |
896: | |
897: | public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type |
898: | { |
899: | return $this->intersectTypes(static fn (Type $type): Type => $type->sliceArray($offsetType, $lengthType, $preserveKeys)); |
900: | } |
901: | |
902: | public function getEnumCases(): array |
903: | { |
904: | $compare = []; |
905: | foreach ($this->types as $type) { |
906: | $oneType = []; |
907: | foreach ($type->getEnumCases() as $enumCase) { |
908: | $oneType[$enumCase->getClassName() . '::' . $enumCase->getEnumCaseName()] = $enumCase; |
909: | } |
910: | $compare[] = $oneType; |
911: | } |
912: | |
913: | return array_values(array_intersect_key(...$compare)); |
914: | } |
915: | |
916: | public function isCallable(): TrinaryLogic |
917: | { |
918: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isCallable()); |
919: | } |
920: | |
921: | public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array |
922: | { |
923: | if ($this->isCallable()->no()) { |
924: | throw new ShouldNotHappenException(); |
925: | } |
926: | |
927: | return [new TrivialParametersAcceptor()]; |
928: | } |
929: | |
930: | public function isCloneable(): TrinaryLogic |
931: | { |
932: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isCloneable()); |
933: | } |
934: | |
935: | public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
936: | { |
937: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThan($otherType, $phpVersion)); |
938: | } |
939: | |
940: | public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
941: | { |
942: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThanOrEqual($otherType, $phpVersion)); |
943: | } |
944: | |
945: | public function isNull(): TrinaryLogic |
946: | { |
947: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isNull()); |
948: | } |
949: | |
950: | public function isConstantValue(): TrinaryLogic |
951: | { |
952: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isConstantValue()); |
953: | } |
954: | |
955: | public function isConstantScalarValue(): TrinaryLogic |
956: | { |
957: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isConstantScalarValue()); |
958: | } |
959: | |
960: | public function getConstantScalarTypes(): array |
961: | { |
962: | $scalarTypes = []; |
963: | foreach ($this->types as $type) { |
964: | foreach ($type->getConstantScalarTypes() as $scalarType) { |
965: | $scalarTypes[] = $scalarType; |
966: | } |
967: | } |
968: | |
969: | return $scalarTypes; |
970: | } |
971: | |
972: | public function getConstantScalarValues(): array |
973: | { |
974: | $values = []; |
975: | foreach ($this->types as $type) { |
976: | foreach ($type->getConstantScalarValues() as $value) { |
977: | $values[] = $value; |
978: | } |
979: | } |
980: | |
981: | return $values; |
982: | } |
983: | |
984: | public function isTrue(): TrinaryLogic |
985: | { |
986: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isTrue()); |
987: | } |
988: | |
989: | public function isFalse(): TrinaryLogic |
990: | { |
991: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isFalse()); |
992: | } |
993: | |
994: | public function isBoolean(): TrinaryLogic |
995: | { |
996: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isBoolean()); |
997: | } |
998: | |
999: | public function isFloat(): TrinaryLogic |
1000: | { |
1001: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isFloat()); |
1002: | } |
1003: | |
1004: | public function isInteger(): TrinaryLogic |
1005: | { |
1006: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isInteger()); |
1007: | } |
1008: | |
1009: | public function isGreaterThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
1010: | { |
1011: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $otherType->isSmallerThan($type, $phpVersion)); |
1012: | } |
1013: | |
1014: | public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
1015: | { |
1016: | return $this->intersectResults(static fn (Type $type): TrinaryLogic => $otherType->isSmallerThanOrEqual($type, $phpVersion)); |
1017: | } |
1018: | |
1019: | public function getSmallerType(PhpVersion $phpVersion): Type |
1020: | { |
1021: | return $this->intersectTypes(static fn (Type $type): Type => $type->getSmallerType($phpVersion)); |
1022: | } |
1023: | |
1024: | public function getSmallerOrEqualType(PhpVersion $phpVersion): Type |
1025: | { |
1026: | return $this->intersectTypes(static fn (Type $type): Type => $type->getSmallerOrEqualType($phpVersion)); |
1027: | } |
1028: | |
1029: | public function getGreaterType(PhpVersion $phpVersion): Type |
1030: | { |
1031: | return $this->intersectTypes(static fn (Type $type): Type => $type->getGreaterType($phpVersion)); |
1032: | } |
1033: | |
1034: | public function getGreaterOrEqualType(PhpVersion $phpVersion): Type |
1035: | { |
1036: | return $this->intersectTypes(static fn (Type $type): Type => $type->getGreaterOrEqualType($phpVersion)); |
1037: | } |
1038: | |
1039: | public function toBoolean(): BooleanType |
1040: | { |
1041: | $type = $this->intersectTypes(static fn (Type $type): BooleanType => $type->toBoolean()); |
1042: | |
1043: | if (!$type instanceof BooleanType) { |
1044: | return new BooleanType(); |
1045: | } |
1046: | |
1047: | return $type; |
1048: | } |
1049: | |
1050: | public function toNumber(): Type |
1051: | { |
1052: | $type = $this->intersectTypes(static fn (Type $type): Type => $type->toNumber()); |
1053: | |
1054: | return $type; |
1055: | } |
1056: | |
1057: | public function toAbsoluteNumber(): Type |
1058: | { |
1059: | $type = $this->intersectTypes(static fn (Type $type): Type => $type->toAbsoluteNumber()); |
1060: | |
1061: | return $type; |
1062: | } |
1063: | |
1064: | public function toString(): Type |
1065: | { |
1066: | $type = $this->intersectTypes(static fn (Type $type): Type => $type->toString()); |
1067: | |
1068: | return $type; |
1069: | } |
1070: | |
1071: | public function toInteger(): Type |
1072: | { |
1073: | $type = $this->intersectTypes(static fn (Type $type): Type => $type->toInteger()); |
1074: | |
1075: | return $type; |
1076: | } |
1077: | |
1078: | public function toFloat(): Type |
1079: | { |
1080: | $type = $this->intersectTypes(static fn (Type $type): Type => $type->toFloat()); |
1081: | |
1082: | return $type; |
1083: | } |
1084: | |
1085: | public function toArray(): Type |
1086: | { |
1087: | $type = $this->intersectTypes(static fn (Type $type): Type => $type->toArray()); |
1088: | |
1089: | return $type; |
1090: | } |
1091: | |
1092: | public function toArrayKey(): Type |
1093: | { |
1094: | if ($this->isNumericString()->yes()) { |
1095: | return new IntegerType(); |
1096: | } |
1097: | |
1098: | if ($this->isString()->yes()) { |
1099: | return $this; |
1100: | } |
1101: | |
1102: | return $this->intersectTypes(static fn (Type $type): Type => $type->toArrayKey()); |
1103: | } |
1104: | |
1105: | public function toCoercedArgumentType(bool $strictTypes): Type |
1106: | { |
1107: | return $this->intersectTypes(static fn (Type $type): Type => $type->toCoercedArgumentType($strictTypes)); |
1108: | } |
1109: | |
1110: | public function inferTemplateTypes(Type $receivedType): TemplateTypeMap |
1111: | { |
1112: | $types = TemplateTypeMap::createEmpty(); |
1113: | |
1114: | foreach ($this->types as $type) { |
1115: | $types = $types->intersect($type->inferTemplateTypes($receivedType)); |
1116: | } |
1117: | |
1118: | return $types; |
1119: | } |
1120: | |
1121: | public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array |
1122: | { |
1123: | $references = []; |
1124: | |
1125: | foreach ($this->types as $type) { |
1126: | foreach ($type->getReferencedTemplateTypes($positionVariance) as $reference) { |
1127: | $references[] = $reference; |
1128: | } |
1129: | } |
1130: | |
1131: | return $references; |
1132: | } |
1133: | |
1134: | public function traverse(callable $cb): Type |
1135: | { |
1136: | $types = []; |
1137: | $changed = false; |
1138: | |
1139: | foreach ($this->types as $type) { |
1140: | $newType = $cb($type); |
1141: | if ($type !== $newType) { |
1142: | $changed = true; |
1143: | } |
1144: | $types[] = $newType; |
1145: | } |
1146: | |
1147: | if ($changed) { |
1148: | return TypeCombinator::intersect(...$types); |
1149: | } |
1150: | |
1151: | return $this; |
1152: | } |
1153: | |
1154: | public function traverseSimultaneously(Type $right, callable $cb): Type |
1155: | { |
1156: | $types = []; |
1157: | $changed = false; |
1158: | |
1159: | if (!$right instanceof self) { |
1160: | return $this; |
1161: | } |
1162: | |
1163: | if (count($this->getTypes()) !== count($right->getTypes())) { |
1164: | return $this; |
1165: | } |
1166: | |
1167: | foreach ($this->getSortedTypes() as $i => $leftType) { |
1168: | $rightType = $right->getSortedTypes()[$i]; |
1169: | $newType = $cb($leftType, $rightType); |
1170: | if ($leftType !== $newType) { |
1171: | $changed = true; |
1172: | } |
1173: | $types[] = $newType; |
1174: | } |
1175: | |
1176: | if ($changed) { |
1177: | return TypeCombinator::intersect(...$types); |
1178: | } |
1179: | |
1180: | return $this; |
1181: | } |
1182: | |
1183: | public function tryRemove(Type $typeToRemove): ?Type |
1184: | { |
1185: | return $this->intersectTypes(static fn (Type $type): Type => TypeCombinator::remove($type, $typeToRemove)); |
1186: | } |
1187: | |
1188: | public function exponentiate(Type $exponent): Type |
1189: | { |
1190: | return $this->intersectTypes(static fn (Type $type): Type => $type->exponentiate($exponent)); |
1191: | } |
1192: | |
1193: | public function getFiniteTypes(): array |
1194: | { |
1195: | $compare = []; |
1196: | foreach ($this->types as $type) { |
1197: | $oneType = []; |
1198: | foreach ($type->getFiniteTypes() as $finiteType) { |
1199: | $oneType[md5($finiteType->describe(VerbosityLevel::typeOnly()))] = $finiteType; |
1200: | } |
1201: | $compare[] = $oneType; |
1202: | } |
1203: | |
1204: | $result = array_values(array_intersect_key(...$compare)); |
1205: | |
1206: | if (count($result) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) { |
1207: | return []; |
1208: | } |
1209: | |
1210: | return $result; |
1211: | } |
1212: | |
1213: | |
1214: | |
1215: | |
1216: | private function intersectResults(callable $getResult): TrinaryLogic |
1217: | { |
1218: | return TrinaryLogic::lazyMaxMin($this->types, $getResult); |
1219: | } |
1220: | |
1221: | |
1222: | |
1223: | |
1224: | private function intersectTypes(callable $getType): Type |
1225: | { |
1226: | $operands = array_map($getType, $this->types); |
1227: | return TypeCombinator::intersect(...$operands); |
1228: | } |
1229: | |
1230: | public function toPhpDocNode(): TypeNode |
1231: | { |
1232: | $baseTypes = []; |
1233: | $typesToDescribe = []; |
1234: | $skipTypeNames = []; |
1235: | |
1236: | $nonEmptyStr = false; |
1237: | $nonFalsyStr = false; |
1238: | $isList = $this->isList()->yes(); |
1239: | $isArray = $this->isArray()->yes(); |
1240: | $isNonEmptyArray = $this->isIterableAtLeastOnce()->yes(); |
1241: | $describedTypes = []; |
1242: | |
1243: | foreach ($this->getSortedTypes() as $i => $type) { |
1244: | if ($type instanceof AccessoryNonEmptyStringType |
1245: | || $type instanceof AccessoryLiteralStringType |
1246: | || $type instanceof AccessoryNumericStringType |
1247: | || $type instanceof AccessoryNonFalsyStringType |
1248: | || $type instanceof AccessoryLowercaseStringType |
1249: | || $type instanceof AccessoryUppercaseStringType |
1250: | ) { |
1251: | if ($type instanceof AccessoryNonFalsyStringType) { |
1252: | $nonFalsyStr = true; |
1253: | } |
1254: | if ($type instanceof AccessoryNonEmptyStringType) { |
1255: | $nonEmptyStr = true; |
1256: | } |
1257: | if ($nonEmptyStr && $nonFalsyStr) { |
1258: | |
1259: | foreach ($typesToDescribe as $key => $typeToDescribe) { |
1260: | if (!($typeToDescribe instanceof AccessoryNonEmptyStringType)) { |
1261: | continue; |
1262: | } |
1263: | |
1264: | unset($typesToDescribe[$key]); |
1265: | } |
1266: | } |
1267: | |
1268: | $typesToDescribe[$i] = $type; |
1269: | $skipTypeNames[] = 'string'; |
1270: | continue; |
1271: | } |
1272: | |
1273: | if ($isList || $isArray) { |
1274: | if ($type instanceof ArrayType) { |
1275: | $keyType = $type->getKeyType(); |
1276: | $valueType = $type->getItemType(); |
1277: | if ($isList) { |
1278: | $isMixedValueType = $valueType instanceof MixedType && $valueType->describe(VerbosityLevel::precise()) === 'mixed' && !$valueType->isExplicitMixed(); |
1279: | $identifierTypeNode = new IdentifierTypeNode($isNonEmptyArray ? 'non-empty-list' : 'list'); |
1280: | if (!$isMixedValueType) { |
1281: | $describedTypes[$i] = new GenericTypeNode($identifierTypeNode, [ |
1282: | $valueType->toPhpDocNode(), |
1283: | ]); |
1284: | } else { |
1285: | $describedTypes[$i] = $identifierTypeNode; |
1286: | } |
1287: | } else { |
1288: | $isMixedKeyType = $keyType instanceof MixedType && $keyType->describe(VerbosityLevel::precise()) === 'mixed' && !$keyType->isExplicitMixed(); |
1289: | $isMixedValueType = $valueType instanceof MixedType && $valueType->describe(VerbosityLevel::precise()) === 'mixed' && !$valueType->isExplicitMixed(); |
1290: | $identifierTypeNode = new IdentifierTypeNode($isNonEmptyArray ? 'non-empty-array' : 'array'); |
1291: | if (!$isMixedKeyType) { |
1292: | $describedTypes[$i] = new GenericTypeNode($identifierTypeNode, [ |
1293: | $keyType->toPhpDocNode(), |
1294: | $valueType->toPhpDocNode(), |
1295: | ]); |
1296: | } elseif (!$isMixedValueType) { |
1297: | $describedTypes[$i] = new GenericTypeNode($identifierTypeNode, [ |
1298: | $valueType->toPhpDocNode(), |
1299: | ]); |
1300: | } else { |
1301: | $describedTypes[$i] = $identifierTypeNode; |
1302: | } |
1303: | } |
1304: | continue; |
1305: | } elseif ($type instanceof ConstantArrayType) { |
1306: | $constantArrayTypeNode = $type->toPhpDocNode(); |
1307: | if ($constantArrayTypeNode instanceof ArrayShapeNode) { |
1308: | $newKind = $constantArrayTypeNode->kind; |
1309: | if ($isList) { |
1310: | if ($isNonEmptyArray && !$type->isIterableAtLeastOnce()->yes()) { |
1311: | $newKind = ArrayShapeNode::KIND_NON_EMPTY_LIST; |
1312: | } else { |
1313: | $newKind = ArrayShapeNode::KIND_LIST; |
1314: | } |
1315: | } elseif ($isNonEmptyArray && !$type->isIterableAtLeastOnce()->yes()) { |
1316: | $newKind = ArrayShapeNode::KIND_NON_EMPTY_ARRAY; |
1317: | } |
1318: | |
1319: | if ($newKind !== $constantArrayTypeNode->kind) { |
1320: | if ($constantArrayTypeNode->sealed) { |
1321: | $constantArrayTypeNode = ArrayShapeNode::createSealed($constantArrayTypeNode->items, $newKind); |
1322: | } else { |
1323: | $constantArrayTypeNode = ArrayShapeNode::createUnsealed($constantArrayTypeNode->items, $constantArrayTypeNode->unsealedType, $newKind); |
1324: | } |
1325: | } |
1326: | |
1327: | $describedTypes[$i] = $constantArrayTypeNode; |
1328: | continue; |
1329: | } |
1330: | } |
1331: | if ($type instanceof NonEmptyArrayType || $type instanceof AccessoryArrayListType) { |
1332: | continue; |
1333: | } |
1334: | } |
1335: | |
1336: | if (!$type instanceof AccessoryType) { |
1337: | $baseTypes[$i] = $type; |
1338: | continue; |
1339: | } |
1340: | |
1341: | $accessoryPhpDocNode = $type->toPhpDocNode(); |
1342: | if ($accessoryPhpDocNode instanceof IdentifierTypeNode && $accessoryPhpDocNode->name === '') { |
1343: | continue; |
1344: | } |
1345: | |
1346: | $typesToDescribe[$i] = $type; |
1347: | } |
1348: | |
1349: | foreach ($baseTypes as $i => $type) { |
1350: | $typeNode = $type->toPhpDocNode(); |
1351: | if ($typeNode instanceof GenericTypeNode && $typeNode->type->name === 'array') { |
1352: | $nonEmpty = false; |
1353: | $typeName = 'array'; |
1354: | foreach ($typesToDescribe as $j => $typeToDescribe) { |
1355: | if ($typeToDescribe instanceof AccessoryArrayListType) { |
1356: | $typeName = 'list'; |
1357: | if (count($typeNode->genericTypes) > 1) { |
1358: | array_shift($typeNode->genericTypes); |
1359: | } |
1360: | } elseif ($typeToDescribe instanceof NonEmptyArrayType) { |
1361: | $nonEmpty = true; |
1362: | } else { |
1363: | continue; |
1364: | } |
1365: | |
1366: | unset($typesToDescribe[$j]); |
1367: | } |
1368: | |
1369: | if ($nonEmpty) { |
1370: | $typeName = 'non-empty-' . $typeName; |
1371: | } |
1372: | |
1373: | $describedTypes[$i] = new GenericTypeNode( |
1374: | new IdentifierTypeNode($typeName), |
1375: | $typeNode->genericTypes, |
1376: | ); |
1377: | continue; |
1378: | } |
1379: | |
1380: | if ($typeNode instanceof IdentifierTypeNode && in_array($typeNode->name, $skipTypeNames, true)) { |
1381: | continue; |
1382: | } |
1383: | |
1384: | $describedTypes[$i] = $typeNode; |
1385: | } |
1386: | |
1387: | foreach ($typesToDescribe as $i => $typeToDescribe) { |
1388: | $describedTypes[$i] = $typeToDescribe->toPhpDocNode(); |
1389: | } |
1390: | |
1391: | ksort($describedTypes); |
1392: | |
1393: | $describedTypes = array_values($describedTypes); |
1394: | |
1395: | if (count($describedTypes) === 1) { |
1396: | return $describedTypes[0]; |
1397: | } |
1398: | |
1399: | return new IntersectionTypeNode($describedTypes); |
1400: | } |
1401: | |
1402: | } |
1403: | |