| Conditions | 5 |
| Total Lines | 60 |
| 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 |
||
| 15 | public function setup(): Cmd { |
||
| 16 | return Cmd::root() |
||
| 17 | |||
| 18 | ->addOption('h|help', [ |
||
| 19 | 'description' => 'Displays the command help.' |
||
| 20 | ]) |
||
| 21 | |||
| 22 | ->addOption('v|verbose', [ |
||
| 23 | 'description' => 'Flag to enable verbose output.' |
||
| 24 | ]) |
||
| 25 | |||
| 26 | ->addOption('name:', [ |
||
| 27 | 'description' => 'Name option. This option requires a value.', |
||
| 28 | 'isa' => 'string', |
||
| 29 | 'default' => 'World' |
||
| 30 | ]) |
||
| 31 | ->setRunner( |
||
| 32 | (new class extends CmdRunner { |
||
| 33 | |||
| 34 | public function run(): void { |
||
| 35 | |||
| 36 | $cmd = $this->getCmd(); |
||
| 37 | |||
| 38 | if ($cmd->hasProvidedOption('help')) { |
||
| 39 | $cmd->help(); |
||
| 40 | return; |
||
| 41 | } |
||
| 42 | |||
| 43 | $name = $cmd->getProvidedOption('name'); |
||
| 44 | |||
| 45 | self::eol(); |
||
| 46 | self::echo(sprintf('Hello %s', $name), Color::FOREGROUND_COLOR_YELLOW); |
||
| 47 | self::eol(); |
||
| 48 | self::eol(); |
||
| 49 | |||
| 50 | if ($cmd->hasProvidedOption('verbose')) { |
||
| 51 | |||
| 52 | self::echo('--- VERBOSE OUTPUT ---', Color::FOREGROUND_COLOR_GREEN); |
||
| 53 | self::eol(); |
||
| 54 | self::eol(); |
||
| 55 | |||
| 56 | self::echo(' All current options...', Color::FOREGROUND_COLOR_GREEN); |
||
| 57 | self::eol(); |
||
| 58 | |||
| 59 | $pOptions = $cmd->getAllProvidedOptions(); |
||
| 60 | foreach ($pOptions as $k => $v) { |
||
| 61 | self::echo(' ' . $k . ': ' . $v, Color::FOREGROUND_COLOR_GREEN); |
||
| 62 | self::eol(); |
||
| 63 | } |
||
| 64 | self::eol(); |
||
| 65 | |||
| 66 | self::echo(' All current arguments...', Color::FOREGROUND_COLOR_GREEN); |
||
| 67 | self::eol(); |
||
| 68 | |||
| 69 | $args = $cmd->getAllProvidedArguments(); |
||
| 70 | foreach ($args as $a) { |
||
| 71 | self::echo(' ' . $a, Color::FOREGROUND_COLOR_GREEN); |
||
| 72 | self::eol(); |
||
| 73 | } |
||
| 74 | self::eol(); |
||
| 75 | |||
| 85 |