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 XtubeThumbsNails 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 XtubeThumbsNails, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class XtubeThumbsNails |
||
|
|||
32 | { |
||
33 | public $_imgName = 'blank.gif'; |
||
34 | public $_img_path = 'uploads'; |
||
35 | public $_img_savepath = 'thumbs'; |
||
36 | |||
37 | public $_source_path = ''; |
||
38 | public $_save_path = ''; |
||
39 | public $_source_url = ''; |
||
40 | public $_source_image = ''; |
||
41 | public $_save_image = ''; |
||
42 | |||
43 | public $_usethumbs = 0; |
||
44 | public $_image_type = 'gd2'; |
||
45 | public $_return_fullpath = 0; |
||
46 | |||
47 | public $img_width = 100; |
||
48 | public $img_height = 100; |
||
49 | public $img_quality = 100; |
||
50 | public $img_update = 1; |
||
51 | public $img_aspect = 1; |
||
52 | |||
53 | // @access private |
||
54 | public $_img_info = array(); |
||
55 | |||
56 | /** |
||
57 | * Constructor |
||
58 | * |
||
59 | * @param null $img_name |
||
60 | * @param null $img_path |
||
61 | * @param null $img_savepath |
||
62 | * |
||
63 | * @internal param string $_imgName |
||
64 | * @internal param string $_img_path |
||
65 | * @internal param string $_img_savepath |
||
66 | */ |
||
67 | public function __construct($img_name = null, $img_path = null, $img_savepath = null) |
||
98 | |||
99 | /** |
||
100 | * wfThumbsNails::setUseThumbs() |
||
101 | * |
||
102 | * @param integer $value |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | public function setUseThumbs($value = 1) |
||
110 | |||
111 | /** |
||
112 | * XtubeThumbsNails::setImageType() |
||
113 | * |
||
114 | * @param string $value |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | public function setImageType($value = 'gd2') |
||
122 | |||
123 | /** |
||
124 | * ThumbsNails::createThumbnail() |
||
125 | * |
||
126 | * @param int $img_width |
||
127 | * @param int $img_height |
||
128 | * @param int $img_quality |
||
129 | * @param int $img_update |
||
130 | * @param int $img_aspect |
||
131 | * |
||
132 | * @return bool|string |
||
133 | */ |
||
134 | public function createThumbnail($img_width = null, $img_height = null, $img_quality = null, $img_update = null, $img_aspect = null) |
||
182 | |||
183 | /** |
||
184 | * @param $value |
||
185 | */ |
||
186 | public function setImageName($value) |
||
190 | |||
191 | /** |
||
192 | * @param $value |
||
193 | */ |
||
194 | public function setImagePath($value) |
||
198 | |||
199 | /** |
||
200 | * @param $value |
||
201 | */ |
||
202 | public function setImgSavePath($value) |
||
206 | |||
207 | // ThumbsNails::resizeThumbnail() |
||
208 | // @return |
||
209 | /** |
||
210 | * @return bool|string |
||
211 | */ |
||
212 | public function resizeThumbnail() |
||
312 | |||
313 | // ThumbsNails::checkPaths() |
||
314 | // @return |
||
315 | /** |
||
316 | * @return bool |
||
317 | */ |
||
318 | public function checkPaths() |
||
329 | |||
330 | /** |
||
331 | * @return bool |
||
332 | */ |
||
333 | public function checkImage() |
||
342 | |||
343 | // wfsThumbs::checkGdLibrary() |
||
344 | // Private function |
||
345 | // @return false if gd lib not found on the system |
||
346 | /** |
||
347 | * @return array|bool |
||
348 | */ |
||
349 | public function checkGdLibrary() |
||
361 | |||
362 | // ThumbsNails::useThumbs() |
||
363 | // |
||
364 | // @return |
||
365 | /** |
||
366 | * @return bool |
||
367 | */ |
||
368 | public function useThumbs() |
||
376 | } |
||
377 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.