Skip to content

Commit f3e2700

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

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -701,11 +701,28 @@ 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
725+
count($scalarKeyTypes) >= 2
709726
&& count($scalarKeyTypes) <= InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT
710727
) {
711728
$hasNewKey = false;
@@ -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)