Total Complexity | 54 |
Total Lines | 223 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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); |
||
25 | trait FilesManagement |
||
26 | { |
||
27 | /** |
||
28 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
29 | * |
||
30 | * @param string $folder The full path of the directory to check |
||
31 | */ |
||
32 | public static function createFolder($folder): void |
||
33 | { |
||
34 | try { |
||
35 | if (!\is_dir($folder)) { |
||
36 | if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) { |
||
37 | throw new RuntimeException(\sprintf('Unable to create the %s directory', $folder)); |
||
38 | } |
||
39 | |||
40 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
41 | } |
||
42 | } catch (\Throwable $e) { |
||
43 | echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
||
44 | } |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param string $file |
||
49 | * @param string $folder |
||
50 | * @return bool |
||
51 | */ |
||
52 | public static function copyFile(string $file, string $folder): bool |
||
53 | { |
||
54 | return \copy($file, $folder); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param $src |
||
59 | * @param $dst |
||
60 | */ |
||
61 | public static function recurseCopy($src, $dst): void |
||
62 | { |
||
63 | $dir = \opendir($src); |
||
64 | // @mkdir($dst); |
||
65 | if (!@\mkdir($dst) && !\is_dir($dst)) { |
||
66 | throw new RuntimeException('The directory ' . $dst . ' could not be created.'); |
||
67 | } |
||
68 | while (false !== ($file = \readdir($dir))) { |
||
69 | if (('.' !== $file) && ('..' !== $file)) { |
||
70 | if (\is_dir($src . '/' . $file)) { |
||
71 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
72 | } else { |
||
73 | \copy($src . '/' . $file, $dst . '/' . $file); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | \closedir($dir); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Remove files and (sub)directories |
||
82 | * |
||
83 | * @param string $src source directory to delete |
||
84 | * |
||
85 | * @return bool true on success |
||
86 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
87 | * |
||
88 | * @uses \Xmf\Module\Helper::getHelper() |
||
89 | */ |
||
90 | public static function deleteDirectory($src): bool |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Recursively remove directory |
||
128 | * |
||
129 | * @todo currently won't remove directories with hidden files, should it? |
||
130 | * |
||
131 | * @param string $src directory to remove (delete) |
||
132 | * |
||
133 | * @return bool true on success |
||
134 | */ |
||
135 | public static function rrmdir($src): bool |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Recursively move files from one directory to another |
||
170 | * |
||
171 | * @param string $src - Source of files being moved |
||
172 | * @param string $dest - Destination of files being moved |
||
173 | * |
||
174 | * @return bool true on success |
||
175 | */ |
||
176 | public static function rmove($src, $dest): bool |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Recursively copy directories and files from one directory to another |
||
211 | * |
||
212 | * @param string $src - Source of files being moved |
||
213 | * @param string $dest - Destination of files being moved |
||
214 | * |
||
215 | * @return bool true on success |
||
216 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
217 | * |
||
218 | * @uses \Xmf\Module\Helper::getHelper() |
||
219 | */ |
||
220 | public static function rcopy($src, $dest): bool |
||
250 |