Total Complexity | 64 |
Total Lines | 269 |
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 |
||
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 | * @throws \RuntimeException |
||
28 | */ |
||
29 | public static function createFolder($folder) |
||
41 | } |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param $file |
||
46 | * @param $folder |
||
47 | * @return bool |
||
48 | */ |
||
49 | public static function copyFile($file, $folder) |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param $src |
||
56 | * @param $dst |
||
57 | */ |
||
58 | public static function recurseCopy($src, $dst) |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Copy a file, or recursively copy a folder and its contents |
||
79 | * @param string $source Source path |
||
80 | * @param string $dest Destination path |
||
81 | * @return bool Returns true on success, false on failure |
||
82 | * @author Aidan Lister <[email protected]> |
||
83 | * @version 1.0.1 |
||
84 | * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
85 | */ |
||
86 | public static function xcopy($source, $dest) |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Remove files and (sub)directories |
||
125 | * |
||
126 | * @param string $src source directory to delete |
||
127 | * |
||
128 | * @return bool true on success |
||
129 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
130 | * |
||
131 | * @uses \Xmf\Module\Helper::getHelper() |
||
132 | */ |
||
133 | public static function deleteDirectory($src) |
||
134 | { |
||
135 | // Only continue if user is a 'global' Admin |
||
136 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
137 | return false; |
||
138 | } |
||
139 | |||
140 | $success = true; |
||
141 | // remove old files |
||
142 | $dirInfo = new \SplFileInfo($src); |
||
143 | // validate is a directory |
||
144 | if ($dirInfo->isDir()) { |
||
145 | $fileList = \array_diff(\scandir($src, \SCANDIR_SORT_NONE), ['..', '.']); |
||
146 | foreach ($fileList as $k => $v) { |
||
147 | $fileInfo = new \SplFileInfo("{$src}/{$v}"); |
||
148 | if ($fileInfo->isDir()) { |
||
149 | // recursively handle subdirectories |
||
150 | if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
||
151 | break; |
||
152 | } |
||
153 | } elseif (!($success = \unlink($fileInfo->getRealPath()))) { |
||
154 | break; |
||
155 | } |
||
156 | } |
||
157 | // now delete this (sub)directory if all the files are gone |
||
158 | if ($success) { |
||
159 | $success = \rmdir($dirInfo->getRealPath()); |
||
160 | } |
||
161 | } else { |
||
162 | // input is not a valid directory |
||
163 | $success = false; |
||
164 | } |
||
165 | |||
166 | return $success; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Recursively remove directory |
||
171 | * |
||
172 | * @todo currently won't remove directories with hidden files, should it? |
||
173 | * |
||
174 | * @param string $src directory to remove (delete) |
||
175 | * |
||
176 | * @return bool true on success |
||
177 | */ |
||
178 | public static function rrmdir($src) |
||
179 | { |
||
180 | // Only continue if user is a 'global' Admin |
||
181 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
182 | return false; |
||
183 | } |
||
184 | |||
185 | // If source is not a directory stop processing |
||
186 | if (!\is_dir($src)) { |
||
187 | return false; |
||
188 | } |
||
189 | |||
190 | $success = true; |
||
191 | |||
192 | // Open the source directory to read in files |
||
193 | $iterator = new \DirectoryIterator($src); |
||
194 | foreach ($iterator as $fObj) { |
||
195 | if ($fObj->isFile()) { |
||
196 | $filename = $fObj->getPathname(); |
||
197 | $fObj = null; // clear this iterator object to close the file |
||
198 | if (!\unlink($filename)) { |
||
199 | return false; // couldn't delete the file |
||
200 | } |
||
201 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
202 | // Try recursively on directory |
||
203 | self::rrmdir($fObj->getPathname()); |
||
204 | } |
||
205 | } |
||
206 | $iterator = null; // clear iterator Obj to close file/directory |
||
207 | return \rmdir($src); // remove the directory & return results |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Recursively move files from one directory to another |
||
212 | * |
||
213 | * @param string $src - Source of files being moved |
||
214 | * @param string $dest - Destination of files being moved |
||
215 | * |
||
216 | * @return bool true on success |
||
217 | */ |
||
218 | public static function rmove($src, $dest) |
||
219 | { |
||
220 | // Only continue if user is a 'global' Admin |
||
221 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
222 | return false; |
||
223 | } |
||
224 | |||
225 | // If source is not a directory stop processing |
||
226 | if (!\is_dir($src)) { |
||
227 | return false; |
||
228 | } |
||
229 | |||
230 | // If the destination directory does not exist and could not be created stop processing |
||
231 | if (!\is_dir($dest) && !\mkdir($dest) && !\is_dir($dest)) { |
||
232 | return false; |
||
233 | } |
||
234 | |||
235 | // Open the source directory to read in files |
||
236 | $iterator = new \DirectoryIterator($src); |
||
237 | foreach ($iterator as $fObj) { |
||
238 | if ($fObj->isFile()) { |
||
239 | \rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
240 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
241 | // Try recursively on directory |
||
242 | self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
243 | // rmdir($fObj->getPath()); // now delete the directory |
||
244 | } |
||
245 | } |
||
246 | $iterator = null; // clear iterator Obj to close file/directory |
||
247 | return \rmdir($src); // remove the directory & return results |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Recursively copy directories and files from one directory to another |
||
252 | * |
||
253 | * @param string $src - Source of files being moved |
||
254 | * @param string $dest - Destination of files being moved |
||
255 | * |
||
256 | * @return bool true on success |
||
257 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
258 | * |
||
259 | * @uses \Xmf\Module\Helper::getHelper() |
||
260 | */ |
||
261 | public static function rcopy($src, $dest) |
||
289 | } |
||
290 | } |
||
291 |