| Conditions | 15 |
| Paths | 1 |
| Total Lines | 51 |
| Code Lines | 36 |
| 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 |
||
| 216 | protected function _format(string $message, string $format, string $date_format, string $level, array $context = []): string |
||
| 217 | { |
||
| 218 | $parsed = preg_replace_callback('/(\{(([a-z]\.*)+)\})/', function ($match) use ($message, $level, $date_format, $context) { |
||
| 219 | $key = ''; |
||
| 220 | $context = array_merge($this->context, $context); |
||
| 221 | |||
| 222 | if ($sub_context = strpos($match[2], '.')) { |
||
| 223 | $parts = explode('.', $match[2]); |
||
| 224 | $name = $parts[0]; |
||
| 225 | $key = $parts[1]; |
||
| 226 | } else { |
||
| 227 | $name = $match[2]; |
||
| 228 | } |
||
| 229 | |||
| 230 | switch ($name) { |
||
| 231 | case 'level': |
||
| 232 | return $match[0] = $level; |
||
| 233 | break; |
||
| 234 | case 'date': |
||
| 235 | return $match[0] = date($date_format); |
||
| 236 | break; |
||
| 237 | case 'message': |
||
| 238 | $replace = []; |
||
| 239 | foreach ($context as $key => $val) { |
||
| 240 | if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
||
| 241 | $replace['{'.$key.'}'] = $val; |
||
| 242 | } else { |
||
| 243 | $replace['{'.$key.'}'] = json_encode($val); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | return $match[0] = strtr($message, $replace); |
||
| 248 | break; |
||
| 249 | case 'context': |
||
| 250 | if ($sub_context) { |
||
| 251 | if (array_key_exists($key, $context)) { |
||
| 252 | if (!is_array($context[$key]) && (!is_object($context[$key]) || method_exists($context[$key], '__toString'))) { |
||
| 253 | return $match[0] = $context[$key]; |
||
| 254 | } |
||
| 255 | |||
| 256 | return $match[0] = json_encode($context[$key]); |
||
| 257 | } |
||
| 258 | } else { |
||
| 259 | return $match[0] = json_encode($context); |
||
| 260 | } |
||
| 261 | |||
| 262 | break; |
||
| 263 | } |
||
| 264 | }, $format); |
||
| 265 | |||
| 266 | return $parsed; |
||
| 267 | } |
||
| 269 |