Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
10 / 11 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| HasJsonOption | |
90.91% |
10 / 11 |
|
66.67% |
2 / 3 |
4.01 | |
0.00% |
0 / 1 |
| addJsonOption | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| isJsonOutput | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| isPrettyJsonOutput | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Fast Forward Development Tools for PHP projects. |
| 7 | * |
| 8 | * This file is part of fast-forward/dev-tools project. |
| 9 | * |
| 10 | * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | * @license https://opensource.org/licenses/MIT MIT License |
| 12 | * |
| 13 | * @see https://github.com/php-fast-forward/ |
| 14 | * @see https://github.com/php-fast-forward/dev-tools |
| 15 | * @see https://github.com/php-fast-forward/dev-tools/issues |
| 16 | * @see https://php-fast-forward.github.io/dev-tools/ |
| 17 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 18 | */ |
| 19 | |
| 20 | namespace FastForward\DevTools\Console\Input; |
| 21 | |
| 22 | use Symfony\Component\Console\Input\InputInterface; |
| 23 | use Symfony\Component\Console\Input\InputOption; |
| 24 | |
| 25 | /** |
| 26 | * Provides the standard JSON output options used by DevTools commands. |
| 27 | */ |
| 28 | trait HasJsonOption |
| 29 | { |
| 30 | /** |
| 31 | * Adds the standard JSON output options to the current command. |
| 32 | * |
| 33 | * @return static |
| 34 | */ |
| 35 | protected function addJsonOption(): static |
| 36 | { |
| 37 | return $this |
| 38 | ->addOption(name: 'json', mode: InputOption::VALUE_NONE, description: 'Emit structured JSON output.') |
| 39 | ->addOption( |
| 40 | name: 'pretty-json', |
| 41 | mode: InputOption::VALUE_NONE, |
| 42 | description: 'Emit structured JSON output using indentation for readability.', |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Determines whether JSON output was requested. |
| 48 | * |
| 49 | * The pretty-json flag SHALL imply JSON output. |
| 50 | * |
| 51 | * @param InputInterface $input |
| 52 | */ |
| 53 | protected function isJsonOutput(InputInterface $input): bool |
| 54 | { |
| 55 | if ($this->isPrettyJsonOutput($input)) { |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | return (bool) $input->getOption('json'); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Determines whether pretty JSON output was requested. |
| 64 | * |
| 65 | * @param InputInterface $input |
| 66 | */ |
| 67 | protected function isPrettyJsonOutput(InputInterface $input): bool |
| 68 | { |
| 69 | return (bool) $input->getOption('pretty-json'); |
| 70 | } |
| 71 | } |