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:
1 | <?php declare(strict_types=1); |
||
12 | class GD extends AbstractImage |
||
13 | { |
||
14 | /** |
||
15 | * GD constructor. |
||
16 | * @param string $image |
||
17 | * @throws Exception |
||
18 | */ |
||
19 | 9 | public function __construct(string $image) |
|
23 | |||
24 | /** |
||
25 | * @param int $width |
||
26 | * @param int $height |
||
27 | * @return ImageInterface |
||
28 | * @throws Exception |
||
29 | */ |
||
30 | public function resizeByTransparentBackground(int $width, int $height): ImageInterface |
||
37 | |||
38 | /** |
||
39 | * @param int $width |
||
40 | * @param int $height |
||
41 | * @return ImageInterface |
||
42 | * @throws Exception |
||
43 | */ |
||
44 | View Code Duplication | public function resizeByBlurBackground(int $width, int $height): ImageInterface |
|
|
|||
45 | { |
||
46 | $background = new Image(base64_encode((string) $this), Image::GD); |
||
47 | $background->blur()->resize($width, $height); |
||
48 | |||
49 | return $this->setBackground($width, $height, $background); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param int $level |
||
54 | * @return ImageInterface |
||
55 | */ |
||
56 | View Code Duplication | public function brightness(int $level): ImageInterface |
|
66 | |||
67 | /** |
||
68 | * @param int $level |
||
69 | * @return ImageInterface |
||
70 | */ |
||
71 | View Code Duplication | public function contrast(int $level): ImageInterface |
|
81 | |||
82 | /** |
||
83 | * @return ImageInterface |
||
84 | */ |
||
85 | public function negate(): ImageInterface |
||
91 | |||
92 | /** |
||
93 | * @return ImageInterface |
||
94 | */ |
||
95 | public function blur(): ImageInterface |
||
106 | |||
107 | /** |
||
108 | * @return ImageInterface |
||
109 | */ |
||
110 | public function flip(): ImageInterface |
||
116 | |||
117 | /** |
||
118 | * @return ImageInterface |
||
119 | */ |
||
120 | public function flop(): ImageInterface |
||
126 | |||
127 | /** |
||
128 | * @return ImageInterface |
||
129 | */ |
||
130 | public function grayscale(): ImageInterface |
||
136 | |||
137 | /** |
||
138 | * @param string $text |
||
139 | * @param string $font |
||
140 | * @param string $position |
||
141 | * @return $this |
||
142 | * @throws InvalidArgumentException |
||
143 | * @throws Exception |
||
144 | */ |
||
145 | 1 | public function copyright(string $text, string $font, string $position = 'SouthWest'): ImageInterface |
|
165 | |||
166 | /** |
||
167 | * @param string $text |
||
168 | * @param string $font |
||
169 | * @return resource |
||
170 | * @throws InvalidArgumentException |
||
171 | * @throws Exception |
||
172 | */ |
||
173 | private function prepareImage(string $text, string $font) |
||
200 | |||
201 | /** |
||
202 | * @param int $width |
||
203 | * @param int $height |
||
204 | * @return resource |
||
205 | */ |
||
206 | 3 | protected function newImage(int $width, int $height) |
|
216 | |||
217 | /** |
||
218 | * @param int $width |
||
219 | * @param int $height |
||
220 | * @return ImageInterface |
||
221 | */ |
||
222 | 2 | public function resize(int $width, int $height): ImageInterface |
|
232 | |||
233 | 8 | protected function setSizes(): void |
|
239 | |||
240 | /** |
||
241 | * @param int $width |
||
242 | * @param int $height |
||
243 | * @param int $x |
||
244 | * @param int $y |
||
245 | * @return ImageInterface |
||
246 | */ |
||
247 | public function crop(int $width, int $height, int $x, int $y): ImageInterface |
||
258 | |||
259 | /** |
||
260 | * @param int $angle |
||
261 | * @return ImageInterface |
||
262 | */ |
||
263 | 1 | public function rotate(int $angle = 90): ImageInterface |
|
275 | |||
276 | /** |
||
277 | * @throws LogicException |
||
278 | * @throws RangeException |
||
279 | * @return string |
||
280 | */ |
||
281 | 1 | public function __toString(): string |
|
288 | |||
289 | /** |
||
290 | * @param string $filename |
||
291 | * @param int $quality |
||
292 | * @return bool |
||
293 | */ |
||
294 | 1 | public function save(string $filename, $quality = 100): bool |
|
298 | |||
299 | /** |
||
300 | * @param int $width |
||
301 | * @param int $height |
||
302 | * @param int $newWidth |
||
303 | * @param int $newHeight |
||
304 | * @return ImageInterface |
||
305 | */ |
||
306 | 1 | protected function prepareThumbnail( |
|
329 | |||
330 | /** |
||
331 | * @param string $source |
||
332 | * @return ImageInterface |
||
333 | * @throws InvalidArgumentException |
||
334 | * @throws Exception |
||
335 | */ |
||
336 | 8 | protected function tmp(string $source): ImageInterface |
|
349 | |||
350 | /** |
||
351 | * @param ImageInterface $watermark |
||
352 | * @param int $x |
||
353 | * @param int $y |
||
354 | * @return ImageInterface |
||
355 | */ |
||
356 | protected function prepareWatermark($watermark, int $x, int $y): ImageInterface |
||
375 | } |
||
376 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.