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() |
|
121 | |||
122 | /** |
||
123 | * Resolve the device pixel ratio. |
||
124 | * @return double The device pixel ratio. |
||
125 | */ |
||
126 | 9 | 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 | 9 | public function resolveMissingDimensions(Image $image, $width, $height) |
|
147 | { |
||
148 | 9 | if (is_null($width) and is_null($height)) { |
|
149 | 3 | $width = $image->width(); |
|
150 | 3 | $height = $image->height(); |
|
151 | } |
||
152 | |||
153 | 9 | if (is_null($width)) { |
|
154 | 3 | $width = $height * ($image->width() / $image->height()); |
|
155 | } |
||
156 | |||
157 | 9 | if (is_null($height)) { |
|
158 | 6 | $height = $width / ($image->width() / $image->height()); |
|
159 | } |
||
160 | |||
161 | return [ |
||
162 | 9 | (int) $width, |
|
163 | 9 | (int) $height, |
|
164 | ]; |
||
165 | } |
||
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 | 6 | public function applyDpr($width, $height, $dpr) |
|
175 | { |
||
176 | 6 | $width = $width * $dpr; |
|
177 | 6 | $height = $height * $dpr; |
|
178 | |||
179 | return [ |
||
180 | 6 | (int) $width, |
|
181 | 6 | (int) $height, |
|
182 | ]; |
||
183 | } |
||
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 | 9 | public function limitImageSize($width, $height) |
|
192 | { |
||
193 | 9 | if ($this->maxImageSize !== null) { |
|
194 | 3 | $imageSize = $width * $height; |
|
195 | |||
196 | 3 | if ($imageSize > $this->maxImageSize) { |
|
197 | 3 | $width = $width / sqrt($imageSize / $this->maxImageSize); |
|
198 | 3 | $height = $height / sqrt($imageSize / $this->maxImageSize); |
|
199 | } |
||
200 | } |
||
201 | |||
202 | return [ |
||
203 | 9 | (int) $width, |
|
204 | 9 | (int) $height, |
|
205 | ]; |
||
206 | } |
||
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 | 9 | 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 | 12 | public function runContainResize(Image $image, $width, $height) |
|
249 | { |
||
250 | 4 | return $image->resize($width, $height, function ($constraint) { |
|
251 | $constraint->aspectRatio(); |
||
252 | 12 | }); |
|
253 | } |
||
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 | 9 | public function runMaxResize(Image $image, $width, $height) |
|
263 | { |
||
264 | 3 | return $image->resize($width, $height, function ($constraint) { |
|
265 | $constraint->aspectRatio(); |
||
266 | $constraint->upsize(); |
||
267 | 9 | }); |
|
268 | } |
||
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 | 6 | public function runFillResize($image, $width, $height) |
|
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 | 6 | 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 | 6 | 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 | * @return array The resize dimensions. |
||
324 | */ |
||
325 | 6 | 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 | * @return array The crop offset. |
||
340 | */ |
||
341 | 6 | public function resolveCropOffset(Image $image, $width, $height) |
|
369 | |||
370 | /** |
||
371 | * Resolve crop with zoom. |
||
372 | * @return integer[] The resolved crop. |
||
373 | */ |
||
374 | 9 | public function getCrop() |
|
408 | } |
||
409 |