Skip to content

Commit b80991a

Browse files
Refacto
1 parent 0c8cd2a commit b80991a

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

src/Type/Php/ArrayMapFunctionReturnTypeExtension.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use PHPStan\Type\NeverType;
1818
use PHPStan\Type\Type;
1919
use PHPStan\Type\TypeCombinator;
20+
use PHPStan\Type\TypeTraverser;
2021
use PHPStan\Type\TypeUtils;
21-
use PHPStan\Type\UnionType;
2222
use function array_slice;
2323
use function count;
2424

@@ -120,24 +120,13 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
120120

121121
private function mapValueType(Type $initialValue, Type $returnType): Type
122122
{
123-
$newValues = [];
124-
if ($returnType instanceof UnionType) {
125-
foreach ($returnType->getTypes() as $type) {
126-
if ($type instanceof ConditionalTypeForParameter) {
127-
$newValues[] = $type->toConditional($initialValue);
128-
} else {
129-
$newValues[] = $type;
130-
}
123+
return TypeTraverser::map($returnType, static function (Type $type, callable $traverse) use ($initialValue): Type {
124+
if ($type instanceof ConditionalTypeForParameter) {
125+
$type = $type->toConditional($initialValue);
131126
}
132127

133-
return TypeCombinator::union(...$newValues);
134-
}
135-
136-
if ($returnType instanceof ConditionalTypeForParameter) {
137-
return $returnType->toConditional($initialValue);
138-
}
139-
140-
return $returnType;
128+
return $traverse($type);
129+
});
141130
}
142131

143132
}

0 commit comments

Comments
 (0)