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 |
||
20 | trait FilesManagement |
||
21 | { |
||
22 | /** |
||
23 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
24 | * |
||
25 | * @param string $folder The full path of the directory to check |
||
26 | * |
||
27 | * @return void |
||
28 | * @throws \RuntimeException |
||
29 | */ |
||
30 | public static function createFolder($folder) |
||
44 | |||
45 | /** |
||
46 | * @param $file |
||
47 | * @param $folder |
||
48 | * @return bool |
||
49 | */ |
||
50 | public static function copyFile($file, $folder) |
||
54 | |||
55 | /** |
||
56 | * @param $src |
||
57 | * @param $dst |
||
58 | */ |
||
59 | public static function recurseCopy($src, $dst) |
||
77 | |||
78 | /** |
||
79 | * Copy a file, or recursively copy a folder and its contents |
||
80 | * @param string $source Source path |
||
81 | * @param string $dest Destination path |
||
82 | * @return bool Returns true on success, false on failure |
||
83 | * @author Aidan Lister <[email protected]> |
||
84 | * @version 1.0.1 |
||
85 | * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
86 | */ |
||
87 | public static function xcopy($source, $dest) |
||
123 | |||
124 | /** |
||
125 | * Remove files and (sub)directories |
||
126 | * |
||
127 | * @param string $src source directory to delete |
||
128 | * |
||
129 | * @return bool true on success |
||
130 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
131 | * |
||
132 | * @uses \Xmf\Module\Helper::getHelper() |
||
133 | */ |
||
134 | View Code Duplication | public static function deleteDirectory($src) |
|
169 | |||
170 | /** |
||
171 | * Recursively remove directory |
||
172 | * |
||
173 | * @todo currently won't remove directories with hidden files, should it? |
||
174 | * |
||
175 | * @param string $src directory to remove (delete) |
||
176 | * |
||
177 | * @return bool true on success |
||
178 | */ |
||
179 | View Code Duplication | public static function rrmdir($src) |
|
210 | |||
211 | /** |
||
212 | * Recursively move files from one directory to another |
||
213 | * |
||
214 | * @param string $src - Source of files being moved |
||
215 | * @param string $dest - Destination of files being moved |
||
216 | * |
||
217 | * @return bool true on success |
||
218 | */ |
||
219 | View Code Duplication | public static function rmove($src, $dest) |
|
250 | |||
251 | /** |
||
252 | * Recursively copy directories and files from one directory to another |
||
253 | * |
||
254 | * @param string $src - Source of files being moved |
||
255 | * @param string $dest - Destination of files being moved |
||
256 | * |
||
257 | * @return bool true on success |
||
258 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
259 | * |
||
260 | * @uses \Xmf\Module\Helper::getHelper() |
||
261 | */ |
||
262 | View Code Duplication | public static function rcopy($src, $dest) |
|
291 | } |
||
292 |
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.