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