| Conditions | 2 |
| Total Lines | 86 |
| 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 ( |
||
| 14 | new class extends Cmd { |
||
| 15 | |||
| 16 | public function init(): void { |
||
| 17 | } |
||
| 18 | |||
| 19 | public function run(): void { |
||
| 20 | $this->runHelp(); |
||
| 21 | } |
||
| 22 | }) |
||
| 23 | ->addChild((new class('show') extends Cmd { |
||
| 24 | |||
| 25 | public function init(): void { |
||
| 26 | |||
| 27 | $this |
||
| 28 | ->setDescription( |
||
| 29 | 'This command is used to show something. Take a look at the subcommands.' |
||
| 30 | ); |
||
| 31 | |||
| 32 | } |
||
| 33 | |||
| 34 | public function run(): void { |
||
| 35 | $this->runHelp(); |
||
| 36 | } |
||
| 37 | |||
| 38 | }) |
||
| 39 | ->addChild((new class('hello') extends Cmd { |
||
| 40 | |||
| 41 | public function init(): void { |
||
| 42 | |||
| 43 | $this |
||
| 44 | ->setDescription( |
||
| 45 | 'Displays hello world.' |
||
| 46 | ) |
||
| 47 | ->addOption('h|help', [ |
||
| 48 | 'description' => 'Displays the command help.' |
||
| 49 | ]) |
||
| 50 | ->addOption('name:', [ |
||
| 51 | 'description' => 'Name option. This option requires a value.', |
||
| 52 | 'isa' => 'string', |
||
| 53 | 'default' => 'World' |
||
| 54 | ]); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function run(): void { |
||
| 58 | |||
| 59 | if ($this->hasProvidedOption('help')) { |
||
| 60 | $this->runHelp(); |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | |||
| 64 | $name = $this->getProvidedOption('name'); |
||
| 65 | |||
| 66 | self::eol(); |
||
| 67 | self::echo(sprintf('Hello %s!', $name), Color::FOREGROUND_COLOR_CYAN); |
||
| 68 | self::eol(); |
||
| 69 | self::eol(); |
||
| 70 | } |
||
| 71 | })) |
||
| 72 | ->addChild((new class('phpversion') extends Cmd { |
||
| 73 | |||
| 74 | public function init(): void { |
||
| 75 | |||
| 76 | $this |
||
| 77 | ->addOption('h|help', [ |
||
| 78 | 'description' => 'Displays the command help.' |
||
| 79 | ]) |
||
| 80 | ->setDescription( |
||
| 81 | 'Displays the current php version of your cli.' |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function run(): void { |
||
| 86 | |||
| 87 | if ($this->hasProvidedOption('help')) { |
||
| 88 | $this->runHelp(); |
||
| 89 | return; |
||
| 90 | } |
||
| 91 | |||
| 92 | self::eol(); |
||
| 93 | self::echo(' Your PHP version is:', Color::FOREGROUND_COLOR_YELLOW); |
||
| 94 | self::eol(); |
||
| 95 | self::echo(' ' . PHP_VERSION); |
||
| 96 | self::eol(); |
||
| 97 | self::eol(); |
||
| 98 | } |
||
| 104 |