| Conditions | 10 |
| Paths | 6 |
| Total Lines | 88 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 | <?php |
||
| 192 | public function help(): void { |
||
| 193 | |||
| 194 | |||
| 195 | $help = <<<EOT |
||
| 196 | |||
| 197 | o |
||
| 198 | ` /_\ ' |
||
| 199 | - (o o) - |
||
| 200 | ----------ooO--(_)--Ooo---------- |
||
| 201 | Need help? |
||
| 202 | --------------------------------- |
||
| 203 | EOT; |
||
| 204 | |||
| 205 | |||
| 206 | App::echo($help, Color::FOREGROUND_COLOR_YELLOW); |
||
| 207 | |||
| 208 | App::eol(); |
||
| 209 | |||
| 210 | $oc = $this->getOptionCollection(); |
||
| 211 | $has_options = !empty($oc->options); |
||
| 212 | |||
| 213 | $has_subcommands = !empty($this->subcommands); |
||
| 214 | |||
| 215 | App::eol(); |
||
| 216 | App::echo('Usage: ', Color::FOREGROUND_COLOR_YELLOW); |
||
| 217 | App::eol(); |
||
| 218 | |||
| 219 | App::echo( |
||
| 220 | ' ' . |
||
| 221 | ($this->parent !== null ? $this->cmd : 'command') . |
||
| 222 | ($has_options ? ' [options]' : '') . |
||
| 223 | ($has_subcommands ? ' [command]' : '') |
||
| 224 | ); |
||
| 225 | |||
| 226 | App::eol(); |
||
| 227 | |||
| 228 | |||
| 229 | |||
| 230 | if ($has_options) { |
||
| 231 | |||
| 232 | App::eol(); |
||
| 233 | App::echo('Options: ', Color::FOREGROUND_COLOR_YELLOW); |
||
| 234 | App::eol(); |
||
| 235 | |||
| 236 | foreach ($oc->options as $option) { |
||
| 237 | |||
| 238 | $s = ' '; |
||
| 239 | if(!empty($option->short)) { |
||
| 240 | $s = '-' . $option->short . ', '; |
||
| 241 | } |
||
| 242 | $s .= '--' . $option->long; |
||
| 243 | |||
| 244 | $s = ' ' . str_pad($s, 20, ' '); |
||
| 245 | App::echo($s, Color::FOREGROUND_COLOR_GREEN); |
||
| 246 | |||
| 247 | $s = ' ' . $option->desc; |
||
| 248 | App::echo($s); |
||
| 249 | |||
| 250 | if ($option->defaultValue) { |
||
| 251 | $s = ' [default: ' . $option->defaultValue . ']'; |
||
| 252 | App::echo($s, Color::FOREGROUND_COLOR_YELLOW); |
||
| 253 | } |
||
| 254 | |||
| 255 | App::eol(); |
||
| 256 | |||
| 257 | } |
||
| 258 | |||
| 259 | App::eol(); |
||
| 260 | } |
||
| 261 | |||
| 262 | if($has_subcommands) { |
||
| 263 | |||
| 264 | App::eol(); |
||
| 265 | App::echo('Available commands: ', Color::FOREGROUND_COLOR_YELLOW); |
||
| 266 | App::eol(); |
||
| 267 | |||
| 268 | foreach ($this->subcommands as $cmd) { |
||
| 269 | |||
| 270 | $s = ' ' . str_pad($cmd->cmd, 20, ' '); |
||
| 271 | App::echo($s, Color::FOREGROUND_COLOR_GREEN); |
||
| 272 | |||
| 273 | $s = ' ' . $cmd->descr; |
||
| 274 | App::echo($s); |
||
| 275 | |||
| 276 | App::eol(); |
||
| 277 | } |
||
| 278 | |||
| 279 | App::eol(); |
||
| 280 | } |
||
| 321 | } |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.