| 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 |
||
| 149 | public function getAvatar($user, array $options = []) |
||
| 150 | { |
||
| 151 | $getAvatar = function () use ($user, $options) { |
||
| 152 | Stopwatch::start('UserHelper::getAvatar()'); |
||
| 153 | $defaults = [ |
||
| 154 | 'class' => 'avatar-image', |
||
| 155 | 'link' => [ |
||
| 156 | 'class' => 'avatar-link', |
||
| 157 | 'escape' => false |
||
| 158 | ], |
||
| 159 | 'size' => 50, |
||
| 160 | 'style' => '', |
||
| 161 | 'tag' => 'span' |
||
| 162 | ]; |
||
| 163 | $options = array_replace_recursive($defaults, $options); |
||
| 164 | $size = $options['size']; |
||
| 165 | |||
| 166 | $avatar = $user->get('avatar'); |
||
| 167 | if ($avatar) { |
||
| 168 | $userId = $user->get('id'); |
||
| 169 | $url = "useruploads/users/avatar/{$userId}/square_{$avatar}"; |
||
| 170 | $imgUri = $this->Url->assetUrl($url); |
||
| 171 | } else { |
||
| 172 | $name = $user->get('username'); |
||
| 173 | $hdpi = 2 * $size; |
||
| 174 | $imgUri = (new Identicon)->getImageDataUri($name, $hdpi); |
||
| 175 | } |
||
| 176 | |||
| 177 | $style = "background-image: url({$imgUri});" . $options['style']; |
||
| 178 | |||
| 179 | $html = $this->Html->tag( |
||
| 180 | $options['tag'], |
||
| 181 | '', |
||
| 182 | [ |
||
| 183 | 'class' => $options['class'], |
||
| 184 | 'style' => $style, |
||
| 185 | ] |
||
| 186 | ); |
||
| 187 | |||
| 188 | if ($options['link'] !== false) { |
||
| 189 | $options['link']['title'] = $html; |
||
| 190 | $html = $this->linkToUserProfile($user, true, $options['link']); |
||
| 191 | } |
||
| 192 | Stopwatch::end('UserHelper::getAvatar()'); |
||
| 193 | |||
| 194 | return $html; |
||
| 195 | }; |
||
| 196 | |||
| 197 | $name = $user->get('username'); |
||
| 198 | $hash = 'avatar.' . md5($name . serialize($options)); |
||
| 199 | |||
| 200 | return $this->remember($hash, $getAvatar); |
||
| 201 | } |
||
| 203 |