| Conditions | 30 |
| Paths | 144 |
| Total Lines | 93 |
| Code Lines | 84 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 102 | public function log(string $level, string $message, array $context = []): void |
||
| 103 | { |
||
| 104 | $template = $this->getUserTemplate(); |
||
| 105 | $msg = []; |
||
| 106 | $module = ArrayHelper::getValue($context, 'module'); |
||
| 107 | foreach ($this->template as $tmp) { |
||
| 108 | switch ($tmp) { |
||
| 109 | case '%W': |
||
| 110 | $msg[] = ArrayHelper::getValue($template, $tmp, -1); |
||
| 111 | break; |
||
| 112 | case '%L': |
||
| 113 | $msg[] = $level; |
||
| 114 | break; |
||
| 115 | case '%M': |
||
| 116 | $msg[] = str_replace($this->split, ' ', empty($context) ? $message : strtr($message, $context)); |
||
| 117 | break; |
||
| 118 | case '%T': |
||
| 119 | case '%t': |
||
| 120 | if ($this->isMicroTime > 0) { |
||
| 121 | $micsec = $this->isMicroTime > 3 ? 3 : $this->isMicroTime; |
||
| 122 | $mtimestamp = sprintf("%.{$micsec}f", microtime(true)); // 带毫秒的时间戳 |
||
| 123 | $timestamp = floor($mtimestamp); // 时间戳 |
||
|
|
|||
| 124 | $milliseconds = round(($mtimestamp - $timestamp) * 1000); // 毫秒 |
||
| 125 | } else { |
||
| 126 | $timestamp = time(); |
||
| 127 | $milliseconds = 0; |
||
| 128 | } |
||
| 129 | if ($tmp === '%T') { |
||
| 130 | $msg[] = date($this->datetime_format, (int)$timestamp) . '.' . (int)$milliseconds; |
||
| 131 | } else { |
||
| 132 | $msg[] = date($this->datetime_format, (int)$timestamp); |
||
| 133 | } |
||
| 134 | break; |
||
| 135 | case '%Q': |
||
| 136 | $msg[] = ArrayHelper::getValue($template, $tmp, uniqid()); |
||
| 137 | break; |
||
| 138 | case '%H': |
||
| 139 | $msg[] = ArrayHelper::getValue($template, $tmp, $_SERVER['HOSTNAME']); |
||
| 140 | break; |
||
| 141 | case '%P': |
||
| 142 | $msg[] = ArrayHelper::getValue($template, $tmp, getmypid()); |
||
| 143 | break; |
||
| 144 | case '%D': |
||
| 145 | $msg[] = ArrayHelper::getValue($template, $tmp, 'cli'); |
||
| 146 | break; |
||
| 147 | case '%R': |
||
| 148 | $msg[] = ArrayHelper::getValue($template, $tmp, $_SERVER['SCRIPT_NAME']); |
||
| 149 | break; |
||
| 150 | case '%m': |
||
| 151 | $method = ArrayHelper::getValue($template, $tmp); |
||
| 152 | $msg[] = $method ? strtoupper($method) : $_SERVER['SHELL']; |
||
| 153 | break; |
||
| 154 | case '%I': |
||
| 155 | $msg[] = ArrayHelper::getValue($template, $tmp, 'local'); |
||
| 156 | break; |
||
| 157 | case '%F': |
||
| 158 | case '%C': |
||
| 159 | $trace = Co::getBackTrace(Co::getCid(), DEBUG_BACKTRACE_IGNORE_ARGS, |
||
| 160 | $this->recall_depth + 2); |
||
| 161 | if ($tmp === '%F') { |
||
| 162 | $trace = $trace[$this->recall_depth]; |
||
| 163 | $msg[] = $this->useBasename ? basename($trace['file']) . ':' . $trace['line'] : $trace['file'] . ':' . $trace['line']; |
||
| 164 | } else { |
||
| 165 | $trace = $trace[$this->recall_depth + 1]; |
||
| 166 | $msg[] = $trace['class'] . $trace['type'] . $trace['function']; |
||
| 167 | } |
||
| 168 | break; |
||
| 169 | case '%U': |
||
| 170 | $msg[] = memory_get_usage(); |
||
| 171 | break; |
||
| 172 | case '%u': |
||
| 173 | $msg[] = memory_get_peak_usage(); |
||
| 174 | break; |
||
| 175 | case '%A': |
||
| 176 | $customerTemplate = ArrayHelper::getValue($context, 'template', |
||
| 177 | []) ?? ArrayHelper::getValue($template, $tmp, |
||
| 178 | []); |
||
| 179 | switch ($this->customerType) { |
||
| 180 | case AbstractConfig::TYPE_JSON: |
||
| 181 | $msg[] = json_encode($customerTemplate, JSON_UNESCAPED_UNICODE); |
||
| 182 | break; |
||
| 183 | case AbstractConfig::TYPE_FIELD: |
||
| 184 | default: |
||
| 185 | $msg[] = implode($this->split, $customerTemplate); |
||
| 186 | } |
||
| 187 | break; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | $color = ArrayHelper::getValue($template, '%c'); |
||
| 191 | $color && $msg['%c'] = $color; |
||
| 192 | $key = $this->appName . ($module ? '_' . $module : ''); |
||
| 193 | $this->buffer[$key][] = $msg; |
||
| 194 | $this->flush(); |
||
| 195 | } |
||
| 212 | } |