| Conditions | 3 |
| Paths | 1 |
| Total Lines | 52 |
| Code Lines | 35 |
| 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 |
||
| 168 | public function getAvatar($user, array $options = []) |
||
| 169 | { |
||
| 170 | $getAvatar = function () use ($user, $options) { |
||
| 171 | Stopwatch::start('UserHelper::getAvatar()'); |
||
| 172 | $defaults = [ |
||
| 173 | 'class' => 'avatar-image', |
||
| 174 | 'link' => [ |
||
| 175 | 'class' => 'avatar-link', |
||
| 176 | 'escape' => false |
||
| 177 | ], |
||
| 178 | 'size' => 50, |
||
| 179 | 'style' => '', |
||
| 180 | 'tag' => 'span' |
||
| 181 | ]; |
||
| 182 | $options = array_replace_recursive($defaults, $options); |
||
| 183 | $size = $options['size']; |
||
| 184 | |||
| 185 | $avatar = $user->get('avatar'); |
||
| 186 | if ($avatar) { |
||
| 187 | $userId = $user->get('id'); |
||
| 188 | $url = "useruploads/users/avatar/{$userId}/square_{$avatar}"; |
||
| 189 | $imgUri = $this->Url->assetUrl($url); |
||
| 190 | } else { |
||
| 191 | $name = $user->get('username'); |
||
| 192 | $hdpi = 2 * $size; |
||
| 193 | $imgUri = (new Identicon)->getImageDataUri($name, $hdpi); |
||
| 194 | } |
||
| 195 | |||
| 196 | $style = "background-image: url({$imgUri});" . $options['style']; |
||
| 197 | |||
| 198 | $html = $this->Html->tag( |
||
| 199 | $options['tag'], |
||
| 200 | '', |
||
| 201 | [ |
||
| 202 | 'class' => $options['class'], |
||
| 203 | 'style' => $style, |
||
| 204 | ] |
||
| 205 | ); |
||
| 206 | |||
| 207 | if ($options['link'] !== false) { |
||
| 208 | $options['link']['title'] = $html; |
||
| 209 | $html = $this->linkToUserProfile($user, true, $options['link']); |
||
| 210 | } |
||
| 211 | Stopwatch::end('UserHelper::getAvatar()'); |
||
| 212 | |||
| 213 | return $html; |
||
| 214 | }; |
||
| 215 | |||
| 216 | $name = $user->get('username'); |
||
| 217 | $hash = 'avatar.' . md5($name . serialize($options)); |
||
| 218 | |||
| 219 | return $this->remember($hash, $getAvatar); |
||
| 220 | } |
||
| 222 |