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 int|null |
||
| 18 | */ |
||
| 19 | protected $maxImageSize; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Create Size instance. |
||
| 23 | * @param int|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 int|null Maximum image size in pixels. |
||
| 33 | */ |
||
| 34 | 6 | public function setMaxImageSize($maxImageSize) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Get the maximum image size. |
||
| 41 | * @return int|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) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Resolve width. |
||
| 73 | * @return int The resolved width. |
||
| 74 | */ |
||
| 75 | 9 | public function getWidth() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * Resolve height. |
||
| 90 | * @return int 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() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Resolve crop. |
||
| 124 | * @return array The resolved crop. |
||
| 125 | */ |
||
| 126 | 9 | public function getCrop() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Resolve the device pixel ratio. |
||
| 157 | * @return double The device pixel ratio. |
||
| 158 | */ |
||
| 159 | 9 | public function getDpr() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Resolve missing image dimensions. |
||
| 174 | * @param Image $image The source image. |
||
| 175 | * @param int|null $width The image width. |
||
| 176 | * @param int|null $height The image height. |
||
| 177 | * @return int[] The resolved width and height. |
||
| 178 | */ |
||
| 179 | 9 | public function resolveMissingDimensions(Image $image, $width, $height) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Apply the device pixel ratio. |
||
| 202 | * @param int $width The target image width. |
||
| 203 | * @param int $height The target image height. |
||
| 204 | * @param int $dpr The device pixel ratio. |
||
| 205 | * @return int[] The modified width and height. |
||
| 206 | */ |
||
| 207 | 6 | public function applyDpr($width, $height, $dpr) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Limit image size to maximum allowed image size. |
||
| 220 | * @param int $width The image width. |
||
| 221 | * @param int $height The image height. |
||
| 222 | * @return int[] The limited width and height. |
||
| 223 | */ |
||
| 224 | 9 | public function limitImageSize($width, $height) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Perform resize image manipulation. |
||
| 243 | * @param Image $image The source image. |
||
| 244 | * @param string $fit The fit. |
||
| 245 | * @param int $width The width. |
||
| 246 | * @param int $height The height. |
||
| 247 | * @return Image The manipulated image. |
||
| 248 | */ |
||
| 249 | 9 | public function runResize(Image $image, $fit, $width, $height) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Perform contain resize image manipulation. |
||
| 276 | * @param Image $image The source image. |
||
| 277 | * @param int $width The width. |
||
| 278 | * @param int $height The height. |
||
| 279 | * @return Image The manipulated image. |
||
| 280 | */ |
||
| 281 | 12 | public function runContainResize(Image $image, $width, $height) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Perform max resize image manipulation. |
||
| 290 | * @param Image $image The source image. |
||
| 291 | * @param int $width The width. |
||
| 292 | * @param int $height The height. |
||
| 293 | * @return Image The manipulated image. |
||
| 294 | */ |
||
| 295 | 9 | public function runMaxResize(Image $image, $width, $height) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Perform fill resize image manipulation. |
||
| 305 | * @param Image $image The source image. |
||
| 306 | * @param int $width The width. |
||
| 307 | * @param int $height The height. |
||
| 308 | * @return Image The manipulated image. |
||
| 309 | */ |
||
| 310 | 6 | public function runFillResize($image, $width, $height) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Perform stretch resize image manipulation. |
||
| 319 | * @param Image $image The source image. |
||
| 320 | * @param int $width The width. |
||
| 321 | * @param int $height The height. |
||
| 322 | * @return Image The manipulated image. |
||
| 323 | */ |
||
| 324 | 6 | public function runStretchResize(Image $image, $width, $height) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Perform crop resize image manipulation. |
||
| 331 | * @param Image $image The source image. |
||
| 332 | * @param int $width The width. |
||
| 333 | * @param int $height The height. |
||
| 334 | * @return Image The manipulated image. |
||
| 335 | */ |
||
| 336 | 6 | public function runCropResize(Image $image, $width, $height) |
|
| 381 | } |
||
| 382 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: