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) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Inverts the image vertically. |
||
| 148 | * |
||
| 149 | * @return self |
||
| 150 | */ |
||
| 151 | public function flip() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Inverts the image horizontally. |
||
| 160 | * |
||
| 161 | * @return self |
||
| 162 | */ |
||
| 163 | public function flop() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Saves the image in a file. |
||
| 172 | * |
||
| 173 | * @param string $filename Name of the file where the image will be saved. If it's not defined, The original file will be overwritten. |
||
| 174 | * |
||
| 175 | * @return self |
||
| 176 | */ |
||
| 177 | public function save($filename = null) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Gets the image data in a string. |
||
| 186 | * |
||
| 187 | * @return string The image data |
||
| 188 | */ |
||
| 189 | public function getString() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Gets the mime type of the image. |
||
| 196 | * |
||
| 197 | * @return string The mime type |
||
| 198 | */ |
||
| 199 | public function getMimeType() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Gets the width of the image. |
||
| 206 | * |
||
| 207 | * @return int The width in pixels |
||
| 208 | */ |
||
| 209 | public function getWidth() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Gets the height of the image. |
||
| 216 | * |
||
| 217 | * @return int The height in pixels |
||
| 218 | */ |
||
| 219 | public function getHeight() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Converts the image to other format. |
||
| 226 | * |
||
| 227 | * @param string $format The new format: png, jpg, gif |
||
| 228 | * |
||
| 229 | * @return self |
||
| 230 | */ |
||
| 231 | public function format($format) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Resizes the image maintaining the proportion (A 800x600 image resized to 400x400 becomes to 400x300). |
||
| 240 | * |
||
| 241 | * @param int|string $width The max width of the image. It can be a number (pixels) or percentaje |
||
| 242 | * @param int|string $height The max height of the image. It can be a number (pixels) or percentaje |
||
| 243 | * @param bool $cover |
||
| 244 | * |
||
| 245 | * @return self |
||
| 246 | */ |
||
| 247 | public function resize($width, $height = 0, $cover = false) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Crops the image. |
||
| 269 | * |
||
| 270 | * @param int|string $width The new width of the image. It can be a number (pixels) or percentaje |
||
| 271 | * @param int|string $height The new height of the image. It can be a number (pixels) or percentaje |
||
| 272 | * @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 |
||
| 273 | * @param int|string $y The "y" position to crop. It can be number (pixels), percentaje or [top, middle, bottom] |
||
| 274 | * |
||
| 275 | * @return self |
||
| 276 | */ |
||
| 277 | public function crop($width, $height, $x = 'center', $y = 'middle') |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Adjust the image to the given dimmensions. Resizes and crops the image maintaining the proportions. |
||
| 304 | * |
||
| 305 | * @param int|string $width The new width in number (pixels) or percentaje |
||
| 306 | * @param int|string $height The new height in number (pixels) or percentaje |
||
| 307 | * @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 |
||
| 308 | * @param int|string $y The "y" position to crop. It can be number (pixels), percentaje or [top, middle, bottom] |
||
| 309 | * |
||
| 310 | * @return self |
||
| 311 | */ |
||
| 312 | public function resizeCrop($width, $height, $x = 'center', $y = 'middle') |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Rotates the image. |
||
| 322 | * |
||
| 323 | * @param int $angle Rotation angle in degrees (anticlockwise) |
||
| 324 | * |
||
| 325 | * @return self |
||
| 326 | */ |
||
| 327 | public function rotate($angle) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Define the image compression quality for jpg images. |
||
| 338 | * |
||
| 339 | * @param int $quality The quality (from 0 to 100) |
||
| 340 | * |
||
| 341 | * @return self |
||
| 342 | */ |
||
| 343 | public function quality($quality) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Reads the EXIF data from a JPEG and returns an associative array |
||
| 360 | * (requires the exif PHP extension enabled). |
||
| 361 | * |
||
| 362 | * @param null|string $key |
||
| 363 | * |
||
| 364 | * @return null|array |
||
| 365 | */ |
||
| 366 | public function getExifData($key = null) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Transform the image executing various operations of crop, resize, resizeCrop and format. |
||
| 381 | * |
||
| 382 | * @param string $operations The string with all operations separated by "|". |
||
| 383 | * |
||
| 384 | * @return self |
||
| 385 | */ |
||
| 386 | public function transform($operations = null) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Send the HTTP header with the content-type, output the image data and die. |
||
| 421 | */ |
||
| 422 | View Code Duplication | public function show() |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Returns the image as base64 url. |
||
| 432 | * |
||
| 433 | * @return string|null |
||
| 434 | */ |
||
| 435 | View Code Duplication | public function base64() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Auto-rotate the image according with its exif data |
||
| 446 | * Taken from: http://php.net/manual/en/function.exif-read-data.php#76964. |
||
| 447 | * |
||
| 448 | * @return self |
||
| 449 | */ |
||
| 450 | public function autoRotate() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Check whether the image is an animated gif. |
||
| 487 | * |
||
| 488 | * Copied from: https://github.com/Sybio/GifFrameExtractor/blob/master/src/GifFrameExtractor/GifFrameExtractor.php#L181 |
||
| 489 | * |
||
| 490 | * @return bool |
||
| 491 | */ |
||
| 492 | protected function isAnimatedGif() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Converts a string with operations in an array. |
||
| 512 | * |
||
| 513 | * @param string $operations The operations string |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | private static function parseOperations($operations) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Checks the library to use and returns its class. |
||
| 542 | * |
||
| 543 | * @param string $library The library name (Gd, Imagick) |
||
| 544 | * |
||
| 545 | * @throws ImageException if the image library does not exists. |
||
| 546 | * |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | private static function getLibraryClass($library) |
||
| 567 | } |
||
| 568 |
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.