Total Complexity | 54 |
Total Lines | 212 |
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 |
||
22 | trait FilesManagement |
||
23 | { |
||
24 | /** |
||
25 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
26 | * |
||
27 | * @param string $folder The full path of the directory to check |
||
28 | */ |
||
29 | public static function createFolder(string $folder): void |
||
30 | { |
||
31 | try { |
||
32 | if (!\is_dir($folder)) { |
||
33 | if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) { |
||
34 | throw new \RuntimeException(\sprintf('Unable to create the %s directory', $folder)); |
||
35 | } |
||
36 | |||
37 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
38 | } |
||
39 | } catch (\Exception $e) { |
||
40 | echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
||
41 | } |
||
42 | } |
||
43 | |||
44 | public static function copyFile(string $file, string $folder): bool |
||
45 | { |
||
46 | return \copy($file, $folder); |
||
47 | } |
||
48 | |||
49 | public static function recurseCopy(string $src, string $dst): void |
||
50 | { |
||
51 | $dir = \opendir($src); |
||
52 | // @mkdir($dst); |
||
53 | if (!@\mkdir($dst) && !\is_dir($dst)) { |
||
54 | throw new \RuntimeException('The directory ' . $dst . ' could not be created.'); |
||
55 | } |
||
56 | while (false !== ($file = \readdir($dir))) { |
||
57 | if (('.' !== $file) && ('..' !== $file)) { |
||
58 | if (\is_dir($src . '/' . $file)) { |
||
59 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
60 | } else { |
||
61 | \copy($src . '/' . $file, $dst . '/' . $file); |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | \closedir($dir); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Remove files and (sub)directories |
||
70 | * |
||
71 | * @param string $src source directory to delete |
||
72 | * |
||
73 | * @return bool true on success |
||
74 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
75 | * |
||
76 | * @uses \Xmf\Module\Helper::getHelper() |
||
77 | */ |
||
78 | public static function deleteDirectory(string $src): bool |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Recursively remove directory |
||
116 | * |
||
117 | * @todo currently won't remove directories with hidden files, should it? |
||
118 | * |
||
119 | * @param string $src directory to remove (delete) |
||
120 | * |
||
121 | * @return bool true on success |
||
122 | */ |
||
123 | public static function rrmdir(string $src): bool |
||
124 | { |
||
125 | // Only continue if user is a 'global' Admin |
||
126 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
127 | return false; |
||
128 | } |
||
129 | |||
130 | // If source is not a directory stop processing |
||
131 | if (!\is_dir($src)) { |
||
132 | return false; |
||
133 | } |
||
134 | |||
135 | $success = true; |
||
|
|||
136 | |||
137 | // Open the source directory to read in files |
||
138 | $iterator = new \DirectoryIterator($src); |
||
139 | foreach ($iterator as $fObj) { |
||
140 | if ($fObj->isFile()) { |
||
141 | $filename = $fObj->getPathname(); |
||
142 | $fObj = null; // clear this iterator object to close the file |
||
143 | if (!\unlink($filename)) { |
||
144 | return false; // couldn't delete the file |
||
145 | } |
||
146 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
147 | // Try recursively on directory |
||
148 | self::rrmdir($fObj->getPathname()); |
||
149 | } |
||
150 | } |
||
151 | $iterator = null; // clear iterator Obj to close file/directory |
||
152 | return \rmdir($src); // remove the directory & return results |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Recursively move files from one directory to another |
||
157 | * |
||
158 | * @param string $src - Source of files being moved |
||
159 | * @param string $dest - Destination of files being moved |
||
160 | * |
||
161 | * @return bool true on success |
||
162 | */ |
||
163 | public static function rmove(string $src, string $dest): bool |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Recursively copy directories and files from one directory to another |
||
197 | * |
||
198 | * @param string $src - Source of files being moved |
||
199 | * @param string $dest - Destination of files being moved |
||
200 | * |
||
201 | * @return bool true on success |
||
202 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
203 | * |
||
204 | * @uses \Xmf\Module\Helper::getHelper() |
||
205 | */ |
||
206 | public static function rcopy(string $src, string $dest): bool |
||
234 | } |
||
235 | } |
||
236 |