| Total Complexity | 55 |
| Total Lines | 218 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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); |
||
| 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 | final public static function createFolder(string $folder): void |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | final public static function copyFile(string $file, string $folder): bool |
||
| 45 | } |
||
| 46 | |||
| 47 | final public static function recurseCopy(string $src, string $dst): void |
||
| 48 | {
|
||
| 49 | $dir = \opendir($src); |
||
| 50 | // @mkdir($dst); |
||
| 51 | try {
|
||
| 52 | if (!\mkdir($dst) && !\is_dir($dst)) {
|
||
| 53 | throw new \RuntimeException('The directory ' . $dst . ' could not be created.');
|
||
| 54 | } |
||
| 55 | } catch (\RuntimeException $e) {
|
||
| 56 | echo 'Caught exception: ', $e->getMessage(), "<br>\n"; |
||
| 57 | } |
||
| 58 | while (false !== ($file = \readdir($dir))) {
|
||
| 59 | if (('.' !== $file) && ('..' !== $file)) {
|
||
| 60 | if (\is_dir($src . '/' . $file)) {
|
||
| 61 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
| 62 | } else {
|
||
| 63 | \copy($src . '/' . $file, $dst . '/' . $file); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | \closedir($dir); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Remove files and (sub)directories |
||
| 72 | * |
||
| 73 | * @param string $src source directory to delete |
||
| 74 | * |
||
| 75 | * @return bool true on success |
||
| 76 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 77 | * |
||
| 78 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 79 | */ |
||
| 80 | final public static function deleteDirectory(string $src): bool |
||
| 81 | {
|
||
| 82 | // Only continue if user is a 'global' Admin |
||
| 83 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) {
|
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 87 | $success = true; |
||
| 88 | // remove old files |
||
| 89 | $dirInfo = new \SplFileInfo($src); |
||
| 90 | // validate is a directory |
||
| 91 | if ($dirInfo->isDir()) {
|
||
| 92 | $fileList = \array_diff(\scandir($src, \SCANDIR_SORT_NONE), ['..', '.']); |
||
| 93 | foreach ($fileList as $k => $v) {
|
||
| 94 | $fileInfo = new \SplFileInfo("{$src}/{$v}");
|
||
| 95 | if ($fileInfo->isDir()) {
|
||
| 96 | // recursively handle subdirectories |
||
| 97 | if (!$success = self::deleteDirectory($fileInfo->getRealPath())) {
|
||
| 98 | break; |
||
| 99 | } |
||
| 100 | } elseif (!($success = \unlink($fileInfo->getRealPath()))) {
|
||
| 101 | break; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | // now delete this (sub)directory if all the files are gone |
||
| 105 | if ($success) {
|
||
| 106 | $success = \rmdir($dirInfo->getRealPath()); |
||
| 107 | } |
||
| 108 | } else {
|
||
| 109 | // input is not a valid directory |
||
| 110 | $success = false; |
||
| 111 | } |
||
| 112 | |||
| 113 | return $success; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Recursively remove directory |
||
| 118 | * |
||
| 119 | * @todo currently won't remove directories with hidden files, should it? |
||
| 120 | * |
||
| 121 | * @param string $src directory to remove (delete) |
||
| 122 | * |
||
| 123 | * @return bool true on success |
||
| 124 | */ |
||
| 125 | final public static function rrmdir(string $src): bool |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Recursively move files from one directory to another |
||
| 160 | * |
||
| 161 | * @param string $src - Source of files being moved |
||
| 162 | * @param string $dest - Destination of files being moved |
||
| 163 | * |
||
| 164 | * @return bool true on success |
||
| 165 | */ |
||
| 166 | final public static function rmove(string $src, string $dest): bool |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Recursively copy directories and files from one directory to another |
||
| 201 | * |
||
| 202 | * @param string $src - Source of files being moved |
||
| 203 | * @param string $dest - Destination of files being moved |
||
| 204 | * |
||
| 205 | * @return bool true on success |
||
| 206 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 207 | * |
||
| 208 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 209 | */ |
||
| 210 | final public function rcopy(string $src, string $dest): bool |
||
| 238 | } |
||
| 239 | } |
||
| 240 |