Skip to content

Commit 7d5b33e

Browse files
committed
CommandProcessor: throws exception for invalid option values
1 parent ba7b85f commit 7d5b33e

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/CommandProcessor.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ public function process($app, array $args, array $env = NULL)
4949
$_c = "$key ";
5050
}
5151

52+
if (is_bool($value)) {
53+
$value = $value ? '1' : '0';
54+
55+
} elseif ($value instanceof CommitId) {
56+
$value = $value->toString();
57+
58+
} elseif ($value === NULL) {
59+
// ignored
60+
continue;
61+
62+
} elseif (!is_scalar($value)) {
63+
throw new InvalidStateException('Unknow option value type ' . (is_object($value) ? get_class($value) : gettype($value)) . '.');
64+
}
65+
5266
$cmd[] = $_c . $this->escapeArgument($value);
5367
}
5468

tests/GitPhp/CommandProcessor.phpt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,30 @@ test(function () {
3535
'first',
3636
[
3737
'--second' => new CommitId('734713bc047d87bf7eac9674765ae793478c50d3'),
38+
'--one' => TRUE,
39+
'--two' => FALSE,
40+
'--three' => NULL,
41+
TRUE,
42+
FALSE,
43+
NULL,
3844
],
3945
NULL,
4046
'arg',
4147
new CommitId('734713bc047d87bf7eac9674765ae793478c50d3'),
4248
];
4349

4450
$processor = new CommandProcessor(CommandProcessor::MODE_NON_WINDOWS);
45-
Assert::same('git first --second 734713bc047d87bf7eac9674765ae793478c50d3 arg 734713bc047d87bf7eac9674765ae793478c50d3', $processor->process('git', $options));
51+
Assert::same(implode(' ', [
52+
'git',
53+
'first',
54+
'--second 734713bc047d87bf7eac9674765ae793478c50d3',
55+
'--one 1',
56+
'--two 0',
57+
'1',
58+
'0',
59+
'arg',
60+
'734713bc047d87bf7eac9674765ae793478c50d3',
61+
]), $processor->process('git', $options));
4662
});
4763

4864

@@ -66,4 +82,12 @@ test(function () {
6682
FALSE,
6783
]);
6884
}, InvalidStateException::class, 'Unknow argument type boolean.');
85+
86+
Assert::exception(function () use ($processor) {
87+
$processor->process('git', [
88+
[
89+
(object) [],
90+
],
91+
]);
92+
}, InvalidStateException::class, 'Unknow option value type stdClass.');
6993
});

0 commit comments

Comments
 (0)