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