Skip to content

Commit eafd010

Browse files
phpstan-botclaude
andcommitted
Refactor: use Type::getArrays() instead of manual union decomposition
Use $arrayType->getArrays() to iterate over array variants directly, instead of manually decomposing union types and then calling getConstantArrays(). This simplifies the iteration and avoids instanceof UnionType checks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6e39abc commit eafd010

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/Type/Php/InArrayFunctionTypeSpecifyingExtension.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use PHPStan\Type\NeverType;
2222
use PHPStan\Type\Type;
2323
use PHPStan\Type\TypeCombinator;
24-
use PHPStan\Type\UnionType;
2524
use function count;
2625
use function strtolower;
2726

@@ -187,26 +186,24 @@ private function computeNeedleNarrowingType(TypeSpecifierContext $context, Type
187186
return null;
188187
}
189188

190-
$innerTypes = $arrayType instanceof UnionType ? $arrayType->getTypes() : [$arrayType];
189+
$arrays = $arrayType->getArrays();
191190
$innerValueTypes = [];
192-
foreach ($innerTypes as $innerType) {
193-
$constantArrays = $innerType->getConstantArrays();
191+
foreach ($arrays as $array) {
192+
$constantArrays = $array->getConstantArrays();
194193
if (count($constantArrays) > 0) {
195-
$perArrayTypes = [];
196194
foreach ($constantArrays as $constantArray) {
197195
$guaranteedTypes = [];
198196
foreach ($constantArray->getValueTypes() as $i => $valueType) {
199197
if (!$constantArray->isOptionalKey($i)) {
200198
$guaranteedTypes[] = $valueType;
201199
}
202200
}
203-
$perArrayTypes[] = count($guaranteedTypes) > 0
201+
$innerValueTypes[] = count($guaranteedTypes) > 0
204202
? TypeCombinator::union(...$guaranteedTypes)
205203
: new NeverType();
206204
}
207-
$innerValueTypes[] = TypeCombinator::intersect(...$perArrayTypes);
208205
} else {
209-
$innerValueTypes[] = $innerType->getIterableValueType();
206+
$innerValueTypes[] = $array->getIterableValueType();
210207
}
211208
}
212209

0 commit comments

Comments
 (0)