Skip to content

Commit 9770799

Browse files
phpstan-botclaude
andcommitted
Simplify callable array offset type narrowing with if/elseif/else
Use direct UnionType construction instead of building an array and calling TypeCombinator::union, as suggested in review. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d2a90f6 commit 9770799

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

src/Type/IntersectionType.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -940,16 +940,14 @@ public function getOffsetValueType(Type $offsetType): Type
940940
$arrayKeyOffsetType = $offsetType->toArrayKey();
941941
$callableArrayOffsetType = new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(1)]);
942942
if ($callableArrayOffsetType->isSuperTypeOf($arrayKeyOffsetType)->yes()) {
943-
$narrowedTypes = [];
944-
if (!$arrayKeyOffsetType->isSuperTypeOf(new ConstantIntegerType(0))->no()) {
945-
$narrowedTypes[] = new UnionType([new ClassStringType(), new ObjectWithoutClassType()]);
946-
}
947-
if (!$arrayKeyOffsetType->isSuperTypeOf(new ConstantIntegerType(1))->no()) {
948-
$narrowedTypes[] = new StringType();
949-
}
950-
if ($narrowedTypes !== []) {
951-
$result = TypeCombinator::intersect($result, TypeCombinator::union(...$narrowedTypes));
943+
if ((new ConstantIntegerType(0))->isSuperTypeOf($arrayKeyOffsetType)->yes()) {
944+
$narrowedType = new UnionType([new ClassStringType(), new ObjectWithoutClassType()]);
945+
} elseif ((new ConstantIntegerType(1))->isSuperTypeOf($arrayKeyOffsetType)->yes()) {
946+
$narrowedType = new StringType();
947+
} else {
948+
$narrowedType = new UnionType([new StringType(), new ObjectWithoutClassType()]);
952949
}
950+
$result = TypeCombinator::intersect($result, $narrowedType);
953951
}
954952
}
955953

0 commit comments

Comments
 (0)