| Conditions | 18 |
| Paths | 1344 |
| Total Lines | 90 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 124 | protected function formatMessageAttachment($message) |
||
|
|
|||
| 125 | { |
||
| 126 | list($text, $level, $category, $timestamp) = $message; |
||
| 127 | $attachment = [ |
||
| 128 | 'fallback' => static::encodeMessage($this->formatMessage($message)), |
||
| 129 | 'title' => ucwords(Logger::getLevelName($level)), |
||
| 130 | 'fields' => [ |
||
| 131 | [ |
||
| 132 | 'title' => 'Level', |
||
| 133 | 'value' => Logger::getLevelName($level), |
||
| 134 | 'short' => true |
||
| 135 | ], |
||
| 136 | [ |
||
| 137 | 'title' => 'Category', |
||
| 138 | 'value' => '`' . static::encodeMessage($category) . '`', |
||
| 139 | 'short' => true |
||
| 140 | ] |
||
| 141 | ], |
||
| 142 | 'footer' => static::class, |
||
| 143 | 'ts' => (integer) round($timestamp), |
||
| 144 | 'mrkdwn_in' => [ |
||
| 145 | 'fields', |
||
| 146 | 'text' |
||
| 147 | ] |
||
| 148 | ]; |
||
| 149 | if ($this->prefix !== null) { |
||
| 150 | $attachment['fields'][] = [ |
||
| 151 | 'title' => 'Prefix', |
||
| 152 | 'value' => '`' . static::encodeMessage(call_user_func($this->prefix, $message)) . '`', |
||
| 153 | 'short' => true, |
||
| 154 | ]; |
||
| 155 | } |
||
| 156 | if (isset(\Yii::$app)) { |
||
| 157 | if (isset($_SERVER['argv'])) { |
||
| 158 | $attachment['author_name'] = implode(' ', $_SERVER['argv']); |
||
| 159 | } elseif (\Yii::$app->request instanceof Request) { |
||
| 160 | $attachment['author_name'] = Url::current([], true); |
||
|
1 ignored issue
–
show
|
|||
| 161 | $attachment['author_link'] = $attachment['author_name']; |
||
| 162 | $attachment['fields'][] = [ |
||
| 163 | 'title' => 'User IP', |
||
| 164 | 'value' => \Yii::$app->request->userIP, |
||
| 165 | 'short' => true, |
||
| 166 | ]; |
||
| 167 | } |
||
| 168 | if (\Yii::$app->has('user', true) && isset(\Yii::$app->user)) { |
||
| 169 | $user = \Yii::$app->user->getIdentity(false); |
||
| 170 | if (isset($user)) { |
||
| 171 | $attachment['fields'][] = [ |
||
| 172 | 'title' => 'User ID', |
||
| 173 | 'value' => $user->getId(), |
||
| 174 | 'short' => true, |
||
| 175 | ]; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | if (\Yii::$app->has('session', true) && isset(\Yii::$app->session)) { |
||
| 179 | if (\Yii::$app->session->isActive) { |
||
| 180 | $attachment['fields'][] = [ |
||
| 181 | 'title' => 'Session ID', |
||
| 182 | 'value' => \Yii::$app->session->id, |
||
| 183 | 'short' => true, |
||
| 184 | ]; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | if (isset($this->colors[$level])) { |
||
| 189 | $attachment['color'] = $this->colors[$level]; |
||
| 190 | } |
||
| 191 | if (!is_string($text)) { |
||
| 192 | if ($text instanceof \Throwable || $text instanceof \Exception) { |
||
| 193 | $text = (string) $text; |
||
| 194 | } else { |
||
| 195 | $text = VarDumper::export($text); |
||
|
1 ignored issue
–
show
|
|||
| 196 | } |
||
| 197 | } |
||
| 198 | $attachment['text'] = "```\n" . static::encodeMessage($text) . "\n```"; |
||
| 199 | $traces = []; |
||
| 200 | if (isset($message[4])) { |
||
| 201 | foreach ($message[4] as $trace) { |
||
| 202 | $traces[] = "in {$trace['file']}:{$trace['line']}"; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | if (!empty($traces)) { |
||
| 206 | $attachment['fields'][] = [ |
||
| 207 | 'title' => 'Stack Trace', |
||
| 208 | 'value' => "```\n" . static::encodeMessage(implode("\n", $traces)) . "\n```", |
||
| 209 | 'short' => false, |
||
| 210 | ]; |
||
| 211 | } |
||
| 212 | return $attachment; |
||
| 213 | } |
||
| 214 | } |
||
| 215 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: