| Total Complexity | 54 |
| Total Lines | 227 |
| 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 (\Exception $e) { |
||
| 41 | echo 'Caught exception: ', $e->getMessage(), "\n", '<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 | * @throws \RuntimeException |
||
| 59 | */ |
||
| 60 | public static function recurseCopy($src, $dst) |
||
| 61 | { |
||
| 62 | $dir = opendir($src); |
||
| 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 | * 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 = true; |
||
| 96 | // remove old files |
||
| 97 | $dirInfo = new \SplFileInfo($src); |
||
| 98 | // validate is a directory |
||
| 99 | if ($dirInfo->isDir()) { |
||
| 100 | $fileList = array_diff(scandir($src, SCANDIR_SORT_NONE), ['..', '.']); |
||
| 101 | foreach ($fileList as $k => $v) { |
||
| 102 | $fileInfo = new \SplFileInfo("{$src}/{$v}"); |
||
| 103 | if ($fileInfo->isDir()) { |
||
| 104 | // recursively handle subdirectories |
||
| 105 | if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
||
| 106 | break; |
||
| 107 | } |
||
| 108 | } else { |
||
| 109 | // delete the file |
||
| 110 | if (!($success = unlink($fileInfo->getRealPath()))) { |
||
| 111 | break; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | // now delete this (sub)directory if all the files are gone |
||
| 116 | if ($success) { |
||
| 117 | $success = rmdir($dirInfo->getRealPath()); |
||
| 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) |
||
| 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) |
||
| 249 |