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