| Total Complexity | 43 |
| Total Lines | 393 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Application often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 62 | class Application |
||
| 63 | { |
||
| 64 | /** |
||
| 65 | * List of commands |
||
| 66 | * @var Command[] |
||
| 67 | */ |
||
| 68 | protected array $commands = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Raw input arguments send to parser |
||
| 72 | * @var array<int, string> |
||
| 73 | */ |
||
| 74 | protected array $argv = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The commands aliases |
||
| 78 | * @var array<string, string> |
||
| 79 | */ |
||
| 80 | protected array $aliases = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The name of application |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected string $name; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The application version |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected string $version = ''; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The application logo using ASCII text |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected string $logo = ''; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The default command to use if none is provided |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | protected string $default = '__default__'; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The input/output instance |
||
| 108 | * @var Interactor|null |
||
| 109 | */ |
||
| 110 | protected ?Interactor $io = null; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The callable to perform exit |
||
| 114 | * @var callable |
||
| 115 | */ |
||
| 116 | protected $onExit; |
||
| 117 | |||
| 118 | public function __construct( |
||
| 119 | string $name, |
||
| 120 | string $version = '1.0.0', |
||
| 121 | ?callable $onExit = null |
||
| 122 | ) { |
||
| 123 | $this->name = $name; |
||
| 124 | $this->version = $version; |
||
| 125 | |||
| 126 | $this->onExit = $onExit ?? function (int $exitCode = 0) { |
||
| 127 | //@codeCoverageIgnoreStart |
||
| 128 | exit($exitCode); |
||
|
|
|||
| 129 | //@codeCoverageIgnoreEnd |
||
| 130 | }; |
||
| 131 | |||
| 132 | $this->command('__default__', 'Default command', '', true, true) |
||
| 133 | ->on([$this, 'showHelp'], 'help'); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Return the list of command |
||
| 138 | * @return array<Command> |
||
| 139 | */ |
||
| 140 | public function getCommands(): array |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Return the command line argument |
||
| 151 | * @return array<int,string> |
||
| 152 | */ |
||
| 153 | public function argv(): array |
||
| 154 | { |
||
| 155 | return $this->argv; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Return the application name |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function getName(): string |
||
| 163 | { |
||
| 164 | return $this->name; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return the application version |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getVersion(): string |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Return the application logo |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getLogo(): string |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Set application logo |
||
| 187 | * @param string $logo |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | public function setLogo(string $logo): self |
||
| 191 | { |
||
| 192 | $this->logo = $logo; |
||
| 193 | |||
| 194 | return $this; |
||
| 195 | } |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Add a command by it's name, alias |
||
| 200 | * @param string $name |
||
| 201 | * @param string $description |
||
| 202 | * @param string $alias |
||
| 203 | * @param bool $allowUnknown |
||
| 204 | * @param bool $default |
||
| 205 | * @return Command |
||
| 206 | */ |
||
| 207 | public function command( |
||
| 208 | string $name, |
||
| 209 | string $description = '', |
||
| 210 | string $alias = '', |
||
| 211 | bool $allowUnknown = false, |
||
| 212 | bool $default = false |
||
| 213 | ): Command { |
||
| 214 | $command = new Command($name, $description, $allowUnknown, $this); |
||
| 215 | |||
| 216 | $this->addCommand($command, $alias, $default); |
||
| 217 | |||
| 218 | return $command; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Add new command |
||
| 223 | * @param Command $command |
||
| 224 | * @param string $alias |
||
| 225 | * @param bool $default |
||
| 226 | * @return $this |
||
| 227 | */ |
||
| 228 | public function addCommand( |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Gets matching command for given arguments |
||
| 270 | * @param array<int, string> $argv |
||
| 271 | * @return Command |
||
| 272 | */ |
||
| 273 | public function getCommandForArgument(array $argv): Command |
||
| 274 | { |
||
| 275 | $argv += [null, null, null]; |
||
| 276 | |||
| 277 | //Command |
||
| 278 | if (isset($this->commands[$argv[1]])) { |
||
| 279 | return $this->commands[$argv[1]]; |
||
| 280 | } |
||
| 281 | |||
| 282 | //Alias |
||
| 283 | $alias = isset($this->aliases[$argv[1]]) |
||
| 284 | ? $this->aliases[$argv[1]] |
||
| 285 | : null; |
||
| 286 | |||
| 287 | if (isset($this->commands[$alias])) { |
||
| 288 | return $this->commands[$alias]; |
||
| 289 | } |
||
| 290 | |||
| 291 | //Default command |
||
| 292 | return $this->commands[$this->default]; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Return the input/output instance |
||
| 297 | * @param Interactor|null $io |
||
| 298 | * @return Interactor |
||
| 299 | */ |
||
| 300 | public function io(?Interactor $io = null): Interactor |
||
| 301 | { |
||
| 302 | if ($io || $this->io === null) { |
||
| 303 | $this->io = $io ?? new Interactor(); |
||
| 304 | } |
||
| 305 | |||
| 306 | return $this->io; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Parse the arguments via the matching command |
||
| 311 | * but don't execute command. |
||
| 312 | * @param array<int, string> $argv |
||
| 313 | * @return Command |
||
| 314 | */ |
||
| 315 | public function parse(array $argv): Command |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Handle the request, execute command and call exit handler. |
||
| 339 | * @param array<int, string> $argv |
||
| 340 | * @return mixed |
||
| 341 | */ |
||
| 342 | public function handle(array $argv) |
||
| 343 | { |
||
| 344 | if (count($argv) < 2) { |
||
| 345 | return $this->showHelp(); |
||
| 346 | } |
||
| 347 | |||
| 348 | try { |
||
| 349 | $command = $this->parse($argv); |
||
| 350 | $result = $this->executeCommand($command); |
||
| 351 | |||
| 352 | $exitCode = is_int($result) ? $result : 0; |
||
| 353 | } catch (Throwable $ex) { |
||
| 354 | if ($ex instanceof ConsoleException) { |
||
| 355 | $this->io()->writer()->red($ex->getMessage(), true); |
||
| 356 | } else { |
||
| 357 | $this->outputHelper()->printTrace($ex); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | return ($this->onExit)(isset($exitCode) ? $exitCode : 255); |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Show help of all commands. |
||
| 366 | * @return mixed |
||
| 367 | */ |
||
| 368 | public function showHelp(): mixed |
||
| 369 | { |
||
| 370 | $writer = $this->io()->writer(); |
||
| 371 | |||
| 372 | $header = sprintf( |
||
| 373 | '%s, version %s', |
||
| 374 | $this->name, |
||
| 375 | $this->version |
||
| 376 | ); |
||
| 377 | |||
| 378 | $footer = 'Run `<command> --help` for specific help'; |
||
| 379 | |||
| 380 | if ($this->logo) { |
||
| 381 | $writer->write($this->logo, true); |
||
| 382 | } |
||
| 383 | |||
| 384 | $this->outputHelper() |
||
| 385 | ->showCommandsHelp( |
||
| 386 | $this->getCommands(), |
||
| 387 | $header, |
||
| 388 | $footer |
||
| 389 | ); |
||
| 390 | |||
| 391 | return ($this->onExit)(0); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Return the list of alias for given command |
||
| 396 | * @param Command $command |
||
| 397 | * @return array<int, string> |
||
| 398 | */ |
||
| 399 | public function getAliasesForCommand(Command $command): array |
||
| 400 | { |
||
| 401 | $name = $command->getName(); |
||
| 402 | $aliases = [$name]; |
||
| 403 | |||
| 404 | foreach ($this->aliases as $alias => $commandName) { |
||
| 405 | if (in_array($name, [$alias, $commandName])) { |
||
| 406 | $aliases[] = $alias; |
||
| 407 | $aliases[] = $commandName; |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | return $aliases; |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Execute the given command and return the result |
||
| 416 | * @param Command $command |
||
| 417 | * @return mixed |
||
| 418 | */ |
||
| 419 | protected function executeCommand(Command $command): mixed |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Return the output helper instance |
||
| 434 | * @return OutputHelper |
||
| 435 | */ |
||
| 436 | protected function outputHelper(): OutputHelper |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Command not found handler. |
||
| 445 | * @return mixed |
||
| 446 | */ |
||
| 447 | protected function showCommandNotFound(): mixed |
||
| 457 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.