| Total Complexity | 64 |
| Total Lines | 268 |
| 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); |
||
| 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(string $folder): void |
||
| 30 | { |
||
| 31 | try { |
||
| 32 | if (!\is_dir($folder)) { |
||
| 33 | if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) { |
||
| 34 | throw new \RuntimeException(\sprintf('Unable to create the %s directory', $folder)); |
||
| 35 | } |
||
| 36 | |||
| 37 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
| 38 | } |
||
| 39 | } catch (\Exception $e) { |
||
| 40 | echo 'Caught exception: ', $e->getMessage(), '<br>'; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $file |
||
| 46 | * @param string $folder |
||
| 47 | * @return bool |
||
| 48 | */ |
||
| 49 | public static function copyFile(string $file, string $folder): bool |
||
| 50 | { |
||
| 51 | return \copy($file, $folder); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $src |
||
| 56 | * @param string $dst |
||
| 57 | */ |
||
| 58 | public static function recurseCopy(string $src, string $dst): void |
||
| 59 | { |
||
| 60 | $dir = \opendir($src); |
||
| 61 | if (!\mkdir($dst) && !\is_dir($dst)) { |
||
| 62 | throw new \RuntimeException(\sprintf('Directory "%s" was not created', $dst)); |
||
| 63 | } |
||
| 64 | while (false !== ($file = \readdir($dir))) { |
||
| 65 | if (('.' !== $file) && ('..' !== $file)) { |
||
| 66 | if (\is_dir($src . '/' . $file)) { |
||
| 67 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
| 68 | } else { |
||
| 69 | \copy($src . '/' . $file, $dst . '/' . $file); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | \closedir($dir); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Copy a file, or recursively copy a folder and its contents |
||
| 78 | * @param string $source Source path |
||
| 79 | * @param string $dest Destination path |
||
| 80 | * @return bool Returns true on success, false on failure |
||
| 81 | * @author Aidan Lister <[email protected]> |
||
| 82 | * @version 1.0.1 |
||
| 83 | * @link https://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
| 84 | */ |
||
| 85 | public static function xcopy(string $source, string $dest): bool |
||
| 86 | { |
||
| 87 | // Check for symlinks |
||
| 88 | if (\is_link($source)) { |
||
| 89 | return \symlink(\readlink($source), $dest); |
||
| 90 | } |
||
| 91 | |||
| 92 | // Simple copy for a file |
||
| 93 | if (\is_file($source)) { |
||
| 94 | return \copy($source, $dest); |
||
| 95 | } |
||
| 96 | |||
| 97 | // Make destination directory |
||
| 98 | if (!\is_dir($dest) && !\mkdir($dest) && !\is_dir($dest)) { |
||
| 99 | throw new \RuntimeException(\sprintf('Directory "%s" was not created', $dest)); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Loop through the folder |
||
| 103 | $dir = \dir($source); |
||
| 104 | if (@\is_dir((string)$dir)) { |
||
| 105 | while (false !== $entry = $dir->read()) { |
||
| 106 | // Skip pointers |
||
| 107 | if ('.' === $entry || '..' === $entry) { |
||
| 108 | continue; |
||
| 109 | } |
||
| 110 | // Deep copy directories |
||
| 111 | self::xcopy("$source/$entry", "$dest/$entry"); |
||
| 112 | } |
||
| 113 | // Clean up |
||
| 114 | $dir->close(); |
||
| 115 | } |
||
| 116 | |||
| 117 | return true; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Remove files and (sub)directories |
||
| 122 | * |
||
| 123 | * @param string $src source directory to delete |
||
| 124 | * |
||
| 125 | * @return bool true on success |
||
| 126 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 127 | * |
||
| 128 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 129 | */ |
||
| 130 | public static function deleteDirectory(string $src): bool |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Recursively remove directory |
||
| 168 | * |
||
| 169 | * @todo currently won't remove directories with hidden files, should it? |
||
| 170 | * |
||
| 171 | * @param string $src directory to remove (delete) |
||
| 172 | * |
||
| 173 | * @return bool true on success |
||
| 174 | */ |
||
| 175 | public static function rrmdir(string $src): bool |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Recursively move files from one directory to another |
||
| 210 | * |
||
| 211 | * @param string $src - Source of files being moved |
||
| 212 | * @param string $dest - Destination of files being moved |
||
| 213 | * |
||
| 214 | * @return bool true on success |
||
| 215 | */ |
||
| 216 | public static function rmove(string $src, string $dest): bool |
||
| 217 | { |
||
| 218 | // Only continue if user is a 'global' Admin |
||
| 219 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 220 | return false; |
||
| 221 | } |
||
| 222 | |||
| 223 | // If source is not a directory stop processing |
||
| 224 | if (!\is_dir($src)) { |
||
| 225 | return false; |
||
| 226 | } |
||
| 227 | |||
| 228 | // If the destination directory does not exist and could not be created stop processing |
||
| 229 | if (!\is_dir($dest) && !\mkdir($dest) && !\is_dir($dest)) { |
||
| 230 | return false; |
||
| 231 | } |
||
| 232 | |||
| 233 | // Open the source directory to read in files |
||
| 234 | $iterator = new \DirectoryIterator($src); |
||
| 235 | foreach ($iterator as $fObj) { |
||
| 236 | if ($fObj->isFile()) { |
||
| 237 | \rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 238 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
| 239 | // Try recursively on directory |
||
| 240 | self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 241 | // rmdir($fObj->getPath()); // now delete the directory |
||
| 242 | } |
||
| 243 | } |
||
| 244 | $iterator = null; // clear iterator Obj to close file/directory |
||
| 245 | |||
| 246 | return \rmdir($src); // remove the directory & return results |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Recursively copy directories and files from one directory to another |
||
| 251 | * |
||
| 252 | * @param string $src - Source of files being moved |
||
| 253 | * @param string $dest - Destination of files being moved |
||
| 254 | * |
||
| 255 | * @return bool true on success |
||
| 256 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 257 | * |
||
| 258 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 259 | */ |
||
| 260 | public static function rcopy(string $src, string $dest): bool |
||
| 288 | } |
||
| 289 | } |
||
| 290 |