Total Complexity | 54 |
Total Lines | 229 |
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 |
||
22 | trait FilesManagement |
||
23 | { |
||
24 | /** |
||
25 | * Function responsible for checking if a directory exists, we can also write in and create an index.php file |
||
26 | * |
||
27 | * @param string $folder The full path of the directory to check |
||
28 | * |
||
29 | * @return void |
||
30 | */ |
||
31 | public static function createFolder(string $folder): void |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * Copy a file to a folder |
||
48 | * |
||
49 | * @param string $file |
||
50 | * @param string $folder |
||
51 | * @return bool |
||
52 | */ |
||
53 | public static function copyFile(string $file, string $folder): bool |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Recursive copy from one location to another |
||
60 | * |
||
61 | * @param string $src |
||
62 | * @param string $dst |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | public static function recurseCopy(string $src, string $dst): void |
||
67 | { |
||
68 | $dir = \opendir($src); |
||
69 | // @mkdir($dst); |
||
70 | if (!@\mkdir($dst) && !\is_dir($dst)) { |
||
71 | throw new RuntimeException('The directory ' . $dst . ' could not be created.'); |
||
|
|||
72 | } |
||
73 | while (false !== ($file = \readdir($dir))) { |
||
74 | if (('.' !== $file) && ('..' !== $file)) { |
||
75 | if (\is_dir($src . '/' . $file)) { |
||
76 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
77 | } else { |
||
78 | \copy($src . '/' . $file, $dst . '/' . $file); |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | \closedir($dir); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Remove files and (sub)directories |
||
87 | * |
||
88 | * @param string $src source directory to delete |
||
89 | * |
||
90 | * @return bool true on success |
||
91 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
92 | * |
||
93 | * @uses \Xmf\Module\Helper::getHelper() |
||
94 | */ |
||
95 | public static function deleteDirectory(string $src): bool |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Recursively remove directory |
||
133 | * |
||
134 | * @TODO currently won't remove directories with hidden files, should it? |
||
135 | * |
||
136 | * @param string $src directory to remove (delete) |
||
137 | * |
||
138 | * @return bool true on success |
||
139 | */ |
||
140 | public static function rrmdir(string $src): bool |
||
141 | { |
||
142 | // Only continue if user is a 'global' Admin |
||
143 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
144 | return false; |
||
145 | } |
||
146 | |||
147 | // If source is not a directory stop processing |
||
148 | if (!\is_dir($src)) { |
||
149 | return false; |
||
150 | } |
||
151 | |||
152 | $success = true; |
||
153 | |||
154 | // Open the source directory to read in files |
||
155 | $iterator = new \DirectoryIterator($src); |
||
156 | foreach ($iterator as $fObj) { |
||
157 | if ($fObj->isFile()) { |
||
158 | $filename = $fObj->getPathname(); |
||
159 | $fObj = null; // clear this iterator object to close the file |
||
160 | if (!\unlink($filename)) { |
||
161 | return false; // couldn't delete the file |
||
162 | } |
||
163 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
164 | // Try recursively on directory |
||
165 | self::rrmdir($fObj->getPathname()); |
||
166 | } |
||
167 | } |
||
168 | $iterator = null; // clear iterator Obj to close file/directory |
||
169 | return \rmdir($src); // remove the directory & return results |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Recursively move files from one directory to another |
||
174 | * |
||
175 | * @param string $src - Source of files being moved |
||
176 | * @param string $dest - Destination of files being moved |
||
177 | * |
||
178 | * @return bool true on success |
||
179 | */ |
||
180 | public static function rmove(string $src, string $dest): bool |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Recursively copy directories and files from one directory to another |
||
214 | * |
||
215 | * @param string $src - Source of files being moved |
||
216 | * @param string $dest - Destination of files being moved |
||
217 | * |
||
218 | * @return bool true on success |
||
219 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
220 | * |
||
221 | * @uses \Xmf\Module\Helper::getHelper() |
||
222 | */ |
||
223 | public static function rcopy(string $src, string $dest): bool |
||
251 | } |
||
252 | } |
||
253 |