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 FilesManagement 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 FilesManagement, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Xoopsmodules\randomquote\common; |
||
23 | trait FilesManagement |
||
24 | { |
||
25 | /** |
||
26 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
27 | * |
||
28 | * @param string $folder The full path of the directory to check |
||
29 | * |
||
30 | * @return void |
||
31 | * @throws \RuntimeException |
||
32 | */ |
||
33 | public static function createFolder($folder) |
||
48 | |||
49 | /** |
||
50 | * @param $file |
||
51 | * @param $folder |
||
52 | * @return bool |
||
53 | */ |
||
54 | public static function copyFile($file, $folder) |
||
58 | |||
59 | /** |
||
60 | * @param $src |
||
61 | * @param $dst |
||
62 | */ |
||
63 | public static function recurseCopy($src, $dst) |
||
78 | |||
79 | /** |
||
80 | * Copy a file, or recursively copy a folder and its contents |
||
81 | * @author Aidan Lister <[email protected]> |
||
82 | * @version 1.0.1 |
||
83 | * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
84 | * @param string $source Source path |
||
85 | * @param string $dest Destination path |
||
86 | * @return bool Returns true on success, false on failure |
||
87 | */ |
||
88 | public static function xcopy($source, $dest) |
||
121 | |||
122 | /** |
||
123 | * |
||
124 | * Remove files and (sub)directories |
||
125 | * |
||
126 | * @param string $src source directory to delete |
||
127 | * |
||
128 | * @uses \Xmf\Module\Helper::getHelper() |
||
129 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
130 | * |
||
131 | * @return bool true on success |
||
132 | */ |
||
133 | public static function deleteDirectory($src) |
||
170 | |||
171 | /** |
||
172 | * |
||
173 | * Recursively remove directory |
||
174 | * |
||
175 | * @todo currently won't remove directories with hidden files, should it? |
||
176 | * |
||
177 | * @param string $src directory to remove (delete) |
||
178 | * |
||
179 | * @return bool true on success |
||
180 | */ |
||
181 | public static function rrmdir($src) |
||
212 | |||
213 | /** |
||
214 | * Recursively move files from one directory to another |
||
215 | * |
||
216 | * @param string $src - Source of files being moved |
||
217 | * @param string $dest - Destination of files being moved |
||
218 | * |
||
219 | * @return bool true on success |
||
220 | */ |
||
221 | View Code Duplication | public static function rmove($src, $dest) |
|
252 | |||
253 | /** |
||
254 | * Recursively copy directories and files from one directory to another |
||
255 | * |
||
256 | * @param string $src - Source of files being moved |
||
257 | * @param string $dest - Destination of files being moved |
||
258 | * |
||
259 | * @uses \Xmf\Module\Helper::getHelper() |
||
260 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
261 | * |
||
262 | * @return bool true on success |
||
263 | */ |
||
264 | View Code Duplication | public static function rcopy($src, $dest) |
|
292 | } |
||
293 |
If you suppress an error, we recommend checking for the error condition explicitly: