| Total Complexity | 64 |
| Total Lines | 272 |
| 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 |
||
| 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 | } |
||
| 40 | catch (\Throwable $e) { |
||
| 41 | echo 'Caught exception: ', $e->getMessage(), '<br>'; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param $file |
||
| 47 | * @param $folder |
||
| 48 | * @return bool |
||
| 49 | */ |
||
| 50 | public static function copyFile($file, $folder) |
||
| 51 | { |
||
| 52 | return copy($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 | * @author Aidan Lister <[email protected]> |
||
| 81 | * @version 1.0.1 |
||
| 82 | * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
| 83 | * @param string $source Source path |
||
| 84 | * @param string $dest Destination path |
||
| 85 | * @return bool Returns true on success, false on failure |
||
| 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) && !mkdir($dest) && !is_dir($dest)) { |
||
| 101 | throw new \RuntimeException(sprintf('Directory "%s" was not created', $dest)); |
||
| 102 | } |
||
| 103 | |||
| 104 | // Loop through the folder |
||
| 105 | $dir = dir($source); |
||
| 106 | if (@is_dir($dir)) { |
||
|
|
|||
| 107 | while (false !== $entry = $dir->read()) { |
||
| 108 | // Skip pointers |
||
| 109 | if ('.' === $entry || '..' === $entry) { |
||
| 110 | continue; |
||
| 111 | } |
||
| 112 | // Deep copy directories |
||
| 113 | self::xcopy("$source/$entry", "$dest/$entry"); |
||
| 114 | } |
||
| 115 | // Clean up |
||
| 116 | $dir->close(); |
||
| 117 | } |
||
| 118 | |||
| 119 | return true; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Remove files and (sub)directories |
||
| 124 | * |
||
| 125 | * @param string $src source directory to delete |
||
| 126 | * |
||
| 127 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 128 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 129 | * |
||
| 130 | * @return bool true on success |
||
| 131 | */ |
||
| 132 | public static function deleteDirectory($src) |
||
| 133 | { |
||
| 134 | // Only continue if user is a 'global' Admin |
||
| 135 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 136 | return false; |
||
| 137 | } |
||
| 138 | |||
| 139 | $success = true; |
||
| 140 | // remove old files |
||
| 141 | $dirInfo = new \SplFileInfo($src); |
||
| 142 | // validate is a directory |
||
| 143 | if ($dirInfo->isDir()) { |
||
| 144 | $fileList = array_diff(scandir($src, SCANDIR_SORT_NONE), ['..', '.']); |
||
| 145 | foreach ($fileList as $k => $v) { |
||
| 146 | $fileInfo = new \SplFileInfo("{$src}/{$v}"); |
||
| 147 | if ($fileInfo->isDir()) { |
||
| 148 | // recursively handle subdirectories |
||
| 149 | if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
||
| 150 | break; |
||
| 151 | } |
||
| 152 | } else { |
||
| 153 | // delete the file |
||
| 154 | if (!($success = unlink($fileInfo->getRealPath()))) { |
||
| 155 | break; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | // now delete this (sub)directory if all the files are gone |
||
| 160 | if ($success) { |
||
| 161 | $success = rmdir($dirInfo->getRealPath()); |
||
| 162 | } |
||
| 163 | } else { |
||
| 164 | // input is not a valid directory |
||
| 165 | $success = false; |
||
| 166 | } |
||
| 167 | |||
| 168 | return $success; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Recursively remove directory |
||
| 173 | * |
||
| 174 | * @todo currently won't remove directories with hidden files, should it? |
||
| 175 | * |
||
| 176 | * @param string $src directory to remove (delete) |
||
| 177 | * |
||
| 178 | * @return bool true on success |
||
| 179 | */ |
||
| 180 | public static function rrmdir($src) |
||
| 181 | { |
||
| 182 | // Only continue if user is a 'global' Admin |
||
| 183 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 184 | return false; |
||
| 185 | } |
||
| 186 | |||
| 187 | // If source is not a directory stop processing |
||
| 188 | if (!is_dir($src)) { |
||
| 189 | return false; |
||
| 190 | } |
||
| 191 | |||
| 192 | $success = true; |
||
| 193 | |||
| 194 | // Open the source directory to read in files |
||
| 195 | $iterator = new \DirectoryIterator($src); |
||
| 196 | foreach ($iterator as $fObj) { |
||
| 197 | if ($fObj->isFile()) { |
||
| 198 | $filename = $fObj->getPathname(); |
||
| 199 | $fObj = null; // clear this iterator object to close the file |
||
| 200 | if (false === @unlink($filename)) { |
||
| 201 | // throw new \RuntimeException('The file '.$filename.' could not be deleted.'); |
||
| 202 | return false; // couldn't delete the file |
||
| 203 | } |
||
| 204 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
| 205 | // Try recursively on directory |
||
| 206 | self::rrmdir($fObj->getPathname()); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | $iterator = null; // clear iterator Obj to close file/directory |
||
| 210 | return rmdir($src); // remove the directory & return results |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Recursively move files from one directory to another |
||
| 215 | * |
||
| 216 | * @param string $src - Source of files being moved |
||
| 217 | * @param string $dest - Destination of files being moved |
||
| 218 | * |
||
| 219 | * @return bool true on success |
||
| 220 | */ |
||
| 221 | public static function rmove($src, $dest) |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Recursively copy directories and files from one directory to another |
||
| 255 | * |
||
| 256 | * @param string $src - Source of files being moved |
||
| 257 | * @param string $dest - Destination of files being moved |
||
| 258 | * |
||
| 259 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 260 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 261 | * |
||
| 262 | * @return bool true on success |
||
| 263 | */ |
||
| 264 | public static function rcopy($src, $dest) |
||
| 292 | } |
||
| 293 | } |
||
| 294 |