Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Avatar often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Avatar, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
58 | class Avatar implements IAvatar { |
||
59 | /** @var ISimpleFolder */ |
||
60 | private $folder; |
||
61 | /** @var IL10N */ |
||
62 | private $l; |
||
63 | /** @var User */ |
||
64 | private $user; |
||
65 | /** @var ILogger */ |
||
66 | private $logger; |
||
67 | /** @var IConfig */ |
||
68 | private $config; |
||
69 | |||
70 | /** |
||
71 | * constructor |
||
72 | * |
||
73 | * @param ISimpleFolder $folder The folder where the avatars are |
||
74 | * @param IL10N $l |
||
75 | * @param User $user |
||
76 | * @param ILogger $logger |
||
77 | * @param IConfig $config |
||
78 | */ |
||
79 | View Code Duplication | public function __construct(ISimpleFolder $folder, |
|
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | public function get ($size = 64) { |
||
105 | |||
106 | /** |
||
107 | * Check if an avatar exists for the user |
||
108 | * |
||
109 | * @return bool |
||
110 | */ |
||
111 | public function exists() { |
||
115 | |||
116 | /** |
||
117 | * sets the users avatar |
||
118 | * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
||
119 | * @throws \Exception if the provided file is not a jpg or png image |
||
120 | * @throws \Exception if the provided image is not valid |
||
121 | * @throws NotSquareException if the image is not square |
||
122 | * @return void |
||
123 | */ |
||
124 | public function set ($data) { |
||
178 | |||
179 | /** |
||
180 | * remove the users avatar |
||
181 | * @return void |
||
182 | */ |
||
183 | public function remove () { |
||
195 | |||
196 | /** |
||
197 | * @inheritdoc |
||
198 | */ |
||
199 | public function getFile($size) { |
||
254 | |||
255 | /** |
||
256 | * Get the extension of the avatar. If there is no avatar throw Exception |
||
257 | * |
||
258 | * @return string |
||
259 | * @throws NotFoundException |
||
260 | */ |
||
261 | private function getExtension() { |
||
269 | |||
270 | /** |
||
271 | * @param string $userDisplayName |
||
272 | * @param int $size |
||
273 | * @return string |
||
274 | */ |
||
275 | private function generateAvatar($userDisplayName, $size) { |
||
276 | $text = mb_strtoupper(mb_substr($userDisplayName, 0, 1), 'UTF-8'); |
||
277 | $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
||
278 | |||
279 | $im = imagecreatetruecolor($size, $size); |
||
280 | $background = imagecolorallocate($im, $backgroundColor->r, $backgroundColor->g, $backgroundColor->b); |
||
281 | $white = imagecolorallocate($im, 255, 255, 255); |
||
282 | imagefilledrectangle($im, 0, 0, $size, $size, $background); |
||
283 | |||
284 | $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.ttf'; |
||
285 | |||
286 | $fontSize = $size * 0.4; |
||
287 | $box = imagettfbbox($fontSize, 0, $font, $text); |
||
288 | |||
289 | $x = ($size - ($box[2] - $box[0])) / 2; |
||
290 | $y = ($size - ($box[1] - $box[7])) / 2; |
||
291 | $x += 1; |
||
292 | $y -= $box[7]; |
||
293 | imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text); |
||
294 | |||
295 | ob_start(); |
||
296 | imagepng($im); |
||
297 | $data = ob_get_contents(); |
||
298 | ob_end_clean(); |
||
299 | |||
300 | return $data; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Calculate steps between two Colors |
||
305 | * @param object Color $steps start color |
||
306 | * @param object Color $ends end color |
||
307 | * @return array [r,g,b] steps for each color to go from $steps to $ends |
||
308 | */ |
||
309 | private function stepCalc($steps, $ends) { |
||
310 | $step = array(); |
||
311 | $step[0] = ($ends[1]->r - $ends[0]->r) / $steps; |
||
312 | $step[1] = ($ends[1]->g - $ends[0]->g) / $steps; |
||
313 | $step[2] = ($ends[1]->b - $ends[0]->b) / $steps; |
||
314 | return $step; |
||
315 | } |
||
316 | /** |
||
317 | * Convert a string to an integer evenly |
||
318 | * @param string $hash the text to parse |
||
319 | * @param int $maximum the maximum range |
||
320 | * @return int between 0 and $maximum |
||
321 | */ |
||
322 | private function mixPalette($steps, $color1, $color2) { |
||
323 | $count = $steps + 1; |
||
324 | $palette = array($color1); |
||
325 | $step = $this->stepCalc($steps, [$color1, $color2]); |
||
326 | for ($i = 1; $i < $steps; $i++) { |
||
327 | $r = intval($color1->r + ($step[0] * $i)); |
||
328 | $g = intval($color1->g + ($step[1] * $i)); |
||
329 | $b = intval($color1->b + ($step[2] * $i)); |
||
330 | $palette[] = new Color($r, $g, $b); |
||
331 | } |
||
332 | return $palette; |
||
333 | } |
||
334 | |||
335 | |||
336 | /** |
||
337 | * Convert a string to an integer evenly |
||
338 | * @param string $hash the text to parse |
||
339 | * @param int $maximum the maximum range |
||
340 | * @return int between 0 and $maximum |
||
341 | */ |
||
342 | private function hashToInt($hash, $maximum) { |
||
343 | $final = 0; |
||
344 | $result = array(); |
||
345 | |||
346 | // Splitting evenly the string |
||
347 | for ($i=0; $i< strlen($hash); $i++) { |
||
348 | // chars in md5 goes up to f, hex:16 |
||
349 | $result[] = intval(substr($hash, $i, 1), 16) % 16; |
||
350 | } |
||
351 | // Adds up all results |
||
352 | foreach ($result as $value) { |
||
353 | $final += $value; |
||
354 | } |
||
355 | // chars in md5 goes up to f, hex:16 |
||
356 | return intval($final % $maximum); |
||
357 | } |
||
358 | |||
359 | |||
360 | /** |
||
361 | * @param string $text |
||
362 | * @return Color Object containting r g b int in the range [0, 255] |
||
363 | */ |
||
364 | function avatarBackgroundColor($text) { |
||
365 | $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
||
366 | |||
367 | $hash = md5($hash); |
||
368 | $hashChars = str_split($hash); |
||
369 | |||
370 | $red = new Color(182, 70, 157); |
||
371 | $yellow = new Color(221, 203, 85); |
||
372 | $blue = new Color(0, 130, 201); // Nextcloud blue |
||
373 | // Number of steps to go from a color to another |
||
374 | // 3 colors * 6 will result in 18 generated colors |
||
375 | $steps = 6; |
||
376 | |||
377 | $palette1 = $this->mixPalette($steps, $red, $yellow); |
||
378 | $palette2 = $this->mixPalette($steps, $yellow, $blue); |
||
379 | $palette3 = $this->mixPalette($steps, $blue, $red); |
||
380 | |||
381 | $finalPalette = array_merge($palette1, $palette2, $palette3); |
||
382 | |||
383 | return $finalPalette[$this->hashToInt($hash, $steps * 3 )]; |
||
384 | } |
||
385 | |||
386 | public function userChanged($feature, $oldValue, $newValue) { |
||
399 | |||
400 | } |
||
401 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.