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 Utility 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 Utility, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Utility |
||
38 | { |
||
39 | use Common\VersionChecks; //checkVerXoops, checkVerPhp Traits |
||
40 | |||
41 | use Common\ServerStats; // getServerStats Trait |
||
42 | |||
43 | use Common\FilesManagement; // Files Management Trait |
||
44 | |||
45 | /** |
||
46 | * |
||
47 | * Verifies XOOPS version meets minimum requirements for this module |
||
48 | * @static |
||
49 | * @param \XoopsModule $module |
||
50 | * |
||
51 | * @return bool true if meets requirements, false if not |
||
52 | */ |
||
53 | public static function checkVerXoops($module) |
||
66 | |||
67 | /** |
||
68 | * |
||
69 | * Verifies PHP version meets minimum requirements for this module |
||
70 | * @static |
||
71 | * @param \XoopsModule $module |
||
72 | * |
||
73 | * @return bool true if meets requirements, false if not |
||
74 | */ |
||
75 | public static function checkVerPHP($module) |
||
90 | |||
91 | /** |
||
92 | * |
||
93 | * Remove files and (sub)directories |
||
94 | * |
||
95 | * @param string $src source directory to delete |
||
96 | * |
||
97 | * @return bool true on success |
||
98 | * @see \XoopsModules\Xoopsfaq\Helper::isUserAdmin() |
||
99 | * |
||
100 | * @see \XoopsModules\Xoopsfaq\Helper::getHelper() |
||
101 | */ |
||
102 | View Code Duplication | public static function deleteDirectory($src) |
|
136 | |||
137 | /** |
||
138 | * |
||
139 | * Recursively remove directory |
||
140 | * |
||
141 | * @todo currently won't remove directories with hidden files, should it? |
||
142 | * |
||
143 | * @param string $src directory to remove (delete) |
||
144 | * |
||
145 | * @return bool true on success |
||
146 | */ |
||
147 | View Code Duplication | public static function rrmdir($src) |
|
176 | |||
177 | /** |
||
178 | * Recursively move files from one directory to another |
||
179 | * |
||
180 | * @param String $src - Source of files being moved |
||
181 | * @param String $dest - Destination of files being moved |
||
182 | * |
||
183 | * @return bool true on success |
||
184 | */ |
||
185 | View Code Duplication | public static function rmove($src, $dest) |
|
215 | |||
216 | /** |
||
217 | * Recursively copy directories and files from one directory to another |
||
218 | * |
||
219 | * @param string $src - Source of files being moved |
||
220 | * @param string $dest - Destination of files being moved |
||
221 | * |
||
222 | * @return bool true on success |
||
223 | * @see \XoopsModules\Xoopsfaq\Helper::isUserAdmin() |
||
224 | * |
||
225 | * @see \XoopsModules\Xoopsfaq\Helper::getHelper() |
||
226 | */ |
||
227 | View Code Duplication | public static function rcopy($src, $dest) |
|
255 | |||
256 | /** |
||
257 | * Render the icon links |
||
258 | * |
||
259 | * @param array $icon_array contains operation=>icon_name as key=>value |
||
260 | * @param mixed $param HTML parameter |
||
261 | * @param mixed $value HTML parameter value to set |
||
262 | * @param mixed $extra are any additional HTML attributes desired for the <a> tag |
||
263 | * @return string |
||
264 | */ |
||
265 | public static function renderIconLinks($icon_array = [], $param, $value = null, $extra = null) |
||
290 | } |
||
291 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.