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