Conditions | 11 |
Paths | 42 |
Total Lines | 38 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 132 |
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 |
||
114 | protected function console(string $type, $msg) |
||
115 | { |
||
116 | $type = strtolower($type); |
||
117 | $trace_tabs = array_values($this->config['tabs']); |
||
118 | $line = []; |
||
119 | $line[] = ($type == $trace_tabs[0] || '调试' == $type || '错误' == $type) |
||
120 | ? "console.group('{$type}');" |
||
121 | : "console.groupCollapsed('{$type}');"; |
||
122 | |||
123 | foreach ((array) $msg as $key => $m) { |
||
124 | switch ($type) { |
||
125 | case '调试': |
||
1 ignored issue
–
show
|
|||
126 | $var_type = gettype($m); |
||
127 | if (in_array($var_type, ['array', 'string'])) { |
||
1 ignored issue
–
show
|
|||
128 | $line[] = "console.log(" . json_encode($m) . ");"; |
||
129 | } else { |
||
1 ignored issue
–
show
|
|||
130 | $line[] = "console.log(" . json_encode(var_export($m, true)) . ");"; |
||
131 | } |
||
1 ignored issue
–
show
|
|||
132 | break; |
||
133 | case '错误': |
||
1 ignored issue
–
show
|
|||
134 | $msg = str_replace("\n", '\n', addslashes(is_scalar($m) ? $m : json_encode($m))); |
||
135 | $style = 'color:#F4006B;font-size:14px;'; |
||
136 | $line[] = "console.error(\"%c{$msg}\", \"{$style}\");"; |
||
137 | break; |
||
138 | case 'sql': |
||
1 ignored issue
–
show
|
|||
139 | $msg = str_replace("\n", '\n', addslashes($m)); |
||
140 | $style = "color:#009bb4;"; |
||
141 | $line[] = "console.log(\"%c{$msg}\", \"{$style}\");"; |
||
142 | break; |
||
143 | default: |
||
1 ignored issue
–
show
|
|||
144 | $m = is_string($key) ? $key . ' ' . $m : $key + 1 . ' ' . $m; |
||
145 | $msg = json_encode($m); |
||
146 | $line[] = "console.log({$msg});"; |
||
147 | break; |
||
148 | } |
||
149 | } |
||
150 | $line[] = "console.groupEnd();"; |
||
151 | return implode(PHP_EOL, $line); |
||
152 | } |
||
171 |