Skip to content

Commit 4193c3a

Browse files
committed
Merge branch 'cli-check-option-values'
2 parents eb1f43d + cdfa07a commit 4193c3a

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/Toolkit/Cli/CliCommand.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,35 @@ final protected function normaliseOptionValues(
866866
return $_values ?? [];
867867
}
868868

869+
/**
870+
* Check that an array of option values is valid
871+
*
872+
* @param mixed[] $values
873+
* @phpstan-assert-if-true array<string,array<string|int|bool>|string|int|bool|null> $values
874+
*/
875+
final protected function checkOptionValues(array $values): bool
876+
{
877+
foreach ($values as $value) {
878+
if (
879+
$value === null
880+
|| is_string($value)
881+
|| is_int($value)
882+
|| is_bool($value)
883+
) {
884+
continue;
885+
}
886+
if (!is_array($value)) {
887+
return false;
888+
}
889+
foreach ($value as $v) {
890+
if (!(is_string($v) || is_int($v) || is_bool($v))) {
891+
return false;
892+
}
893+
}
894+
}
895+
return true;
896+
}
897+
869898
/**
870899
* Get an array that maps option names to values
871900
*

tests/fixtures/Toolkit/Cli/Command/TestOptions.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Salient\Tests\Cli\Command;
44

5+
use Salient\Cli\Exception\CliInvalidArgumentsException;
56
use Salient\Cli\CliCommand;
67
use Salient\Cli\CliOption;
78
use Salient\Contract\Cli\CliOptionType;
@@ -183,16 +184,20 @@ protected function run(string ...$args)
183184
case self::ACTION_APPLY_VALUES:
184185
/** @var string */
185186
$data = $this->Data;
186-
/** @var array<string,array<string|int|bool>|string|int|bool|null> */
187187
$data = Json::parseObjectAsArray($data);
188+
if (!is_array($data) || !$this->checkOptionValues($data)) {
189+
throw new CliInvalidArgumentsException('Invalid option values');
190+
}
188191
$this->applyOptionValues($data, true, true, false, true, true);
189192
break;
190193

191194
case self::ACTION_APPLY_SCHEMA_VALUES:
192195
/** @var string */
193196
$data = $this->Data;
194-
/** @var array<string,array<string|int|bool>|string|int|bool|null> */
195197
$data = Json::parseObjectAsArray($data);
198+
if (!is_array($data) || !$this->checkOptionValues($data)) {
199+
throw new CliInvalidArgumentsException('Invalid option values');
200+
}
196201
$this->applyOptionValues($data, true, true, true, true, true);
197202
break;
198203

tests/unit/Toolkit/Cli/CliApplicationTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,24 @@ public static function commandProvider(): array
372372
fn(TestOptions $command) =>
373373
static::assertSame($command, $command->Result),
374374
],
375+
'apply option values (invalid values)' => [
376+
null,
377+
1,
378+
[
379+
'--action=apply-values',
380+
'--data=' . Json::stringify(['value' => 3.14]),
381+
],
382+
[
383+
[Level::ERROR, 'Error: Invalid option values'],
384+
[Level::INFO, <<<'EOF'
385+
386+
app [-fF] [--nullable] [-v <entity>] [-V <value>,...] [-s <date>]
387+
[-r[<pattern>]]
388+
389+
See 'app --help' for more information.
390+
EOF],
391+
],
392+
],
375393
];
376394
}
377395

0 commit comments

Comments
 (0)