Total Complexity | 64 |
Total Lines | 273 |
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 |
||
20 | trait FilesManagement |
||
21 | { |
||
22 | /** |
||
23 | * Function responsible for checking if a directory exists, we can also write in and create an index.php 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) |
||
31 | { |
||
32 | try { |
||
33 | if (!\file_exists($folder)) { |
||
34 | if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) { |
||
35 | throw new \RuntimeException(\sprintf('Unable to create the %s directory', $folder)); |
||
36 | } |
||
37 | |||
38 | \file_put_contents($folder . '/index.php', "<?php\nheader('HTTP/1.0 404 Not Found');"); |
||
39 | } |
||
40 | } catch (\Exception $e) { |
||
41 | echo 'Caught exception: ', $e->getMessage(), '<br>'; |
||
42 | } |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param $file |
||
47 | * @param $folder |
||
48 | * @return bool |
||
49 | */ |
||
50 | public static function copyFile($file, $folder) |
||
51 | { |
||
52 | return \copy($file, $folder); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param $src |
||
57 | * @param $dst |
||
58 | */ |
||
59 | public static function recurseCopy($src, $dst) |
||
76 | } |
||
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) |
||
122 | } |
||
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 | public static function deleteDirectory($src) |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Recursively remove directory |
||
175 | * |
||
176 | * @todo currently won't remove directories with hidden files, should it? |
||
177 | * |
||
178 | * @param string $src directory to remove (delete) |
||
179 | * |
||
180 | * @return bool true on success |
||
181 | */ |
||
182 | public static function rrmdir($src) |
||
183 | { |
||
184 | // Only continue if user is a 'global' Admin |
||
185 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
186 | return false; |
||
187 | } |
||
188 | |||
189 | // If source is not a directory stop processing |
||
190 | if (!\is_dir($src)) { |
||
191 | return false; |
||
192 | } |
||
193 | |||
194 | $success = true; |
||
195 | |||
196 | // Open the source directory to read in files |
||
197 | $iterator = new \DirectoryIterator($src); |
||
198 | foreach ($iterator as $fObj) { |
||
199 | if ($fObj->isFile()) { |
||
200 | $filename = $fObj->getPathname(); |
||
201 | $fObj = null; // clear this iterator object to close the file |
||
202 | if (!\unlink($filename)) { |
||
203 | return false; // couldn't delete the file |
||
204 | } |
||
205 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
206 | // Try recursively on directory |
||
207 | self::rrmdir($fObj->getPathname()); |
||
208 | } |
||
209 | } |
||
210 | $iterator = null; // clear iterator Obj to close file/directory |
||
211 | return \rmdir($src); // remove the directory & return results |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Recursively move files from one directory to another |
||
216 | * |
||
217 | * @param string $src - Source of files being moved |
||
218 | * @param string $dest - Destination of files being moved |
||
219 | * |
||
220 | * @return bool true on success |
||
221 | */ |
||
222 | public static function rmove($src, $dest) |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Recursively copy directories and files from one directory to another |
||
256 | * |
||
257 | * @param string $src - Source of files being moved |
||
258 | * @param string $dest - Destination of files being moved |
||
259 | * |
||
260 | * @return bool true on success |
||
261 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
262 | * |
||
263 | * @uses \Xmf\Module\Helper::getHelper() |
||
264 | */ |
||
265 | public static function rcopy($src, $dest) |
||
295 |