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 |
||
| 10 | final class Image |
||
| 11 | { |
||
| 12 | const CROP_ENTROPY = 'Entropy'; |
||
| 13 | const CROP_BALANCED = 'Balanced'; |
||
| 14 | const CROP_FACE = 'Face'; |
||
| 15 | |||
| 16 | private $adapter; |
||
| 17 | private $filename; |
||
| 18 | private $clientHints = [ |
||
| 19 | 'dpr' => null, |
||
| 20 | 'viewport-width' => null, |
||
| 21 | 'width' => null, |
||
| 22 | ]; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create a new Imagecow instance from an image file. |
||
| 26 | */ |
||
| 27 | public static function fromFile(string $filename, string $adapter = null): Image |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Create a new Imagecow instance from a string. |
||
| 49 | */ |
||
| 50 | public static function fromString(string $string, string $adapter = null): Image |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Constructor. |
||
| 75 | */ |
||
| 76 | public function __construct(AdapterInterface $adapter, string $filename = null) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Set the available client hints. |
||
| 84 | */ |
||
| 85 | public function clientHints(array $clientHints): self |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set a default background color used to fill in some transformation functions |
||
| 104 | * in rgb format, for example: array(0,127,34) |
||
| 105 | */ |
||
| 106 | public function background(array $background): self |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Inverts the image vertically. |
||
| 115 | */ |
||
| 116 | public function flip(): self |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Inverts the image horizontally. |
||
| 125 | */ |
||
| 126 | public function flop(): self |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Saves the image in a file (or override the previous opened file). |
||
| 135 | */ |
||
| 136 | public function save(string $filename = null): self |
||
| 142 | |||
| 143 | public function getAdapter(): AdapterInterface |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Gets the image data in a string. |
||
| 150 | */ |
||
| 151 | public function getString(): string |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Gets the mime type of the image. |
||
| 158 | */ |
||
| 159 | public function getMimeType(): string |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Gets the width of the image in pixels. |
||
| 166 | */ |
||
| 167 | public function getWidth(int $percentage = null): int |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Gets the height of the image in pixels. |
||
| 176 | */ |
||
| 177 | public function getHeight(int $percentage = null): int |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Converts the image to other format (png, jpg, gif or webp). |
||
| 186 | */ |
||
| 187 | public function format($format): self |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Resizes the image maintaining the proportion (A 800x600 image resized to 400x400 becomes to 400x300). |
||
| 196 | * |
||
| 197 | * @param int|string $width The max width of the image. It can be a number (pixels) or percentaje |
||
| 198 | * @param int|string $height The max height of the image. It can be a number (pixels) or percentaje |
||
| 199 | */ |
||
| 200 | public function resize($width, $height = 0, bool $cover = false): self |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Crops the image. |
||
| 227 | * |
||
| 228 | * @param int|string $width The new width of the image. It can be a number (pixels) or percentaje |
||
| 229 | * @param int|string $height The new height of the image. It can be a number (pixels) or percentaje |
||
| 230 | * @param int|string $x The "x" position to crop. It can be number (pixels), percentaje or one of the Image::CROP_* constants |
||
| 231 | * @param int|string $y The "y" position to crop. It can be number (pixels) or percentaje |
||
| 232 | */ |
||
| 233 | public function crop($width, $height, $x = '50%', $y = '50%'): self |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Adjust the image to the given dimmensions. Resizes and crops the image maintaining the proportions. |
||
| 262 | * |
||
| 263 | * @param int|string $width The new width in number (pixels) or percentaje |
||
| 264 | * @param int|string $height The new height in number (pixels) or percentaje |
||
| 265 | * @param int|string $x The "x" position to crop. It can be number (pixels), percentaje or one of the Image::CROP_* constants |
||
| 266 | * @param int|string $y The "y" position to crop. It can be number (pixels) or percentaje |
||
| 267 | */ |
||
| 268 | public function resizeCrop($width, $height, $x = '50%', $y = '50%'): self |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Rotates the image (in degrees, anticlockwise). |
||
| 278 | */ |
||
| 279 | public function rotate(int $angle): self |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Apply blur to image |
||
| 290 | */ |
||
| 291 | public function blur(int $loops = 4): self |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Define the image compression quality for jpg images (from 0 to 100). |
||
| 300 | */ |
||
| 301 | public function quality(int $quality): self |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Add a watermark to current image. |
||
| 318 | * |
||
| 319 | * @param string|int $x Horizontal position |
||
| 320 | * @param string|int $y Vertical position |
||
| 321 | */ |
||
| 322 | public function watermark(Image $image, $x = '100%', $y = '100%'): self |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Add opacity to image from 0 (transparent) to 100 (opaque). |
||
| 340 | */ |
||
| 341 | public function opacity(int $opacity): self |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Set the image progressive or not |
||
| 350 | */ |
||
| 351 | public function progressive(bool $progressive = true): self |
||
| 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 | public function transform(string $operations = null): self |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Send the HTTP header with the content-type, output the image data and die. |
||
| 424 | */ |
||
| 425 | public function show() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Returns the image as base64 url. |
||
| 435 | * |
||
| 436 | * @return string|null |
||
| 437 | */ |
||
| 438 | public function base64() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Auto-rotate the image according with its exif data |
||
| 449 | * Taken from: http://php.net/manual/en/function.exif-read-data.php#76964. |
||
| 450 | */ |
||
| 451 | public function autoRotate(): self |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Get the fixed size according with the client hints. |
||
| 488 | */ |
||
| 489 | private function calculateClientSize(int $width, int $height): array |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Check whether the image is an animated gif. |
||
| 509 | * Copied from: https://github.com/Sybio/GifFrameExtractor/blob/master/src/GifFrameExtractor/GifFrameExtractor.php#L181. |
||
| 510 | * |
||
| 511 | * @param resource A stream pointer opened by fopen() |
||
| 512 | */ |
||
| 513 | private static function isAnimatedGif($stream): bool |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Converts a string with operations in an array. |
||
| 527 | */ |
||
| 528 | private static function parseOperations(string $operations): array |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Checks the library to use and returns its class. |
||
| 553 | * |
||
| 554 | * @throws ImageException if the image library does not exists. |
||
| 555 | */ |
||
| 556 | private static function getAdapterClass(string $adapter = null): string |
||
| 572 | |||
| 573 | private static function getPercentage($value): ?float |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Calculates the x/y position. |
||
| 588 | * |
||
| 589 | * @param string|int $position |
||
| 590 | * @param int $cropWidth |
||
| 591 | * |
||
| 592 | * @return int |
||
| 593 | */ |
||
| 594 | private function getPositionX($position, int $cropWidth): int |
||
| 614 | } |
||
| 615 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.