Skip to content

Commit faf7dd9

Browse files
authored
refactor: Leverage match to simplify the condition (#327)
1 parent 1f472c7 commit faf7dd9

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

src/Input/InputOptionsSerializer.php

+20-24
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private function __construct()
4646
public static function serialize(
4747
InputDefinition $commandDefinition,
4848
InputInterface $input,
49-
array $excludedOptionNames
49+
array $excludedOptionNames,
5050
): array {
5151
$filteredOptions = array_diff_key(
5252
RawInput::getRawOptions($input),
@@ -78,36 +78,32 @@ private static function serializeOption(
7878
string $name,
7979
array|bool|float|int|string|null $value,
8080
): string|array {
81-
if ($option->isNegatable()) {
82-
return sprintf(
83-
'--%s%s',
84-
$value ? '' : 'no-',
85-
$name,
86-
);
87-
}
88-
89-
if (!$option->acceptValue()) {
90-
return sprintf(
91-
'--%s',
92-
$name,
93-
);
94-
}
95-
96-
if ($option->isArray()) {
97-
/** @var array<string|bool|int|float|null> $value */
98-
return array_map(
81+
return match (true) {
82+
$option->isNegatable() => sprintf('--%s%s', $value ? '' : 'no-', $name),
83+
!$option->acceptValue() => sprintf('--%s', $name),
84+
self::isArray($option, $value) => array_map(
9985
static fn ($item) => self::serializeOptionWithValue($name, $item),
10086
$value,
101-
);
102-
}
87+
),
88+
default => self::serializeOptionWithValue($name, $value),
89+
};
90+
}
10391

104-
/** @var string|bool|int|float|null $value */
105-
return self::serializeOptionWithValue($name, $value);
92+
/**
93+
* @param string|bool|int|float|null|array<string|bool|int|float|null> $value
94+
*
95+
* @phpstan-assert-if-true array<string|bool|int|float|null> $value
96+
*/
97+
private static function isArray(
98+
InputOption $option,
99+
array|bool|float|int|string|null $value,
100+
): bool {
101+
return $option->isArray();
106102
}
107103

108104
private static function serializeOptionWithValue(
109105
string $name,
110-
bool|float|int|string|null $value
106+
bool|float|int|string|null $value,
111107
): string {
112108
return sprintf(
113109
'--%s=%s',

0 commit comments

Comments
 (0)