| 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) |
||
| 30 | { |
||
| 31 | try { |
||
| 32 | if (!file_exists($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 $file |
||
| 46 | * @param $folder |
||
| 47 | * @return bool |
||
| 48 | */ |
||
| 49 | public static function copyFile($file, $folder) |
||
| 50 | { |
||
| 51 | return copy($file, $folder); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param $src |
||
| 56 | * @param $dst |
||
| 57 | */ |
||
| 58 | public static function recurseCopy($src, $dst) |
||
| 59 | { |
||
| 60 | $dir = opendir($src); |
||
| 61 | // @mkdir($dst); |
||
| 62 | if (!@mkdir($dst) && !is_dir($dst)) { |
||
| 63 | throw new \RuntimeException('The directory ' . $dst . ' could not be created.'); |
||
| 64 | } |
||
| 65 | while (false !== ($file = readdir($dir))) { |
||
|
|
|||
| 66 | if (('.' !== $file) && ('..' !== $file)) { |
||
| 67 | if (is_dir($src . '/' . $file)) { |
||
| 68 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
| 69 | } else { |
||
| 70 | copy($src . '/' . $file, $dst . '/' . $file); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | closedir($dir); |
||
| 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) |
||
| 87 | { |
||
| 88 | // Check for symlinks |
||
| 89 | if (is_link($source)) { |
||
| 90 | return symlink(readlink($source), $dest); |
||
| 91 | } |
||
| 92 | |||
| 93 | // Simple copy for a file |
||
| 94 | if (is_file($source)) { |
||
| 95 | return copy($source, $dest); |
||
| 96 | } |
||
| 97 | |||
| 98 | // Make destination directory |
||
| 99 | if (!is_dir($dest)) { |
||
| 100 | if (!mkdir($dest) && !is_dir($dest)) { |
||
| 101 | throw new \RuntimeException(sprintf('Directory "%s" was not created', $dest)); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | // Loop through the folder |
||
| 106 | $dir = dir($source); |
||
| 107 | if (@is_dir($dir)) { |
||
| 108 | while (false !== $entry = $dir->read()) { |
||
| 109 | // Skip pointers |
||
| 110 | if ('.' === $entry || '..' === $entry) { |
||
| 111 | continue; |
||
| 112 | } |
||
| 113 | // Deep copy directories |
||
| 114 | self::xcopy("$source/$entry", "$dest/$entry"); |
||
| 115 | } |
||
| 116 | // Clean up |
||
| 117 | $dir->close(); |
||
| 118 | } |
||
| 119 | |||
| 120 | return true; |
||
| 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) |
||
| 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) |
||
| 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 |