| Conditions | 18 |
| Paths | 1344 |
| Total Lines | 90 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 131 | protected function formatMessageAttachment($message) |
||
|
|
|||
| 132 | { |
||
| 133 | list($text, $level, $category, $timestamp) = $message; |
||
| 134 | $attachment = [ |
||
| 135 | 'fallback' => static::encodeMessage($this->formatMessage($message)), |
||
| 136 | 'title' => ucwords(Logger::getLevelName($level)), |
||
| 137 | 'fields' => [ |
||
| 138 | [ |
||
| 139 | 'title' => 'Level', |
||
| 140 | 'value' => Logger::getLevelName($level), |
||
| 141 | 'short' => true |
||
| 142 | ], |
||
| 143 | [ |
||
| 144 | 'title' => 'Category', |
||
| 145 | 'value' => '`' . static::encodeMessage($category) . '`', |
||
| 146 | 'short' => true |
||
| 147 | ] |
||
| 148 | ], |
||
| 149 | 'footer' => static::class, |
||
| 150 | 'ts' => (integer) round($timestamp), |
||
| 151 | 'mrkdwn_in' => [ |
||
| 152 | 'fields', |
||
| 153 | 'text' |
||
| 154 | ] |
||
| 155 | ]; |
||
| 156 | if ($this->prefix !== null) { |
||
| 157 | $attachment['fields'][] = [ |
||
| 158 | 'title' => 'Prefix', |
||
| 159 | 'value' => '`' . static::encodeMessage(call_user_func($this->prefix, $message)) . '`', |
||
| 160 | 'short' => true, |
||
| 161 | ]; |
||
| 162 | } |
||
| 163 | if (isset(\Yii::$app)) { |
||
| 164 | if (isset($_SERVER['argv'])) { |
||
| 165 | $attachment['author_name'] = implode(' ', $_SERVER['argv']); |
||
| 166 | } elseif (\Yii::$app->getRequest() instanceof Request) { |
||
| 167 | $attachment['author_name'] = Url::current([], true); |
||
|
1 ignored issue
–
show
|
|||
| 168 | $attachment['author_link'] = $attachment['author_name']; |
||
| 169 | $attachment['fields'][] = [ |
||
| 170 | 'title' => 'User IP', |
||
| 171 | 'value' => \Yii::$app->getRequest()->getUserIP(), |
||
| 172 | 'short' => true, |
||
| 173 | ]; |
||
| 174 | } |
||
| 175 | if (\Yii::$app->has('user', true) && !is_null(\Yii::$app->getUser())) { |
||
| 176 | $user = \Yii::$app->getUser()->getIdentity(false); |
||
|
1 ignored issue
–
show
|
|||
| 177 | if (isset($user)) { |
||
| 178 | $attachment['fields'][] = [ |
||
| 179 | 'title' => 'User ID', |
||
| 180 | 'value' => $user->getId(), |
||
| 181 | 'short' => true, |
||
| 182 | ]; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | if (\Yii::$app->has('session', true) && !is_null(\Yii::$app->getSession())) { |
||
| 186 | if (\Yii::$app->getSession()->getIsActive()) { |
||
| 187 | $attachment['fields'][] = [ |
||
| 188 | 'title' => 'Session ID', |
||
| 189 | 'value' => \Yii::$app->getSession()->getId(), |
||
|
1 ignored issue
–
show
|
|||
| 190 | 'short' => true, |
||
| 191 | ]; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | if (isset($this->colors[$level])) { |
||
| 196 | $attachment['color'] = $this->colors[$level]; |
||
| 197 | } |
||
| 198 | if (!is_string($text)) { |
||
| 199 | if ($text instanceof \Throwable || $text instanceof \Exception) { |
||
| 200 | $text = (string) $text; |
||
| 201 | } else { |
||
| 202 | $text = VarDumper::export($text); |
||
|
1 ignored issue
–
show
|
|||
| 203 | } |
||
| 204 | } |
||
| 205 | $attachment['text'] = "```\n" . static::encodeMessage($text) . "\n```"; |
||
| 206 | $traces = []; |
||
| 207 | if (isset($message[4])) { |
||
| 208 | foreach ($message[4] as $trace) { |
||
| 209 | $traces[] = "in {$trace['file']}:{$trace['line']}"; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | if (!empty($traces)) { |
||
| 213 | $attachment['fields'][] = [ |
||
| 214 | 'title' => 'Stack Trace', |
||
| 215 | 'value' => "```\n" . static::encodeMessage(implode("\n", $traces)) . "\n```", |
||
| 216 | 'short' => false, |
||
| 217 | ]; |
||
| 218 | } |
||
| 219 | return $attachment; |
||
| 220 | } |
||
| 221 | } |
||
| 222 |
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: