Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Imagick 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 Imagick, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Imagick extends AbstractLib implements LibInterface |
||
13 | { |
||
14 | protected $image; |
||
15 | |||
16 | /** |
||
17 | * {@inheritdoc} |
||
18 | */ |
||
19 | public static function checkCompatibility() |
||
23 | |||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | * |
||
27 | * @return Imagick |
||
28 | */ |
||
29 | public static function createFromFile($filename) |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | * |
||
43 | * @return Imagick |
||
44 | */ |
||
45 | public static function createFromString($string) |
||
53 | |||
54 | /** |
||
55 | * Constructor of the class. |
||
56 | * |
||
57 | * @param BaseImagick $image The Imagick instance |
||
58 | */ |
||
59 | public function __construct(BaseImagick $image) |
||
77 | |||
78 | /** |
||
79 | * Destroy the image. |
||
80 | */ |
||
81 | public function __destruct() |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public function flip() |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | public function flop() |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function save($filename) |
||
125 | |||
126 | /** |
||
127 | * Gets the original image object. |
||
128 | * |
||
129 | * @return object |
||
130 | */ |
||
131 | public function getImage() |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | public function getString() |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function getMimeType() |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function getWidth() |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function getHeight() |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function format($format) |
||
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | public function resize($width, $height) |
||
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | public function getCropOffsets($width, $height, $method) |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | */ |
||
246 | public function crop($width, $height, $x, $y) |
||
265 | |||
266 | /** |
||
267 | * {@inheritdoc} |
||
268 | */ |
||
269 | public function rotate($angle) |
||
277 | |||
278 | /** |
||
279 | * Returns a copy of the image compressed and ready to save or print. |
||
280 | * |
||
281 | * @return BaseImagick The instance of the image |
||
282 | */ |
||
283 | private function getCompressed() |
||
322 | |||
323 | /** |
||
324 | * @param object $watermark Watermark image object |
||
325 | * @param array $x Position x |
||
326 | * @param array $y Position y |
||
327 | */ |
||
328 | public function watermark($watermark, $x, $y) |
||
332 | |||
333 | /** |
||
334 | * {@inheritdoc} |
||
335 | */ |
||
336 | public function opacity($opacity) |
||
349 | } |
||
350 |