Skip to content

Commit b7046c8

Browse files
committed
fix
1 parent 5483485 commit b7046c8

File tree

2 files changed

+34
-73
lines changed

2 files changed

+34
-73
lines changed

src/Input/InputOptionsSerializer.php

+25-68
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
use function array_fill_keys;
2121
use function array_keys;
2222
use function array_map;
23+
use function array_merge;
2324
use function implode;
25+
use function is_array;
2426
use function is_string;
2527
use function preg_match;
2628
use function sprintf;
@@ -31,8 +33,6 @@
3133
*/
3234
final class InputOptionsSerializer
3335
{
34-
private const string ESCAPE_TOKEN_PATTERN = '/[\s\W]/';
35-
3636
private function __construct()
3737
{
3838
}
@@ -52,86 +52,43 @@ public static function serialize(
5252
array_fill_keys($excludedOptionNames, ''),
5353
);
5454

55-
return array_map(
56-
static fn (string $name) => self::serializeOption(
57-
$commandDefinition->getOption($name),
58-
$name,
59-
$filteredOptions[$name],
60-
),
61-
array_keys($filteredOptions),
62-
);
55+
$serializedOptions = [];
56+
57+
foreach (array_keys($filteredOptions) as $optionName) {
58+
$serializedOption = self::serializeOption(
59+
$commandDefinition->getOption($optionName),
60+
$optionName,
61+
$filteredOptions[$optionName],
62+
);
63+
64+
$serializedOptions[] = is_array($serializedOption) ? $serializedOption : [$serializedOption];
65+
}
66+
67+
return array_merge(...$serializedOptions);
6368
}
6469

6570
/**
6671
* @param string|bool|int|float|null|array<string|bool|int|float|null> $value
72+
*
73+
* @return string|list<string>
6774
*/
6875
private static function serializeOption(
6976
InputOption $option,
7077
string $name,
7178
array|bool|float|int|string|null $value,
72-
): string {
73-
if ($option->isNegatable()) {
74-
return sprintf(
75-
'--%s%s',
76-
$value ? '' : 'no-',
77-
$name,
78-
);
79-
}
80-
81-
if (!$option->acceptValue()) {
82-
return sprintf(
83-
'--%s',
84-
$name,
85-
);
86-
}
87-
88-
if ($option->isArray()) {
89-
/** @var array<string|bool|int|float|null> $value */
90-
return implode(
91-
'',
92-
array_map(
93-
static fn ($item) => self::serializeOptionWithValue($name, $item),
94-
$value,
95-
),
96-
);
97-
}
98-
99-
/** @var string|bool|int|float|null $value */
100-
return self::serializeOptionWithValue($name, $value);
79+
): string|array {
80+
return match (true) {
81+
$option->isNegatable() => sprintf('--%s%s', $value ? '' : 'no-', $name),
82+
!$option->acceptValue() => sprintf('--%s', $name),
83+
$option->isArray() => array_map(fn ($item) => self::serializeOptionWithValue($name, $item), $value),
84+
default => self::serializeOptionWithValue($name, $value),
85+
};
10186
}
10287

10388
private static function serializeOptionWithValue(
10489
string $name,
10590
bool|float|int|string|null $value
10691
): string {
107-
return sprintf(
108-
'--%s=%s',
109-
$name,
110-
self::quoteOptionValue($value),
111-
);
112-
}
113-
114-
/**
115-
* Ensure that an option value is quoted correctly before it is passed to a
116-
* child process.
117-
*/
118-
private static function quoteOptionValue(bool|float|int|string|null $value): bool|float|int|string|null
119-
{
120-
if (self::isValueRequiresQuoting($value)) {
121-
return sprintf(
122-
'"%s"',
123-
str_replace('"', '\"', (string) $value),
124-
);
125-
}
126-
127-
return $value;
128-
}
129-
130-
/**
131-
* Validate whether a command option requires quoting.
132-
*/
133-
private static function isValueRequiresQuoting(mixed $value): bool
134-
{
135-
return is_string($value) && 0 < preg_match(self::ESCAPE_TOKEN_PATTERN, $value);
92+
return sprintf('--%s=%s', $name, $value);
13693
}
13794
}

tests/Input/InputOptionsSerializerTest.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ private static function optionSerializationProvider(): iterable
255255
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
256256
),
257257
['--opt' => ['v1', 'v2', 'v3']],
258-
['--opt=v1--opt=v2--opt=v3'],
258+
[
259+
'--opt=v1',
260+
'--opt=v2',
261+
'--opt=v3',
262+
],
259263
);
260264

261265
if (!$isSymfony4) {
@@ -315,22 +319,22 @@ private static function escapedValuesProvider(): iterable
315319

316320
yield $createSet(
317321
'"foo"',
318-
'"\"foo\""',
322+
'"foo"',
319323
);
320324

321325
yield $createSet(
322326
'"o_id in(\'20\')"',
323-
'"\"o_id in(\'20\')\""',
327+
'"o_id in(\'20\')"',
324328
);
325329

326330
yield $createSet(
327331
'a b c d',
328-
'"a b c d"',
332+
'a b c d',
329333
);
330334

331335
yield $createSet(
332336
"A\nB'C",
333-
"\"A\nB'C\"",
337+
"A\nB'C",
334338
);
335339
}
336340

0 commit comments

Comments
 (0)