Complex classes like Size 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 Size, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Size extends BaseManipulator |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Maximum image size in pixels. |
||
| 17 | * @var integer|null |
||
| 18 | */ |
||
| 19 | protected $maxImageSize; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Create Size instance. |
||
| 23 | * @param integer|null $maxImageSize Maximum image size in pixels. |
||
| 24 | */ |
||
| 25 | 66 | public function __construct($maxImageSize = null) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Set the maximum image size. |
||
| 32 | * @param integer|null Maximum image size in pixels. |
||
| 33 | */ |
||
| 34 | 6 | public function setMaxImageSize($maxImageSize) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Get the maximum image size. |
||
| 41 | * @return integer|null Maximum image size in pixels. |
||
| 42 | */ |
||
| 43 | 6 | public function getMaxImageSize() |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Perform size image manipulation. |
||
| 50 | * @param Image $image The source image. |
||
| 51 | * @return Image The manipulated image. |
||
| 52 | */ |
||
| 53 | 6 | public function run(Image $image) |
|
| 54 | { |
||
| 55 | 6 | $width = $this->getWidth(); |
|
| 56 | 6 | $height = $this->getHeight(); |
|
| 57 | 6 | $fit = $this->getFit(); |
|
| 58 | 6 | $dpr = $this->getDpr(); |
|
| 59 | |||
| 60 | 6 | list($width, $height) = $this->resolveMissingDimensions($image, $width, $height); |
|
| 61 | 6 | list($width, $height) = $this->applyDpr($width, $height, $dpr); |
|
| 62 | 6 | list($width, $height) = $this->limitImageSize($width, $height); |
|
| 63 | |||
| 64 | 6 | if ((int) $width !== (int) $image->width() or (int) $height !== (int) $image->height()) { |
|
| 65 | 6 | $image = $this->runResize($image, $fit, (int) $width, (int) $height); |
|
| 66 | } |
||
| 67 | |||
| 68 | 6 | return $image; |
|
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Resolve width. |
||
| 73 | * @return integer|null The resolved width. |
||
| 74 | */ |
||
| 75 | 9 | public function getWidth() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Resolve height. |
||
| 90 | * @return integer|null The resolved height. |
||
| 91 | */ |
||
| 92 | 9 | public function getHeight() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Resolve fit. |
||
| 107 | * @return string The resolved fit. |
||
| 108 | */ |
||
| 109 | 9 | public function getFit() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Resolve the device pixel ratio. |
||
| 126 | * @return double The device pixel ratio. |
||
| 127 | */ |
||
| 128 | 9 | public function getDpr() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Resolve missing image dimensions. |
||
| 143 | * @param Image $image The source image. |
||
| 144 | * @param integer|null $width The image width. |
||
| 145 | * @param integer|null $height The image height. |
||
| 146 | * @return integer[] The resolved width and height. |
||
| 147 | */ |
||
| 148 | 9 | public function resolveMissingDimensions(Image $image, $width, $height) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Apply the device pixel ratio. |
||
| 171 | * @param integer $width The target image width. |
||
| 172 | * @param integer $height The target image height. |
||
| 173 | * @param integer $dpr The device pixel ratio. |
||
| 174 | * @return integer[] The modified width and height. |
||
| 175 | */ |
||
| 176 | 6 | public function applyDpr($width, $height, $dpr) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Limit image size to maximum allowed image size. |
||
| 189 | * @param integer $width The image width. |
||
| 190 | * @param integer $height The image height. |
||
| 191 | * @return integer[] The limited width and height. |
||
| 192 | */ |
||
| 193 | 9 | public function limitImageSize($width, $height) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Perform resize image manipulation. |
||
| 212 | * @param Image $image The source image. |
||
| 213 | * @param string $fit The fit. |
||
| 214 | * @param integer $width The width. |
||
| 215 | * @param integer $height The height. |
||
| 216 | * @return Image The manipulated image. |
||
| 217 | */ |
||
| 218 | 9 | public function runResize(Image $image, $fit, $width, $height) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Perform contain resize image manipulation. |
||
| 245 | * @param Image $image The source image. |
||
| 246 | * @param integer $width The width. |
||
| 247 | * @param integer $height The height. |
||
| 248 | * @return Image The manipulated image. |
||
| 249 | */ |
||
| 250 | 12 | public function runContainResize(Image $image, $width, $height) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Perform max resize image manipulation. |
||
| 259 | * @param Image $image The source image. |
||
| 260 | * @param integer $width The width. |
||
| 261 | * @param integer $height The height. |
||
| 262 | * @return Image The manipulated image. |
||
| 263 | */ |
||
| 264 | 9 | public function runMaxResize(Image $image, $width, $height) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Perform fill resize image manipulation. |
||
| 274 | * @param Image $image The source image. |
||
| 275 | * @param integer $width The width. |
||
| 276 | * @param integer $height The height. |
||
| 277 | * @return Image The manipulated image. |
||
| 278 | */ |
||
| 279 | 6 | public function runFillResize($image, $width, $height) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Perform stretch resize image manipulation. |
||
| 288 | * @param Image $image The source image. |
||
| 289 | * @param integer $width The width. |
||
| 290 | * @param integer $height The height. |
||
| 291 | * @return Image The manipulated image. |
||
| 292 | */ |
||
| 293 | 6 | public function runStretchResize(Image $image, $width, $height) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Perform crop resize image manipulation. |
||
| 300 | * @param Image $image The source image. |
||
| 301 | * @param integer $width The width. |
||
| 302 | * @param integer $height The height. |
||
| 303 | * @return Image The manipulated image. |
||
| 304 | */ |
||
| 305 | 6 | public function runCropResize(Image $image, $width, $height) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Resolve the crop resize dimensions. |
||
| 322 | * @param Image $image The source image. |
||
| 323 | * @param integer $width The width. |
||
| 324 | * @param integer $height The height. |
||
| 325 | * @return array The resize dimensions. |
||
| 326 | */ |
||
| 327 | 6 | public function resolveCropResizeDimensions(Image $image, $width, $height) |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Resolve the crop offset. |
||
| 338 | * @param Image $image The source image. |
||
| 339 | * @param integer $width The width. |
||
| 340 | * @param integer $height The height. |
||
| 341 | * @return array The crop offset. |
||
| 342 | */ |
||
| 343 | 6 | public function resolveCropOffset(Image $image, $width, $height) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Resolve crop with zoom. |
||
| 374 | * @return integer[] The resolved crop. |
||
| 375 | */ |
||
| 376 | 9 | public function getCrop() |
|
| 410 | } |
||
| 411 |