Total Lines | 88 |
Code Lines | 48 |
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 | ->addOption('h|help', [ |
||
18 | 'description' => 'Displays the command help.' |
||
19 | ]) |
||
20 | ->addOption('v|verbose', [ |
||
21 | 'description' => 'Flag to enable verbose output.' |
||
22 | ]) |
||
23 | ->addOption('name:', [ |
||
24 | 'description' => 'Name option. This option requires a value.', |
||
25 | 'isa' => 'string', |
||
26 | 'default' => 'World' |
||
27 | ]) |
||
28 | ->setRunner( |
||
29 | (new class extends CmdRunner { |
||
30 | |||
31 | public function run(): void { |
||
32 | |||
33 | $cmd = $this->getCmd(); |
||
34 | |||
35 | if ($cmd->hasProvidedOption('help')) { |
||
36 | $cmd->help(); |
||
37 | return; |
||
38 | } |
||
39 | |||
40 | $name = $cmd->getProvidedOption('name'); |
||
41 | |||
42 | self::eol(); |
||
43 | self::echo(sprintf('Hello %s', $name), Color::FOREGROUND_COLOR_YELLOW); |
||
44 | self::eol(); |
||
45 | self::eol(); |
||
46 | |||
47 | if ($cmd->hasProvidedOption('verbose')) { |
||
48 | $this->verbose(); |
||
49 | } |
||
50 | |||
51 | } |
||
52 | |||
53 | public function help(): void { |
||
54 | |||
55 | echo '' . |
||
56 | ' This is the custom help for ' . Color::green('cli-app-options') . ' command. ' . self::EOL . |
||
57 | ' Please use the --name option to pass your name to this command and you will be greeted personally. ' . self::EOL . |
||
58 | ' ' . self::EOL . |
||
59 | ' ' . Color::green('php cli-app-options.php --name="great Stefaminator"') . self::EOL; |
||
60 | |||
61 | |||
62 | } |
||
63 | |||
64 | private function verbose(): void { |
||
65 | |||
66 | self::echo('--- VERBOSE OUTPUT ---', Color::FOREGROUND_COLOR_GREEN); |
||
67 | self::eol(); |
||
68 | self::eol(); |
||
69 | |||
70 | $this->outputProvidedOptions(); |
||
71 | |||
72 | $this->outputProvidedArguments(); |
||
73 | } |
||
74 | |||
75 | private function outputProvidedOptions(): void { |
||
76 | |||
77 | $cmd = $this->getCmd(); |
||
78 | |||
79 | self::echo(' All current options...', Color::FOREGROUND_COLOR_GREEN); |
||
80 | self::eol(); |
||
81 | |||
82 | $pOptions = $cmd->getAllProvidedOptions(); |
||
83 | foreach ($pOptions as $k => $v) { |
||
84 | self::echo(' ' . $k . ': ' . json_encode($v), Color::FOREGROUND_COLOR_GREEN); |
||
85 | self::eol(); |
||
86 | } |
||
87 | self::eol(); |
||
88 | } |
||
89 | |||
90 | private function outputProvidedArguments(): void { |
||
91 | |||
92 | $cmd = $this->getCmd(); |
||
93 | |||
94 | self::echo(' All current arguments...', Color::FOREGROUND_COLOR_GREEN); |
||
95 | self::eol(); |
||
96 | |||
97 | $args = $cmd->getAllProvidedArguments(); |
||
98 | foreach ($args as $a) { |
||
99 | self::echo(' ' . $a, Color::FOREGROUND_COLOR_GREEN); |
||
100 | self::eol(); |
||
101 | } |
||
102 | self::eol(); |
||
103 | |||
111 |