Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 11 additions & 22 deletions src/Input/InputOptionsSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use function array_keys;
use function array_map;
use function array_merge;
use function is_array;
use function sprintf;

/**
Expand Down Expand Up @@ -51,13 +50,11 @@ public static function serialize(
$serializedOptionsList = [];

foreach (array_keys($filteredOptions) as $name) {
$serializedOption = self::serializeOption(
$serializedOptionsList[] = self::serializeOption(
$commandDefinition->getOption($name),
$name,
$filteredOptions[$name],
);

$serializedOptionsList[] = is_array($serializedOption) ? $serializedOption : [$serializedOption];
}

return array_merge(...$serializedOptionsList);
Expand All @@ -66,33 +63,25 @@ public static function serialize(
/**
* @param string|bool|int|float|null|array<string|bool|int|float|null> $value
*
* @return string|list<string>
* @return list<string>
*/
private static function serializeOption(
InputOption $option,
string $name,
array|bool|float|int|string|null $value,
): string|array {
): array {
return match (true) {
$option->isNegatable() => sprintf('--%s%s', $value ? '' : 'no-', $name),
!$option->acceptValue() => sprintf('--%s', $name),
self::isArray($option, $value) => array_map(static fn ($item) => self::serializeOptionWithValue($name, $item), $value),
default => self::serializeOptionWithValue($name, $value),
$option->isNegatable() => [sprintf('--%s%s', $value ? '' : 'no-', $name)],
!$option->acceptValue() => [sprintf('--%s', $name)],
/** @var list<string|bool|int|float|null> $value */
// @phpstan-ignore-next-line argument.type
$option->isArray() => array_map(static fn ($item) => self::serializeOptionWithValue($name, $item), $value),
/** @var string|int|float|null $value */
// @phpstan-ignore-next-line argument.type
default => [self::serializeOptionWithValue($name, $value)],
};
}

/**
* @param string|bool|int|float|null|array<string|bool|int|float|null> $value
*
* @phpstan-assert-if-true array<string|bool|int|float|null> $value
*/
private static function isArray(
InputOption $option,
array|bool|float|int|string|null $value,
): bool {
return $option->isArray();
}

private static function serializeOptionWithValue(
string $name,
bool|float|int|string|null $value,
Expand Down
Loading