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 AbstractResizeEffect 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 AbstractResizeEffect, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | abstract class AbstractResizeEffect extends AbstractEffect |
||
14 | { |
||
15 | /** |
||
16 | * @var string $mode |
||
17 | */ |
||
18 | private $mode = 'auto'; |
||
19 | |||
20 | /** |
||
21 | * @var mixed $size |
||
22 | */ |
||
23 | private $size; |
||
24 | |||
25 | /** |
||
26 | * @var integer $width |
||
27 | */ |
||
28 | private $width = 0; |
||
29 | |||
30 | /** |
||
31 | * @var integer $height |
||
32 | */ |
||
33 | private $height = 0; |
||
34 | |||
35 | /** |
||
36 | * @var integer $minWidth |
||
37 | */ |
||
38 | private $minWidth = 0; |
||
39 | |||
40 | /** |
||
41 | * @var integer $minHeight |
||
42 | */ |
||
43 | private $minHeight = 0; |
||
44 | |||
45 | /** |
||
46 | * @var integer $maxWidth |
||
47 | */ |
||
48 | private $maxWidth = 0; |
||
49 | |||
50 | /** |
||
51 | * @var integer $maxHeight |
||
52 | */ |
||
53 | private $maxHeight = 0; |
||
54 | |||
55 | /** |
||
56 | * @var string $gravity |
||
57 | */ |
||
58 | private $gravity = 'center'; |
||
59 | |||
60 | /** |
||
61 | * @var string $backgroundColor |
||
62 | */ |
||
63 | private $backgroundColor = 'rgba(100%, 100%, 100%, 0)'; |
||
64 | |||
65 | /** |
||
66 | * @var boolean $adaptive |
||
67 | */ |
||
68 | private $adaptive = false; |
||
69 | |||
70 | /** |
||
71 | * @param string $mode The resize mode. |
||
72 | * @throws InvalidArgumentException If the mode argument is not a valid resize mode. |
||
73 | * @return AbstractResizeEffect Chainable |
||
74 | */ |
||
75 | public function setMode($mode) |
||
95 | |||
96 | /** |
||
97 | * @return string |
||
98 | */ |
||
99 | public function mode() |
||
103 | |||
104 | /** |
||
105 | * Set a complex resize value. |
||
106 | * |
||
107 | * @param mixed $size The size. |
||
108 | * @throws InvalidArgumentException If the size argument is not valid. |
||
109 | * @return Rotate Chainable |
||
110 | */ |
||
111 | public function setSize($size) |
||
123 | |||
124 | /** |
||
125 | * Retrieve the complex resize value. |
||
126 | * |
||
127 | * @return mixed |
||
128 | */ |
||
129 | public function size() |
||
133 | |||
134 | /** |
||
135 | * @param integer $width The target resize width. |
||
136 | * @throws InvalidArgumentException If the width argument is not numeric or lower than 0. |
||
137 | * @return Rotate Chainable |
||
138 | */ |
||
139 | public function setWidth($width) |
||
149 | |||
150 | /** |
||
151 | * @return float |
||
152 | */ |
||
153 | public function width() |
||
157 | |||
158 | /** |
||
159 | * @param integer $height The target resize height. |
||
160 | * @throws InvalidArgumentException If the height argument is not numeric or lower than 0. |
||
161 | * @return Rotate Chainable |
||
162 | */ |
||
163 | View Code Duplication | public function setHeight($height) |
|
173 | |||
174 | /** |
||
175 | * @return float |
||
176 | */ |
||
177 | public function height() |
||
181 | |||
182 | /** |
||
183 | * @param integer $minWidth The resize minimal width. |
||
184 | * @throws InvalidArgumentException If the argument is not numeric or lower than 0. |
||
185 | * @return Rotate Chainable |
||
186 | */ |
||
187 | public function setMinWidth($minWidth) |
||
197 | |||
198 | /** |
||
199 | * @return float |
||
200 | */ |
||
201 | public function minWidth() |
||
205 | |||
206 | /** |
||
207 | * @param integer $minHeight The resize minimal height. |
||
208 | * @throws InvalidArgumentException If the argument is not numeric or lower than 0. |
||
209 | * @return Rotate Chainable |
||
210 | */ |
||
211 | public function setMinHeight($minHeight) |
||
221 | |||
222 | /** |
||
223 | * @return float |
||
224 | */ |
||
225 | public function minHeight() |
||
229 | |||
230 | /** |
||
231 | * @param integer $maxWidth The resize max width. |
||
232 | * @throws InvalidArgumentException If the argument is not numeric or lower than 0. |
||
233 | * @return Rotate Chainable |
||
234 | */ |
||
235 | public function setMaxWidth($maxWidth) |
||
245 | |||
246 | /** |
||
247 | * @return float |
||
248 | */ |
||
249 | public function maxWidth() |
||
253 | |||
254 | /** |
||
255 | * @param integer $maxHeight The resize max height. |
||
256 | * @throws InvalidArgumentException If the argument is not numeric or lower than 0. |
||
257 | * @return Rotate Chainable |
||
258 | */ |
||
259 | public function setMaxHeight($maxHeight) |
||
269 | |||
270 | /** |
||
271 | * @return float |
||
272 | */ |
||
273 | public function maxHeight() |
||
277 | |||
278 | /** |
||
279 | * @param string $gravity The resize gravity. |
||
280 | * @throws InvalidArgumentException If the argument is not a valid gravity name. |
||
281 | * @return AbstractWatermarkEffect Chainable |
||
282 | */ |
||
283 | public function setGravity($gravity) |
||
293 | |||
294 | /** |
||
295 | * @return string |
||
296 | */ |
||
297 | public function gravity() |
||
301 | |||
302 | /** |
||
303 | * @param string $color The resize background color. |
||
304 | * @throws InvalidArgumentException If the color argument is not a string. |
||
305 | * @return AbstractRotateEffect Chainable |
||
306 | */ |
||
307 | public function setBackgroundColor($color) |
||
317 | |||
318 | /** |
||
319 | * @return string |
||
320 | */ |
||
321 | public function backgroundColor() |
||
325 | |||
326 | |||
327 | /** |
||
328 | * @param boolean $adaptive The adaptative resize flag. |
||
329 | * @return AbstractRotateEffect Chainable |
||
330 | */ |
||
331 | public function setAdaptive($adaptive) |
||
336 | |||
337 | /** |
||
338 | * @return boolean |
||
339 | */ |
||
340 | public function adaptive() |
||
344 | |||
345 | /** |
||
346 | * @return string |
||
347 | */ |
||
348 | public function autoMode() |
||
367 | |||
368 | /** |
||
369 | * @param array $data The effect data, if available. |
||
370 | * @throws Exception If the effect data is invalid for its resize mode. |
||
371 | * @return AbstractResizeEffect Chainable |
||
372 | */ |
||
373 | public function process(array $data = null) |
||
478 | |||
479 | /** |
||
480 | * @param integer $width The target width. |
||
481 | * @param integer $height The target height. |
||
482 | * @param boolean $bestFit The "best_fit" flag. |
||
483 | * @return void |
||
484 | */ |
||
485 | abstract protected function doResize($width, $height, $bestFit = false); |
||
486 | } |
||
487 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.