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