| Total Complexity | 54 |
| Total Lines | 223 |
| 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 declare(strict_types=1); |
||
| 26 | trait FilesManagement |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
| 30 | * |
||
| 31 | * @param string $folder The full path of the directory to check |
||
| 32 | */ |
||
| 33 | public static function createFolder($folder): void |
||
| 34 | { |
||
| 35 | try { |
||
| 36 | if (!\is_dir($folder)) { |
||
| 37 | if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) { |
||
| 38 | throw new RuntimeException(\sprintf('Unable to create the %s directory', $folder)); |
||
| 39 | } |
||
| 40 | |||
| 41 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
| 42 | } |
||
| 43 | } catch (Exception $e) { |
||
| 44 | echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param $file |
||
| 50 | * @param $folder |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | public static function copyFile(string $file, string $folder): bool |
||
| 54 | { |
||
| 55 | return \copy($file, $folder); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param $src |
||
| 60 | * @param $dst |
||
| 61 | */ |
||
| 62 | public static function recurseCopy($src, $dst): void |
||
| 63 | { |
||
| 64 | $dir = \opendir($src); |
||
| 65 | // @mkdir($dst); |
||
| 66 | if (!@\mkdir($dst) && !\is_dir($dst)) { |
||
| 67 | throw new RuntimeException('The directory ' . $dst . ' could not be created.'); |
||
| 68 | } |
||
| 69 | while (false !== ($file = \readdir($dir))) { |
||
| 70 | if (('.' !== $file) && ('..' !== $file)) { |
||
| 71 | if (\is_dir($src . '/' . $file)) { |
||
| 72 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
| 73 | } else { |
||
| 74 | \copy($src . '/' . $file, $dst . '/' . $file); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | \closedir($dir); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Remove files and (sub)directories |
||
| 83 | * |
||
| 84 | * @param string $src source directory to delete |
||
| 85 | * |
||
| 86 | * @return bool true on success |
||
| 87 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 88 | * |
||
| 89 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 90 | */ |
||
| 91 | public static function deleteDirectory($src): bool |
||
| 92 | { |
||
| 93 | // Only continue if user is a 'global' Admin |
||
| 94 | if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 98 | $success = true; |
||
| 99 | // remove old files |
||
| 100 | $dirInfo = new SplFileInfo($src); |
||
| 101 | // validate is a directory |
||
| 102 | if ($dirInfo->isDir()) { |
||
| 103 | $fileList = \array_diff(\scandir($src, \SCANDIR_SORT_NONE), ['..', '.']); |
||
| 104 | foreach ($fileList as $k => $v) { |
||
| 105 | $fileInfo = new SplFileInfo("{$src}/{$v}"); |
||
| 106 | if ($fileInfo->isDir()) { |
||
| 107 | // recursively handle subdirectories |
||
| 108 | if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
||
| 109 | break; |
||
| 110 | } |
||
| 111 | } elseif (!($success = \unlink($fileInfo->getRealPath()))) { |
||
| 112 | break; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | // now delete this (sub)directory if all the files are gone |
||
| 116 | if ($success) { |
||
| 117 | $success = \rmdir($dirInfo->getRealPath()); |
||
| 118 | } |
||
| 119 | } else { |
||
| 120 | // input is not a valid directory |
||
| 121 | $success = false; |
||
| 122 | } |
||
| 123 | |||
| 124 | return $success; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Recursively remove directory |
||
| 129 | * |
||
| 130 | * @todo currently won't remove directories with hidden files, should it? |
||
| 131 | * |
||
| 132 | * @param string $src directory to remove (delete) |
||
| 133 | * |
||
| 134 | * @return bool true on success |
||
| 135 | */ |
||
| 136 | public static function rrmdir($src): bool |
||
| 137 | { |
||
| 138 | // Only continue if user is a 'global' Admin |
||
| 139 | if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 140 | return false; |
||
| 141 | } |
||
| 142 | |||
| 143 | // If source is not a directory stop processing |
||
| 144 | if (!\is_dir($src)) { |
||
| 145 | return false; |
||
| 146 | } |
||
| 147 | |||
| 148 | $success = true; |
||
|
|
|||
| 149 | |||
| 150 | // Open the source directory to read in files |
||
| 151 | $iterator = new DirectoryIterator($src); |
||
| 152 | foreach ($iterator as $fObj) { |
||
| 153 | if ($fObj->isFile()) { |
||
| 154 | $filename = $fObj->getPathname(); |
||
| 155 | $fObj = null; // clear this iterator object to close the file |
||
| 156 | if (!\unlink($filename)) { |
||
| 157 | return false; // couldn't delete the file |
||
| 158 | } |
||
| 159 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
| 160 | // Try recursively on directory |
||
| 161 | self::rrmdir($fObj->getPathname()); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | $iterator = null; // clear iterator Obj to close file/directory |
||
| 165 | |||
| 166 | return \rmdir($src); // remove the directory & return results |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Recursively move files from one directory to another |
||
| 171 | * |
||
| 172 | * @param string $src - Source of files being moved |
||
| 173 | * @param string $dest - Destination of files being moved |
||
| 174 | * |
||
| 175 | * @return bool true on success |
||
| 176 | */ |
||
| 177 | public static function rmove($src, $dest): bool |
||
| 178 | { |
||
| 179 | // Only continue if user is a 'global' Admin |
||
| 180 | if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 181 | return false; |
||
| 182 | } |
||
| 183 | |||
| 184 | // If source is not a directory stop processing |
||
| 185 | if (!\is_dir($src)) { |
||
| 186 | return false; |
||
| 187 | } |
||
| 188 | |||
| 189 | // If the destination directory does not exist and could not be created stop processing |
||
| 190 | if (!\is_dir($dest) && !\mkdir($dest) && !\is_dir($dest)) { |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | |||
| 194 | // Open the source directory to read in files |
||
| 195 | $iterator = new DirectoryIterator($src); |
||
| 196 | foreach ($iterator as $fObj) { |
||
| 197 | if ($fObj->isFile()) { |
||
| 198 | \rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 199 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
| 200 | // Try recursively on directory |
||
| 201 | self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 202 | // rmdir($fObj->getPath()); // now delete the directory |
||
| 203 | } |
||
| 204 | } |
||
| 205 | $iterator = null; // clear iterator Obj to close file/directory |
||
| 206 | |||
| 207 | return \rmdir($src); // remove the directory & return results |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Recursively copy directories and 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 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 218 | * |
||
| 219 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 220 | */ |
||
| 221 | public static function rcopy($src, $dest): bool |
||
| 249 | } |
||
| 250 | } |
||
| 251 |