| Total Complexity | 65 |
| Total Lines | 291 |
| 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 declare(strict_types=1); |
||
| 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 | public static function createFolder( |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param $file |
||
| 57 | * @param $folder |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | public static function copyFile( |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param $src |
||
| 69 | * @param $dst |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | public static function recurseCopy($src, $dst) |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Copy a file, or recursively copy a folder and its contents |
||
| 96 | * @param string $source Source path |
||
| 97 | * @param string $dest Destination path |
||
| 98 | * @return bool Returns true on success, false on failure |
||
| 99 | * @author Aidan Lister <[email protected]> |
||
| 100 | * @version 1.0.1 |
||
| 101 | * @link https://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
| 102 | */ |
||
| 103 | public static function xcopy( |
||
| 104 | $source, |
||
| 105 | $dest |
||
| 106 | ) { |
||
| 107 | // Check for symlinks |
||
| 108 | if (\is_link($source)) { |
||
| 109 | return \symlink(\readlink($source), $dest); |
||
| 110 | } |
||
| 111 | // Simple copy for a file |
||
| 112 | if (\is_file($source)) { |
||
| 113 | return \copy($source, $dest); |
||
| 114 | } |
||
| 115 | // Make destination directory |
||
| 116 | if (!\is_dir($dest)) { |
||
| 117 | if (!\mkdir($dest) && !\is_dir($dest)) { |
||
| 118 | throw new RuntimeException(\sprintf('Directory "%s" was not created', $dest)); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | // Loop through the folder |
||
| 122 | /** @var \Directory $dir */ |
||
| 123 | $dir = \dir($source); |
||
| 124 | if (@\is_dir((string)$dir)) { |
||
| 125 | while (false !== $entry = $dir->read()) { |
||
| 126 | // Skip pointers |
||
| 127 | if ('.' === $entry || '..' === $entry) { |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | // Deep copy directories |
||
| 131 | self::xcopy("{$source}/{$entry}", "{$dest}/{$entry}"); |
||
| 132 | } |
||
| 133 | // Clean up |
||
| 134 | $dir->close(); |
||
| 135 | } |
||
| 136 | |||
| 137 | return true; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Remove files and (sub)directories |
||
| 142 | * |
||
| 143 | * @param string $src source directory to delete |
||
| 144 | * |
||
| 145 | * @return bool true on success |
||
| 146 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 147 | * |
||
| 148 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 149 | */ |
||
| 150 | public static function deleteDirectory( |
||
| 151 | $src |
||
| 152 | ) { |
||
| 153 | // Only continue if user is a 'global' Admin |
||
| 154 | if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 155 | return false; |
||
| 156 | } |
||
| 157 | $success = true; |
||
| 158 | // remove old files |
||
| 159 | $dirInfo = new SplFileInfo($src); |
||
| 160 | // validate is a directory |
||
| 161 | if ($dirInfo->isDir()) { |
||
| 162 | $fileList = \array_diff(\scandir($src, \SCANDIR_SORT_NONE), ['..', '.']); |
||
| 163 | foreach ($fileList as $k => $v) { |
||
| 164 | $fileInfo = new SplFileInfo("{$src}/{$v}"); |
||
| 165 | if ($fileInfo->isDir()) { |
||
| 166 | // recursively handle subdirectories |
||
| 167 | if (!$success = self::deleteDirectory( |
||
| 168 | $fileInfo->getRealPath() |
||
| 169 | )) { |
||
| 170 | break; |
||
| 171 | } |
||
| 172 | } elseif (!($success = @\unlink($fileInfo->getRealPath()))) { |
||
| 173 | break; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | // now delete this (sub)directory if all the files are gone |
||
| 177 | if ($success) { |
||
| 178 | $success = \rmdir($dirInfo->getRealPath()); |
||
| 179 | } |
||
| 180 | } else { |
||
| 181 | // input is not a valid directory |
||
| 182 | $success = false; |
||
| 183 | } |
||
| 184 | |||
| 185 | return $success; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Recursively remove directory |
||
| 190 | * |
||
| 191 | * @todo currently won't remove directories with hidden files, should it? |
||
| 192 | * |
||
| 193 | * @param string $src directory to remove (delete) |
||
| 194 | * |
||
| 195 | * @return bool true on success |
||
| 196 | */ |
||
| 197 | public static function rrmdir( |
||
| 198 | $src |
||
| 199 | ) { |
||
| 200 | // Only continue if user is a 'global' Admin |
||
| 201 | if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 202 | return false; |
||
| 203 | } |
||
| 204 | // If source is not a directory stop processing |
||
| 205 | if (!\is_dir($src)) { |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | $success = true; |
||
|
|
|||
| 209 | // Open the source directory to read in files |
||
| 210 | $iterator = new DirectoryIterator($src); |
||
| 211 | foreach ($iterator as $fObj) { |
||
| 212 | if ($fObj->isFile()) { |
||
| 213 | $filename = $fObj->getPathname(); |
||
| 214 | $fObj = null; // clear this iterator object to close the file |
||
| 215 | if (false === @\unlink($filename)) { |
||
| 216 | return false; // couldn't delete the file |
||
| 217 | } |
||
| 218 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
| 219 | // Try recursively on directory |
||
| 220 | self::rrmdir($fObj->getPathname()); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | $iterator = null; // clear iterator Obj to close file/directory |
||
| 224 | |||
| 225 | return \rmdir($src); // remove the directory & return results |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Recursively move files from one directory to another |
||
| 230 | * |
||
| 231 | * @param string $src - Source of files being moved |
||
| 232 | * @param string $dest - Destination of files being moved |
||
| 233 | * |
||
| 234 | * @return bool true on success |
||
| 235 | */ |
||
| 236 | public static function rmove( |
||
| 237 | $src, |
||
| 238 | $dest |
||
| 239 | ) { |
||
| 240 | // Only continue if user is a 'global' Admin |
||
| 241 | if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 242 | return false; |
||
| 243 | } |
||
| 244 | // If source is not a directory stop processing |
||
| 245 | if (!\is_dir($src)) { |
||
| 246 | return false; |
||
| 247 | } |
||
| 248 | // If the destination directory does not exist and could not be created stop processing |
||
| 249 | if (!\is_dir( |
||
| 250 | $dest |
||
| 251 | ) |
||
| 252 | && !\mkdir( |
||
| 253 | $dest |
||
| 254 | ) |
||
| 255 | && !\is_dir( |
||
| 256 | $dest |
||
| 257 | )) { |
||
| 258 | return false; |
||
| 259 | } |
||
| 260 | // Open the source directory to read in files |
||
| 261 | $iterator = new DirectoryIterator($src); |
||
| 262 | foreach ($iterator as $fObj) { |
||
| 263 | if ($fObj->isFile()) { |
||
| 264 | \rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 265 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
| 266 | // Try recursively on directory |
||
| 267 | self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 268 | // rmdir($fObj->getPath()); // now delete the directory |
||
| 269 | } |
||
| 270 | } |
||
| 271 | $iterator = null; // clear iterator Obj to close file/directory |
||
| 272 | |||
| 273 | return \rmdir($src); // remove the directory & return results |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Recursively copy directories and files from one directory to another |
||
| 278 | * |
||
| 279 | * @param string $src - Source of files being moved |
||
| 280 | * @param string $dest - Destination of files being moved |
||
| 281 | * |
||
| 282 | * @return bool true on success |
||
| 283 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 284 | * |
||
| 285 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 286 | */ |
||
| 287 | public static function rcopy( |
||
| 322 | } |
||
| 323 | } |
||
| 324 |