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