| Conditions | 2 |
| Total Lines | 83 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | #!/usr/bin/php |
||
| 12 | public function setup(): Cmd { |
||
| 13 | return (new class extends Cmd { |
||
| 14 | |||
| 15 | public function init(): void { |
||
| 16 | |||
| 17 | $this |
||
| 18 | ->addOption('h|help', [ |
||
| 19 | 'description' => 'Displays the command help.' |
||
| 20 | ]) |
||
| 21 | ->addOption('v|verbose', [ |
||
| 22 | 'description' => 'Flag to enable verbose output.', |
||
| 23 | 'incremental' => true |
||
| 24 | ]) |
||
| 25 | ->addOption('name:', [ |
||
| 26 | 'description' => 'Name option. This option requires a value.', |
||
| 27 | 'isa' => 'string', |
||
| 28 | 'default' => 'World' |
||
| 29 | ]); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function run(): void { |
||
| 33 | |||
| 34 | if ($this->hasProvidedOption('help')) { |
||
| 35 | $this->runHelp(); |
||
| 36 | return; |
||
| 37 | } |
||
| 38 | |||
| 39 | $name = $this->getProvidedOption('name'); |
||
| 40 | |||
| 41 | self::eol(); |
||
| 42 | self::echo(sprintf('Hello %s', $name), Color::FOREGROUND_COLOR_YELLOW); |
||
| 43 | self::eol(); |
||
| 44 | self::eol(); |
||
| 45 | |||
| 46 | if ($this->hasProvidedOption('verbose')) { |
||
| 47 | $this->verbose(); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | public function help(): void { |
||
| 52 | |||
| 53 | echo '' . |
||
| 54 | ' This is the custom help for ' . Color::green('cli-app-options') . ' command. ' . self::EOL . |
||
| 55 | ' Please use the --name option to pass your name to this command and you will be greeted personally. ' . self::EOL . |
||
| 56 | ' ' . self::EOL . |
||
| 57 | ' ' . Color::green('php cli-app-options.php --name="great Stefaminator"') . self::EOL; |
||
| 58 | } |
||
| 59 | |||
| 60 | private function verbose(): void { |
||
| 61 | |||
| 62 | self::echo('--- VERBOSE OUTPUT ---', Color::FOREGROUND_COLOR_GREEN); |
||
| 63 | self::eol(); |
||
| 64 | self::eol(); |
||
| 65 | |||
| 66 | $this->outputProvidedOptions(); |
||
| 67 | |||
| 68 | $this->outputProvidedArguments(); |
||
| 69 | } |
||
| 70 | |||
| 71 | private function outputProvidedOptions(): void { |
||
| 72 | |||
| 73 | self::echo(' All current options...', Color::FOREGROUND_COLOR_GREEN); |
||
| 74 | self::eol(); |
||
| 75 | |||
| 76 | $pOptions = $this->getAllProvidedOptions(); |
||
| 77 | foreach ($pOptions as $k => $v) { |
||
| 78 | self::echo(' ' . $k . ': ' . json_encode($v), Color::FOREGROUND_COLOR_GREEN); |
||
| 79 | self::eol(); |
||
| 80 | } |
||
| 81 | self::eol(); |
||
| 82 | } |
||
| 83 | |||
| 84 | private function outputProvidedArguments(): void { |
||
| 85 | |||
| 86 | self::echo(' All current arguments...', Color::FOREGROUND_COLOR_GREEN); |
||
| 87 | self::eol(); |
||
| 88 | |||
| 89 | $args = $this->arguments(); |
||
| 90 | foreach ($args as $a) { |
||
| 91 | self::echo(' ' . $a, Color::FOREGROUND_COLOR_GREEN); |
||
| 92 | self::eol(); |
||
| 93 | } |
||
| 94 | self::eol(); |
||
| 95 | } |
||
| 100 |