| Conditions | 21 |
| Paths | 2017 |
| Total Lines | 69 |
| Code Lines | 42 |
| 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 |
||
| 44 | public function export() |
||
| 45 | { |
||
| 46 | foreach ($this->messages as $message) { |
||
| 47 | $context = []; |
||
| 48 | if (isset($message[4])) { |
||
| 49 | $context['trace'] = $message[4]; |
||
| 50 | } |
||
| 51 | |||
| 52 | if (isset($message[5])) { |
||
| 53 | $context['memory'] = $message[5]; |
||
| 54 | } |
||
| 55 | |||
| 56 | if (isset($message[2])) { |
||
| 57 | $context['category'] = $message[2]; |
||
| 58 | } |
||
| 59 | |||
| 60 | $text = $message[0]; |
||
| 61 | |||
| 62 | if (!is_string($text)) { |
||
| 63 | // exceptions may not be serializable if in the call stack somewhere is a Closure |
||
| 64 | if ($text instanceof \Throwable || $text instanceof \Exception) { |
||
| 65 | $text = (string)$text; |
||
| 66 | } elseif (\is_array($text)) { |
||
| 67 | $ctx = $text; |
||
| 68 | if (isset($ctx['message'])) { |
||
| 69 | $text = $ctx['message']; |
||
| 70 | unset($ctx['message']); |
||
| 71 | |||
| 72 | if (isset($ctx['exception'])) { |
||
| 73 | $e = $ctx['exception']; |
||
| 74 | unset($ctx['exception']); |
||
| 75 | $context['exception'] = [ |
||
| 76 | 'message' => $e->getMessage(), |
||
| 77 | 'line' => $e->getLine(), |
||
| 78 | 'file' => $e->getFile(), |
||
| 79 | 'code' => $e->getCode(), |
||
| 80 | 'trace' => $e->getTrace() |
||
| 81 | ]; |
||
| 82 | } |
||
| 83 | foreach ($ctx as $k => $v) { |
||
| 84 | $context[$k] = $v; |
||
| 85 | } |
||
| 86 | } else { |
||
| 87 | $text = VarDumper::export($text); |
||
| 88 | } |
||
| 89 | } else { |
||
| 90 | $text = VarDumper::export($text); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | // If the user_id is not passed, dynamically set it from the user identity object |
||
| 95 | if (!isset($context['user_id'])) { |
||
| 96 | $context['user_id'] = (isset(Yii::$app) && Yii::$app->has('user') && Yii::$app->user->id != null) ? Yii::$app->user->id : 'system'; |
||
|
|
|||
| 97 | } |
||
| 98 | |||
| 99 | // If the user_id of the event isn't the system, pre-load the policy number |
||
| 100 | if (!\in_array($context['user_id'], ['system', null])) { |
||
| 101 | if (isset(Yii::$app) && Yii::$app->has('user') && Yii::$app->user->id) { |
||
| 102 | $model = Yii::$app->user->identity; |
||
| 103 | } else { |
||
| 104 | $model = Yii::$app->user->identityClass::find()->where(['id' => $context['user_id']])->one(); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!isset($context['timestamp'])) { |
||
| 109 | $context['timestamp'] = \microtime(true); |
||
| 110 | } |
||
| 111 | |||
| 112 | $this->getLogger()->log($this->_psrLevels[$message[1]], $text, $context); |
||
| 113 | } |
||
| 116 |