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 |
||
17 | trait FilesManagement |
||
|
|||
18 | { |
||
19 | /** |
||
20 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
21 | * |
||
22 | * @param string $folder The full path of the directory to check |
||
23 | * |
||
24 | * @return void |
||
25 | */ |
||
26 | public static function createFolder($folder) |
||
41 | |||
42 | /** |
||
43 | * @param $file |
||
44 | * @param $folder |
||
45 | * @return bool |
||
46 | */ |
||
47 | public static function copyFile($file, $folder) |
||
51 | |||
52 | /** |
||
53 | * @param $src |
||
54 | * @param $dst |
||
55 | */ |
||
56 | public static function recurseCopy($src, $dst) |
||
71 | |||
72 | /** |
||
73 | * |
||
74 | * Remove files and (sub)directories |
||
75 | * |
||
76 | * @param string $src source directory to delete |
||
77 | * |
||
78 | * @uses \Xmf\Module\Helper::getHelper() |
||
79 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
80 | * |
||
81 | * @return bool true on success |
||
82 | */ |
||
83 | public static function deleteDirectory($src) |
||
120 | |||
121 | /** |
||
122 | * |
||
123 | * Recursively remove directory |
||
124 | * |
||
125 | * @todo currently won't remove directories with hidden files, should it? |
||
126 | * |
||
127 | * @param string $src directory to remove (delete) |
||
128 | * |
||
129 | * @return bool true on success |
||
130 | */ |
||
131 | public static function rrmdir($src) |
||
162 | |||
163 | /** |
||
164 | * Recursively move files from one directory to another |
||
165 | * |
||
166 | * @param string $src - Source of files being moved |
||
167 | * @param string $dest - Destination of files being moved |
||
168 | * |
||
169 | * @return bool true on success |
||
170 | */ |
||
171 | View Code Duplication | public static function rmove($src, $dest) |
|
202 | |||
203 | /** |
||
204 | * Recursively copy directories and files from one directory to another |
||
205 | * |
||
206 | * @param string $src - Source of files being moved |
||
207 | * @param string $dest - Destination of files being moved |
||
208 | * |
||
209 | * @uses \Xmf\Module\Helper::getHelper() |
||
210 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
211 | * |
||
212 | * @return bool true on success |
||
213 | */ |
||
214 | View Code Duplication | public static function rcopy($src, $dest) |
|
242 | } |
||
243 |
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.