Conditions | 11 |
Paths | 100 |
Total Lines | 62 |
Code Lines | 41 |
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 | <?php |
||
34 | public function handle(ConsoleEvent $event) |
||
35 | { |
||
36 | $cfg = $event->getApp()->getConfig('console'); |
||
37 | if (empty($cfg->router->routes)) { |
||
38 | return; |
||
39 | } |
||
40 | |||
41 | $routes = $cfg->router->routes; |
||
42 | $requiredCommand = $event->getParam('command', 'help'); |
||
43 | |||
44 | // find route |
||
45 | $route = null; |
||
46 | foreach ($routes as $item) { |
||
47 | if (!empty($item->options) && 0 === strpos($item->options->route, $requiredCommand)) { |
||
48 | $route = $item; |
||
49 | break; |
||
50 | } |
||
51 | } |
||
52 | |||
53 | if (empty($route)) { |
||
54 | // command not found |
||
55 | $event->getCli()->draw('404')->br(); |
||
56 | return; |
||
57 | } |
||
58 | |||
59 | $title = (string) $route->options->defaults->title ?? null; |
||
60 | $description = (string) $route->options->defaults->description ?? null; |
||
61 | $argumentsDescription = (array) $route->options->defaults->argumentsDescription ?? []; |
||
62 | $optionsDescription = (array) $route->options->defaults->optionsDescription ?? []; |
||
63 | |||
64 | $newArgumentsDescription = []; |
||
65 | foreach ($argumentsDescription as $key => $value) { |
||
66 | $newArgumentsDescription[" <green>$key</green>"] = '- ' . $value; |
||
67 | } |
||
68 | |||
69 | $newOptionsDescription = []; |
||
70 | foreach ($optionsDescription as $key => $value) { |
||
71 | $newOptionsDescription[" <green>--$key</green>"] = '- ' . $value; |
||
72 | } |
||
73 | |||
74 | $cli = $event->getCli(); |
||
75 | |||
76 | $cli |
||
77 | ->invert()->bold(" $title ")->br() |
||
78 | ->yellow()->bold('Usage:') |
||
79 | ->white()->blackBg(" {$route->options->route} ")->br(); |
||
80 | |||
81 | $description |
||
82 | and $cli |
||
|
|||
83 | ->yellow()->bold('Description:') |
||
84 | ->out(' ' . str_replace(PHP_EOL, PHP_EOL . ' ', $description))->br(); |
||
85 | |||
86 | $newArgumentsDescription |
||
87 | and $cli |
||
88 | ->yellow()->bold('Arguments:') |
||
89 | ->columns($newArgumentsDescription)->br(); |
||
90 | |||
91 | $newOptionsDescription |
||
92 | and $cli |
||
93 | ->yellow()->bold('Options:') |
||
94 | ->columns($newOptionsDescription)->br(); |
||
95 | } |
||
96 | } |
||
97 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.