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 | * imagescale() is not used due a weird black border: |
||
218 | * https://bugs.php.net/bug.php?id=73281&thanks=6 |
||
219 | * |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | public function resize($width, $height) |
||
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | public function getCropOffsets($width, $height, $method) |
||
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | public function crop($width, $height, $x, $y) |
||
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function rotate($angle) |
||
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | public function blur($loops) |
||
303 | |||
304 | /** |
||
305 | * {@inheritdoc} |
||
306 | */ |
||
307 | public function watermark(LibInterface $image, $x, $y) |
||
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | public function opacity($opacity) |
||
349 | |||
350 | /** |
||
351 | * {@inheritdoc} |
||
352 | */ |
||
353 | public function setProgressive($progressive) |
||
357 | |||
358 | /** |
||
359 | * Creates a new truecolor image |
||
360 | * |
||
361 | * @param integer $width |
||
362 | * @param integer $height |
||
363 | * @param array $background |
||
364 | * |
||
365 | * @return resource |
||
366 | */ |
||
367 | private function createImage($width, $height, array $background = [0, 0, 0]) |
||
389 | } |
||
390 |