Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
5 / 5 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Arg | |
100.00% |
6 / 6 |
|
100.00% |
5 / 5 |
|
100.00% |
3 / 3 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| of | |
100.00% |
5 / 5 |
|
100.00% |
4 / 4 |
|
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Celema\Console; |
| 6 | |
| 7 | use Attribute; |
| 8 | use ReflectionAttribute; |
| 9 | use ReflectionClass; |
| 10 | |
| 11 | /** |
| 12 | * Describes one positional argument of a command in its help output. |
| 13 | * |
| 14 | * Arguments render in declaration order in the usage line — `<name>`, or |
| 15 | * `[<name>]` when optional — and as entries of an "Arguments:" section. |
| 16 | * |
| 17 | * A `variadic` argument must be the last one and accepts the remaining |
| 18 | * positionals: at least one, or any number when also `optional`. It |
| 19 | * renders as `<name>...`. |
| 20 | * |
| 21 | * @api |
| 22 | */ |
| 23 | #[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] |
| 24 | final class Arg |
| 25 | { |
| 26 | public function __construct( |
| 27 | public readonly string $name, |
| 28 | public readonly string $description, |
| 29 | public readonly bool $optional = false, |
| 30 | public readonly bool $variadic = false, |
| 31 | ) {} |
| 32 | |
| 33 | /** |
| 34 | * Reads all argument attributes off a command instance or class. |
| 35 | * |
| 36 | * @param class-string|object $command |
| 37 | * @return list<self> |
| 38 | */ |
| 39 | public static function of(object|string $command): array |
| 40 | { |
| 41 | $class = is_object($command) ? $command::class : $command; |
| 42 | |
| 43 | return array_map( |
| 44 | static fn(ReflectionAttribute $attribute): self => $attribute->newInstance(), |
| 45 | new ReflectionClass($class)->getAttributes(self::class), |
| 46 | ); |
| 47 | } |
| 48 | } |