Skip to content

fix: Fix the forwarding of array options #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
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
31 changes: 18 additions & 13 deletions src/Input/InputOptionsSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
use function array_fill_keys;
use function array_keys;
use function array_map;
use function implode;
use function array_merge;
use function is_array;
use function is_string;
use function preg_match;
use function sprintf;
Expand Down Expand Up @@ -52,24 +53,31 @@ public static function serialize(
array_fill_keys($excludedOptionNames, ''),
);

return array_map(
static fn (string $name) => self::serializeOption(
$serializedOptionsList = [];

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

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

return array_merge(...$serializedOptionsList);
}

/**
* @param string|bool|int|float|null|array<string|bool|int|float|null> $value
*
* @return string|list<string>
*/
private static function serializeOption(
InputOption $option,
string $name,
array|bool|float|int|string|null $value,
): string {
): string|array {
if ($option->isNegatable()) {
return sprintf(
'--%s%s',
Expand All @@ -87,12 +95,9 @@ private static function serializeOption(

if ($option->isArray()) {
/** @var array<string|bool|int|float|null> $value */
return implode(
'',
array_map(
static fn ($item) => self::serializeOptionWithValue($name, $item),
$value,
),
return array_map(
static fn ($item) => self::serializeOptionWithValue($name, $item),
$value,
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Input/InputOptionsSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private static function optionSerializationProvider(): iterable
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
),
['--opt' => ['v1', 'v2', 'v3']],
['--opt=v1--opt=v2--opt=v3'],
['--opt=v1', '--opt=v2', '--opt=v3'],
);

if (!$isSymfony4) {
Expand Down
29 changes: 14 additions & 15 deletions tests/Integration/DebugChildProcessInputsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,16 @@ public function test_it_can_run_the_command_without_sub_processes(

public static function inputProvider(): iterable
{
// This test fails...
// yield 'default' => [
// 'item',
// null,
// [],
// DebugChildProcessCommand::createContent(
// 'item',
// '',
// [],
// ),
// ];
yield 'default' => [
'item',
null,
[],
DebugChildProcessCommand::createContent(
'item',
'',
[],
),
];

yield 'with values' => [
'item',
Expand All @@ -93,7 +92,7 @@ public static function inputProvider(): iterable
DebugChildProcessCommand::createContent(
'item',
'option',
['option1--array-option=option2'],
['option1', 'option2'],
),
];

Expand All @@ -104,7 +103,7 @@ public static function inputProvider(): iterable
DebugChildProcessCommand::createContent(
'"foo"',
'"\"bar\""',
['"\"option1\""--array-option="\"option2\""'],
['"\"option1\""', '"\"option2\""'],
),
];

Expand All @@ -115,7 +114,7 @@ public static function inputProvider(): iterable
DebugChildProcessCommand::createContent(
'"o_id in(\'20\')"',
'"\"p_id in(\'22\')\""',
['"\"option in(\'1\')\""--array-option="\"option in(\'2\')\""'],
['"\"option in(\'1\')\""', '"\"option in(\'2\')\""'],
),
];

Expand All @@ -126,7 +125,7 @@ public static function inputProvider(): iterable
DebugChildProcessCommand::createContent(
'a b c d',
'"d c b a"',
['"option 1"--array-option="option 2"'],
['"option 1"', '"option 2"'],
),
];
}
Expand Down
Loading