| Total Complexity | 64 |
| Total Lines | 269 |
| 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 | * @throws \RuntimeException |
||
| 28 | */ |
||
| 29 | public static function createFolder($folder) |
||
| 30 | { |
||
| 31 | try { |
||
| 32 | if (!\is_dir($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 | } catch (\Exception $e) { |
||
| 40 | echo 'Caught exception: ', $e->getMessage(), '<br>'; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param $file |
||
| 46 | * @param $folder |
||
| 47 | * @return bool |
||
| 48 | */ |
||
| 49 | public static function copyFile($file, $folder) |
||
| 50 | { |
||
| 51 | return \copy($file, $folder); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param $src |
||
| 56 | * @param $dst |
||
| 57 | */ |
||
| 58 | public static function recurseCopy($src, $dst) |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Copy a file, or recursively copy a folder and its contents |
||
| 79 | * @param string $source Source path |
||
| 80 | * @param string $dest Destination path |
||
| 81 | * @return bool Returns true on success, false on failure |
||
| 82 | * @author Aidan Lister <[email protected]> |
||
| 83 | * @version 1.0.1 |
||
| 84 | * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
| 85 | */ |
||
| 86 | public static function xcopy($source, $dest) |
||
| 87 | { |
||
| 88 | // Check for symlinks |
||
| 89 | if (\is_link($source)) { |
||
| 90 | return \symlink(\readlink($source), $dest); |
||
| 91 | } |
||
| 92 | |||
| 93 | // Simple copy for a file |
||
| 94 | if (\is_file($source)) { |
||
| 95 | return \copy($source, $dest); |
||
| 96 | } |
||
| 97 | |||
| 98 | // Make destination directory |
||
| 99 | if (!\is_dir($dest)) { |
||
| 100 | if (!mkdir($dest) && !is_dir($dest)) { |
||
| 101 | throw new \RuntimeException(sprintf('Directory "%s" was not created', $dest)); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | // Loop through the folder |
||
| 106 | $dir = \dir($source); |
||
| 107 | if (@\is_dir($dir)) { |
||
| 108 | while (false !== $entry = $dir->read()) { |
||
| 109 | // Skip pointers |
||
| 110 | if ('.' === $entry || '..' === $entry) { |
||
| 111 | continue; |
||
| 112 | } |
||
| 113 | // Deep copy directories |
||
| 114 | self::xcopy("$source/$entry", "$dest/$entry"); |
||
| 115 | } |
||
| 116 | // Clean up |
||
| 117 | $dir->close(); |
||
| 118 | } |
||
| 119 | |||
| 120 | return true; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Remove files and (sub)directories |
||
| 125 | * |
||
| 126 | * @param string $src source directory to delete |
||
| 127 | * |
||
| 128 | * @return bool true on success |
||
| 129 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 130 | * |
||
| 131 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 132 | */ |
||
| 133 | public static function deleteDirectory($src) |
||
| 134 | { |
||
| 135 | // Only continue if user is a 'global' Admin |
||
| 136 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 137 | return false; |
||
| 138 | } |
||
| 139 | |||
| 140 | $success = true; |
||
| 141 | // remove old files |
||
| 142 | $dirInfo = new \SplFileInfo($src); |
||
| 143 | // validate is a directory |
||
| 144 | if ($dirInfo->isDir()) { |
||
| 145 | $fileList = \array_diff(\scandir($src, \SCANDIR_SORT_NONE), ['..', '.']); |
||
| 146 | foreach ($fileList as $k => $v) { |
||
| 147 | $fileInfo = new \SplFileInfo("{$src}/{$v}"); |
||
| 148 | if ($fileInfo->isDir()) { |
||
| 149 | // recursively handle subdirectories |
||
| 150 | if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
||
| 151 | break; |
||
| 152 | } |
||
| 153 | } elseif (!($success = \unlink($fileInfo->getRealPath()))) { |
||
| 154 | break; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | // now delete this (sub)directory if all the files are gone |
||
| 158 | if ($success) { |
||
| 159 | $success = \rmdir($dirInfo->getRealPath()); |
||
| 160 | } |
||
| 161 | } else { |
||
| 162 | // input is not a valid directory |
||
| 163 | $success = false; |
||
| 164 | } |
||
| 165 | |||
| 166 | return $success; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Recursively remove directory |
||
| 171 | * |
||
| 172 | * @todo currently won't remove directories with hidden files, should it? |
||
| 173 | * |
||
| 174 | * @param string $src directory to remove (delete) |
||
| 175 | * |
||
| 176 | * @return bool true on success |
||
| 177 | */ |
||
| 178 | public static function rrmdir($src) |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Recursively move files from one directory to another |
||
| 212 | * |
||
| 213 | * @param string $src - Source of files being moved |
||
| 214 | * @param string $dest - Destination of files being moved |
||
| 215 | * |
||
| 216 | * @return bool true on success |
||
| 217 | */ |
||
| 218 | public static function rmove($src, $dest) |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Recursively copy directories and files from one directory to another |
||
| 252 | * |
||
| 253 | * @param string $src - Source of files being moved |
||
| 254 | * @param string $dest - Destination of files being moved |
||
| 255 | * |
||
| 256 | * @return bool true on success |
||
| 257 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 258 | * |
||
| 259 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 260 | */ |
||
| 261 | public static function rcopy($src, $dest) |
||
| 291 |