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 |
||
7 | class Image |
||
8 | { |
||
9 | const LIB_GD = 'Gd'; |
||
10 | const LIB_IMAGICK = 'Imagick'; |
||
11 | |||
12 | const CROP_ENTROPY = 'Entropy'; |
||
13 | const CROP_BALANCED = 'Balanced'; |
||
14 | |||
15 | protected $image; |
||
16 | protected $filename; |
||
17 | protected $clientHints = [ |
||
18 | 'dpr' => null, |
||
19 | 'viewport-width' => null, |
||
20 | 'width' => null, |
||
21 | ]; |
||
22 | |||
23 | /** |
||
24 | * Static function to create a new Imagecow instance from an image file. |
||
25 | * |
||
26 | * @param string $image The path of the file |
||
27 | * @param string $library The name of the image library to use (Gd or Imagick). If it's not defined, detects automatically the library to use. |
||
28 | * |
||
29 | * @return Image |
||
30 | */ |
||
31 | public static function fromFile($image, $library = null) |
||
37 | |||
38 | /** |
||
39 | * Static function to create a new Imagecow instance from a binary string. |
||
40 | * |
||
41 | * @param string $string The string of the image |
||
42 | * @param string $library The name of the image library to use (Gd or Imagick). If it's not defined, detects automatically the library to use. |
||
43 | * |
||
44 | * @return Image |
||
45 | */ |
||
46 | public static function fromString($string, $library = null) |
||
52 | |||
53 | /** |
||
54 | * Constructor. |
||
55 | * |
||
56 | * @param Libs\LibInterface $image |
||
57 | * @param string $filename Original filename (used to overwrite) |
||
58 | */ |
||
59 | public function __construct(Libs\LibInterface $image, $filename = null) |
||
68 | |||
69 | /** |
||
70 | * Set the available client hints |
||
71 | * |
||
72 | * @param array $hints |
||
|
|||
73 | * |
||
74 | * @return self |
||
75 | */ |
||
76 | public function setClientHints(array $clientHints) { |
||
89 | |||
90 | /** |
||
91 | * Set a default background color used to fill in some transformation functions. |
||
92 | * |
||
93 | * @param array $background The color in rgb, for example: array(0,127,34) |
||
94 | * |
||
95 | * @return self |
||
96 | */ |
||
97 | public function setBackground(array $background) |
||
103 | |||
104 | /** |
||
105 | * Define the image compression quality for jpg images. |
||
106 | * |
||
107 | * @param int $quality The quality (from 0 to 100) |
||
108 | * |
||
109 | * @deprecated Use quality instead |
||
110 | * |
||
111 | * @return self |
||
112 | */ |
||
113 | public function setCompressionQuality($quality) |
||
119 | |||
120 | /** |
||
121 | * Get the fixed size according with the client hints |
||
122 | * |
||
123 | * @param int $width |
||
124 | * @param int $height |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | private function calculateClientSize($width, $height) |
||
141 | |||
142 | /** |
||
143 | * Inverts the image vertically. |
||
144 | * |
||
145 | * @return self |
||
146 | */ |
||
147 | public function flip() |
||
153 | |||
154 | /** |
||
155 | * Inverts the image horizontally. |
||
156 | * |
||
157 | * @return self |
||
158 | */ |
||
159 | public function flop() |
||
165 | |||
166 | /** |
||
167 | * Saves the image in a file. |
||
168 | * |
||
169 | * @param string $filename Name of the file where the image will be saved. If it's not defined, The original file will be overwritten. |
||
170 | * |
||
171 | * @return self |
||
172 | */ |
||
173 | public function save($filename = null) |
||
179 | |||
180 | /** |
||
181 | * Gets the image data in a string. |
||
182 | * |
||
183 | * @return string The image data |
||
184 | */ |
||
185 | public function getString() |
||
189 | |||
190 | /** |
||
191 | * Gets the mime type of the image. |
||
192 | * |
||
193 | * @return string The mime type |
||
194 | */ |
||
195 | public function getMimeType() |
||
199 | |||
200 | /** |
||
201 | * Gets the width of the image. |
||
202 | * |
||
203 | * @return int The width in pixels |
||
204 | */ |
||
205 | public function getWidth() |
||
209 | |||
210 | /** |
||
211 | * Gets the height of the image. |
||
212 | * |
||
213 | * @return int The height in pixels |
||
214 | */ |
||
215 | public function getHeight() |
||
219 | |||
220 | /** |
||
221 | * Converts the image to other format. |
||
222 | * |
||
223 | * @param string $format The new format: png, jpg, gif |
||
224 | * |
||
225 | * @return self |
||
226 | */ |
||
227 | public function format($format) |
||
233 | |||
234 | /** |
||
235 | * Resizes the image maintaining the proportion (A 800x600 image resized to 400x400 becomes to 400x300). |
||
236 | * |
||
237 | * @param int|string $width The max width of the image. It can be a number (pixels) or percentaje |
||
238 | * @param int|string $height The max height of the image. It can be a number (pixels) or percentaje |
||
239 | * @param bool $cover |
||
240 | * |
||
241 | * @return self |
||
242 | */ |
||
243 | public function resize($width, $height = 0, $cover = false) |
||
262 | |||
263 | /** |
||
264 | * Crops the image. |
||
265 | * |
||
266 | * @param int|string $width The new width of the image. It can be a number (pixels) or percentaje |
||
267 | * @param int|string $height The new height of the image. It can be a number (pixels) or percentaje |
||
268 | * @param int|string $x The "x" position to crop. It can be number (pixels), percentaje, [left, center, right] or one of the Image::CROP_* constants |
||
269 | * @param int|string $y The "y" position to crop. It can be number (pixels), percentaje or [top, middle, bottom] |
||
270 | * |
||
271 | * @return self |
||
272 | */ |
||
273 | public function crop($width, $height, $x = 'center', $y = 'middle') |
||
297 | |||
298 | /** |
||
299 | * Adjust the image to the given dimmensions. Resizes and crops the image maintaining the proportions. |
||
300 | * |
||
301 | * @param int|string $width The new width in number (pixels) or percentaje |
||
302 | * @param int|string $height The new height in number (pixels) or percentaje |
||
303 | * @param int|string $x The "x" position to crop. It can be number (pixels), percentaje, [left, center, right] or one of the Image::CROP_* constants |
||
304 | * @param int|string $y The "y" position to crop. It can be number (pixels), percentaje or [top, middle, bottom] |
||
305 | * |
||
306 | * @return self |
||
307 | */ |
||
308 | public function resizeCrop($width, $height, $x = 'center', $y = 'middle') |
||
315 | |||
316 | /** |
||
317 | * Rotates the image. |
||
318 | * |
||
319 | * @param int $angle Rotation angle in degrees (anticlockwise) |
||
320 | * |
||
321 | * @return self |
||
322 | */ |
||
323 | public function rotate($angle) |
||
331 | |||
332 | /** |
||
333 | * Define the image compression quality for jpg images. |
||
334 | * |
||
335 | * @param int $quality The quality (from 0 to 100) |
||
336 | * |
||
337 | * @return self |
||
338 | */ |
||
339 | public function quality($quality) |
||
353 | |||
354 | /** |
||
355 | * Reads the EXIF data from a JPEG and returns an associative array |
||
356 | * (requires the exif PHP extension enabled). |
||
357 | * |
||
358 | * @param null|string $key |
||
359 | * |
||
360 | * @return null|array |
||
361 | */ |
||
362 | public function getExifData($key = null) |
||
374 | |||
375 | /** |
||
376 | * Transform the image executing various operations of crop, resize, resizeCrop and format. |
||
377 | * |
||
378 | * @param string $operations The string with all operations separated by "|". |
||
379 | * |
||
380 | * @return self |
||
381 | */ |
||
382 | public function transform($operations = null) |
||
414 | |||
415 | /** |
||
416 | * Send the HTTP header with the content-type, output the image data and die. |
||
417 | */ |
||
418 | View Code Duplication | public function show() |
|
425 | |||
426 | /** |
||
427 | * Returns the image as base64 url. |
||
428 | * |
||
429 | * @return string|null |
||
430 | */ |
||
431 | View Code Duplication | public function base64() |
|
439 | |||
440 | /** |
||
441 | * Auto-rotate the image according with its exif data |
||
442 | * Taken from: http://php.net/manual/en/function.exif-read-data.php#76964. |
||
443 | * |
||
444 | * @return self |
||
445 | */ |
||
446 | public function autoRotate() |
||
480 | |||
481 | /** |
||
482 | * Check whether the image is an animated gif. |
||
483 | * |
||
484 | * Copied from: https://github.com/Sybio/GifFrameExtractor/blob/master/src/GifFrameExtractor/GifFrameExtractor.php#L181 |
||
485 | * |
||
486 | * @return bool |
||
487 | */ |
||
488 | protected function isAnimatedGif() |
||
505 | |||
506 | /** |
||
507 | * Converts a string with operations in an array. |
||
508 | * |
||
509 | * @param string $operations The operations string |
||
510 | * |
||
511 | * @return array |
||
512 | */ |
||
513 | private static function parseOperations($operations) |
||
535 | |||
536 | /** |
||
537 | * Checks the library to use and returns its class. |
||
538 | * |
||
539 | * @param string $library The library name (Gd, Imagick) |
||
540 | * |
||
541 | * @throws ImageException if the image library does not exists. |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | private static function getLibraryClass($library) |
||
563 | } |
||
564 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.