Skip to content

Commit

Permalink
ArrayColumnHelper: Extract new getReturnIndexType() method
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jan 16, 2025
1 parent 44ac0f2 commit 3893c97
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/Type/Php/ArrayColumnHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,33 @@ public function getReturnValueType(Type $arrayType, Type $columnType, Scope $sco
return [$returnValueType, $iterableAtLeastOnce];
}

public function handleAnyArray(Type $arrayType, Type $columnType, ?Type $indexType, Scope $scope): Type
public function getReturnIndexType(Type $arrayType, ?Type $indexType, Scope $scope): Type
{
[$returnValueType, $iterableAtLeastOnce] = $this->getReturnValueType($arrayType, $columnType, $scope);

if ($returnValueType instanceof NeverType) {
return new ConstantArrayType([], []);
}

if ($indexType !== null) {
$iterableValueType = $arrayType->getIterableValueType();

$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, false);
if ($type !== null) {
$returnKeyType = $type;
} else {
$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, true);
if ($type !== null) {
$returnKeyType = TypeCombinator::union($type, new IntegerType());
} else {
$returnKeyType = new IntegerType();
}
return $type;
}
} else {
$returnKeyType = new IntegerType();

$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, true);
if ($type !== null) {
return TypeCombinator::union($type, new IntegerType());
}
}

return new IntegerType();
}

public function handleAnyArray(Type $arrayType, Type $columnType, ?Type $indexType, Scope $scope): Type
{
[$returnValueType, $iterableAtLeastOnce] = $this->getReturnValueType($arrayType, $columnType, $scope);
if ($returnValueType instanceof NeverType) {
return new ConstantArrayType([], []);
}

$returnKeyType = $this->getReturnIndexType($arrayType, $indexType, $scope);
$returnType = new ArrayType($this->castToArrayKeyType($returnKeyType), $returnValueType);

if ($iterableAtLeastOnce->yes()) {
Expand Down

0 comments on commit 3893c97

Please sign in to comment.