Total Complexity | 64 |
Total Lines | 247 |
Duplicated Lines | 0 % |
Changes | 0 |
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.
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 declare(strict_types=1); |
||
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 | public static function createFolder(string $folder): void |
||
28 | { |
||
29 | try { |
||
30 | if (!\is_dir($folder)) { |
||
31 | if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) { |
||
32 | throw new \RuntimeException(\sprintf('Unable to create the %s directory', $folder)); |
||
33 | } |
||
34 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
35 | } |
||
36 | } catch (\Throwable $e) { |
||
37 | echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
||
38 | } |
||
39 | } |
||
40 | |||
41 | public static function copyFile(string $file, string $folder): bool |
||
42 | { |
||
43 | return \copy($file, $folder); |
||
44 | } |
||
45 | |||
46 | public static function recurseCopy(string $src, string $dst): void |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Copy a file, or recursively copy a folder and its contents |
||
67 | * @param string $source Source path |
||
68 | * @param string $dest Destination path |
||
69 | * @return bool Returns true on success, false on failure |
||
70 | * @author Aidan Lister <[email protected]> |
||
71 | * @version 1.0.1 |
||
72 | * @link https://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
73 | */ |
||
74 | public static function xcopy($source, $dest): bool |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Remove files and (sub)directories |
||
111 | * |
||
112 | * @param string $src source directory to delete |
||
113 | * |
||
114 | * @return bool true on success |
||
115 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
116 | * |
||
117 | * @uses \Xmf\Module\Helper::getHelper() |
||
118 | */ |
||
119 | public static function deleteDirectory(string $src): bool |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Recursively remove directory |
||
156 | * |
||
157 | * @todo currently won't remove directories with hidden files, should it? |
||
158 | * |
||
159 | * @param string $src directory to remove (delete) |
||
160 | * |
||
161 | * @return bool true on success |
||
162 | */ |
||
163 | public static function rrmdir(string $src): bool |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Recursively move files from one directory to another |
||
195 | * |
||
196 | * @param string $src - Source of files being moved |
||
197 | * @param string $dest - Destination of files being moved |
||
198 | * |
||
199 | * @return bool true on success |
||
200 | */ |
||
201 | public static function rmove(string $src, string $dest): bool |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Recursively copy directories and files from one directory to another |
||
233 | * |
||
234 | * @param string $src - Source of files being moved |
||
235 | * @param string $dest - Destination of files being moved |
||
236 | * |
||
237 | * @return bool true on success |
||
238 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
239 | * |
||
240 | * @uses \Xmf\Module\Helper::getHelper() |
||
241 | */ |
||
242 | public static function rcopy(string $src, string $dest): bool |
||
269 |