| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Type; |
| 4: | |
| 5: | use DateTime; |
| 6: | use DateTimeImmutable; |
| 7: | use DateTimeInterface; |
| 8: | use Error; |
| 9: | use Exception; |
| 10: | use PHPStan\DependencyInjection\ReportUnsafeArrayStringKeyCastingToggle; |
| 11: | use PHPStan\Php\PhpVersion; |
| 12: | use PHPStan\PhpDocParser\Ast\Type\TypeNode; |
| 13: | use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode; |
| 14: | use PHPStan\Reflection\ClassConstantReflection; |
| 15: | use PHPStan\Reflection\ClassMemberAccessAnswerer; |
| 16: | use PHPStan\Reflection\ExtendedMethodReflection; |
| 17: | use PHPStan\Reflection\ExtendedPropertyReflection; |
| 18: | use PHPStan\Reflection\InitializerExprTypeResolver; |
| 19: | use PHPStan\Reflection\MissingMethodFromReflectionException; |
| 20: | use PHPStan\Reflection\MissingPropertyFromReflectionException; |
| 21: | use PHPStan\Reflection\ReflectionProvider; |
| 22: | use PHPStan\Reflection\Type\UnionTypeUnresolvedMethodPrototypeReflection; |
| 23: | use PHPStan\Reflection\Type\UnionTypeUnresolvedPropertyPrototypeReflection; |
| 24: | use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection; |
| 25: | use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection; |
| 26: | use PHPStan\ShouldNotHappenException; |
| 27: | use PHPStan\TrinaryLogic; |
| 28: | use PHPStan\Turbo\ShadowedByTurboExtension; |
| 29: | use PHPStan\Type\Enum\EnumCaseObjectType; |
| 30: | use PHPStan\Type\Generic\AbsorbedTemplateArgumentType; |
| 31: | use PHPStan\Type\Generic\GenericClassStringType; |
| 32: | use PHPStan\Type\Generic\TemplateIterableType; |
| 33: | use PHPStan\Type\Generic\TemplateMixedType; |
| 34: | use PHPStan\Type\Generic\TemplateType; |
| 35: | use PHPStan\Type\Generic\TemplateTypeMap; |
| 36: | use PHPStan\Type\Generic\TemplateTypeVariance; |
| 37: | use PHPStan\Type\Generic\TemplateUnionType; |
| 38: | use PHPStan\Type\Generic\UnresolvedTemplateArgumentType; |
| 39: | use PHPStan\Type\Traits\NonGeneralizableTypeTrait; |
| 40: | use PHPStan\Type\Traits\OperatorTypeTrait; |
| 41: | use Throwable; |
| 42: | use function array_diff_assoc; |
| 43: | use function array_fill_keys; |
| 44: | use function array_intersect; |
| 45: | use function array_key_exists; |
| 46: | use function array_keys; |
| 47: | use function array_map; |
| 48: | use function array_merge; |
| 49: | use function array_slice; |
| 50: | use function array_unique; |
| 51: | use function array_values; |
| 52: | use function count; |
| 53: | use function implode; |
| 54: | use function spl_object_id; |
| 55: | use function sprintf; |
| 56: | use function str_contains; |
| 57: | |
| 58: | |
| 59: | #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/UnionType.cpp')] |
| 60: | class UnionType implements CompoundType |
| 61: | { |
| 62: | |
| 63: | use NonGeneralizableTypeTrait; |
| 64: | use OperatorTypeTrait; |
| 65: | |
| 66: | public const EQUAL_UNION_CLASSES = [ |
| 67: | DateTimeInterface::class => [DateTimeImmutable::class, DateTime::class], |
| 68: | Throwable::class => [Error::class, Exception::class], |
| 69: | ]; |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | private ?array $sortedTypesCache = null; |
| 81: | |
| 82: | |
| 83: | private array $cachedDescriptions = []; |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | private FiniteTypeSet|false|null $finiteTypeSet = null; |
| 90: | |
| 91: | |
| 92: | private ?array $finiteTypes = null; |
| 93: | |
| 94: | private ?TrinaryLogic $isNull = null; |
| 95: | |
| 96: | private ?TrinaryLogic $isCallable = null; |
| 97: | |
| 98: | private ?bool $hasTemplateOrLateResolvableType = null; |
| 99: | |
| 100: | |
| 101: | private array $possiblyLooselyEqualTypes = []; |
| 102: | |
| 103: | |
| 104: | private array $alwaysLooselyEqualTypes = []; |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | public function __construct(private array $types, private bool $normalized = false) |
| 111: | { |
| 112: | $throwException = static function () use ($types): void { |
| 113: | throw new ShouldNotHappenException(sprintf( |
| 114: | 'Cannot create %s with: %s', |
| 115: | self::class, |
| 116: | implode(', ', array_map(static fn (Type $type): string => $type->describe(VerbosityLevel::value()), $types)), |
| 117: | )); |
| 118: | }; |
| 119: | if (count($types) < 2) { |
| 120: | $throwException(); |
| 121: | } |
| 122: | foreach ($types as $type) { |
| 123: | if (!($type instanceof UnionType)) { |
| 124: | continue; |
| 125: | } |
| 126: | if ($type instanceof TemplateType) { |
| 127: | continue; |
| 128: | } |
| 129: | |
| 130: | $throwException(); |
| 131: | } |
| 132: | } |
| 133: | |
| 134: | |
| 135: | private array $generalizedTypes = []; |
| 136: | |
| 137: | |
| 138: | private array $cachedGetOffsetValueType = []; |
| 139: | |
| 140: | public function generalize(GeneralizePrecision $precision): Type |
| 141: | { |
| 142: | |
| 143: | |
| 144: | |
| 145: | return $this->generalizedTypes[spl_object_id($precision)] ??= $this->traverse(static fn (Type $type) => $type->generalize($precision)); |
| 146: | } |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | public function getTypes(): array |
| 152: | { |
| 153: | return $this->types; |
| 154: | } |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | public function filterTypes(callable $filterCb): Type |
| 160: | { |
| 161: | $newTypes = []; |
| 162: | $changed = false; |
| 163: | foreach ($this->getTypes() as $innerType) { |
| 164: | if (!$filterCb($innerType)) { |
| 165: | $changed = true; |
| 166: | continue; |
| 167: | } |
| 168: | |
| 169: | $newTypes[] = $innerType; |
| 170: | } |
| 171: | |
| 172: | if (!$changed) { |
| 173: | return $this; |
| 174: | } |
| 175: | |
| 176: | return TypeCombinator::union(...$newTypes); |
| 177: | } |
| 178: | |
| 179: | public function isNormalized(): bool |
| 180: | { |
| 181: | return $this->normalized; |
| 182: | } |
| 183: | |
| 184: | public function getFiniteTypeSet(): ?FiniteTypeSet |
| 185: | { |
| 186: | $finiteTypeSet = $this->finiteTypeSet ??= FiniteTypeSet::create($this->types) ?? false; |
| 187: | if ($finiteTypeSet === false) { |
| 188: | return null; |
| 189: | } |
| 190: | |
| 191: | return $finiteTypeSet; |
| 192: | } |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | protected function getSortedTypes(): array |
| 198: | { |
| 199: | return $this->sortedTypesCache ??= UnionTypeHelper::sortTypes($this->types); |
| 200: | } |
| 201: | |
| 202: | public function getReferencedClasses(): array |
| 203: | { |
| 204: | $classes = []; |
| 205: | foreach ($this->types as $type) { |
| 206: | foreach ($type->getReferencedClasses() as $className) { |
| 207: | $classes[] = $className; |
| 208: | } |
| 209: | } |
| 210: | |
| 211: | return $classes; |
| 212: | } |
| 213: | |
| 214: | public function getObjectClassNames(): array |
| 215: | { |
| 216: | return array_values(array_unique($this->pickFromTypes( |
| 217: | static fn (Type $type) => $type->getObjectClassNames(), |
| 218: | static fn (Type $type) => $type->isObject()->yes(), |
| 219: | ))); |
| 220: | } |
| 221: | |
| 222: | public function getObjectClassReflections(): array |
| 223: | { |
| 224: | return $this->pickFromTypes( |
| 225: | static fn (Type $type) => $type->getObjectClassReflections(), |
| 226: | static fn (Type $type) => $type->isObject()->yes(), |
| 227: | ); |
| 228: | } |
| 229: | |
| 230: | public function getArrays(): array |
| 231: | { |
| 232: | return $this->pickFromTypes( |
| 233: | static fn (Type $type) => $type->getArrays(), |
| 234: | static fn (Type $type) => $type->isArray()->yes(), |
| 235: | ); |
| 236: | } |
| 237: | |
| 238: | public function getConstantArrays(): array |
| 239: | { |
| 240: | return $this->pickFromTypes( |
| 241: | static fn (Type $type) => $type->getConstantArrays(), |
| 242: | static fn (Type $type) => $type->isArray()->yes(), |
| 243: | ); |
| 244: | } |
| 245: | |
| 246: | public function getConstantStrings(): array |
| 247: | { |
| 248: | return $this->pickFromTypes( |
| 249: | static fn (Type $type) => $type->getConstantStrings(), |
| 250: | static fn (Type $type) => $type->isString()->yes(), |
| 251: | ); |
| 252: | } |
| 253: | |
| 254: | public function accepts(Type $type, bool $strictTypes): AcceptsResult |
| 255: | { |
| 256: | $finiteTypeSet = $this->getFiniteTypeSet(); |
| 257: | if ($finiteTypeSet !== null) { |
| 258: | $key = FiniteTypeSet::key($type); |
| 259: | if ($key !== null && $finiteTypeSet->has($key)) { |
| 260: | return AcceptsResult::createYes(); |
| 261: | } |
| 262: | |
| 263: | if ($finiteTypeSet->isComplete()) { |
| 264: | if ($key !== null) { |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | |
| 270: | $result = AcceptsResult::createNo(); |
| 271: | foreach ($finiteTypeSet->getRepresentativesOfOtherKinds($type) as $representative) { |
| 272: | $result = $result->or($representative->accepts($type, $strictTypes)); |
| 273: | } |
| 274: | |
| 275: | return $result; |
| 276: | } |
| 277: | |
| 278: | if ($type instanceof self && !$type instanceof TemplateType) { |
| 279: | $otherFiniteTypeSet = $type->getFiniteTypeSet(); |
| 280: | if ($otherFiniteTypeSet !== null && $otherFiniteTypeSet->isComplete()) { |
| 281: | |
| 282: | |
| 283: | |
| 284: | |
| 285: | return $type->isAcceptedBy($this, $strictTypes); |
| 286: | } |
| 287: | } |
| 288: | } |
| 289: | } |
| 290: | |
| 291: | if ($type instanceof IterableType) { |
| 292: | return $this->accepts($type->toArrayOrTraversable(), $strictTypes); |
| 293: | } |
| 294: | |
| 295: | foreach (self::EQUAL_UNION_CLASSES as $baseClass => $classes) { |
| 296: | if (!$type->equals(new ObjectType($baseClass))) { |
| 297: | continue; |
| 298: | } |
| 299: | |
| 300: | $union = TypeCombinator::union( |
| 301: | ...array_map(static fn (string $objectClass): Type => new ObjectType($objectClass), $classes), |
| 302: | ); |
| 303: | if ($this->accepts($union, $strictTypes)->yes()) { |
| 304: | return AcceptsResult::createYes(); |
| 305: | } |
| 306: | break; |
| 307: | } |
| 308: | |
| 309: | if ($type instanceof self && !$type instanceof TemplateType) { |
| 310: | |
| 311: | |
| 312: | |
| 313: | |
| 314: | return $type->isAcceptedBy($this, $strictTypes); |
| 315: | } |
| 316: | |
| 317: | $innerAccepts = []; |
| 318: | $result = AcceptsResult::createNo(); |
| 319: | foreach ($this->getSortedTypes() as $i => $innerType) { |
| 320: | $innerResult = $innerType->accepts($type, $strictTypes); |
| 321: | $innerAccepts[$i] = $innerResult; |
| 322: | $result = $result->or($innerResult->decorateReasons(static fn (string $reason) => sprintf('Type #%d from the union: %s', $i + 1, $reason))); |
| 323: | } |
| 324: | if ($result->yes()) { |
| 325: | return $result; |
| 326: | } |
| 327: | |
| 328: | $commonReasons = null; |
| 329: | foreach ($innerAccepts as $innerResult) { |
| 330: | if ($commonReasons === null) { |
| 331: | $commonReasons = $innerResult->reasons; |
| 332: | continue; |
| 333: | } |
| 334: | $commonReasons = array_values(array_intersect($commonReasons, $innerResult->reasons)); |
| 335: | } |
| 336: | if ($commonReasons !== null && count($commonReasons) > 0) { |
| 337: | $decorated = []; |
| 338: | foreach (array_keys($innerAccepts) as $i) { |
| 339: | foreach ($commonReasons as $reason) { |
| 340: | $decorated[] = sprintf('Type #%d from the union: %s', $i + 1, $reason); |
| 341: | } |
| 342: | } |
| 343: | $result = new AcceptsResult($result->result, $decorated); |
| 344: | } |
| 345: | |
| 346: | if ($type instanceof CompoundType && !$type instanceof CallableType && !$type instanceof TemplateType && !$type instanceof IntersectionType) { |
| 347: | return $type->isAcceptedBy($this, $strictTypes); |
| 348: | } |
| 349: | |
| 350: | if ($type instanceof TemplateUnionType) { |
| 351: | return $result->or($type->isAcceptedBy($this, $strictTypes)); |
| 352: | } |
| 353: | |
| 354: | if ($type->isEnum()->yes() && !$this->isEnum()->no()) { |
| 355: | $enumCasesUnion = TypeCombinator::union(...$type->getEnumCases()); |
| 356: | if (!$type->equals($enumCasesUnion)) { |
| 357: | return $this->accepts($enumCasesUnion, $strictTypes); |
| 358: | } |
| 359: | } |
| 360: | |
| 361: | return $result; |
| 362: | } |
| 363: | |
| 364: | public function isSuperTypeOf(Type $otherType): IsSuperTypeOfResult |
| 365: | { |
| 366: | if ( |
| 367: | ($otherType instanceof self && !$otherType instanceof TemplateUnionType) |
| 368: | || ($otherType instanceof IterableType && !$otherType instanceof TemplateIterableType) |
| 369: | || $otherType instanceof NeverType |
| 370: | || $otherType instanceof IntegerRangeType |
| 371: | |
| 372: | || $otherType instanceof UnresolvedTemplateArgumentType |
| 373: | ) { |
| 374: | return $otherType->isSubTypeOf($this); |
| 375: | } |
| 376: | |
| 377: | $types = $this->types; |
| 378: | $finiteTypeSet = $this->getFiniteTypeSet(); |
| 379: | if ($finiteTypeSet !== null) { |
| 380: | $key = FiniteTypeSet::key($otherType); |
| 381: | if ($key !== null) { |
| 382: | if ($finiteTypeSet->has($key)) { |
| 383: | return IsSuperTypeOfResult::createYes(); |
| 384: | } |
| 385: | |
| 386: | |
| 387: | |
| 388: | if ($finiteTypeSet->isComplete()) { |
| 389: | return IsSuperTypeOfResult::createNo(); |
| 390: | } |
| 391: | |
| 392: | $types = $finiteTypeSet->getOthers(); |
| 393: | } |
| 394: | } |
| 395: | |
| 396: | $results = []; |
| 397: | foreach ($types as $innerType) { |
| 398: | $result = $innerType->isSuperTypeOf($otherType); |
| 399: | if ($result->yes()) { |
| 400: | return $result; |
| 401: | } |
| 402: | $results[] = $result; |
| 403: | } |
| 404: | $result = IsSuperTypeOfResult::createNo()->or(...$results); |
| 405: | |
| 406: | if ( |
| 407: | $otherType instanceof TemplateUnionType |
| 408: | || ($otherType instanceof LateResolvableType && $otherType instanceof CompoundType && !$otherType instanceof TemplateType) |
| 409: | ) { |
| 410: | return $result->or($otherType->isSubTypeOf($this)); |
| 411: | } |
| 412: | |
| 413: | return $result; |
| 414: | } |
| 415: | |
| 416: | public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult |
| 417: | { |
| 418: | $containment = $this->finiteTypeSetContainedIn($otherType, false); |
| 419: | if ($containment !== null) { |
| 420: | if ($containment->maybe()) { |
| 421: | return IsSuperTypeOfResult::createMaybe(); |
| 422: | } |
| 423: | |
| 424: | return IsSuperTypeOfResult::createFromBoolean($containment->yes()); |
| 425: | } |
| 426: | |
| 427: | $results = array_map(static fn (Type $innerType) => $otherType->isSuperTypeOf($innerType), $this->types); |
| 428: | if ($this->hasClosureMember()) { |
| 429: | $results = ClosureOrigin::combineOverloadIsSuperTypeOfResults($this->types, $results); |
| 430: | } |
| 431: | |
| 432: | return IsSuperTypeOfResult::extremeIdentity(...$results); |
| 433: | } |
| 434: | |
| 435: | public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult |
| 436: | { |
| 437: | |
| 438: | |
| 439: | $containment = $this->finiteTypeSetContainedIn($acceptingType, true); |
| 440: | if ($containment !== null && $containment->yes()) { |
| 441: | return AcceptsResult::createYes(); |
| 442: | } |
| 443: | |
| 444: | $results = array_map(static fn (Type $innerType) => $acceptingType->accepts($innerType, $strictTypes), $this->types); |
| 445: | if ($this->hasClosureMember()) { |
| 446: | $results = ClosureOrigin::combineOverloadAcceptsResults($this->types, $results); |
| 447: | } |
| 448: | |
| 449: | return AcceptsResult::extremeIdentity(...$results); |
| 450: | } |
| 451: | |
| 452: | |
| 453: | private function hasClosureMember(): bool |
| 454: | { |
| 455: | foreach ($this->types as $type) { |
| 456: | if ($type instanceof ClosureType) { |
| 457: | return true; |
| 458: | } |
| 459: | } |
| 460: | |
| 461: | return false; |
| 462: | } |
| 463: | |
| 464: | |
| 465: | |
| 466: | |
| 467: | |
| 468: | |
| 469: | |
| 470: | |
| 471: | |
| 472: | |
| 473: | |
| 474: | private function finiteTypeSetContainedIn(Type $otherType, bool $yesOnly): ?TrinaryLogic |
| 475: | { |
| 476: | if (!$otherType instanceof self || $otherType instanceof TemplateType) { |
| 477: | |
| 478: | |
| 479: | |
| 480: | |
| 481: | |
| 482: | |
| 483: | $key = FiniteTypeSet::key($otherType); |
| 484: | if ($key === null) { |
| 485: | return null; |
| 486: | } |
| 487: | |
| 488: | $finiteTypeSet = $this->getFiniteTypeSet(); |
| 489: | if ($finiteTypeSet === null || !$finiteTypeSet->isComplete()) { |
| 490: | return null; |
| 491: | } |
| 492: | |
| 493: | $containment = $finiteTypeSet->containedInKey($key); |
| 494: | |
| 495: | |
| 496: | |
| 497: | |
| 498: | |
| 499: | |
| 500: | |
| 501: | if (!$containment->yes() && $yesOnly) { |
| 502: | return null; |
| 503: | } |
| 504: | |
| 505: | return $containment; |
| 506: | } |
| 507: | |
| 508: | $finiteTypeSet = $this->getFiniteTypeSet(); |
| 509: | if ($finiteTypeSet === null || !$finiteTypeSet->isComplete()) { |
| 510: | return null; |
| 511: | } |
| 512: | |
| 513: | $otherFiniteTypeSet = $otherType->getFiniteTypeSet(); |
| 514: | if ($otherFiniteTypeSet === null) { |
| 515: | return null; |
| 516: | } |
| 517: | |
| 518: | $containment = $finiteTypeSet->containedIn($otherFiniteTypeSet); |
| 519: | if (!$containment->yes() && ($yesOnly || !$otherFiniteTypeSet->isComplete())) { |
| 520: | return null; |
| 521: | } |
| 522: | |
| 523: | return $containment; |
| 524: | } |
| 525: | |
| 526: | public function equals(Type $type): bool |
| 527: | { |
| 528: | if (!$type instanceof static) { |
| 529: | return false; |
| 530: | } |
| 531: | |
| 532: | if (count($this->types) !== count($type->types)) { |
| 533: | return false; |
| 534: | } |
| 535: | |
| 536: | $finiteTypeSet = $this->getFiniteTypeSet(); |
| 537: | if ($finiteTypeSet !== null && $finiteTypeSet->isComplete()) { |
| 538: | $otherFiniteTypeSet = $type->getFiniteTypeSet(); |
| 539: | if ($otherFiniteTypeSet !== null && $otherFiniteTypeSet->isComplete()) { |
| 540: | return $finiteTypeSet->containedIn($otherFiniteTypeSet)->yes(); |
| 541: | } |
| 542: | } |
| 543: | |
| 544: | $otherTypes = $type->types; |
| 545: | foreach ($this->types as $innerType) { |
| 546: | $match = false; |
| 547: | foreach ($otherTypes as $i => $otherType) { |
| 548: | if (!$innerType->equals($otherType)) { |
| 549: | continue; |
| 550: | } |
| 551: | |
| 552: | $match = true; |
| 553: | unset($otherTypes[$i]); |
| 554: | break; |
| 555: | } |
| 556: | |
| 557: | if (!$match) { |
| 558: | return false; |
| 559: | } |
| 560: | } |
| 561: | |
| 562: | return count($otherTypes) === 0; |
| 563: | } |
| 564: | |
| 565: | public function describe(VerbosityLevel $level): string |
| 566: | { |
| 567: | if (isset($this->cachedDescriptions[$level->getLevelValue()])) { |
| 568: | return $this->cachedDescriptions[$level->getLevelValue()]; |
| 569: | } |
| 570: | $benevolent = $this instanceof BenevolentUnionType; |
| 571: | $joinTypes = static function (array $types) use ($level, $benevolent): string { |
| 572: | $typeNames = []; |
| 573: | foreach ($types as $i => $type) { |
| 574: | if ($type instanceof ClosureType || $type instanceof CallableType || $type instanceof TemplateUnionType) { |
| 575: | $typeNames[] = sprintf('(%s)', $type->describe($level)); |
| 576: | } elseif ($type instanceof TemplateType) { |
| 577: | $isLast = $i >= count($types) - 1; |
| 578: | $bound = $type->getBound(); |
| 579: | if ( |
| 580: | !$isLast |
| 581: | && ($level->isTypeOnly() || $level->isValue()) |
| 582: | && !($bound instanceof MixedType && $bound->getSubtractedType() === null && !$bound instanceof TemplateMixedType) |
| 583: | ) { |
| 584: | $typeNames[] = sprintf('(%s)', $type->describe($level)); |
| 585: | } else { |
| 586: | $typeNames[] = $type->describe($level); |
| 587: | } |
| 588: | } elseif ($type instanceof IntersectionType) { |
| 589: | $intersectionDescription = $type->describe($level); |
| 590: | if (str_contains($intersectionDescription, '&')) { |
| 591: | $typeNames[] = sprintf('(%s)', $intersectionDescription); |
| 592: | } else { |
| 593: | $typeNames[] = $intersectionDescription; |
| 594: | } |
| 595: | } else { |
| 596: | $typeNames[] = $type->describe($level); |
| 597: | } |
| 598: | } |
| 599: | |
| 600: | if ($level->isPrecise() || $level->isCache()) { |
| 601: | $duplicates = array_diff_assoc($typeNames, array_unique($typeNames)); |
| 602: | if (count($duplicates) > 0) { |
| 603: | $indexByDuplicate = array_fill_keys($duplicates, 0); |
| 604: | foreach ($typeNames as $key => $typeName) { |
| 605: | if (!isset($indexByDuplicate[$typeName])) { |
| 606: | continue; |
| 607: | } |
| 608: | |
| 609: | $typeNames[$key] = $typeName . '#' . ++$indexByDuplicate[$typeName]; |
| 610: | } |
| 611: | } |
| 612: | } else { |
| 613: | $typeNames = array_unique($typeNames); |
| 614: | } |
| 615: | |
| 616: | if (count($typeNames) > 1024) { |
| 617: | $description = implode('|', array_slice($typeNames, 0, 1024)) . "|\u{2026}"; |
| 618: | } else { |
| 619: | $description = implode('|', $typeNames); |
| 620: | } |
| 621: | |
| 622: | |
| 623: | if ($benevolent && count($typeNames) > 1) { |
| 624: | return '(' . $description . ')'; |
| 625: | } |
| 626: | |
| 627: | return $description; |
| 628: | }; |
| 629: | |
| 630: | return $this->cachedDescriptions[$level->getLevelValue()] = $level->handle( |
| 631: | function () use ($joinTypes): string { |
| 632: | $types = TypeCombinator::union(...array_map(static function (Type $type): Type { |
| 633: | if ( |
| 634: | $type->isConstantValue()->yes() |
| 635: | && $type->isTrue()->or($type->isFalse())->no() |
| 636: | ) { |
| 637: | return $type->generalize(GeneralizePrecision::lessSpecific()); |
| 638: | } |
| 639: | |
| 640: | return $type; |
| 641: | }, $this->getSortedTypes())); |
| 642: | |
| 643: | if ($types instanceof UnionType) { |
| 644: | return $joinTypes($types->getSortedTypes()); |
| 645: | } |
| 646: | |
| 647: | return $joinTypes([$types]); |
| 648: | }, |
| 649: | fn (): string => $joinTypes($this->getSortedTypes()), |
| 650: | ); |
| 651: | } |
| 652: | |
| 653: | |
| 654: | |
| 655: | |
| 656: | |
| 657: | private function hasInternal( |
| 658: | callable $canCallback, |
| 659: | callable $hasCallback, |
| 660: | ): TrinaryLogic |
| 661: | { |
| 662: | return TrinaryLogic::lazyExtremeIdentity($this->types, static function (Type $type) use ($canCallback, $hasCallback): TrinaryLogic { |
| 663: | if ($canCallback($type)->no()) { |
| 664: | return TrinaryLogic::createNo(); |
| 665: | } |
| 666: | |
| 667: | return $hasCallback($type); |
| 668: | }); |
| 669: | } |
| 670: | |
| 671: | |
| 672: | |
| 673: | |
| 674: | |
| 675: | |
| 676: | |
| 677: | private function getInternal( |
| 678: | callable $hasCallback, |
| 679: | callable $getCallback, |
| 680: | ): object |
| 681: | { |
| 682: | |
| 683: | $result = null; |
| 684: | |
| 685: | |
| 686: | $object = null; |
| 687: | foreach ($this->types as $type) { |
| 688: | $has = $hasCallback($type); |
| 689: | if (!$has->yes()) { |
| 690: | continue; |
| 691: | } |
| 692: | if ($result !== null && $result->compareTo($has) !== $has) { |
| 693: | continue; |
| 694: | } |
| 695: | |
| 696: | $get = $getCallback($type); |
| 697: | $result = $has; |
| 698: | $object = $get; |
| 699: | } |
| 700: | |
| 701: | if ($object === null) { |
| 702: | throw new ShouldNotHappenException(); |
| 703: | } |
| 704: | |
| 705: | return $object; |
| 706: | } |
| 707: | |
| 708: | public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type |
| 709: | { |
| 710: | return $this->unionTypes(static fn (Type $type): Type => $type->getTemplateType($ancestorClassName, $templateTypeName)); |
| 711: | } |
| 712: | |
| 713: | public function isObject(): TrinaryLogic |
| 714: | { |
| 715: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isObject()); |
| 716: | } |
| 717: | |
| 718: | public function getClassStringType(): Type |
| 719: | { |
| 720: | return $this->unionTypes(static fn (Type $type): Type => $type->getClassStringType()); |
| 721: | } |
| 722: | |
| 723: | public function isEnum(): TrinaryLogic |
| 724: | { |
| 725: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isEnum()); |
| 726: | } |
| 727: | |
| 728: | public function canAccessProperties(): TrinaryLogic |
| 729: | { |
| 730: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->canAccessProperties()); |
| 731: | } |
| 732: | |
| 733: | public function hasProperty(string $propertyName): TrinaryLogic |
| 734: | { |
| 735: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->hasProperty($propertyName)); |
| 736: | } |
| 737: | |
| 738: | public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection |
| 739: | { |
| 740: | return $this->getUnresolvedPropertyPrototype($propertyName, $scope)->getTransformedProperty(); |
| 741: | } |
| 742: | |
| 743: | public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection |
| 744: | { |
| 745: | $propertyPrototypes = []; |
| 746: | foreach ($this->types as $type) { |
| 747: | if (!$type->hasProperty($propertyName)->yes()) { |
| 748: | continue; |
| 749: | } |
| 750: | |
| 751: | $propertyPrototypes[] = $type->getUnresolvedPropertyPrototype($propertyName, $scope)->withFechedOnType($this); |
| 752: | } |
| 753: | |
| 754: | $propertiesCount = count($propertyPrototypes); |
| 755: | if ($propertiesCount === 0) { |
| 756: | throw new MissingPropertyFromReflectionException($this->describe(VerbosityLevel::typeOnly()), $propertyName); |
| 757: | } |
| 758: | |
| 759: | if ($propertiesCount === 1) { |
| 760: | return $propertyPrototypes[0]; |
| 761: | } |
| 762: | |
| 763: | return new UnionTypeUnresolvedPropertyPrototypeReflection($propertyPrototypes); |
| 764: | } |
| 765: | |
| 766: | public function hasInstanceProperty(string $propertyName): TrinaryLogic |
| 767: | { |
| 768: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->hasInstanceProperty($propertyName)); |
| 769: | } |
| 770: | |
| 771: | public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection |
| 772: | { |
| 773: | return $this->getUnresolvedInstancePropertyPrototype($propertyName, $scope)->getTransformedProperty(); |
| 774: | } |
| 775: | |
| 776: | public function getUnresolvedInstancePropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection |
| 777: | { |
| 778: | $propertyPrototypes = []; |
| 779: | foreach ($this->types as $type) { |
| 780: | if (!$type->hasInstanceProperty($propertyName)->yes()) { |
| 781: | continue; |
| 782: | } |
| 783: | |
| 784: | $propertyPrototypes[] = $type->getUnresolvedInstancePropertyPrototype($propertyName, $scope)->withFechedOnType($this); |
| 785: | } |
| 786: | |
| 787: | $propertiesCount = count($propertyPrototypes); |
| 788: | if ($propertiesCount === 0) { |
| 789: | throw new MissingPropertyFromReflectionException($this->describe(VerbosityLevel::typeOnly()), $propertyName); |
| 790: | } |
| 791: | |
| 792: | if ($propertiesCount === 1) { |
| 793: | return $propertyPrototypes[0]; |
| 794: | } |
| 795: | |
| 796: | return new UnionTypeUnresolvedPropertyPrototypeReflection($propertyPrototypes); |
| 797: | } |
| 798: | |
| 799: | public function hasStaticProperty(string $propertyName): TrinaryLogic |
| 800: | { |
| 801: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->hasStaticProperty($propertyName)); |
| 802: | } |
| 803: | |
| 804: | public function getStaticProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection |
| 805: | { |
| 806: | return $this->getUnresolvedStaticPropertyPrototype($propertyName, $scope)->getTransformedProperty(); |
| 807: | } |
| 808: | |
| 809: | public function getUnresolvedStaticPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection |
| 810: | { |
| 811: | $propertyPrototypes = []; |
| 812: | foreach ($this->types as $type) { |
| 813: | if (!$type->hasStaticProperty($propertyName)->yes()) { |
| 814: | continue; |
| 815: | } |
| 816: | |
| 817: | $propertyPrototypes[] = $type->getUnresolvedStaticPropertyPrototype($propertyName, $scope)->withFechedOnType($this); |
| 818: | } |
| 819: | |
| 820: | $propertiesCount = count($propertyPrototypes); |
| 821: | if ($propertiesCount === 0) { |
| 822: | throw new MissingPropertyFromReflectionException($this->describe(VerbosityLevel::typeOnly()), $propertyName); |
| 823: | } |
| 824: | |
| 825: | if ($propertiesCount === 1) { |
| 826: | return $propertyPrototypes[0]; |
| 827: | } |
| 828: | |
| 829: | return new UnionTypeUnresolvedPropertyPrototypeReflection($propertyPrototypes); |
| 830: | } |
| 831: | |
| 832: | public function canCallMethods(): TrinaryLogic |
| 833: | { |
| 834: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->canCallMethods()); |
| 835: | } |
| 836: | |
| 837: | public function hasMethod(string $methodName): TrinaryLogic |
| 838: | { |
| 839: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->hasMethod($methodName)); |
| 840: | } |
| 841: | |
| 842: | public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection |
| 843: | { |
| 844: | return $this->getUnresolvedMethodPrototype($methodName, $scope)->getTransformedMethod(); |
| 845: | } |
| 846: | |
| 847: | public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection |
| 848: | { |
| 849: | $methodPrototypes = []; |
| 850: | foreach ($this->types as $type) { |
| 851: | if (!$type->hasMethod($methodName)->yes()) { |
| 852: | continue; |
| 853: | } |
| 854: | |
| 855: | $prototype = $type->getUnresolvedMethodPrototype($methodName, $scope); |
| 856: | if ($this instanceof TemplateType) { |
| 857: | $prototype = $prototype->withCalledOnType($this); |
| 858: | } |
| 859: | $methodPrototypes[] = $prototype; |
| 860: | } |
| 861: | |
| 862: | $methodsCount = count($methodPrototypes); |
| 863: | if ($methodsCount === 0) { |
| 864: | throw new MissingMethodFromReflectionException($this->describe(VerbosityLevel::typeOnly()), $methodName); |
| 865: | } |
| 866: | |
| 867: | if ($methodsCount === 1) { |
| 868: | return $methodPrototypes[0]; |
| 869: | } |
| 870: | |
| 871: | return new UnionTypeUnresolvedMethodPrototypeReflection($methodName, $methodPrototypes); |
| 872: | } |
| 873: | |
| 874: | public function canAccessConstants(): TrinaryLogic |
| 875: | { |
| 876: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->canAccessConstants()); |
| 877: | } |
| 878: | |
| 879: | public function hasConstant(string $constantName): TrinaryLogic |
| 880: | { |
| 881: | return $this->hasInternal( |
| 882: | static fn (Type $type): TrinaryLogic => $type->canAccessConstants(), |
| 883: | static fn (Type $type): TrinaryLogic => $type->hasConstant($constantName), |
| 884: | ); |
| 885: | } |
| 886: | |
| 887: | public function getConstant(string $constantName): ClassConstantReflection |
| 888: | { |
| 889: | return $this->getInternal( |
| 890: | static fn (Type $type): TrinaryLogic => $type->hasConstant($constantName), |
| 891: | static fn (Type $type): ClassConstantReflection => $type->getConstant($constantName), |
| 892: | ); |
| 893: | } |
| 894: | |
| 895: | public function isIterable(): TrinaryLogic |
| 896: | { |
| 897: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isIterable()); |
| 898: | } |
| 899: | |
| 900: | public function isIterableAtLeastOnce(): TrinaryLogic |
| 901: | { |
| 902: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isIterableAtLeastOnce()); |
| 903: | } |
| 904: | |
| 905: | public function getArraySize(): Type |
| 906: | { |
| 907: | return $this->unionTypes(static fn (Type $type): Type => $type->getArraySize()); |
| 908: | } |
| 909: | |
| 910: | public function getIterableKeyType(): Type |
| 911: | { |
| 912: | return $this->unionTypes(static fn (Type $type): Type => $type->getIterableKeyType()); |
| 913: | } |
| 914: | |
| 915: | public function getFirstIterableKeyType(): Type |
| 916: | { |
| 917: | return $this->unionTypes(static fn (Type $type): Type => $type->getIterableKeyType()); |
| 918: | } |
| 919: | |
| 920: | public function getLastIterableKeyType(): Type |
| 921: | { |
| 922: | return $this->unionTypes(static fn (Type $type): Type => $type->getIterableKeyType()); |
| 923: | } |
| 924: | |
| 925: | public function getIterableValueType(): Type |
| 926: | { |
| 927: | return $this->unionTypes(static fn (Type $type): Type => $type->getIterableValueType()); |
| 928: | } |
| 929: | |
| 930: | public function getFirstIterableValueType(): Type |
| 931: | { |
| 932: | return $this->unionTypes(static fn (Type $type): Type => $type->getIterableValueType()); |
| 933: | } |
| 934: | |
| 935: | public function getLastIterableValueType(): Type |
| 936: | { |
| 937: | return $this->unionTypes(static fn (Type $type): Type => $type->getIterableValueType()); |
| 938: | } |
| 939: | |
| 940: | public function isArray(): TrinaryLogic |
| 941: | { |
| 942: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isArray()); |
| 943: | } |
| 944: | |
| 945: | public function isConstantArray(): TrinaryLogic |
| 946: | { |
| 947: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isConstantArray()); |
| 948: | } |
| 949: | |
| 950: | public function isOversizedArray(): TrinaryLogic |
| 951: | { |
| 952: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isOversizedArray()); |
| 953: | } |
| 954: | |
| 955: | public function isList(): TrinaryLogic |
| 956: | { |
| 957: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isList()); |
| 958: | } |
| 959: | |
| 960: | public function isString(): TrinaryLogic |
| 961: | { |
| 962: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isString()); |
| 963: | } |
| 964: | |
| 965: | public function isNumericString(): TrinaryLogic |
| 966: | { |
| 967: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isNumericString()); |
| 968: | } |
| 969: | |
| 970: | public function isDecimalIntegerString(): TrinaryLogic |
| 971: | { |
| 972: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isDecimalIntegerString()); |
| 973: | } |
| 974: | |
| 975: | public function isNonEmptyString(): TrinaryLogic |
| 976: | { |
| 977: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isNonEmptyString()); |
| 978: | } |
| 979: | |
| 980: | public function isNonFalsyString(): TrinaryLogic |
| 981: | { |
| 982: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isNonFalsyString()); |
| 983: | } |
| 984: | |
| 985: | public function isLiteralString(): TrinaryLogic |
| 986: | { |
| 987: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isLiteralString()); |
| 988: | } |
| 989: | |
| 990: | public function isLowercaseString(): TrinaryLogic |
| 991: | { |
| 992: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isLowercaseString()); |
| 993: | } |
| 994: | |
| 995: | public function isUppercaseString(): TrinaryLogic |
| 996: | { |
| 997: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isUppercaseString()); |
| 998: | } |
| 999: | |
| 1000: | public function isClassString(): TrinaryLogic |
| 1001: | { |
| 1002: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isClassString()); |
| 1003: | } |
| 1004: | |
| 1005: | public function getClassStringObjectType(): Type |
| 1006: | { |
| 1007: | return $this->unionTypes(static fn (Type $type): Type => $type->getClassStringObjectType()); |
| 1008: | } |
| 1009: | |
| 1010: | public function getObjectTypeOrClassStringObjectType(): Type |
| 1011: | { |
| 1012: | return $this->unionTypes(static fn (Type $type): Type => $type->getObjectTypeOrClassStringObjectType()); |
| 1013: | } |
| 1014: | |
| 1015: | public function isVoid(): TrinaryLogic |
| 1016: | { |
| 1017: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isVoid()); |
| 1018: | } |
| 1019: | |
| 1020: | public function isScalar(): TrinaryLogic |
| 1021: | { |
| 1022: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isScalar()); |
| 1023: | } |
| 1024: | |
| 1025: | public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType |
| 1026: | { |
| 1027: | return $this->notBenevolentUnionResults( |
| 1028: | static fn (Type $innerType): TrinaryLogic => $innerType->looseCompare($type, $phpVersion)->toTrinaryLogic(), |
| 1029: | )->toBooleanType(); |
| 1030: | } |
| 1031: | |
| 1032: | public function isOffsetAccessible(): TrinaryLogic |
| 1033: | { |
| 1034: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isOffsetAccessible()); |
| 1035: | } |
| 1036: | |
| 1037: | public function isOffsetAccessLegal(): TrinaryLogic |
| 1038: | { |
| 1039: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isOffsetAccessLegal()); |
| 1040: | } |
| 1041: | |
| 1042: | public function hasOffsetValueType(Type $offsetType): TrinaryLogic |
| 1043: | { |
| 1044: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->hasOffsetValueType($offsetType)); |
| 1045: | } |
| 1046: | |
| 1047: | public function getOffsetValueType(Type $offsetType): Type |
| 1048: | { |
| 1049: | |
| 1050: | |
| 1051: | |
| 1052: | |
| 1053: | return $this->cachedGetOffsetValueType[$offsetType->describe(VerbosityLevel::cache())] ??= $this->doGetOffsetValueType($offsetType); |
| 1054: | } |
| 1055: | |
| 1056: | private function doGetOffsetValueType(Type $offsetType): Type |
| 1057: | { |
| 1058: | $types = []; |
| 1059: | foreach ($this->types as $innerType) { |
| 1060: | $valueType = $innerType->getOffsetValueType($offsetType); |
| 1061: | if ($valueType instanceof ErrorType) { |
| 1062: | continue; |
| 1063: | } |
| 1064: | |
| 1065: | $types[] = $valueType; |
| 1066: | } |
| 1067: | |
| 1068: | if (count($types) === 0) { |
| 1069: | return new ErrorType(); |
| 1070: | } |
| 1071: | |
| 1072: | return TypeCombinator::union(...$types); |
| 1073: | } |
| 1074: | |
| 1075: | public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type |
| 1076: | { |
| 1077: | return $this->unionTypes(static fn (Type $type): Type => $type->setOffsetValueType($offsetType, $valueType, $unionValues)); |
| 1078: | } |
| 1079: | |
| 1080: | public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type |
| 1081: | { |
| 1082: | return $this->unionTypes(static fn (Type $type): Type => $type->setExistingOffsetValueType($offsetType, $valueType)); |
| 1083: | } |
| 1084: | |
| 1085: | public function unsetOffset(Type $offsetType): Type |
| 1086: | { |
| 1087: | return $this->unionTypes(static fn (Type $type): Type => $type->unsetOffset($offsetType)); |
| 1088: | } |
| 1089: | |
| 1090: | public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type |
| 1091: | { |
| 1092: | return $this->unionTypes(static fn (Type $type): Type => $type->getKeysArrayFiltered($filterValueType, $strict)); |
| 1093: | } |
| 1094: | |
| 1095: | public function getKeysArray(): Type |
| 1096: | { |
| 1097: | return $this->unionTypes(static fn (Type $type): Type => $type->getKeysArray()); |
| 1098: | } |
| 1099: | |
| 1100: | public function getValuesArray(): Type |
| 1101: | { |
| 1102: | return $this->unionTypes(static fn (Type $type): Type => $type->getValuesArray()); |
| 1103: | } |
| 1104: | |
| 1105: | public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type |
| 1106: | { |
| 1107: | return $this->unionTypes(static fn (Type $type): Type => $type->chunkArray($lengthType, $preserveKeys)); |
| 1108: | } |
| 1109: | |
| 1110: | public function fillKeysArray(Type $valueType): Type |
| 1111: | { |
| 1112: | return $this->unionTypes(static fn (Type $type): Type => $type->fillKeysArray($valueType)); |
| 1113: | } |
| 1114: | |
| 1115: | public function flipArray(): Type |
| 1116: | { |
| 1117: | return $this->unionTypes(static fn (Type $type): Type => $type->flipArray()); |
| 1118: | } |
| 1119: | |
| 1120: | public function intersectKeyArray(Type $otherArraysType): Type |
| 1121: | { |
| 1122: | return $this->unionTypes(static fn (Type $type): Type => $type->intersectKeyArray($otherArraysType)); |
| 1123: | } |
| 1124: | |
| 1125: | public function popArray(): Type |
| 1126: | { |
| 1127: | return $this->unionTypes(static fn (Type $type): Type => $type->popArray()); |
| 1128: | } |
| 1129: | |
| 1130: | public function reverseArray(TrinaryLogic $preserveKeys): Type |
| 1131: | { |
| 1132: | return $this->unionTypes(static fn (Type $type): Type => $type->reverseArray($preserveKeys)); |
| 1133: | } |
| 1134: | |
| 1135: | public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type |
| 1136: | { |
| 1137: | return $this->unionTypes(static fn (Type $type): Type => $type->searchArray($needleType, $strict)); |
| 1138: | } |
| 1139: | |
| 1140: | public function shiftArray(): Type |
| 1141: | { |
| 1142: | return $this->unionTypes(static fn (Type $type): Type => $type->shiftArray()); |
| 1143: | } |
| 1144: | |
| 1145: | public function shuffleArray(): Type |
| 1146: | { |
| 1147: | return $this->unionTypes(static fn (Type $type): Type => $type->shuffleArray()); |
| 1148: | } |
| 1149: | |
| 1150: | public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type |
| 1151: | { |
| 1152: | return $this->unionTypes(static fn (Type $type): Type => $type->sliceArray($offsetType, $lengthType, $preserveKeys)); |
| 1153: | } |
| 1154: | |
| 1155: | public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type |
| 1156: | { |
| 1157: | return $this->unionTypes(static fn (Type $type): Type => $type->spliceArray($offsetType, $lengthType, $replacementType)); |
| 1158: | } |
| 1159: | |
| 1160: | public function truncateListToSize(Type $sizeType): Type |
| 1161: | { |
| 1162: | return $this->unionTypes(static fn (Type $type): Type => $type->truncateListToSize($sizeType)); |
| 1163: | } |
| 1164: | |
| 1165: | public function makeListMaybe(): Type |
| 1166: | { |
| 1167: | return $this->unionTypes(static fn (Type $type): Type => $type->makeListMaybe()); |
| 1168: | } |
| 1169: | |
| 1170: | public function mapValueType(callable $cb): Type |
| 1171: | { |
| 1172: | return $this->unionTypes(static fn (Type $type): Type => $type->mapValueType($cb)); |
| 1173: | } |
| 1174: | |
| 1175: | public function mapKeyType(callable $cb): Type |
| 1176: | { |
| 1177: | return $this->unionTypes(static fn (Type $type): Type => $type->mapKeyType($cb)); |
| 1178: | } |
| 1179: | |
| 1180: | public function makeAllArrayKeysOptional(): Type |
| 1181: | { |
| 1182: | return $this->unionTypes(static fn (Type $type): Type => $type->makeAllArrayKeysOptional()); |
| 1183: | } |
| 1184: | |
| 1185: | public function changeKeyCaseArray(?int $case): Type |
| 1186: | { |
| 1187: | return $this->unionTypes(static fn (Type $type): Type => $type->changeKeyCaseArray($case)); |
| 1188: | } |
| 1189: | |
| 1190: | public function filterArrayRemovingFalsey(): Type |
| 1191: | { |
| 1192: | return $this->unionTypes(static fn (Type $type): Type => $type->filterArrayRemovingFalsey()); |
| 1193: | } |
| 1194: | |
| 1195: | public function getEnumCases(): array |
| 1196: | { |
| 1197: | return $this->pickFromTypes( |
| 1198: | static fn (Type $type) => $type->getEnumCases(), |
| 1199: | static fn (Type $type) => $type->isObject()->yes(), |
| 1200: | ); |
| 1201: | } |
| 1202: | |
| 1203: | public function getEnumCaseObject(): ?EnumCaseObjectType |
| 1204: | { |
| 1205: | $cases = $this->getEnumCases(); |
| 1206: | |
| 1207: | if (count($cases) === 1) { |
| 1208: | return $cases[0]; |
| 1209: | } |
| 1210: | |
| 1211: | return null; |
| 1212: | } |
| 1213: | |
| 1214: | public function isCallable(): TrinaryLogic |
| 1215: | { |
| 1216: | return $this->isCallable ??= $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isCallable()); |
| 1217: | } |
| 1218: | |
| 1219: | public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array |
| 1220: | { |
| 1221: | $acceptors = []; |
| 1222: | |
| 1223: | foreach ($this->types as $type) { |
| 1224: | if ($type->isCallable()->no()) { |
| 1225: | continue; |
| 1226: | } |
| 1227: | |
| 1228: | $acceptors = array_merge($acceptors, $type->getCallableParametersAcceptors($scope)); |
| 1229: | } |
| 1230: | |
| 1231: | if (count($acceptors) === 0) { |
| 1232: | throw new ShouldNotHappenException(); |
| 1233: | } |
| 1234: | |
| 1235: | return $acceptors; |
| 1236: | } |
| 1237: | |
| 1238: | public function isCloneable(): TrinaryLogic |
| 1239: | { |
| 1240: | return $this->unionResults(static fn (Type $type): TrinaryLogic => $type->isCloneable()); |
| 1241: | } |
| 1242: | |
| 1243: | public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
| 1244: | { |
| 1245: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThan($otherType, $phpVersion)); |
| 1246: | } |
| 1247: | |
| 1248: | public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
| 1249: | { |
| 1250: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isSmallerThanOrEqual($otherType, $phpVersion)); |
| 1251: | } |
| 1252: | |
| 1253: | public function isNull(): TrinaryLogic |
| 1254: | { |
| 1255: | return $this->isNull ??= $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isNull()); |
| 1256: | } |
| 1257: | |
| 1258: | public function isConstantValue(): TrinaryLogic |
| 1259: | { |
| 1260: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isConstantValue()); |
| 1261: | } |
| 1262: | |
| 1263: | public function isConstantScalarValue(): TrinaryLogic |
| 1264: | { |
| 1265: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isConstantScalarValue()); |
| 1266: | } |
| 1267: | |
| 1268: | public function getConstantScalarTypes(): array |
| 1269: | { |
| 1270: | return $this->notBenevolentPickFromTypes(static fn (Type $type) => $type->getConstantScalarTypes()); |
| 1271: | } |
| 1272: | |
| 1273: | public function getConstantScalarValues(): array |
| 1274: | { |
| 1275: | return $this->notBenevolentPickFromTypes(static fn (Type $type) => $type->getConstantScalarValues()); |
| 1276: | } |
| 1277: | |
| 1278: | public function isTrue(): TrinaryLogic |
| 1279: | { |
| 1280: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isTrue()); |
| 1281: | } |
| 1282: | |
| 1283: | public function isFalse(): TrinaryLogic |
| 1284: | { |
| 1285: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isFalse()); |
| 1286: | } |
| 1287: | |
| 1288: | public function isBoolean(): TrinaryLogic |
| 1289: | { |
| 1290: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isBoolean()); |
| 1291: | } |
| 1292: | |
| 1293: | public function isFloat(): TrinaryLogic |
| 1294: | { |
| 1295: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isFloat()); |
| 1296: | } |
| 1297: | |
| 1298: | public function isInteger(): TrinaryLogic |
| 1299: | { |
| 1300: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $type->isInteger()); |
| 1301: | } |
| 1302: | |
| 1303: | public function getSmallerType(PhpVersion $phpVersion): Type |
| 1304: | { |
| 1305: | return $this->unionTypes(static fn (Type $type): Type => $type->getSmallerType($phpVersion)); |
| 1306: | } |
| 1307: | |
| 1308: | public function getSmallerOrEqualType(PhpVersion $phpVersion): Type |
| 1309: | { |
| 1310: | return $this->unionTypes(static fn (Type $type): Type => $type->getSmallerOrEqualType($phpVersion)); |
| 1311: | } |
| 1312: | |
| 1313: | public function getGreaterType(PhpVersion $phpVersion): Type |
| 1314: | { |
| 1315: | return $this->unionTypes(static fn (Type $type): Type => $type->getGreaterType($phpVersion)); |
| 1316: | } |
| 1317: | |
| 1318: | public function getGreaterOrEqualType(PhpVersion $phpVersion): Type |
| 1319: | { |
| 1320: | return $this->unionTypes(static fn (Type $type): Type => $type->getGreaterOrEqualType($phpVersion)); |
| 1321: | } |
| 1322: | |
| 1323: | public function isGreaterThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
| 1324: | { |
| 1325: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $otherType->isSmallerThan($type, $phpVersion)); |
| 1326: | } |
| 1327: | |
| 1328: | public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic |
| 1329: | { |
| 1330: | return $this->notBenevolentUnionResults(static fn (Type $type): TrinaryLogic => $otherType->isSmallerThanOrEqual($type, $phpVersion)); |
| 1331: | } |
| 1332: | |
| 1333: | public function toBoolean(): BooleanType |
| 1334: | { |
| 1335: | |
| 1336: | $type = $this->unionTypes(static fn (Type $type): BooleanType => $type->toBoolean()); |
| 1337: | |
| 1338: | return $type; |
| 1339: | } |
| 1340: | |
| 1341: | public function toNumber(): Type |
| 1342: | { |
| 1343: | $type = $this->unionTypes(static fn (Type $type): Type => $type->toNumber()); |
| 1344: | |
| 1345: | return $type; |
| 1346: | } |
| 1347: | |
| 1348: | public function toNumberSet(): ?NumberSet |
| 1349: | { |
| 1350: | $integerIntervals = []; |
| 1351: | $floatIntervals = []; |
| 1352: | $nan = false; |
| 1353: | foreach ($this->types as $type) { |
| 1354: | $numberSet = $type->toNumberSet(); |
| 1355: | if ($numberSet === null) { |
| 1356: | return null; |
| 1357: | } |
| 1358: | |
| 1359: | $integerIntervals = array_merge($integerIntervals, $numberSet->getIntegerIntervals()); |
| 1360: | $floatIntervals = array_merge($floatIntervals, $numberSet->getFloatIntervals()); |
| 1361: | $nan = $nan || $numberSet->hasNan(); |
| 1362: | } |
| 1363: | |
| 1364: | return new NumberSet($integerIntervals, $floatIntervals, $nan); |
| 1365: | } |
| 1366: | |
| 1367: | public function toBitwiseNotType(): Type |
| 1368: | { |
| 1369: | return $this->unionTypes(static fn (Type $type): Type => $type->toBitwiseNotType()); |
| 1370: | } |
| 1371: | |
| 1372: | public function toGetClassResultType(): Type |
| 1373: | { |
| 1374: | return $this->unionTypes(static fn (Type $type): Type => $type->toGetClassResultType()); |
| 1375: | } |
| 1376: | |
| 1377: | public function toClassConstantType(ReflectionProvider $reflectionProvider): Type |
| 1378: | { |
| 1379: | return $this->unionTypes(static fn (Type $type): Type => $type->toClassConstantType($reflectionProvider)); |
| 1380: | } |
| 1381: | |
| 1382: | public function toObjectTypeForInstanceofCheck(): ClassNameToObjectTypeResult |
| 1383: | { |
| 1384: | $types = []; |
| 1385: | $uncertainty = false; |
| 1386: | foreach ($this->getTypes() as $innerType) { |
| 1387: | $result = $innerType->toObjectTypeForInstanceofCheck(); |
| 1388: | $types[] = $result->type; |
| 1389: | if (!$result->uncertainty) { |
| 1390: | continue; |
| 1391: | } |
| 1392: | |
| 1393: | $uncertainty = true; |
| 1394: | } |
| 1395: | |
| 1396: | return new ClassNameToObjectTypeResult(TypeCombinator::union(...$types), $uncertainty); |
| 1397: | } |
| 1398: | |
| 1399: | public function toObjectTypeForIsACheck(Type $objectOrClassType, bool $allowString, bool $allowSameClass): ClassNameToObjectTypeResult |
| 1400: | { |
| 1401: | $types = []; |
| 1402: | $uncertainty = false; |
| 1403: | foreach ($this->getTypes() as $innerType) { |
| 1404: | $result = $innerType->toObjectTypeForIsACheck($objectOrClassType, $allowString, $allowSameClass); |
| 1405: | $types[] = $result->type; |
| 1406: | if (!$result->uncertainty) { |
| 1407: | continue; |
| 1408: | } |
| 1409: | |
| 1410: | $uncertainty = true; |
| 1411: | } |
| 1412: | |
| 1413: | return new ClassNameToObjectTypeResult(TypeCombinator::union(...$types), $uncertainty); |
| 1414: | } |
| 1415: | |
| 1416: | public function toAbsoluteNumber(): Type |
| 1417: | { |
| 1418: | $type = $this->unionTypes(static fn (Type $type): Type => $type->toAbsoluteNumber()); |
| 1419: | |
| 1420: | return $type; |
| 1421: | } |
| 1422: | |
| 1423: | public function toString(): Type |
| 1424: | { |
| 1425: | $type = $this->unionTypes(static fn (Type $type): Type => $type->toString()); |
| 1426: | |
| 1427: | return $type; |
| 1428: | } |
| 1429: | |
| 1430: | public function toInteger(): Type |
| 1431: | { |
| 1432: | $type = $this->unionTypes(static fn (Type $type): Type => $type->toInteger()); |
| 1433: | |
| 1434: | return $type; |
| 1435: | } |
| 1436: | |
| 1437: | public function toFloat(): Type |
| 1438: | { |
| 1439: | $type = $this->unionTypes(static fn (Type $type): Type => $type->toFloat()); |
| 1440: | |
| 1441: | return $type; |
| 1442: | } |
| 1443: | |
| 1444: | public function toArray(): Type |
| 1445: | { |
| 1446: | $type = $this->unionTypes(static fn (Type $type): Type => $type->toArray()); |
| 1447: | |
| 1448: | return $type; |
| 1449: | } |
| 1450: | |
| 1451: | public function toArrayKey(): Type |
| 1452: | { |
| 1453: | $level = ReportUnsafeArrayStringKeyCastingToggle::getLevel(); |
| 1454: | if ($level !== ReportUnsafeArrayStringKeyCastingToggle::PREVENT || $this->isInteger()->no()) { |
| 1455: | return $this->unionTypes(static fn (Type $type): Type => $type->toArrayKey()); |
| 1456: | } |
| 1457: | |
| 1458: | return $this->unionTypes(static function (Type $type): Type { |
| 1459: | if ($type instanceof StringType) { |
| 1460: | return $type; |
| 1461: | } |
| 1462: | |
| 1463: | return $type->toArrayKey(); |
| 1464: | }); |
| 1465: | } |
| 1466: | |
| 1467: | public function toCoercedArgumentType(bool $strictTypes): Type |
| 1468: | { |
| 1469: | return $this->unionTypes(static fn (Type $type): Type => $type->toCoercedArgumentType($strictTypes)); |
| 1470: | } |
| 1471: | |
| 1472: | public function inferTemplateTypes(Type $receivedType): TemplateTypeMap |
| 1473: | { |
| 1474: | if ($receivedType instanceof IterableType) { |
| 1475: | $receivedType = $receivedType->toArrayOrTraversable(); |
| 1476: | } |
| 1477: | |
| 1478: | |
| 1479: | |
| 1480: | |
| 1481: | |
| 1482: | |
| 1483: | |
| 1484: | $types = TemplateTypeMap::createEmpty(); |
| 1485: | $receivedTypes = $receivedType instanceof UnionType ? $receivedType->getTypes() : [$receivedType]; |
| 1486: | $remainingReceivedTypes = []; |
| 1487: | foreach ($receivedTypes as $receivedInnerType) { |
| 1488: | if ($receivedInnerType instanceof NeverType) { |
| 1489: | $remainingReceivedTypes[] = $receivedInnerType; |
| 1490: | continue; |
| 1491: | } |
| 1492: | foreach ($this->types as $type) { |
| 1493: | if ($type->isSuperTypeOf($receivedInnerType)->yes()) { |
| 1494: | $types = $types->union($type->inferTemplateTypes($receivedInnerType)); |
| 1495: | continue 2; |
| 1496: | } |
| 1497: | } |
| 1498: | $remainingReceivedTypes[] = $receivedInnerType; |
| 1499: | } |
| 1500: | if (count($remainingReceivedTypes) === 0) { |
| 1501: | return $types->union($this->getAbsorbedTemplateTypes($types)); |
| 1502: | } |
| 1503: | if (count($remainingReceivedTypes) !== count($receivedTypes)) { |
| 1504: | $receivedType = TypeCombinator::union(...$remainingReceivedTypes); |
| 1505: | } |
| 1506: | |
| 1507: | foreach ($this->types as $type) { |
| 1508: | if (self::getNakedTemplateType($type) !== null) { |
| 1509: | continue; |
| 1510: | } |
| 1511: | $types = $types->union($type->inferTemplateTypes($receivedType)); |
| 1512: | } |
| 1513: | |
| 1514: | if (!$types->isEmpty()) { |
| 1515: | return $types; |
| 1516: | } |
| 1517: | |
| 1518: | foreach ($this->types as $type) { |
| 1519: | $types = $types->union($type->inferTemplateTypes($receivedType)); |
| 1520: | } |
| 1521: | |
| 1522: | return $types; |
| 1523: | } |
| 1524: | |
| 1525: | private static function getNakedTemplateType(Type $type): ?TemplateType |
| 1526: | { |
| 1527: | if ($type instanceof TemplateType) { |
| 1528: | return $type; |
| 1529: | } |
| 1530: | |
| 1531: | if ($type instanceof GenericClassStringType) { |
| 1532: | $genericType = $type->getGenericType(); |
| 1533: | if ($genericType instanceof TemplateType) { |
| 1534: | return $genericType; |
| 1535: | } |
| 1536: | } |
| 1537: | |
| 1538: | return null; |
| 1539: | } |
| 1540: | |
| 1541: | |
| 1542: | |
| 1543: | |
| 1544: | |
| 1545: | |
| 1546: | private function getAbsorbedTemplateTypes(TemplateTypeMap $inferred): TemplateTypeMap |
| 1547: | { |
| 1548: | $absorbed = []; |
| 1549: | foreach ($this->types as $type) { |
| 1550: | $templateType = self::getNakedTemplateType($type); |
| 1551: | if ($templateType === null) { |
| 1552: | continue; |
| 1553: | } |
| 1554: | if ($inferred->hasType($templateType->getName())) { |
| 1555: | continue; |
| 1556: | } |
| 1557: | |
| 1558: | $absorbed[$templateType->getName()] = new AbsorbedTemplateArgumentType(); |
| 1559: | } |
| 1560: | |
| 1561: | if (count($absorbed) === 0) { |
| 1562: | return TemplateTypeMap::createEmpty(); |
| 1563: | } |
| 1564: | |
| 1565: | return new TemplateTypeMap($absorbed); |
| 1566: | } |
| 1567: | |
| 1568: | public function inferTemplateTypesOn(Type $templateType): TemplateTypeMap |
| 1569: | { |
| 1570: | $types = TemplateTypeMap::createEmpty(); |
| 1571: | |
| 1572: | foreach ($this->types as $type) { |
| 1573: | $types = $types->union($templateType->inferTemplateTypes($type)); |
| 1574: | } |
| 1575: | |
| 1576: | return $types; |
| 1577: | } |
| 1578: | |
| 1579: | public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array |
| 1580: | { |
| 1581: | $references = []; |
| 1582: | |
| 1583: | foreach ($this->types as $type) { |
| 1584: | foreach ($type->getReferencedTemplateTypes($positionVariance) as $reference) { |
| 1585: | $references[] = $reference; |
| 1586: | } |
| 1587: | } |
| 1588: | |
| 1589: | return $references; |
| 1590: | } |
| 1591: | |
| 1592: | public function traverse(callable $cb): Type |
| 1593: | { |
| 1594: | $types = []; |
| 1595: | $changed = false; |
| 1596: | |
| 1597: | foreach ($this->types as $type) { |
| 1598: | $newType = $cb($type); |
| 1599: | if ($type !== $newType) { |
| 1600: | $changed = true; |
| 1601: | } |
| 1602: | $types[] = $newType; |
| 1603: | } |
| 1604: | |
| 1605: | if ($changed) { |
| 1606: | return TypeCombinator::union(...$types); |
| 1607: | } |
| 1608: | |
| 1609: | return $this; |
| 1610: | } |
| 1611: | |
| 1612: | public function traverseSimultaneously(Type $right, callable $cb): Type |
| 1613: | { |
| 1614: | $rightTypes = TypeUtils::flattenTypes($right); |
| 1615: | $newTypes = []; |
| 1616: | $changed = false; |
| 1617: | foreach ($this->types as $innerType) { |
| 1618: | $candidates = []; |
| 1619: | foreach ($rightTypes as $i => $rightType) { |
| 1620: | if (!$innerType->isSuperTypeOf($rightType)->yes()) { |
| 1621: | continue; |
| 1622: | } |
| 1623: | |
| 1624: | $candidates[] = $rightType; |
| 1625: | unset($rightTypes[$i]); |
| 1626: | } |
| 1627: | |
| 1628: | if (count($candidates) === 0) { |
| 1629: | $newTypes[] = $innerType; |
| 1630: | continue; |
| 1631: | } |
| 1632: | |
| 1633: | $newType = $cb($innerType, TypeCombinator::union(...$candidates)); |
| 1634: | if ($innerType !== $newType) { |
| 1635: | $changed = true; |
| 1636: | } |
| 1637: | |
| 1638: | $newTypes[] = $newType; |
| 1639: | } |
| 1640: | |
| 1641: | if ($changed) { |
| 1642: | return TypeCombinator::union(...$newTypes); |
| 1643: | } |
| 1644: | |
| 1645: | return $this; |
| 1646: | } |
| 1647: | |
| 1648: | public function tryRemove(Type $typeToRemove): ?Type |
| 1649: | { |
| 1650: | $finiteTypeSet = $this->getFiniteTypeSet(); |
| 1651: | if ($finiteTypeSet !== null && $finiteTypeSet->isComplete()) { |
| 1652: | $key = FiniteTypeSet::key($typeToRemove); |
| 1653: | if ($key !== null) { |
| 1654: | if (!$finiteTypeSet->has($key)) { |
| 1655: | return null; |
| 1656: | } |
| 1657: | |
| 1658: | $remainingTypes = []; |
| 1659: | foreach ($finiteTypeSet->getMembers() as $memberKey => $member) { |
| 1660: | if ($memberKey === $key) { |
| 1661: | continue; |
| 1662: | } |
| 1663: | |
| 1664: | $remainingTypes[] = $member; |
| 1665: | } |
| 1666: | |
| 1667: | if (count($remainingTypes) === 1) { |
| 1668: | return $remainingTypes[0]; |
| 1669: | } |
| 1670: | |
| 1671: | return new UnionType($remainingTypes); |
| 1672: | } |
| 1673: | |
| 1674: | |
| 1675: | |
| 1676: | |
| 1677: | |
| 1678: | if ($typeToRemove instanceof self) { |
| 1679: | $keysToRemove = []; |
| 1680: | foreach ($typeToRemove->getTypes() as $innerTypeToRemove) { |
| 1681: | $innerKey = FiniteTypeSet::key($innerTypeToRemove); |
| 1682: | if ($innerKey === null) { |
| 1683: | $keysToRemove = null; |
| 1684: | break; |
| 1685: | } |
| 1686: | |
| 1687: | $keysToRemove[$innerKey] = true; |
| 1688: | } |
| 1689: | |
| 1690: | if ($keysToRemove !== null) { |
| 1691: | $remainingTypes = []; |
| 1692: | $removedAny = false; |
| 1693: | foreach ($finiteTypeSet->getMembers() as $memberKey => $member) { |
| 1694: | if (array_key_exists($memberKey, $keysToRemove)) { |
| 1695: | $removedAny = true; |
| 1696: | continue; |
| 1697: | } |
| 1698: | |
| 1699: | $remainingTypes[] = $member; |
| 1700: | } |
| 1701: | |
| 1702: | if (!$removedAny) { |
| 1703: | return null; |
| 1704: | } |
| 1705: | |
| 1706: | if (count($remainingTypes) === 0) { |
| 1707: | return new NeverType(); |
| 1708: | } |
| 1709: | |
| 1710: | if (count($remainingTypes) === 1) { |
| 1711: | return $remainingTypes[0]; |
| 1712: | } |
| 1713: | |
| 1714: | return new UnionType($remainingTypes); |
| 1715: | } |
| 1716: | } |
| 1717: | } |
| 1718: | |
| 1719: | $innerTypes = []; |
| 1720: | $changed = false; |
| 1721: | foreach ($this->types as $innerType) { |
| 1722: | $removed = TypeCombinator::remove($innerType, $typeToRemove); |
| 1723: | if (!$removed->equals($innerType)) { |
| 1724: | $changed = true; |
| 1725: | } |
| 1726: | if ($removed instanceof NeverType) { |
| 1727: | continue; |
| 1728: | } |
| 1729: | if ($removed instanceof self && !$removed instanceof TemplateType) { |
| 1730: | foreach ($removed->getTypes() as $removedInnerType) { |
| 1731: | $innerTypes[] = $removedInnerType; |
| 1732: | } |
| 1733: | } else { |
| 1734: | $innerTypes[] = $removed; |
| 1735: | } |
| 1736: | } |
| 1737: | |
| 1738: | if (!$changed) { |
| 1739: | return null; |
| 1740: | } |
| 1741: | |
| 1742: | if (count($innerTypes) === 0) { |
| 1743: | return new NeverType(); |
| 1744: | } |
| 1745: | |
| 1746: | if (count($innerTypes) === 1) { |
| 1747: | return $innerTypes[0]; |
| 1748: | } |
| 1749: | |
| 1750: | return new UnionType($innerTypes); |
| 1751: | } |
| 1752: | |
| 1753: | public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type |
| 1754: | { |
| 1755: | return $this->unionTypes(static fn (Type $type): Type => $type->arithmeticPower($exponent, $phpVersion)); |
| 1756: | } |
| 1757: | |
| 1758: | public function getFiniteTypes(): array |
| 1759: | { |
| 1760: | if ($this->finiteTypes !== null) { |
| 1761: | return $this->finiteTypes; |
| 1762: | } |
| 1763: | |
| 1764: | $types = $this->notBenevolentPickFromTypes(static fn (Type $type) => $type->getFiniteTypes()); |
| 1765: | $uniquedTypes = []; |
| 1766: | foreach ($types as $type) { |
| 1767: | |
| 1768: | |
| 1769: | |
| 1770: | |
| 1771: | $uniquedTypes[FiniteTypeSet::key($type) ?? $type->describe(VerbosityLevel::cache())] = $type; |
| 1772: | } |
| 1773: | |
| 1774: | if (count($uniquedTypes) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) { |
| 1775: | return $this->finiteTypes = []; |
| 1776: | } |
| 1777: | |
| 1778: | return $this->finiteTypes = array_values($uniquedTypes); |
| 1779: | } |
| 1780: | |
| 1781: | |
| 1782: | |
| 1783: | |
| 1784: | protected function unionResults(callable $getResult): TrinaryLogic |
| 1785: | { |
| 1786: | return TrinaryLogic::lazyExtremeIdentity($this->types, $getResult); |
| 1787: | } |
| 1788: | |
| 1789: | |
| 1790: | |
| 1791: | |
| 1792: | private function notBenevolentUnionResults(callable $getResult): TrinaryLogic |
| 1793: | { |
| 1794: | return TrinaryLogic::lazyExtremeIdentity($this->types, $getResult); |
| 1795: | } |
| 1796: | |
| 1797: | |
| 1798: | |
| 1799: | |
| 1800: | protected function unionTypes(callable $getType): Type |
| 1801: | { |
| 1802: | $newTypes = []; |
| 1803: | $changed = false; |
| 1804: | foreach ($this->types as $type) { |
| 1805: | $newType = $getType($type); |
| 1806: | if ($newType !== $type) { |
| 1807: | $changed = true; |
| 1808: | } |
| 1809: | $newTypes[] = $newType; |
| 1810: | } |
| 1811: | |
| 1812: | if (!$changed) { |
| 1813: | return $this; |
| 1814: | } |
| 1815: | |
| 1816: | return TypeCombinator::union(...$newTypes); |
| 1817: | } |
| 1818: | |
| 1819: | |
| 1820: | |
| 1821: | |
| 1822: | |
| 1823: | |
| 1824: | |
| 1825: | protected function pickFromTypes( |
| 1826: | callable $getValues, |
| 1827: | callable $criteria, |
| 1828: | ): array |
| 1829: | { |
| 1830: | $values = []; |
| 1831: | foreach ($this->types as $type) { |
| 1832: | $innerValues = $getValues($type); |
| 1833: | if ($innerValues === []) { |
| 1834: | return []; |
| 1835: | } |
| 1836: | |
| 1837: | foreach ($innerValues as $innerType) { |
| 1838: | $values[] = $innerType; |
| 1839: | } |
| 1840: | } |
| 1841: | |
| 1842: | return $values; |
| 1843: | } |
| 1844: | |
| 1845: | public function toPhpDocNode(): TypeNode |
| 1846: | { |
| 1847: | return new UnionTypeNode(array_map(static fn (Type $type) => $type->toPhpDocNode(), $this->getSortedTypes())); |
| 1848: | } |
| 1849: | |
| 1850: | public function toNativePhpTypeNode(NativeTypePosition $position) |
| 1851: | { |
| 1852: | return NativePhpTypeNodeHelper::union($this, $position); |
| 1853: | } |
| 1854: | |
| 1855: | |
| 1856: | |
| 1857: | |
| 1858: | |
| 1859: | |
| 1860: | private function notBenevolentPickFromTypes(callable $getValues): array |
| 1861: | { |
| 1862: | $values = []; |
| 1863: | foreach ($this->types as $type) { |
| 1864: | $innerValues = $getValues($type); |
| 1865: | if ($innerValues === []) { |
| 1866: | return []; |
| 1867: | } |
| 1868: | |
| 1869: | foreach ($innerValues as $innerType) { |
| 1870: | $values[] = $innerType; |
| 1871: | } |
| 1872: | } |
| 1873: | |
| 1874: | return $values; |
| 1875: | } |
| 1876: | |
| 1877: | public function hasTemplateOrLateResolvableType(): bool |
| 1878: | { |
| 1879: | if ($this->hasTemplateOrLateResolvableType !== null) { |
| 1880: | return $this->hasTemplateOrLateResolvableType; |
| 1881: | } |
| 1882: | |
| 1883: | foreach ($this->types as $type) { |
| 1884: | if (!$type->hasTemplateOrLateResolvableType()) { |
| 1885: | continue; |
| 1886: | } |
| 1887: | |
| 1888: | return $this->hasTemplateOrLateResolvableType = true; |
| 1889: | } |
| 1890: | |
| 1891: | return $this->hasTemplateOrLateResolvableType = false; |
| 1892: | } |
| 1893: | |
| 1894: | public function getPossiblyLooselyEqualType(PhpVersion $phpVersion): Type |
| 1895: | { |
| 1896: | return $this->possiblyLooselyEqualTypes[$phpVersion->getVersionId()] ??= $this->toValueSet()->getPossiblyLooselyEqualType($phpVersion); |
| 1897: | } |
| 1898: | |
| 1899: | public function getAlwaysLooselyEqualType(PhpVersion $phpVersion): Type |
| 1900: | { |
| 1901: | return $this->alwaysLooselyEqualTypes[$phpVersion->getVersionId()] ??= $this->toValueSet()->getAlwaysLooselyEqualType($phpVersion); |
| 1902: | } |
| 1903: | |
| 1904: | public function toValueSet(): ValueSet |
| 1905: | { |
| 1906: | $valueSet = ValueSet::createEmpty(); |
| 1907: | foreach ($this->types as $type) { |
| 1908: | $valueSet = $valueSet->union($type->toValueSet()); |
| 1909: | } |
| 1910: | |
| 1911: | return $valueSet; |
| 1912: | } |
| 1913: | |
| 1914: | } |
| 1915: | |