| 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 |
||
| 28 | trait FilesManagement |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
| 32 | * |
||
| 33 | * @param string $folder The full path of the directory to check |
||
| 34 | * |
||
| 35 | * @throws \RuntimeException |
||
| 36 | */ |
||
| 37 | public static function createFolder($folder) |
||
| 38 | { |
||
| 39 | try { |
||
| 40 | if (!\is_dir($folder)) { |
||
| 41 | if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) { |
||
| 42 | throw new RuntimeException(\sprintf('Unable to create the %s directory', $folder)); |
||
| 43 | } |
||
| 44 | |||
| 45 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
| 46 | } |
||
| 47 | } catch (Exception $e) { |
||
| 48 | echo 'Caught exception: ', $e->getMessage(), '<br>'; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param $file |
||
| 54 | * @param $folder |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | public static function copyFile($file, $folder) |
||
| 58 | { |
||
| 59 | return \copy($file, $folder); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param $src |
||
| 64 | * @param $dst |
||
| 65 | */ |
||
| 66 | public static function recurseCopy($src, $dst) |
||
| 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 | * Copy a file, or recursively copy a folder and its contents |
||
| 87 | * @param string $source Source path |
||
| 88 | * @param string $dest Destination path |
||
| 89 | * @return bool Returns true on success, false on failure |
||
| 90 | * @author Aidan Lister <[email protected]> |
||
| 91 | * @version 1.0.1 |
||
| 92 | * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
| 93 | */ |
||
| 94 | public static function xcopy($source, $dest) |
||
| 95 | { |
||
| 96 | // Check for symlinks |
||
| 97 | if (\is_link($source)) { |
||
| 98 | return \symlink(\readlink($source), $dest); |
||
| 99 | } |
||
| 100 | |||
| 101 | // Simple copy for a file |
||
| 102 | if (\is_file($source)) { |
||
| 103 | return \copy($source, $dest); |
||
| 104 | } |
||
| 105 | |||
| 106 | // Make destination directory |
||
| 107 | if (!\is_dir($dest)) { |
||
| 108 | if (!\mkdir($dest) && !\is_dir($dest)) { |
||
| 109 | throw new RuntimeException(\sprintf('Directory "%s" was not created', $dest)); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | // Loop through the folder |
||
| 114 | $dir = \dir($source); |
||
| 115 | if (@\is_dir($dir)) { |
||
|
|
|||
| 116 | while (false !== $entry = $dir->read()) { |
||
| 117 | // Skip pointers |
||
| 118 | if ('.' === $entry || '..' === $entry) { |
||
| 119 | continue; |
||
| 120 | } |
||
| 121 | // Deep copy directories |
||
| 122 | self::xcopy("$source/$entry", "$dest/$entry"); |
||
| 123 | } |
||
| 124 | // Clean up |
||
| 125 | $dir->close(); |
||
| 126 | } |
||
| 127 | |||
| 128 | return true; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Remove files and (sub)directories |
||
| 133 | * |
||
| 134 | * @param string $src source directory to delete |
||
| 135 | * |
||
| 136 | * @return bool true on success |
||
| 137 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 138 | * |
||
| 139 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 140 | */ |
||
| 141 | public static function deleteDirectory($src) |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Recursively remove directory |
||
| 179 | * |
||
| 180 | * @todo currently won't remove directories with hidden files, should it? |
||
| 181 | * |
||
| 182 | * @param string $src directory to remove (delete) |
||
| 183 | * |
||
| 184 | * @return bool true on success |
||
| 185 | */ |
||
| 186 | public static function rrmdir($src) |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Recursively move files from one directory to another |
||
| 220 | * |
||
| 221 | * @param string $src - Source of files being moved |
||
| 222 | * @param string $dest - Destination of files being moved |
||
| 223 | * |
||
| 224 | * @return bool true on success |
||
| 225 | */ |
||
| 226 | public static function rmove($src, $dest) |
||
| 227 | { |
||
| 228 | // Only continue if user is a 'global' Admin |
||
| 229 | if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 230 | return false; |
||
| 231 | } |
||
| 232 | |||
| 233 | // If source is not a directory stop processing |
||
| 234 | if (!\is_dir($src)) { |
||
| 235 | return false; |
||
| 236 | } |
||
| 237 | |||
| 238 | // If the destination directory does not exist and could not be created stop processing |
||
| 239 | if (!\is_dir($dest) && !\mkdir($dest) && !\is_dir($dest)) { |
||
| 240 | return false; |
||
| 241 | } |
||
| 242 | |||
| 243 | // Open the source directory to read in files |
||
| 244 | $iterator = new DirectoryIterator($src); |
||
| 245 | foreach ($iterator as $fObj) { |
||
| 246 | if ($fObj->isFile()) { |
||
| 247 | \rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 248 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
| 249 | // Try recursively on directory |
||
| 250 | self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 251 | // rmdir($fObj->getPath()); // now delete the directory |
||
| 252 | } |
||
| 253 | } |
||
| 254 | $iterator = null; // clear iterator Obj to close file/directory |
||
| 255 | return \rmdir($src); // remove the directory & return results |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Recursively copy directories and files from one directory to another |
||
| 260 | * |
||
| 261 | * @param string $src - Source of files being moved |
||
| 262 | * @param string $dest - Destination of files being moved |
||
| 263 | * |
||
| 264 | * @return bool true on success |
||
| 265 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 266 | * |
||
| 267 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 268 | */ |
||
| 269 | public static function rcopy($src, $dest) |
||
| 297 | } |
||
| 298 | } |
||
| 299 |