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); |
||
13 | class Imagick2 extends AbstractImage |
||
14 | { |
||
15 | /** |
||
16 | * Imagick constructor. |
||
17 | * @param string $image |
||
18 | * @throws Exception |
||
19 | */ |
||
20 | 9 | public function __construct(string $image) |
|
24 | |||
25 | /** |
||
26 | * @param int $width |
||
27 | * @param int $height |
||
28 | * @return ImageInterface |
||
29 | * @throws ImagickException |
||
30 | */ |
||
31 | 1 | public function resize(int $width, int $height): ImageInterface |
|
38 | |||
39 | 9 | protected function setSizes(): void |
|
46 | |||
47 | /** |
||
48 | * @param int $angle |
||
49 | * @return ImageInterface |
||
50 | */ |
||
51 | 1 | public function rotate(int $angle = 90): ImageInterface |
|
58 | |||
59 | /** |
||
60 | * @return ImageInterface |
||
61 | */ |
||
62 | public function flip(): ImageInterface |
||
68 | |||
69 | /** |
||
70 | * @return ImageInterface |
||
71 | */ |
||
72 | public function flop(): ImageInterface |
||
78 | |||
79 | public function grayscale(): ImageInterface |
||
88 | |||
89 | /** |
||
90 | * @param string $text |
||
91 | * @param string $position |
||
92 | * @param string $font |
||
93 | * @return ImageInterface |
||
94 | * @throws InvalidArgumentException |
||
95 | * @throws ImagickException |
||
96 | */ |
||
97 | 1 | public function copyright(string $text, string $font = 'Courier', string $position = 'SouthWest'): ImageInterface |
|
118 | |||
119 | 1 | public function getFontsList(): array |
|
123 | |||
124 | /** |
||
125 | * @param string $text |
||
126 | * @param int $position |
||
127 | * @param string $font |
||
128 | * @return Imagick |
||
129 | * @throws ImagickException |
||
130 | */ |
||
131 | private function prepareImage(string $text, int $position, string $font): Imagick |
||
153 | |||
154 | /** |
||
155 | * @param int $width |
||
156 | * @param int $height |
||
157 | * @param int $startX |
||
158 | * @param int $startY |
||
159 | * @return ImageInterface |
||
160 | */ |
||
161 | public function crop(int $width, int $height, int $startX, int $startY): ImageInterface |
||
168 | |||
169 | 1 | public function save(string $filename, $quality = 100): bool |
|
176 | |||
177 | 1 | public function __toString(): string |
|
181 | |||
182 | /** |
||
183 | * @param int $width |
||
184 | * @param int $height |
||
185 | * @return ImageInterface |
||
186 | * @throws ImagickException |
||
187 | */ |
||
188 | 1 | protected function prepareThumbnail(int $width, int $height): ImageInterface |
|
194 | |||
195 | /** |
||
196 | * @param string $source |
||
197 | * @return ImageInterface |
||
198 | * @throws ImagickException |
||
199 | */ |
||
200 | 9 | protected function tmp(string $source): ImageInterface |
|
216 | |||
217 | /** |
||
218 | * @param int $width |
||
219 | * @param int $height |
||
220 | * @return Imagick |
||
221 | * @throws ImagickException |
||
222 | */ |
||
223 | 9 | protected function newImage(int $width, int $height): Imagick |
|
231 | |||
232 | /** |
||
233 | * @param Image $watermark |
||
234 | * @param int $x |
||
235 | * @param int $y |
||
236 | * @return ImageInterface |
||
237 | * @throws Exception |
||
238 | */ |
||
239 | protected function prepareWatermark($watermark, int $x, int $y): ImageInterface |
||
247 | |||
248 | public function resizeByTransparentBackground(int $width, int $height): ImageInterface |
||
255 | |||
256 | View Code Duplication | public function resizeByBlurBackground(int $width, int $height): ImageInterface |
|
263 | |||
264 | /** |
||
265 | * @param int $level |
||
266 | * @return ImageInterface |
||
267 | */ |
||
268 | View Code Duplication | public function brightness(int $level): ImageInterface |
|
278 | |||
279 | /** |
||
280 | * @param int $level |
||
281 | * @return ImageInterface |
||
282 | */ |
||
283 | View Code Duplication | public function contrast(int $level): ImageInterface |
|
293 | |||
294 | /** |
||
295 | * @return ImageInterface |
||
296 | */ |
||
297 | public function negate(): ImageInterface |
||
303 | |||
304 | /** |
||
305 | * @return ImageInterface |
||
306 | */ |
||
307 | public function blur(): ImageInterface |
||
313 | } |
||
314 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: