| Total Complexity | 54 |
| Total Lines | 223 |
| 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 declare(strict_types=1); |
||
| 25 | trait FilesManagement |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Function responsible for checking if a directory exists, we can also write in and create an index.html file |
||
| 29 | * |
||
| 30 | * @param string $folder The full path of the directory to check |
||
| 31 | */ |
||
| 32 | public static function createFolder($folder): void |
||
| 33 | { |
||
| 34 | try { |
||
| 35 | if (!\is_dir($folder)) { |
||
| 36 | if (!\is_dir($folder) && !\mkdir($folder) && !\is_dir($folder)) { |
||
| 37 | throw new RuntimeException(\sprintf('Unable to create the %s directory', $folder)); |
||
| 38 | } |
||
| 39 | |||
| 40 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
| 41 | } |
||
| 42 | } catch (\Throwable $e) { |
||
| 43 | echo 'Caught exception: ', $e->getMessage(), "\n", '<br>'; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param $file |
||
| 49 | * @param $folder |
||
| 50 | * @return bool |
||
| 51 | */ |
||
| 52 | public static function copyFile(string $file, string $folder): bool |
||
| 53 | { |
||
| 54 | return \copy($file, $folder); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param $src |
||
| 59 | * @param $dst |
||
| 60 | */ |
||
| 61 | public static function recurseCopy($src, $dst): void |
||
| 62 | { |
||
| 63 | $dir = \opendir($src); |
||
| 64 | // @mkdir($dst); |
||
| 65 | if (!@\mkdir($dst) && !\is_dir($dst)) { |
||
| 66 | throw new RuntimeException('The directory ' . $dst . ' could not be created.'); |
||
| 67 | } |
||
| 68 | while (false !== ($file = \readdir($dir))) { |
||
| 69 | if (('.' !== $file) && ('..' !== $file)) { |
||
| 70 | if (\is_dir($src . '/' . $file)) { |
||
| 71 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
| 72 | } else { |
||
| 73 | \copy($src . '/' . $file, $dst . '/' . $file); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } |
||
| 77 | \closedir($dir); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Remove files and (sub)directories |
||
| 82 | * |
||
| 83 | * @param string $src source directory to delete |
||
| 84 | * |
||
| 85 | * @return bool true on success |
||
| 86 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 87 | * |
||
| 88 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 89 | */ |
||
| 90 | public static function deleteDirectory($src) |
||
| 91 | { |
||
| 92 | // Only continue if user is a 'global' Admin |
||
| 93 | if (!($GLOBALS['xoopsUser'] instanceof XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 94 | return false; |
||
| 95 | } |
||
| 96 | |||
| 97 | $success = true; |
||
| 98 | // remove old files |
||
| 99 | $dirInfo = new SplFileInfo($src); |
||
| 100 | // validate is a directory |
||
| 101 | if ($dirInfo->isDir()) { |
||
| 102 | $fileList = \array_diff(\scandir($src, \SCANDIR_SORT_NONE), ['..', '.']); |
||
| 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; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | // now delete this (sub)directory if all the files are gone |
||
| 115 | if ($success) { |
||
| 116 | $success = \rmdir($dirInfo->getRealPath()); |
||
| 117 | } |
||
| 118 | } else { |
||
| 119 | // input is not a valid directory |
||
| 120 | $success = false; |
||
| 121 | } |
||
| 122 | |||
| 123 | return $success; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Recursively remove directory |
||
| 128 | * |
||
| 129 | * @todo currently won't remove directories with hidden files, should it? |
||
| 130 | * |
||
| 131 | * @param string $src directory to remove (delete) |
||
| 132 | * |
||
| 133 | * @return bool true on success |
||
| 134 | */ |
||
| 135 | 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) |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Recursively copy directories and files from one directory to another |
||
| 211 | * |
||
| 212 | * @param string $src - Source of files being moved |
||
| 213 | * @param string $dest - Destination of files being moved |
||
| 214 | * |
||
| 215 | * @return bool true on success |
||
| 216 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 217 | * |
||
| 218 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 219 | */ |
||
| 220 | public static function rcopy($src, $dest) |
||
| 248 | } |
||
| 249 | } |
||
| 250 |