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) |
|
72 | |||
73 | /** |
||
74 | * Resolve width. |
||
75 | * @return string The resolved width. |
||
76 | */ |
||
77 | 9 | public function getWidth() |
|
89 | |||
90 | /** |
||
91 | * Resolve height. |
||
92 | * @return string The resolved height. |
||
93 | */ |
||
94 | 9 | public function getHeight() |
|
106 | |||
107 | /** |
||
108 | * Resolve fit. |
||
109 | * @return string The resolved fit. |
||
110 | */ |
||
111 | 9 | public function getFit() |
|
136 | |||
137 | /** |
||
138 | * Resolve crop. |
||
139 | * @return string The resolved crop. |
||
140 | */ |
||
141 | 9 | public function getCrop() |
|
161 | |||
162 | /** |
||
163 | * Resolve the device pixel ratio. |
||
164 | * @return double The device pixel ratio. |
||
165 | */ |
||
166 | 9 | public function getDpr() |
|
167 | { |
||
168 | 9 | if (!is_numeric($this->dpr)) { |
|
169 | 9 | return 1.0; |
|
170 | } |
||
171 | |||
172 | 3 | if ($this->dpr < 0 or $this->dpr > 8) { |
|
173 | 3 | return 1.0; |
|
174 | } |
||
175 | |||
176 | 3 | return (double) $this->dpr; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Resolve missing image dimensions. |
||
181 | * @param Image $image The source image. |
||
182 | * @param double|null $width The image width. |
||
183 | * @param double|null $height The image height. |
||
184 | * @return double[] The resolved width and height. |
||
185 | */ |
||
186 | 9 | public function resolveMissingDimensions(Image $image, $width, $height) |
|
206 | |||
207 | /** |
||
208 | * Apply the device pixel ratio. |
||
209 | * @param double $width The target image width. |
||
210 | * @param double $height The target image height. |
||
211 | * @param double $dpr The device pixel ratio. |
||
212 | * @return double[] The modified width and height. |
||
213 | */ |
||
214 | 6 | public function applyDpr($width, $height, $dpr) |
|
224 | |||
225 | /** |
||
226 | * Limit image size to maximum allowed image size. |
||
227 | * @param double $width The image width. |
||
228 | * @param double $height The image height. |
||
229 | * @return double[] The limited width and height. |
||
230 | */ |
||
231 | 9 | public function limitImageSize($width, $height) |
|
247 | |||
248 | /** |
||
249 | * Perform resize image manipulation. |
||
250 | * @param Image $image The source image. |
||
251 | * @param string $fit The fit. |
||
252 | * @param string $width The width. |
||
253 | * @param string $height The height. |
||
254 | * @param string|null $crop The crop. |
||
255 | * @return Image The manipulated image. |
||
256 | */ |
||
257 | 9 | public function runResize(Image $image, $fit, $width, $height, $crop = null) |
|
258 | { |
||
259 | 9 | if ($fit === 'contain') { |
|
260 | 9 | return $this->runContainResize($image, $width, $height); |
|
261 | } |
||
262 | |||
263 | 3 | if ($fit === 'fill') { |
|
264 | 3 | return $this->runFillResize($image, $width, $height); |
|
265 | } |
||
266 | |||
267 | 3 | if ($fit === 'max') { |
|
268 | 3 | return $this->runMaxResize($image, $width, $height); |
|
269 | } |
||
270 | |||
271 | 3 | if ($fit === 'stretch') { |
|
272 | 3 | return $this->runStretchResize($image, $width, $height); |
|
273 | } |
||
274 | |||
275 | 3 | if ($fit === 'crop') { |
|
276 | 3 | return $this->runCropResize($image, $width, $height, $crop); |
|
277 | } |
||
278 | |||
279 | 3 | return $image; |
|
280 | } |
||
281 | |||
282 | /** |
||
283 | * Perform contain resize image manipulation. |
||
284 | * @param Image $image The source image. |
||
285 | * @param string $width The width. |
||
286 | * @param string $height The height. |
||
287 | * @return Image The manipulated image. |
||
288 | */ |
||
289 | 12 | public function runContainResize(Image $image, $width, $height) |
|
295 | |||
296 | /** |
||
297 | * Perform max resize image manipulation. |
||
298 | * @param Image $image The source image. |
||
299 | * @param string $width The width. |
||
300 | * @param string $height The height. |
||
301 | * @return Image The manipulated image. |
||
302 | */ |
||
303 | 9 | public function runMaxResize(Image $image, $width, $height) |
|
310 | |||
311 | /** |
||
312 | * Perform fill resize image manipulation. |
||
313 | * @param Image $image The source image. |
||
314 | * @param string $width The width. |
||
315 | * @param string $height The height. |
||
316 | * @return Image The manipulated image. |
||
317 | */ |
||
318 | 6 | public function runFillResize($image, $width, $height) |
|
324 | |||
325 | /** |
||
326 | * Perform stretch resize image manipulation. |
||
327 | * @param Image $image The source image. |
||
328 | * @param string $width The width. |
||
329 | * @param string $height The height. |
||
330 | * @return Image The manipulated image. |
||
331 | */ |
||
332 | 6 | public function runStretchResize(Image $image, $width, $height) |
|
336 | |||
337 | /** |
||
338 | * Perform crop resize image manipulation. |
||
339 | * @param Image $image The source image. |
||
340 | * @param string $width The width. |
||
341 | * @param string $height The height. |
||
342 | * @param string $crop The crop. |
||
343 | * @return Image The manipulated image. |
||
344 | */ |
||
345 | 6 | public function runCropResize(Image $image, $width, $height, $crop) |
|
355 | } |
||
356 |