Skip to content

Commit cdaf2f1

Browse files
committed
Update ConstantArrayType.php
1 parent 101f110 commit cdaf2f1

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -701,12 +701,29 @@ public function getOffsetValueType(Type $offsetType): Type
701701
public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
702702
{
703703
if ($offsetType !== null) {
704-
$scalarKeyTypes = $this->resolveFiniteScalarKeyTypes($offsetType);
704+
$offsetType = $offsetType->toArrayKey();
705+
706+
$scalarKeyTypes = $offsetType->getConstantStrings();
707+
if (count($scalarKeyTypes) === 0) {
708+
$integerRanges = TypeUtils::getIntegerRanges($offsetType);
709+
if (count($integerRanges) > 0) {
710+
foreach ($integerRanges as $integerRange) {
711+
$finiteTypes = $integerRange->getFiniteTypes();
712+
if (count($finiteTypes) === 0) {
713+
break;
714+
}
715+
716+
foreach ($finiteTypes as $finiteType) {
717+
$scalarKeyTypes[] = $finiteType;
718+
}
719+
}
720+
}
721+
}
722+
705723
// turn into tagged union for more precise results
706724
if (
707-
$scalarKeyTypes !== null
708-
&& count($scalarKeyTypes) >= 2
709-
&& count($scalarKeyTypes) <= InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT
725+
count($scalarKeyTypes) >= 2
726+
&& count($scalarKeyTypes) <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT
710727
) {
711728
$hasNewKey = false;
712729
foreach ($scalarKeyTypes as $scalarKeyType) {
@@ -750,34 +767,6 @@ public function setExistingOffsetValueType(Type $offsetType, Type $valueType): T
750767
return $builder->getArray();
751768
}
752769

753-
/** @return array<ConstantIntegerType|ConstantStringType>|null */
754-
private function resolveFiniteScalarKeyTypes(Type $offsetType): ?array
755-
{
756-
$result = [];
757-
758-
$offsetType = $offsetType->toArrayKey();
759-
$constantStrings = $offsetType->getConstantStrings();
760-
if (count($constantStrings) > 0) {
761-
foreach ($constantStrings as $constantString) {
762-
$result[] = $constantString;
763-
}
764-
} else {
765-
$integerRanges = TypeUtils::getIntegerRanges($offsetType);
766-
foreach ($integerRanges as $integerRange) {
767-
$finiteTypes = $integerRange->getFiniteTypes();
768-
if ($finiteTypes === []) {
769-
return null;
770-
}
771-
772-
foreach ($finiteTypes as $finiteType) {
773-
$result[$finiteType->getValue()] = $finiteType;
774-
}
775-
}
776-
}
777-
778-
return $result;
779-
}
780-
781770
public function unsetOffset(Type $offsetType): Type
782771
{
783772
$offsetType = $offsetType->toArrayKey();

0 commit comments

Comments
 (0)