| Total Complexity | 64 |
| Total Lines | 270 |
| 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 | * @return void |
||
| 28 | * @throws \RuntimeException |
||
| 29 | */ |
||
| 30 | public static function createFolder($folder) |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param $file |
||
| 47 | * @param $folder |
||
| 48 | * @return bool |
||
| 49 | */ |
||
| 50 | public static function copyFile($file, $folder) |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param $src |
||
| 57 | * @param $dst |
||
| 58 | */ |
||
| 59 | public static function recurseCopy($src, $dst) |
||
| 60 | { |
||
| 61 | $dir = opendir($src); |
||
| 62 | // @mkdir($dst); |
||
| 63 | if (!@mkdir($dst) && !is_dir($dst)) { |
||
| 64 | throw new \RuntimeException('The directory ' . $dst . ' could not be created.'); |
||
| 65 | } |
||
| 66 | while (false !== ($file = readdir($dir))) { |
||
|
|
|||
| 67 | if (('.' !== $file) && ('..' !== $file)) { |
||
| 68 | if (is_dir($src . '/' . $file)) { |
||
| 69 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
| 70 | } else { |
||
| 71 | copy($src . '/' . $file, $dst . '/' . $file); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | closedir($dir); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Copy a file, or recursively copy a folder and its contents |
||
| 80 | * @param string $source Source path |
||
| 81 | * @param string $dest Destination path |
||
| 82 | * @return bool Returns true on success, false on failure |
||
| 83 | * @author Aidan Lister <[email protected]> |
||
| 84 | * @version 1.0.1 |
||
| 85 | * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
| 86 | */ |
||
| 87 | public static function xcopy($source, $dest) |
||
| 88 | { |
||
| 89 | // Check for symlinks |
||
| 90 | if (is_link($source)) { |
||
| 91 | return symlink(readlink($source), $dest); |
||
| 92 | } |
||
| 93 | |||
| 94 | // Simple copy for a file |
||
| 95 | if (is_file($source)) { |
||
| 96 | return copy($source, $dest); |
||
| 97 | } |
||
| 98 | |||
| 99 | // Make destination directory |
||
| 100 | if (!is_dir($dest)) { |
||
| 101 | if (!mkdir($dest) && !is_dir($dest)) { |
||
| 102 | throw new \RuntimeException(sprintf('Directory "%s" was not created', $dest)); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | // Loop through the folder |
||
| 107 | $dir = dir($source); |
||
| 108 | if (@is_dir($dir)) { |
||
| 109 | while (false !== $entry = $dir->read()) { |
||
| 110 | // Skip pointers |
||
| 111 | if ('.' === $entry || '..' === $entry) { |
||
| 112 | continue; |
||
| 113 | } |
||
| 114 | // Deep copy directories |
||
| 115 | self::xcopy("$source/$entry", "$dest/$entry"); |
||
| 116 | } |
||
| 117 | // Clean up |
||
| 118 | $dir->close(); |
||
| 119 | } |
||
| 120 | |||
| 121 | return true; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Remove files and (sub)directories |
||
| 126 | * |
||
| 127 | * @param string $src source directory to delete |
||
| 128 | * |
||
| 129 | * @return bool true on success |
||
| 130 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 131 | * |
||
| 132 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 133 | */ |
||
| 134 | public static function deleteDirectory($src) |
||
| 135 | { |
||
| 136 | // Only continue if user is a 'global' Admin |
||
| 137 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | |||
| 141 | $success = true; |
||
| 142 | // remove old files |
||
| 143 | $dirInfo = new \SplFileInfo($src); |
||
| 144 | // validate is a directory |
||
| 145 | if ($dirInfo->isDir()) { |
||
| 146 | $fileList = array_diff(scandir($src, SCANDIR_SORT_NONE), ['..', '.']); |
||
| 147 | foreach ($fileList as $k => $v) { |
||
| 148 | $fileInfo = new \SplFileInfo("{$src}/{$v}"); |
||
| 149 | if ($fileInfo->isDir()) { |
||
| 150 | // recursively handle subdirectories |
||
| 151 | if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
||
| 152 | break; |
||
| 153 | } |
||
| 154 | } elseif (!($success = unlink($fileInfo->getRealPath()))) { |
||
| 155 | break; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | // now delete this (sub)directory if all the files are gone |
||
| 159 | if ($success) { |
||
| 160 | $success = rmdir($dirInfo->getRealPath()); |
||
| 161 | } |
||
| 162 | } else { |
||
| 163 | // input is not a valid directory |
||
| 164 | $success = false; |
||
| 165 | } |
||
| 166 | |||
| 167 | return $success; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Recursively remove directory |
||
| 172 | * |
||
| 173 | * @todo currently won't remove directories with hidden files, should it? |
||
| 174 | * |
||
| 175 | * @param string $src directory to remove (delete) |
||
| 176 | * |
||
| 177 | * @return bool true on success |
||
| 178 | */ |
||
| 179 | public static function rrmdir($src) |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Recursively move files from one directory to another |
||
| 213 | * |
||
| 214 | * @param string $src - Source of files being moved |
||
| 215 | * @param string $dest - Destination of files being moved |
||
| 216 | * |
||
| 217 | * @return bool true on success |
||
| 218 | */ |
||
| 219 | public static function rmove($src, $dest) |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Recursively copy directories and files from one directory to another |
||
| 253 | * |
||
| 254 | * @param string $src - Source of files being moved |
||
| 255 | * @param string $dest - Destination of files being moved |
||
| 256 | * |
||
| 257 | * @return bool true on success |
||
| 258 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 259 | * |
||
| 260 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 261 | */ |
||
| 262 | public static function rcopy($src, $dest) |
||
| 290 | } |
||
| 291 | } |
||
| 292 |