| Total Complexity | 65 |
| Total Lines | 356 |
| 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 |
||
| 31 | trait FilesManagement |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
| 35 | * |
||
| 36 | * @param string $folder The full path of the directory to check |
||
| 37 | * |
||
| 38 | * @throws RuntimeException |
||
| 39 | */ |
||
| 40 | |||
| 41 | public static function createFolder( |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param $file |
||
| 59 | * @param $folder |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | |||
| 63 | public static function copyFile( |
||
| 64 | $file, |
||
| 65 | $folder |
||
| 66 | ) { |
||
| 67 | return \copy($file, $folder); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param $src |
||
| 72 | * @param $dst |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | |||
| 76 | public static function recurseCopy($src, $dst) |
||
| 77 | { |
||
| 78 | $dir = \opendir($src); |
||
| 79 | |||
| 80 | if (false === $dir) { |
||
| 81 | return false; |
||
| 82 | } |
||
| 83 | |||
| 84 | // @mkdir($dst); |
||
| 85 | |||
| 86 | if (!@\mkdir($dst) && !\is_dir($dst)) { |
||
| 87 | throw new RuntimeException('The directory ' . $dst . ' could not be created.'); |
||
| 88 | } |
||
| 89 | |||
| 90 | while (false !== ($file = \readdir($dir))) { |
||
| 91 | if (('.' !== $file) && ('..' !== $file)) { |
||
| 92 | if (\is_dir($src . '/' . $file)) { |
||
| 93 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
| 94 | } else { |
||
| 95 | \copy($src . '/' . $file, $dst . '/' . $file); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | \closedir($dir); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Copy a file, or recursively copy a folder and its contents |
||
| 105 | * @param string $source Source path |
||
| 106 | * @param string $dest Destination path |
||
| 107 | * @return bool Returns true on success, false on failure |
||
| 108 | * @author Aidan Lister <[email protected]> |
||
| 109 | * @version 1.0.1 |
||
| 110 | * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
| 111 | */ |
||
| 112 | |||
| 113 | public static function xcopy( |
||
| 114 | $source, |
||
| 115 | $dest |
||
| 116 | ) { |
||
| 117 | // Check for symlinks |
||
| 118 | |||
| 119 | if (\is_link($source)) { |
||
| 120 | return \symlink(\readlink($source), $dest); |
||
| 121 | } |
||
| 122 | |||
| 123 | // Simple copy for a file |
||
| 124 | |||
| 125 | if (\is_file($source)) { |
||
| 126 | return \copy($source, $dest); |
||
| 127 | } |
||
| 128 | |||
| 129 | // Make destination directory |
||
| 130 | |||
| 131 | if (!\is_dir($dest)) { |
||
| 132 | if (!\mkdir($dest) && !\is_dir($dest)) { |
||
| 133 | throw new RuntimeException(\sprintf('Directory "%s" was not created', $dest)); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | // Loop through the folder |
||
| 138 | |||
| 139 | $dir = \dir($source); |
||
| 140 | |||
| 141 | if (@\is_dir($dir)) { |
||
| 142 | while (false !== $entry = $dir->read()) { |
||
| 143 | // Skip pointers |
||
| 144 | |||
| 145 | if ('.' === $entry || '..' === $entry) { |
||
| 146 | continue; |
||
| 147 | } |
||
| 148 | |||
| 149 | // Deep copy directories |
||
| 150 | |||
| 151 | self::xcopy("${source}/${entry}", "${dest}/${entry}"); |
||
| 152 | } |
||
| 153 | |||
| 154 | // Clean up |
||
| 155 | |||
| 156 | $dir->close(); |
||
| 157 | } |
||
| 158 | |||
| 159 | return true; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Remove files and (sub)directories |
||
| 164 | * |
||
| 165 | * @param string $src source directory to delete |
||
| 166 | * |
||
| 167 | * @return bool true on success |
||
| 168 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 169 | * |
||
| 170 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 171 | */ |
||
| 172 | |||
| 173 | public static function deleteDirectory( |
||
| 174 | $src |
||
| 175 | ) { |
||
| 176 | // Only continue if user is a 'global' Admin |
||
| 177 | |||
| 178 | if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 179 | return false; |
||
| 180 | } |
||
| 181 | |||
| 182 | $success = true; |
||
| 183 | |||
| 184 | // remove old files |
||
| 185 | |||
| 186 | $dirInfo = new SplFileInfo($src); |
||
| 187 | |||
| 188 | // validate is a directory |
||
| 189 | |||
| 190 | if ($dirInfo->isDir()) { |
||
| 191 | $fileList = \array_diff(\scandir($src, \SCANDIR_SORT_NONE), ['..', '.']); |
||
|
|
|||
| 192 | |||
| 193 | foreach ($fileList as $k => $v) { |
||
| 194 | $fileInfo = new SplFileInfo("{$src}/{$v}"); |
||
| 195 | |||
| 196 | if ($fileInfo->isDir()) { |
||
| 197 | // recursively handle subdirectories |
||
| 198 | |||
| 199 | if (!$success = self::deleteDirectory( |
||
| 200 | $fileInfo->getRealPath() |
||
| 201 | )) { |
||
| 202 | break; |
||
| 203 | } |
||
| 204 | } elseif (!($success = \unlink($fileInfo->getRealPath()))) { |
||
| 205 | break; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | // now delete this (sub)directory if all the files are gone |
||
| 210 | |||
| 211 | if ($success) { |
||
| 212 | $success = \rmdir($dirInfo->getRealPath()); |
||
| 213 | } |
||
| 214 | } else { |
||
| 215 | // input is not a valid directory |
||
| 216 | |||
| 217 | $success = false; |
||
| 218 | } |
||
| 219 | |||
| 220 | return $success; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Recursively remove directory |
||
| 225 | * |
||
| 226 | * @todo currently won't remove directories with hidden files, should it? |
||
| 227 | * |
||
| 228 | * @param string $src directory to remove (delete) |
||
| 229 | * |
||
| 230 | * @return bool true on success |
||
| 231 | */ |
||
| 232 | |||
| 233 | public static function rrmdir( |
||
| 234 | $src |
||
| 235 | ) { |
||
| 236 | // Only continue if user is a 'global' Admin |
||
| 237 | |||
| 238 | if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 239 | return false; |
||
| 240 | } |
||
| 241 | |||
| 242 | // If source is not a directory stop processing |
||
| 243 | |||
| 244 | if (!\is_dir($src)) { |
||
| 245 | return false; |
||
| 246 | } |
||
| 247 | |||
| 248 | $success = true; |
||
| 249 | |||
| 250 | // Open the source directory to read in files |
||
| 251 | |||
| 252 | $iterator = new DirectoryIterator($src); |
||
| 253 | |||
| 254 | foreach ($iterator as $fObj) { |
||
| 255 | if ($fObj->isFile()) { |
||
| 256 | $filename = $fObj->getPathname(); |
||
| 257 | |||
| 258 | $fObj = null; // clear this iterator object to close the file |
||
| 259 | |||
| 260 | if (!\unlink($filename)) { |
||
| 261 | return false; // couldn't delete the file |
||
| 262 | } |
||
| 263 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
| 264 | // Try recursively on directory |
||
| 265 | |||
| 266 | self::rrmdir($fObj->getPathname()); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | $iterator = null; // clear iterator Obj to close file/directory |
||
| 271 | return \rmdir($src); // remove the directory & return results |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Recursively move files from one directory to another |
||
| 276 | * |
||
| 277 | * @param string $src - Source of files being moved |
||
| 278 | * @param string $dest - Destination of files being moved |
||
| 279 | * |
||
| 280 | * @return bool true on success |
||
| 281 | */ |
||
| 282 | |||
| 283 | public static function rmove( |
||
| 284 | $src, |
||
| 285 | $dest |
||
| 286 | ) { |
||
| 287 | // Only continue if user is a 'global' Admin |
||
| 288 | |||
| 289 | if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 290 | return false; |
||
| 291 | } |
||
| 292 | |||
| 293 | // If source is not a directory stop processing |
||
| 294 | |||
| 295 | if (!\is_dir($src)) { |
||
| 296 | return false; |
||
| 297 | } |
||
| 298 | |||
| 299 | // If the destination directory does not exist and could not be created stop processing |
||
| 300 | |||
| 301 | if (!\is_dir( |
||
| 302 | $dest |
||
| 303 | ) |
||
| 304 | && !\mkdir( |
||
| 305 | $dest |
||
| 306 | ) |
||
| 307 | && !\is_dir( |
||
| 308 | $dest |
||
| 309 | )) { |
||
| 310 | return false; |
||
| 311 | } |
||
| 312 | |||
| 313 | // Open the source directory to read in files |
||
| 314 | |||
| 315 | $iterator = new DirectoryIterator($src); |
||
| 316 | |||
| 317 | foreach ($iterator as $fObj) { |
||
| 318 | if ($fObj->isFile()) { |
||
| 319 | \rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 320 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
| 321 | // Try recursively on directory |
||
| 322 | |||
| 323 | self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 324 | // rmdir($fObj->getPath()); // now delete the directory |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | $iterator = null; // clear iterator Obj to close file/directory |
||
| 329 | return \rmdir($src); // remove the directory & return results |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Recursively copy directories and files from one directory to another |
||
| 334 | * |
||
| 335 | * @param string $src - Source of files being moved |
||
| 336 | * @param string $dest - Destination of files being moved |
||
| 337 | * |
||
| 338 | * @return bool true on success |
||
| 339 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 340 | * |
||
| 341 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 342 | */ |
||
| 343 | |||
| 344 | public static function rcopy( |
||
| 387 | } |
||
| 388 | } |
||
| 389 |