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