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) |
||
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() |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function getMimeType() |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public function getWidth() |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function getHeight() |
||
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | public function format($format) |
||
216 | |||
217 | /** |
||
218 | * imagescale() is not used due a weird black border: |
||
219 | * https://bugs.php.net/bug.php?id=73281&thanks=6 |
||
220 | * |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | public function resize($width, $height) |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function getCropOffsets($width, $height, $method) |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | public function crop($width, $height, $x, $y) |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | public function rotate($angle) |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function blur($loops) |
||
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | public function watermark(LibInterface $image, $x, $y) |
||
318 | |||
319 | /** |
||
320 | * {@inheritdoc} |
||
321 | */ |
||
322 | public function opacity($opacity) |
||
352 | |||
353 | /** |
||
354 | * {@inheritdoc} |
||
355 | */ |
||
356 | public function setProgressive($progressive) |
||
360 | |||
361 | /** |
||
362 | * Creates a new truecolor image |
||
363 | * |
||
364 | * @param integer $width |
||
365 | * @param integer $height |
||
366 | * @param array $background |
||
367 | * |
||
368 | * @return resource |
||
369 | */ |
||
370 | private function createImage($width, $height, array $background = [0, 0, 0]) |
||
392 | } |
||
393 |