Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
|
100.00% |
4 / 4 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Argument | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
|
100.00% |
4 / 4 |
|
100.00% |
4 / 4 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| optional | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| variadic | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| convert | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celema\Console; |
| 6 | |
| 7 | use BackedEnum; |
| 8 | use Celema\Console\Exception\InvalidUsage; |
| 9 | use ReflectionParameter; |
| 10 | |
| 11 | /** |
| 12 | * An `#[Arg]` parameter of a command's `__invoke()`. |
| 13 | * |
| 14 | * @internal |
| 15 | */ |
| 16 | final class Argument |
| 17 | { |
| 18 | public readonly string $name; |
| 19 | |
| 20 | public function __construct( |
| 21 | public readonly ReflectionParameter $parameter, |
| 22 | public readonly Arg $arg, |
| 23 | public readonly Type $type, |
| 24 | ) { |
| 25 | $this->name = Signature::kebab($parameter->getName()); |
| 26 | } |
| 27 | |
| 28 | public function optional(): bool |
| 29 | { |
| 30 | return $this->parameter->isDefaultValueAvailable(); |
| 31 | } |
| 32 | |
| 33 | public function variadic(): bool |
| 34 | { |
| 35 | return $this->type->name === 'array'; |
| 36 | } |
| 37 | |
| 38 | public function convert(string $value): string|int|float|BackedEnum |
| 39 | { |
| 40 | return ( |
| 41 | $this->type->convert($value) |
| 42 | ?? throw new InvalidUsage( |
| 43 | "Argument '<{$this->name}>' expects {$this->type->expected()}, got '{$value}'", |
| 44 | ) |
| 45 | ); |
| 46 | } |
| 47 | } |