Lines
97.14%
34 / 35
Methods
85.71%
6 / 7
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| doRun | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 3 | ||
| getDefaultInputDefinition | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| getCommandName | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 2 | ||
| findCommandNameAfterLeadingOptions | 90.90% | 10 / 11 | 0.00% | 0 / 1 | 7.04 | ||
| findLeadingOption | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 6 | ||
| buildLeadingOptionDefinition | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| 15 | final class StewartApplication extends Application | |
| 16 | { | |
| 17 | private const string END_OF_OPTIONS = '--'; | |
| 18 | ||
| 19 | private const string LIST_COMMAND = 'list'; | |
| 20 | ||
| 21 | public function __construct(string $version) | |
| 22 | { | |
| 23 | parent::__construct('Stewart', $version); | |
| 24 | $this->setDefaultCommand(RunCommand::NAME); | |
| 25 | } | |
| 26 | ||
| 27 | public function doRun(InputInterface $input, OutputInterface $output): int | |
| 28 | { | |
| 29 | // Symfony would show the default command's help instead. | |
| 30 | if ($this->getCommandName($input) === null && $input->hasParameterOption(['--help', '-h'], true)) { | |
| 31 | return parent::doRun(new ArrayInput(['command' => self::LIST_COMMAND]), $output); | |
| 32 | } | |
| 33 | ||
| 34 | return parent::doRun($input, $output); | |
| 35 | } | |
| 36 | ||
| 37 | protected function getDefaultInputDefinition(): InputDefinition | |
| 38 | { | |
| 39 | $definition = parent::getDefaultInputDefinition(); | |
| 40 | $options = $definition->getOptions(); | |
| 41 | $options['help'] = new InputOption('help', 'h', InputOption::VALUE_NONE, 'Display help for the given command, or list the commands when none is given'); | |
| 42 | $definition->setOptions(array_values($options)); | |
| 43 | ||
| 44 | return $definition; | |
| 45 | } | |
| 46 | ||
| 47 | protected function getCommandName(InputInterface $input): ?string | |
| 48 | { | |
| 49 | if (!$input instanceof ArgvInput) { | |
| 50 | return parent::getCommandName($input); | |
| 51 | } | |
| 52 | ||
| 53 | // Options before the command name belong to the default command, so their values are not command names. | |
| 54 | return $this->findCommandNameAfterLeadingOptions($input->getRawTokens(), $this->buildLeadingOptionDefinition()); | |
| 55 | } | |
| 56 | ||
| 57 | /** @param list<string> $tokens */ | |
| 58 | private function findCommandNameAfterLeadingOptions(array $tokens, InputDefinition $leadingOptions): ?string | |
| 59 | { | |
| 60 | $skipNext = false; | |
| 61 | ||
| 62 | foreach ($tokens as $index => $token) { | |
| 63 | if ($skipNext) { | |
| 64 | $skipNext = false; | |
| 65 | ||
| 66 | continue; | |
| 67 | } | |
| 68 | ||
| 69 | if ($token === self::END_OF_OPTIONS) { | |
| 70 | return $tokens[$index + 1] ?? null; | |
| 71 | } | |
| 72 | ||
| 73 | if ($token === '' || $token === '-' || $token[0] !== '-') { | |
| 74 | return $token; | |
| 75 | } | |
| 76 | ||
| 77 | $skipNext = $this->findLeadingOption($token, $leadingOptions)?->acceptValue() ?? false; | |
| 78 | } | |
| 79 | ||
| 80 | return null; | |
| 81 | } | |
| 82 | ||
| 83 | private function findLeadingOption(string $token, InputDefinition $leadingOptions): ?InputOption | |
| 84 | { | |
| 85 | if (str_contains($token, '=')) { | |
| 86 | return null; | |
| 87 | } | |
| 88 | ||
| 89 | if (str_starts_with($token, self::END_OF_OPTIONS)) { | |
| 90 | $name = substr($token, \strlen(self::END_OF_OPTIONS)); | |
| 91 | ||
| 92 | return $leadingOptions->hasOption($name) ? $leadingOptions->getOption($name) : null; | |
| 93 | } | |
| 94 | ||
| 95 | $shortcut = substr($token, 1); | |
| 96 | ||
| 97 | return \strlen($shortcut) === 1 && $leadingOptions->hasShortcut($shortcut) ? $leadingOptions->getOptionForShortcut($shortcut) : null; | |
| 98 | } | |
| 99 | ||
| 100 | private function buildLeadingOptionDefinition(): InputDefinition | |
| 101 | { | |
| 102 | return new InputDefinition([ | |
| 103 | ...$this->getDefinition()->getOptions(), | |
| 104 | ...$this->get(RunCommand::NAME)->getNativeDefinition()->getOptions(), | |
| 105 | ]); | |
| 106 | } | |
| 107 | } |