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) |
||
71 | |||
72 | /** |
||
73 | * Destroy the image. |
||
74 | */ |
||
75 | public function __destruct() |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function flip() |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function flop() |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function save($filename) |
||
108 | |||
109 | /** |
||
110 | * Gets the original image object. |
||
111 | * |
||
112 | * @return resource |
||
113 | */ |
||
114 | public function getImage() |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function getString() |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function getMimeType() |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function getWidth() |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function getHeight() |
||
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | public function format($format) |
||
218 | |||
219 | /** |
||
220 | * imagescale() is not used due a weird black border: |
||
221 | * https://bugs.php.net/bug.php?id=73281&thanks=6 |
||
222 | * |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function resize($width, $height) |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | public function getCropOffsets($width, $height, $method) |
||
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | public function crop($width, $height, $x, $y) |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function rotate($angle) |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function blur($loops) |
||
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | */ |
||
312 | public function watermark(LibInterface $image, $x, $y) |
||
320 | |||
321 | /** |
||
322 | * {@inheritdoc} |
||
323 | */ |
||
324 | public function opacity($opacity) |
||
354 | |||
355 | /** |
||
356 | * {@inheritdoc} |
||
357 | */ |
||
358 | public function setProgressive($progressive) |
||
362 | |||
363 | /** |
||
364 | * Creates a new truecolor image |
||
365 | * |
||
366 | * @param integer $width |
||
367 | * @param integer $height |
||
368 | * @param array $background |
||
369 | * |
||
370 | * @return resource |
||
371 | */ |
||
372 | private function createImage($width, $height, array $background = [0, 0, 0]) |
||
394 | } |
||
395 |