Skip to content

Commit 599bdee

Browse files
authored
refactor: Simplify the input options serializer (#331)
1 parent 7208c56 commit 599bdee

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/Input/InputOptionsSerializer.php

+11-22
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use function array_keys;
2222
use function array_map;
2323
use function array_merge;
24-
use function is_array;
2524
use function sprintf;
2625

2726
/**
@@ -51,13 +50,11 @@ public static function serialize(
5150
$serializedOptionsList = [];
5251

5352
foreach (array_keys($filteredOptions) as $name) {
54-
$serializedOption = self::serializeOption(
53+
$serializedOptionsList[] = self::serializeOption(
5554
$commandDefinition->getOption($name),
5655
$name,
5756
$filteredOptions[$name],
5857
);
59-
60-
$serializedOptionsList[] = is_array($serializedOption) ? $serializedOption : [$serializedOption];
6158
}
6259

6360
return array_merge(...$serializedOptionsList);
@@ -66,33 +63,25 @@ public static function serialize(
6663
/**
6764
* @param string|bool|int|float|null|array<string|bool|int|float|null> $value
6865
*
69-
* @return string|list<string>
66+
* @return list<string>
7067
*/
7168
private static function serializeOption(
7269
InputOption $option,
7370
string $name,
7471
array|bool|float|int|string|null $value,
75-
): string|array {
72+
): array {
7673
return match (true) {
77-
$option->isNegatable() => sprintf('--%s%s', $value ? '' : 'no-', $name),
78-
!$option->acceptValue() => sprintf('--%s', $name),
79-
self::isArray($option, $value) => array_map(static fn ($item) => self::serializeOptionWithValue($name, $item), $value),
80-
default => self::serializeOptionWithValue($name, $value),
74+
$option->isNegatable() => [sprintf('--%s%s', $value ? '' : 'no-', $name)],
75+
!$option->acceptValue() => [sprintf('--%s', $name)],
76+
/** @var list<string|bool|int|float|null> $value */
77+
// @phpstan-ignore-next-line argument.type
78+
$option->isArray() => array_map(static fn ($item) => self::serializeOptionWithValue($name, $item), $value),
79+
/** @var string|int|float|null $value */
80+
// @phpstan-ignore-next-line argument.type
81+
default => [self::serializeOptionWithValue($name, $value)],
8182
};
8283
}
8384

84-
/**
85-
* @param string|bool|int|float|null|array<string|bool|int|float|null> $value
86-
*
87-
* @phpstan-assert-if-true array<string|bool|int|float|null> $value
88-
*/
89-
private static function isArray(
90-
InputOption $option,
91-
array|bool|float|int|string|null $value,
92-
): bool {
93-
return $option->isArray();
94-
}
95-
9685
private static function serializeOptionWithValue(
9786
string $name,
9887
bool|float|int|string|null $value,

0 commit comments

Comments
 (0)