| Total Complexity | 56 |
| Total Lines | 227 |
| 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 | public static function createFolder($folder) |
||
| 28 | { |
||
| 29 | try { |
||
| 30 | if (!file_exists($folder)) { |
||
| 31 | if (!is_dir($folder) && !mkdir($folder) && !is_dir($folder)) { |
||
| 32 | throw new \RuntimeException(sprintf('Unable to create the %s directory', $folder)); |
||
| 33 | } |
||
| 34 | |||
| 35 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | catch (\Exception $e) { |
||
| 39 | echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param $file |
||
| 45 | * @param $folder |
||
| 46 | * @return bool |
||
| 47 | */ |
||
| 48 | public static function copyFile($file, $folder) |
||
| 49 | { |
||
| 50 | return copy($file, $folder); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param $src |
||
| 55 | * @param $dst |
||
| 56 | */ |
||
| 57 | public static function recurseCopy($src, $dst) |
||
| 58 | { |
||
| 59 | $dir = opendir($src); |
||
| 60 | // @mkdir($dst); |
||
| 61 | if (!@mkdir($dst) && !is_dir($dst)) { |
||
| 62 | throw new \RuntimeException('The directory ' . $dst . ' could not be created.'); |
||
| 63 | } |
||
| 64 | if (false !== $dir) { |
||
| 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 | /** |
||
| 79 | * Remove files and (sub)directories |
||
| 80 | * |
||
| 81 | * @param string $src source directory to delete |
||
| 82 | * |
||
| 83 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 84 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 85 | * |
||
| 86 | * @return bool true on success |
||
| 87 | */ |
||
| 88 | public static function deleteDirectory($src) |
||
| 89 | { |
||
| 90 | // Only continue if user is a 'global' Admin |
||
| 91 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 92 | return false; |
||
| 93 | } |
||
| 94 | |||
| 95 | $success = false; |
||
| 96 | // remove old files |
||
| 97 | $dirInfo = new \SplFileInfo($src); |
||
| 98 | // validate is a directory |
||
| 99 | if ($dirInfo->isDir()) { |
||
| 100 | $dirScan = scandir($src, SCANDIR_SORT_NONE); |
||
| 101 | if (false !== $dirScan) { |
||
| 102 | $fileList = array_diff($dirScan, ['..', '.']); |
||
| 103 | foreach ($fileList as $k => $v) { |
||
| 104 | $fileInfo = new \SplFileInfo("{$src}/{$v}"); |
||
| 105 | if ($fileInfo->isDir()) { |
||
| 106 | // recursively handle subdirectories |
||
| 107 | if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
||
| 108 | break; |
||
| 109 | } |
||
| 110 | } elseif (!($success = unlink($fileInfo->getRealPath()))) { |
||
| 111 | break; // delete the file |
||
| 112 | } |
||
| 113 | } |
||
| 114 | // now delete this (sub)directory if all the files are gone |
||
| 115 | if ($success) { |
||
| 116 | $success = rmdir($dirInfo->getRealPath()); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | // } else { |
||
| 120 | // // input is not a valid directory |
||
| 121 | // $success = false; |
||
| 122 | } |
||
| 123 | |||
| 124 | return $success; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Recursively remove directory |
||
| 129 | * |
||
| 130 | * @todo currently won't remove directories with hidden files, should it? |
||
| 131 | * |
||
| 132 | * @param string $src directory to remove (delete) |
||
| 133 | * |
||
| 134 | * @return bool true on success |
||
| 135 | */ |
||
| 136 | public static function rrmdir($src) |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Recursively move files from one directory to another |
||
| 170 | * |
||
| 171 | * @param string $src - Source of files being moved |
||
| 172 | * @param string $dest - Destination of files being moved |
||
| 173 | * |
||
| 174 | * @return bool true on success |
||
| 175 | */ |
||
| 176 | public static function rmove($src, $dest) |
||
| 177 | { |
||
| 178 | // Only continue if user is a 'global' Admin |
||
| 179 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 180 | return false; |
||
| 181 | } |
||
| 182 | |||
| 183 | // If source is not a directory stop processing |
||
| 184 | if (!is_dir($src)) { |
||
| 185 | return false; |
||
| 186 | } |
||
| 187 | |||
| 188 | // If the destination directory does not exist and could not be created stop processing |
||
| 189 | if (!is_dir($dest) && !mkdir($dest) && !is_dir($dest)) { |
||
| 190 | return false; |
||
| 191 | } |
||
| 192 | |||
| 193 | // Open the source directory to read in files |
||
| 194 | $iterator = new \DirectoryIterator($src); |
||
| 195 | foreach ($iterator as $fObj) { |
||
| 196 | if ($fObj->isFile()) { |
||
| 197 | rename($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 198 | } elseif (!$fObj->isDot() && $fObj->isDir()) { |
||
| 199 | // Try recursively on directory |
||
| 200 | self::rmove($fObj->getPathname(), "{$dest}/" . $fObj->getFilename()); |
||
| 201 | // rmdir($fObj->getPath()); // now delete the directory |
||
| 202 | } |
||
| 203 | } |
||
| 204 | $iterator = null; // clear iterator Obj to close file/directory |
||
| 205 | return rmdir($src); // remove the directory & return results |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Recursively copy directories and files from one directory to another |
||
| 210 | * |
||
| 211 | * @param string $src - Source of files being moved |
||
| 212 | * @param string $dest - Destination of files being moved |
||
| 213 | * |
||
| 214 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 215 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 216 | * |
||
| 217 | * @return bool true on success |
||
| 218 | */ |
||
| 219 | public static function rcopy($src, $dest) |
||
| 247 | } |
||
| 248 | } |
||
| 249 |