Skip to content

Commit

Permalink
Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Oct 18, 2022
1 parent cbf413f commit ede9c22
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,26 +275,30 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
$offsetType = self::castToArrayKeyType($offsetType);
}

if (
($offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType)
&& $offsetType->isSuperTypeOf($this->keyType)->yes()
) {
$builder = ConstantArrayTypeBuilder::createEmpty();
$builder->setOffsetValueType($offsetType, $valueType);
return $builder->getArray();
}

$array = new self(
TypeCombinator::union($this->keyType, $offsetType),
$unionValues || $offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType
? TypeCombinator::union($this->itemType, $valueType)
: $valueType,
);
if ($offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType) {
return TypeCombinator::intersect($array, new HasOffsetValueType($offsetType, $valueType), new NonEmptyArrayType());
if ($offsetType->isSuperTypeOf($this->keyType)->yes()) {
$builder = ConstantArrayTypeBuilder::createEmpty();
$builder->setOffsetValueType($offsetType, $valueType);
return $builder->getArray();
}

return TypeCombinator::intersect(
new self(
TypeCombinator::union($this->keyType, $offsetType),
TypeCombinator::union($this->itemType, $valueType),
),
new HasOffsetValueType($offsetType, $valueType),
new NonEmptyArrayType()
);
}

return TypeCombinator::intersect($array, new NonEmptyArrayType());
return TypeCombinator::intersect(
new self(
TypeCombinator::union($this->keyType, $offsetType),
$unionValues ? TypeCombinator::union($this->itemType, $valueType) : $valueType,
),
new NonEmptyArrayType()
);
}

public function unsetOffset(Type $offsetType): Type
Expand Down

0 comments on commit ede9c22

Please sign in to comment.