Complex classes like Manipulations 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 Manipulations, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Manipulations |
||
| 10 | { |
||
| 11 | const CROP_TOP_LEFT = 'crop-top-left'; |
||
| 12 | const CROP_TOP = 'crop-top'; |
||
| 13 | const CROP_TOP_RIGHT = 'crop-top-right'; |
||
| 14 | const CROP_LEFT = 'crop-left'; |
||
| 15 | const CROP_CENTER = 'crop-center'; |
||
| 16 | const CROP_RIGHT = 'crop-right'; |
||
| 17 | const CROP_BOTTOM_LEFT = 'crop-bottom-left'; |
||
| 18 | const CROP_BOTTOM = 'crop-bottom'; |
||
| 19 | const CROP_BOTTOM_RIGHT = 'crop-bottom-right'; |
||
| 20 | |||
| 21 | const ORIENTATION_AUTO = 'auto'; |
||
| 22 | const ORIENTATION_90 = 90; |
||
| 23 | const ORIENTATION_180 = 180; |
||
| 24 | const ORIENTATION_270 = 270; |
||
| 25 | |||
| 26 | const FIT_CONTAIN = 'contain'; |
||
| 27 | const FIT_MAX = 'max'; |
||
| 28 | const FIT_FILL = 'fill'; |
||
| 29 | const FIT_STRETCH = 'stretch'; |
||
| 30 | const FIT_CROP = 'crop'; |
||
| 31 | |||
| 32 | const BORDER_OVERLAY = 'overlay'; |
||
| 33 | const BORDER_SHRINK = 'shrink'; |
||
| 34 | const BORDER_EXPAND = 'expand'; |
||
| 35 | |||
| 36 | const FORMAT_JPG = 'jpg'; |
||
| 37 | const FORMAT_PJPG = 'pjpg'; |
||
| 38 | const FORMAT_PNG = 'png'; |
||
| 39 | const FORMAT_GIF = 'gif'; |
||
| 40 | |||
| 41 | const FILTER_GREYSCALE = 'greyscale'; |
||
| 42 | const FILTER_SEPIA = 'sepia'; |
||
| 43 | |||
| 44 | const UNIT_PIXELS = 'px'; |
||
| 45 | const UNIT_PERCENT = '%'; |
||
| 46 | |||
| 47 | const POSITION_TOP_LEFT = 'top-left'; |
||
| 48 | const POSITION_TOP = 'top'; |
||
| 49 | const POSITION_TOP_RIGHT = 'top-right'; |
||
| 50 | const POSITION_LEFT = 'left'; |
||
| 51 | const POSITION_CENTER = 'center'; |
||
| 52 | const POSITION_RIGHT = 'right'; |
||
| 53 | const POSITION_BOTTOM_LEFT = 'bottom-left'; |
||
| 54 | const POSITION_BOTTOM = 'bottom'; |
||
| 55 | const POSITION_BOTTOM_RIGHT = 'bottom-right'; |
||
| 56 | |||
| 57 | /** @var \Spatie\Image\ManipulationSequence */ |
||
| 58 | protected $manipulationSequence; |
||
| 59 | |||
| 60 | public function __construct(array $manipulations = []) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $orientation |
||
| 67 | * |
||
| 68 | * @return $this |
||
| 69 | * |
||
| 70 | * @throws InvalidManipulation |
||
| 71 | */ |
||
| 72 | public function orientation(string $orientation) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $cropMethod |
||
| 87 | * @param int $width |
||
| 88 | * @param int $height |
||
| 89 | * |
||
| 90 | * @return $this |
||
| 91 | * |
||
| 92 | * @throws InvalidManipulation |
||
| 93 | */ |
||
| 94 | public function crop(string $cropMethod, int $width, int $height) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param int $width |
||
| 112 | * @param int $height |
||
| 113 | * @param int $focalX Crop center X in percent |
||
| 114 | * @param int $focalY Crop center Y in percent |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | public function focalCrop(int $width, int $height, int $focalX, int $focalY) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param int $width |
||
| 128 | * @param int $height |
||
| 129 | * @param int $x |
||
| 130 | * @param int $y |
||
| 131 | * |
||
| 132 | * @return $this |
||
| 133 | * |
||
| 134 | * @throws InvalidManipulation |
||
| 135 | */ |
||
| 136 | public function manualCrop(int $width, int $height, int $x, int $y) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param int $width |
||
| 151 | * |
||
| 152 | * @return $this |
||
| 153 | * |
||
| 154 | * @throws InvalidManipulation |
||
| 155 | */ |
||
| 156 | public function width(int $width) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param int $height |
||
| 167 | * |
||
| 168 | * @return $this |
||
| 169 | * |
||
| 170 | * @throws InvalidManipulation |
||
| 171 | */ |
||
| 172 | public function height(int $height) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $fitMethod |
||
| 183 | * @param int $width |
||
| 184 | * @param int $height |
||
| 185 | * |
||
| 186 | * @return $this |
||
| 187 | * |
||
| 188 | * @throws InvalidManipulation |
||
| 189 | */ |
||
| 190 | public function fit(string $fitMethod, int $width, int $height) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param int $ratio A value between 1 and 8 |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | * |
||
| 211 | * @throws InvalidManipulation |
||
| 212 | */ |
||
| 213 | public function devicePixelRatio(int $ratio) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param int $brightness A value between -100 and 100 |
||
| 224 | * |
||
| 225 | * @return $this |
||
| 226 | * |
||
| 227 | * @throws InvalidManipulation |
||
| 228 | */ |
||
| 229 | public function brightness(int $brightness) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param float $gamma A value between 0.01 and 9.99 |
||
| 240 | * |
||
| 241 | * @return $this |
||
| 242 | * |
||
| 243 | * @throws InvalidManipulation |
||
| 244 | */ |
||
| 245 | public function gamma(float $gamma) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param int $contrast A value between -100 and 100 |
||
| 256 | * |
||
| 257 | * @return $this |
||
| 258 | * |
||
| 259 | * @throws InvalidManipulation |
||
| 260 | */ |
||
| 261 | public function contrast(int $contrast) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param int $sharpen A value between 0 and 100 |
||
| 272 | * |
||
| 273 | * @return $this |
||
| 274 | * |
||
| 275 | * @throws InvalidManipulation |
||
| 276 | */ |
||
| 277 | public function sharpen(int $sharpen) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param int $blur A value between 0 and 100 |
||
| 288 | * |
||
| 289 | * @return $this |
||
| 290 | * |
||
| 291 | * @throws InvalidManipulation |
||
| 292 | */ |
||
| 293 | public function blur(int $blur) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param int $pixelate A value between 0 and 1000 |
||
| 304 | * |
||
| 305 | * @return $this |
||
| 306 | * |
||
| 307 | * @throws InvalidManipulation |
||
| 308 | */ |
||
| 309 | public function pixelate(int $pixelate) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return $this |
||
| 320 | */ |
||
| 321 | public function greyscale() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return $this |
||
| 328 | */ |
||
| 329 | public function sepia() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $colorName |
||
| 336 | * |
||
| 337 | * @return $this |
||
| 338 | */ |
||
| 339 | public function background(string $colorName) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param int $width |
||
| 346 | * @param string $color |
||
| 347 | * @param string $borderType |
||
| 348 | * |
||
| 349 | * @return $this |
||
| 350 | * |
||
| 351 | * @throws InvalidManipulation |
||
| 352 | */ |
||
| 353 | public function border(int $width, string $color, string $borderType = 'overlay') |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param int $quality |
||
| 372 | * |
||
| 373 | * @return $this |
||
| 374 | * |
||
| 375 | * @throws InvalidManipulation |
||
| 376 | */ |
||
| 377 | public function quality(int $quality) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param string $format |
||
| 388 | * |
||
| 389 | * @return $this |
||
| 390 | * |
||
| 391 | * @throws InvalidManipulation |
||
| 392 | */ |
||
| 393 | public function format(string $format) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param string $filterName |
||
| 408 | * |
||
| 409 | * @return $this |
||
| 410 | * |
||
| 411 | * @throws InvalidManipulation |
||
| 412 | */ |
||
| 413 | protected function filter(string $filterName) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param string $filePath |
||
| 428 | * |
||
| 429 | * @return $this |
||
| 430 | * |
||
| 431 | * @throws FileNotFoundException |
||
| 432 | */ |
||
| 433 | public function watermark(string $filePath) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param int $width The width of the watermark in pixels (default) or percent. |
||
| 446 | * @param string $unit The unit of the `$width` parameter. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`. |
||
| 447 | * |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | public function watermarkWidth(int $width, string $unit = 'px') |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param int $height The height of the watermark in pixels (default) or percent. |
||
| 459 | * @param string $unit The unit of the `$height` parameter. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`. |
||
| 460 | * |
||
| 461 | * @return $this |
||
| 462 | */ |
||
| 463 | public function watermarkHeight(int $height, string $unit = 'px') |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param string $fitMethod How is the watermark fitted into the watermarkWidth and watermarkHeight properties. |
||
| 472 | * |
||
| 473 | * @return $this |
||
| 474 | * |
||
| 475 | * @throws InvalidManipulation |
||
| 476 | */ |
||
| 477 | public function watermarkFit(string $fitMethod) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * @param int $xPadding How far is the watermark placed from the top and bottom edges of the image. |
||
| 492 | * @param int|null $yPadding How far is the watermark placed from the left and right edges of the image. |
||
| 493 | * @param string $unit Unit of the padding values. Use `Manipulations::UNIT_PERCENT` or `Manipulations::UNIT_PIXELS`. |
||
| 494 | * |
||
| 495 | * @return $this |
||
| 496 | */ |
||
| 497 | public function watermarkPadding(int $xPadding, ?int $yPadding = null, string $unit = 'px') |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @param string $position |
||
| 512 | * |
||
| 513 | * @return $this |
||
| 514 | * |
||
| 515 | * @throws InvalidManipulation |
||
| 516 | */ |
||
| 517 | public function watermarkPosition(string $position) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Sets the opacity of the watermark. Only works with the `imagick` driver. |
||
| 532 | * |
||
| 533 | * @param int $opacity A value between 0 and 100. |
||
| 534 | * |
||
| 535 | * @return $this |
||
| 536 | * |
||
| 537 | * @throws InvalidManipulation |
||
| 538 | */ |
||
| 539 | public function watermarkOpacity(int $opacity) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @return $this |
||
| 550 | */ |
||
| 551 | public function apply() |
||
| 557 | |||
| 558 | public function removeManipulation(string $name) |
||
| 562 | |||
| 563 | public function hasManipulation(string $manipulationName): bool |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param string $manipulationName |
||
| 570 | * |
||
| 571 | * @return string|null |
||
| 572 | */ |
||
| 573 | public function getManipulationArgument(string $manipulationName) |
||
| 581 | |||
| 582 | protected function addManipulation(string $manipulationName, string $manipulationArgument) |
||
| 588 | |||
| 589 | public function mergeManipulations(Manipulations $manipulations) |
||
| 595 | |||
| 596 | public function getManipulationSequence(): ManipulationSequence |
||
| 600 | |||
| 601 | protected function validateManipulation(string $value, string $constantNamePrefix): bool |
||
| 605 | |||
| 606 | protected function getValidManipulationOptions(string $manipulation): array |
||
| 614 | } |
||
| 615 |