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 Image 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 Image, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Image extends AbstractField |
||
16 | { |
||
17 | use Storage; |
||
18 | use Placeholder; |
||
19 | use Nullable; |
||
20 | use Filename; |
||
21 | |||
22 | protected $encode = false; |
||
23 | protected $crop = false; |
||
24 | protected $shouldAutoOpen = null; |
||
25 | protected $ratio = [ |
||
26 | 'width' => false, |
||
27 | 'height' => false, |
||
28 | ]; |
||
29 | private $originalImageData; |
||
30 | private $defaultImageDataStructure = [ |
||
31 | 'storage' => [ |
||
32 | 'disk' => null, |
||
33 | 'is_encoded' => false, |
||
34 | ], |
||
35 | 'crop' => [ |
||
36 | 'width' => null, |
||
37 | 'height' => null, |
||
38 | 'x' => null, |
||
39 | 'y' => null, |
||
40 | 'rotate' => null, |
||
41 | 'rotate_background' => null, |
||
42 | ], |
||
43 | 'sources' => [ |
||
44 | 'original' => null, |
||
45 | 'cropped' => null, |
||
46 | ], |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | private $quality = 100; |
||
53 | |||
54 | public function __construct() |
||
58 | |||
59 | public function isEncode() |
||
63 | |||
64 | /** |
||
65 | * Encode image as data-url. |
||
66 | * |
||
67 | * @param bool $encode |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function encode(bool $encode = true) |
||
76 | |||
77 | protected function storeFile($filepath, $filename, $width = null, $height = null, $x = null, $y = null, $rotate = null, $rotateBackgroundColor = null) |
||
108 | |||
109 | public function value(Request $request) |
||
188 | |||
189 | public function crop(bool $enabled = true) |
||
195 | |||
196 | public function ratio(int $width, int $height) |
||
203 | |||
204 | public function isCrop() |
||
208 | |||
209 | public function getRatio(string $type) |
||
221 | |||
222 | public function getListView($model) |
||
229 | |||
230 | View Code Duplication | public function getEditFormView($model) |
|
239 | |||
240 | public function getCreateFormView() |
||
247 | |||
248 | public function getImage($data = []): \Yaro\Jarboe\Pack\Image |
||
252 | |||
253 | private function isTransparentColor(string $rgbaColor) |
||
266 | |||
267 | /** |
||
268 | * @param Request $request |
||
269 | * @param UploadedFile|null $file |
||
270 | * @param array $imageData |
||
271 | * @param bool $hasTransparentColor |
||
272 | * @param bool $isOriginalImage |
||
273 | * @return string |
||
274 | */ |
||
275 | private function generateFilename(Request $request, UploadedFile $file = null, array $imageData, bool $hasTransparentColor, bool $isOriginalImage): string |
||
293 | |||
294 | public function getImagesPack($model): array |
||
318 | |||
319 | public function autoOpen(bool $shouldAutoOpen = true) |
||
325 | |||
326 | public function shouldAutoOpenModal(): bool |
||
334 | |||
335 | public function quality(int $quality) |
||
348 | |||
349 | /** |
||
350 | * @return int |
||
351 | */ |
||
352 | public function getQuality(): int |
||
356 | } |
||
357 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.