Complex classes like Gd 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 Gd, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Gd extends AbstractLib implements LibInterface |
||
11 | { |
||
12 | public static $fallbackCropMethods = [ |
||
13 | 'Entropy' => ['center', 'middle'], |
||
14 | 'Balanced' => ['center', 'middle'], |
||
15 | ]; |
||
16 | |||
17 | protected $image; |
||
18 | protected $type; |
||
19 | |||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | public static function checkCompatibility() |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | public static function createFromFile($filename) |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | public static function createFromString($string) |
||
57 | |||
58 | /** |
||
59 | * Constructor of the class. |
||
60 | * |
||
61 | * @param resource $image The Gd resource. |
||
62 | */ |
||
63 | public function __construct($image, $type = null) |
||
72 | |||
73 | /** |
||
74 | * Destroy the image. |
||
75 | */ |
||
76 | public function __destruct() |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function flip() |
||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function flop() |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function save($filename) |
||
109 | |||
110 | /** |
||
111 | * Gets the original image object. |
||
112 | * |
||
113 | * @return resource |
||
114 | */ |
||
115 | public function getImage() |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | public function getString() |
||
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | public function getMimeType() |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function getWidth() |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | */ |
||
162 | public function getHeight() |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | public function format($format) |
||
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | public function resize($width, $height) |
||
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | public function getCropOffsets($width, $height, $method) |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function crop($width, $height, $x, $y) |
||
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | public function rotate($angle) |
||
277 | |||
278 | /** |
||
279 | * {@inheritdoc} |
||
280 | */ |
||
281 | public function blur($loops) |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | public function watermark(LibInterface $image, $x, $y) |
||
312 | |||
313 | /** |
||
314 | * {@inheritdoc} |
||
315 | */ |
||
316 | public function opacity($opacity) |
||
346 | |||
347 | /** |
||
348 | * {@inheritdoc} |
||
349 | */ |
||
350 | public function setProgressive($progressive) |
||
354 | } |
||
355 |