| Total Complexity | 57 |
| Total Lines | 268 |
| 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 |
||
| 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 | * @return void |
||
| 36 | * @throws \RuntimeException |
||
| 37 | */ |
||
| 38 | public static function createFolder($folder) |
||
| 39 | { |
||
| 40 | try { |
||
| 41 | if (!file_exists($folder)) { |
||
| 42 | if (!mkdir($folder) && !is_dir($folder)) { |
||
| 43 | throw new \RuntimeException(sprintf('Unable to create the %s directory', $folder)); |
||
| 44 | } |
||
| 45 | |||
| 46 | file_put_contents($folder . '/index.html', '<script>history.go(-1);</script>'); |
||
| 47 | } |
||
| 48 | } catch (\Exception $e) { |
||
| 49 | echo 'Caught exception: ', $e->getMessage(), '<br>'; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param $file |
||
| 55 | * @param $folder |
||
| 56 | * @return bool |
||
| 57 | */ |
||
| 58 | public static function copyFile($file, $folder) |
||
| 59 | { |
||
| 60 | return copy($file, $folder); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param $src |
||
| 65 | * @param $dst |
||
| 66 | */ |
||
| 67 | public static function recurseCopy($src, $dst) |
||
| 68 | { |
||
| 69 | $dir = opendir($src); |
||
| 70 | @mkdir($dst); |
||
|
|
|||
| 71 | while (false !== ($file = readdir($dir))) { |
||
| 72 | if (('.' !== $file) && ('..' !== $file)) { |
||
| 73 | if (is_dir($src . '/' . $file)) { |
||
| 74 | self::recurseCopy($src . '/' . $file, $dst . '/' . $file); |
||
| 75 | } else { |
||
| 76 | copy($src . '/' . $file, $dst . '/' . $file); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | closedir($dir); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Copy a file, or recursively copy a folder and its contents |
||
| 85 | * @param string $source Source path |
||
| 86 | * @param string $dest Destination path |
||
| 87 | * @param int $permissions New folder creation permissions |
||
| 88 | * @return bool Returns true on success, false on failure |
||
| 89 | * @version 1.0.1 |
||
| 90 | * @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/ |
||
| 91 | * @author Aidan Lister <[email protected]> |
||
| 92 | */ |
||
| 93 | public static function xcopy($source, $dest) |
||
| 94 | { |
||
| 95 | // Check for symlinks |
||
| 96 | if (is_link($source)) { |
||
| 97 | return symlink(readlink($source), $dest); |
||
| 98 | } |
||
| 99 | |||
| 100 | // Simple copy for a file |
||
| 101 | if (is_file($source)) { |
||
| 102 | return copy($source, $dest); |
||
| 103 | } |
||
| 104 | |||
| 105 | // Make destination directory |
||
| 106 | if (!is_dir($dest)) { |
||
| 107 | mkdir($dest); |
||
| 108 | } |
||
| 109 | |||
| 110 | if (@is_dir($source)) { |
||
| 111 | // Loop through the folder |
||
| 112 | $dir = dir($source); |
||
| 113 | while (false !== $entry = $dir->read()) { |
||
| 114 | // Skip pointers |
||
| 115 | if ('.' === $entry || '..' === $entry) { |
||
| 116 | continue; |
||
| 117 | } |
||
| 118 | // Deep copy directories |
||
| 119 | self::xcopy("$source/$entry", "$dest/$entry"); |
||
| 120 | } |
||
| 121 | // Clean up |
||
| 122 | $dir->close(); |
||
| 123 | } |
||
| 124 | return true; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * |
||
| 129 | * Remove files and (sub)directories |
||
| 130 | * |
||
| 131 | * @param string $src source directory to delete |
||
| 132 | * |
||
| 133 | * @return bool true on success |
||
| 134 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 135 | * |
||
| 136 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 137 | */ |
||
| 138 | public static function deleteDirectory($src) |
||
| 139 | { |
||
| 140 | // Only continue if user is a 'global' Admin |
||
| 141 | if (!($GLOBALS['xoopsUser'] instanceof \XoopsUser) || !$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | $success = true; |
||
| 146 | // remove old files |
||
| 147 | $dirInfo = new \SplFileInfo($src); |
||
| 148 | // validate is a directory |
||
| 149 | if ($dirInfo->isDir()) { |
||
| 150 | $fileList = array_diff(scandir($src, SCANDIR_SORT_NONE), ['..', '.']); |
||
| 151 | foreach ($fileList as $k => $v) { |
||
| 152 | $fileInfo = new \SplFileInfo("{$src}/{$v}"); |
||
| 153 | if ($fileInfo->isDir()) { |
||
| 154 | // recursively handle subdirectories |
||
| 155 | if (!$success = self::deleteDirectory($fileInfo->getRealPath())) { |
||
| 156 | break; |
||
| 157 | } |
||
| 158 | } else { |
||
| 159 | // delete the file |
||
| 160 | if (!($success = unlink($fileInfo->getRealPath()))) { |
||
| 161 | break; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | // now delete this (sub)directory if all the files are gone |
||
| 166 | if ($success) { |
||
| 167 | $success = rmdir($dirInfo->getRealPath()); |
||
| 168 | } |
||
| 169 | } else { |
||
| 170 | // input is not a valid directory |
||
| 171 | $success = false; |
||
| 172 | } |
||
| 173 | return $success; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * |
||
| 178 | * Recursively remove directory |
||
| 179 | * |
||
| 180 | * @todo currently won't remove directories with hidden files, should it? |
||
| 181 | * |
||
| 182 | * @param string $src directory to remove (delete) |
||
| 183 | * |
||
| 184 | * @return bool true on success |
||
| 185 | */ |
||
| 186 | public static function rrmdir($src) |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Recursively move files from one directory to another |
||
| 220 | * |
||
| 221 | * @param string $src - Source of files being moved |
||
| 222 | * @param string $dest - Destination of files being moved |
||
| 223 | * |
||
| 224 | * @return bool true on success |
||
| 225 | */ |
||
| 226 | public static function rmove($src, $dest) |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Recursively copy directories and files from one directory to another |
||
| 260 | * |
||
| 261 | * @param string $src - Source of files being moved |
||
| 262 | * @param string $dest - Destination of files being moved |
||
| 263 | * |
||
| 264 | * @return bool true on success |
||
| 265 | * @uses \Xmf\Module\Helper::isUserAdmin() |
||
| 266 | * |
||
| 267 | * @uses \Xmf\Module\Helper::getHelper() |
||
| 268 | */ |
||
| 269 | public static function rcopy($src, $dest) |
||
| 296 | } |
||
| 297 | } |
||
| 298 |
If you suppress an error, we recommend checking for the error condition explicitly: