Complex classes like ImageEditor 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 ImageEditor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class ImageEditor |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var string |
||
| 11 | */ |
||
| 12 | private $filepath; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var int |
||
| 16 | */ |
||
| 17 | private $type; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | private $width; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | private $height; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $mimetype; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var resource |
||
| 36 | */ |
||
| 37 | private $resource; |
||
| 38 | |||
| 39 | const DEFAULT_JPEG_QUALITY = 80; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Constuctor |
||
| 43 | * |
||
| 44 | * @param string $filepath Path to an existing image |
||
| 45 | * @throws InvalidArgumentException |
||
| 46 | * @throws Exception |
||
| 47 | */ |
||
| 48 | public function __construct($filepath) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Destructor |
||
| 70 | */ |
||
| 71 | public function __destruct() |
||
| 77 | |||
| 78 | /* resize ---------------- */ |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Resizes image to be within a maximum width and a maximum height |
||
| 82 | * |
||
| 83 | * @param int $maxwidth |
||
| 84 | * @param int $maxheight |
||
| 85 | * @return ImageEditor |
||
| 86 | */ |
||
| 87 | public function resizeToWithin($maxwidth, $maxheight) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Resizes image to an exact width and height, maintaining aspect ratio. Any overhang is cropped. |
||
| 100 | * If `null` is passed as width or height, image is resized to the new width or height with maintained aspect ratio, without cropping. |
||
| 101 | * |
||
| 102 | * @param int|null $width |
||
| 103 | * @param int|null $height |
||
| 104 | * @return ImageEditor |
||
| 105 | */ |
||
| 106 | public function resizeTo($width, $height = null) |
||
| 119 | |||
| 120 | private function resizeToDimensions(\stdClass $dimensions) |
||
| 124 | |||
| 125 | private function resizeImage($newwidth, $newheight) |
||
| 142 | |||
| 143 | /* crop -------- */ |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Crops the current image |
||
| 147 | * |
||
| 148 | * @param int $x |
||
| 149 | * @param int $y |
||
| 150 | * @param int $width |
||
| 151 | * @param int $height |
||
| 152 | * |
||
| 153 | * @return ImageEditor |
||
| 154 | */ |
||
| 155 | public function crop($x, $y, $width, $height) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Crops the image from the center |
||
| 180 | * |
||
| 181 | * @param int $width |
||
| 182 | * @param int $height |
||
| 183 | * |
||
| 184 | * @return ImageEditor |
||
| 185 | */ |
||
| 186 | public function cropFromCenter($width, $height) |
||
| 193 | |||
| 194 | /* rotation -------- */ |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return ImageEditor |
||
| 198 | */ |
||
| 199 | public function rotateClockwise() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return ImageEditor |
||
| 206 | */ |
||
| 207 | public function rotateCounterClockwise() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return ImageEditor |
||
| 214 | */ |
||
| 215 | public function rotate180() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param int $deg |
||
| 222 | * @return ImageEditor |
||
| 223 | */ |
||
| 224 | private function rotate($deg) |
||
| 230 | |||
| 231 | /* filter -------- */ |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return ImageEditor |
||
| 235 | */ |
||
| 236 | public function grayscale() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param int $red [0, +255] |
||
| 244 | * @param int $green [0, +255] |
||
| 245 | * @param int $blue [0, +255] |
||
| 246 | * @param int $alpha [0, 1] |
||
| 247 | * |
||
| 248 | * @return ImageEditor |
||
| 249 | */ |
||
| 250 | public function coloroverlay($red, $green, $blue, $alpha = 50) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param int $red [0, 255] |
||
| 261 | * @param int $green [0, 255] |
||
| 262 | * @param int $blue [0, 255] |
||
| 263 | * @param int $alpha [0, 127] |
||
| 264 | * |
||
| 265 | * @return ImageEditor |
||
| 266 | */ |
||
| 267 | public function colorize($red, $green, $blue, $alpha = 0) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Paste another image onto this one. |
||
| 275 | * @note Basically a wrapper method for http://www.php.net/manual/en/function.imagecopyresampled.php |
||
| 276 | * |
||
| 277 | * @param ImageEditor|string $imageeditor |
||
| 278 | * @param int $dst_x |
||
| 279 | * @param int $dst_y |
||
| 280 | * @param int $dst_w |
||
| 281 | * @param int $dst_h |
||
| 282 | * @param int $src_x |
||
| 283 | * @param int $src_y |
||
| 284 | * @param int $src_w |
||
| 285 | * @param int $src_h |
||
| 286 | */ |
||
| 287 | public function pasteImage( |
||
| 334 | |||
| 335 | /* -------- */ |
||
| 336 | |||
| 337 | /** |
||
| 338 | * "Duplicates" the image. |
||
| 339 | * Basically unsets the filepath, so we have to use `saveAs()` and not `save()`. |
||
| 340 | * |
||
| 341 | * @return ImageEditor |
||
| 342 | */ |
||
| 343 | public function duplicate() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Saves the image |
||
| 351 | * |
||
| 352 | * @param int $quality 0-100 (only for jpegs) |
||
| 353 | * |
||
| 354 | * @return ImageEditor |
||
| 355 | */ |
||
| 356 | public function save($quality = self::DEFAULT_JPEG_QUALITY) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Saves the image under a path |
||
| 366 | * |
||
| 367 | * @param string $filepath |
||
| 368 | * @param int $type IMAGETYPE constant |
||
| 369 | * @param int $quality 0-100 (only for jpegs) |
||
| 370 | * |
||
| 371 | * @return ImageEditor |
||
| 372 | */ |
||
| 373 | public function saveAs($filepath, $type = null, $quality = self::DEFAULT_JPEG_QUALITY) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Outputs the image with the correct header. |
||
| 383 | * |
||
| 384 | * @param int $quality 0-100 (only for jpegs) |
||
| 385 | */ |
||
| 386 | public function output($quality = self::DEFAULT_JPEG_QUALITY) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param string $filepath |
||
| 394 | * @param int $type IMAGETYPE constant |
||
| 395 | * @param int $quality 0-100 (only for jpegs) |
||
| 396 | */ |
||
| 397 | private function outputImage($filepath = null, $type = null, $quality = self::DEFAULT_JPEG_QUALITY) |
||
| 415 | |||
| 416 | /* -------- */ |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Retrieves format. |
||
| 420 | */ |
||
| 421 | private function extractFileData() |
||
| 439 | |||
| 440 | private function createResource() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param resource $resource |
||
| 457 | */ |
||
| 458 | private function setResource($resource) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param int $width |
||
| 475 | * @param int $height |
||
| 476 | * |
||
| 477 | * @return resource |
||
| 478 | */ |
||
| 479 | private function createImage($width, $height) |
||
| 488 | |||
| 489 | /* -------- GETTER / SETTERS -------- */ |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Gets format of this image [IMAGETYPE_GIF|IMAGETYPE_GIF|IMAGETYPE_GIF] |
||
| 493 | * |
||
| 494 | * @return int |
||
| 495 | */ |
||
| 496 | public function getType() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Gets mimetype |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | public function getMimeType() |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Returns the width. |
||
| 513 | * |
||
| 514 | * @return int |
||
| 515 | */ |
||
| 516 | public function getWidth() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Returns the height. |
||
| 523 | * |
||
| 524 | * @return int |
||
| 525 | */ |
||
| 526 | public function getHeight() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @return resource |
||
| 533 | */ |
||
| 534 | public function getResource() |
||
| 538 | } |
||
| 539 |