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 | 44 | public function __construct($maxImageSize = null) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Set the maximum image size. |
||
| 32 | * @param integer|null Maximum image size in pixels. |
||
| 33 | */ |
||
| 34 | 4 | public function setMaxImageSize($maxImageSize) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Get the maximum image size. |
||
| 41 | * @return integer|null Maximum image size in pixels. |
||
| 42 | */ |
||
| 43 | 4 | 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 | 4 | public function run(Image $image) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Resolve width. |
||
| 73 | * @return integer|null The resolved width. |
||
| 74 | */ |
||
| 75 | 6 | public function getWidth() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Resolve height. |
||
| 90 | * @return integer|null The resolved height. |
||
| 91 | */ |
||
| 92 | 6 | public function getHeight() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Resolve fit. |
||
| 107 | * @return string The resolved fit. |
||
| 108 | */ |
||
| 109 | 6 | public function getFit() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Resolve the device pixel ratio. |
||
| 124 | * @return double The device pixel ratio. |
||
| 125 | */ |
||
| 126 | 6 | public function getDpr() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Resolve missing image dimensions. |
||
| 141 | * @param Image $image The source image. |
||
| 142 | * @param integer|null $width The image width. |
||
| 143 | * @param integer|null $height The image height. |
||
| 144 | * @return integer[] The resolved width and height. |
||
| 145 | */ |
||
| 146 | 6 | public function resolveMissingDimensions(Image $image, $width, $height) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Apply the device pixel ratio. |
||
| 169 | * @param integer $width The target image width. |
||
| 170 | * @param integer $height The target image height. |
||
| 171 | * @param integer $dpr The device pixel ratio. |
||
| 172 | * @return integer[] The modified width and height. |
||
| 173 | */ |
||
| 174 | 4 | public function applyDpr($width, $height, $dpr) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Limit image size to maximum allowed image size. |
||
| 187 | * @param integer $width The image width. |
||
| 188 | * @param integer $height The image height. |
||
| 189 | * @return integer[] The limited width and height. |
||
| 190 | */ |
||
| 191 | 6 | public function limitImageSize($width, $height) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Perform resize image manipulation. |
||
| 210 | * @param Image $image The source image. |
||
| 211 | * @param string $fit The fit. |
||
| 212 | * @param integer $width The width. |
||
| 213 | * @param integer $height The height. |
||
| 214 | * @return Image The manipulated image. |
||
| 215 | */ |
||
| 216 | 6 | public function runResize(Image $image, $fit, $width, $height) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Perform contain resize image manipulation. |
||
| 243 | * @param Image $image The source image. |
||
| 244 | * @param integer $width The width. |
||
| 245 | * @param integer $height The height. |
||
| 246 | * @return Image The manipulated image. |
||
| 247 | */ |
||
| 248 | 8 | public function runContainResize(Image $image, $width, $height) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Perform max resize image manipulation. |
||
| 257 | * @param Image $image The source image. |
||
| 258 | * @param integer $width The width. |
||
| 259 | * @param integer $height The height. |
||
| 260 | * @return Image The manipulated image. |
||
| 261 | */ |
||
| 262 | 6 | public function runMaxResize(Image $image, $width, $height) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Perform fill resize image manipulation. |
||
| 272 | * @param Image $image The source image. |
||
| 273 | * @param integer $width The width. |
||
| 274 | * @param integer $height The height. |
||
| 275 | * @return Image The manipulated image. |
||
| 276 | */ |
||
| 277 | 4 | public function runFillResize($image, $width, $height) |
|
| 278 | { |
||
| 279 | 4 | $image = $this->runMaxResize($image, $width, $height); |
|
| 280 | |||
| 281 | 4 | return $image->resizeCanvas($width, $height, 'center'); |
|
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Perform stretch resize image manipulation. |
||
| 286 | * @param Image $image The source image. |
||
| 287 | * @param integer $width The width. |
||
| 288 | * @param integer $height The height. |
||
| 289 | * @return Image The manipulated image. |
||
| 290 | */ |
||
| 291 | 4 | public function runStretchResize(Image $image, $width, $height) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Perform crop resize image manipulation. |
||
| 298 | * @param Image $image The source image. |
||
| 299 | * @param integer $width The width. |
||
| 300 | * @param integer $height The height. |
||
| 301 | * @return Image The manipulated image. |
||
| 302 | */ |
||
| 303 | 4 | public function runCropResize(Image $image, $width, $height) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Resolve the crop resize dimensions. |
||
| 320 | * @param Image $image The source image. |
||
| 321 | * @param integer $width The width. |
||
| 322 | * @param integer $height The height. |
||
| 323 | 4 | * @return array The resize dimensions. |
|
| 324 | */ |
||
| 325 | 4 | public function resolveCropResizeDimensions(Image $image, $width, $height) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Resolve the crop offset. |
||
| 336 | * @param Image $image The source image. |
||
| 337 | * @param integer $width The width. |
||
| 338 | * @param integer $height The height. |
||
| 339 | 4 | * @return array The crop offset. |
|
| 340 | */ |
||
| 341 | 4 | public function resolveCropOffset(Image $image, $width, $height) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Resolve crop with zoom. |
||
| 372 | 6 | * @return integer[] The resolved crop. |
|
| 373 | */ |
||
| 374 | public function getCrop() |
||
| 408 | } |
||
| 409 |