| Conditions | 15 |
| Paths | 1 |
| Total Lines | 51 |
| Code Lines | 38 |
| 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 |
||
| 215 | protected function _format(string $message, string $format, string $date_format, string $level, array $context=[]): string |
||
| 216 | { |
||
| 217 | $parsed = preg_replace_callback('/(\{(([a-z]\.*)+)\})/', function ($match) use ($message, $level, $date_format, $context) { |
||
| 218 | $key = ''; |
||
| 219 | $context = array_merge($this->context, $context); |
||
| 220 | |||
| 221 | if ($sub_context = strpos($match[2], '.')) { |
||
| 222 | $parts = explode('.', $match[2]); |
||
| 223 | $name = $parts[0]; |
||
| 224 | $key = $parts[1]; |
||
| 225 | } else { |
||
| 226 | $name = $match[2]; |
||
| 227 | } |
||
| 228 | |||
| 229 | switch ($name) { |
||
| 230 | case 'level': |
||
| 231 | return $match[0] = $level; |
||
| 232 | break; |
||
| 233 | case 'date': |
||
| 234 | return $match[0] = date($date_format); |
||
| 235 | break; |
||
| 236 | case 'message': |
||
| 237 | $replace = []; |
||
| 238 | foreach ($context as $key => $val) { |
||
| 239 | if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
||
| 240 | $replace['{' . $key . '}'] = $val; |
||
| 241 | } else { |
||
| 242 | $replace['{' . $key . '}'] = json_encode($val); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | return $match[0] = strtr($message, $replace); |
||
| 247 | break; |
||
| 248 | case 'context': |
||
| 249 | if ($sub_context) { |
||
| 250 | if (array_key_exists($key, $context)) { |
||
| 251 | if (!is_array($context[$key]) && (!is_object($context[$key]) || method_exists($context[$key], '__toString'))) { |
||
| 252 | return $match[0] = $context[$key]; |
||
| 253 | } else { |
||
| 254 | return $match[0] = json_encode($context[$key]); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } else { |
||
| 258 | return $match[0] = json_encode($context); |
||
| 259 | } |
||
| 260 | break; |
||
| 261 | } |
||
| 262 | }, $format); |
||
| 263 | |||
| 264 | return $parsed; |
||
| 265 | } |
||
| 266 | } |
||
| 267 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: