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 XfguestbookUtil 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 XfguestbookUtil, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class XfguestbookUtil extends XoopsObject |
||
|
|||
7 | { |
||
8 | /** |
||
9 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
10 | * |
||
11 | * @param string $folder The full path of the directory to check |
||
12 | * |
||
13 | * @return void |
||
14 | */ |
||
15 | public static function createFolder($folder) |
||
29 | |||
30 | /** |
||
31 | * @param $file |
||
32 | * @param $folder |
||
33 | * @return bool |
||
34 | */ |
||
35 | public static function copyFile($file, $folder) |
||
49 | |||
50 | /** |
||
51 | * @param $src |
||
52 | * @param $dst |
||
53 | */ |
||
54 | public static function recurseCopy($src, $dst) |
||
69 | |||
70 | /** |
||
71 | * |
||
72 | * Verifies XOOPS version meets minimum requirements for this module |
||
73 | * @static |
||
74 | * @param XoopsModule $module |
||
75 | * |
||
76 | * @return bool true if meets requirements, false if not |
||
77 | */ |
||
78 | public static function checkVerXoops(XoopsModule $module) |
||
111 | |||
112 | /** |
||
113 | * |
||
114 | * Verifies PHP version meets minimum requirements for this module |
||
115 | * @static |
||
116 | * @param XoopsModule $module |
||
117 | * |
||
118 | * @return bool true if meets requirements, false if not |
||
119 | */ |
||
120 | public static function checkVerPhp(XoopsModule $module) |
||
136 | |||
137 | public static function upload() |
||
167 | |||
168 | /** |
||
169 | * @param null $criteria |
||
170 | * @param int $limit |
||
171 | * @param int $start |
||
172 | * @return array |
||
173 | */ |
||
174 | View Code Duplication | public static function getCountry($criteria = null, $limit = 0, $start = 0) |
|
190 | |||
191 | /** |
||
192 | * @param null $criteria |
||
193 | * @param int $limit |
||
194 | * @param int $start |
||
195 | * @return array |
||
196 | */ |
||
197 | public static function getAllCountry($criteria = null, $limit = 0, $start = 0) |
||
214 | |||
215 | /** |
||
216 | * @param $user_id |
||
217 | * @return bool |
||
218 | */ |
||
219 | public static function get_user_data($user_id) |
||
246 | |||
247 | // Effacement fichiers temporaires |
||
248 | /** |
||
249 | * @param $dir_path |
||
250 | * @param string $prefix |
||
251 | * @return int |
||
252 | */ |
||
253 | public static function clear_tmp_files($dir_path, $prefix = 'tmp_') |
||
272 | |||
273 | // IP bannies (modérés automatiquement) |
||
274 | /** |
||
275 | * @param null $all |
||
276 | * @return array |
||
277 | */ |
||
278 | public static function get_badips($all = null) |
||
296 | |||
297 | /** |
||
298 | * @param $email |
||
299 | * @return bool |
||
300 | */ |
||
301 | public static function email_exist($email) |
||
311 | |||
312 | } |
||
313 |
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.