Complex classes like TAR 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 TAR, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class TAR extends Archive { |
||
| 38 | const PLAIN = 0; |
||
| 39 | const GZIP = 1; |
||
| 40 | const BZIP = 2; |
||
| 41 | |||
| 42 | private $fileList; |
||
| 43 | private $cachedHeaders; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \Archive_Tar tar |
||
| 47 | */ |
||
| 48 | private $tar = null; |
||
| 49 | private $path; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $source |
||
| 53 | */ |
||
| 54 | public function __construct($source) { |
||
| 55 | $types = array(null, 'gz', 'bz2'); |
||
| 56 | $this->path = $source; |
||
| 57 | $this->tar = new \Archive_Tar($source, $types[self::getTarType($source)]); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * try to detect the type of tar compression |
||
| 62 | * |
||
| 63 | * @param string $file |
||
| 64 | * @return integer |
||
| 65 | */ |
||
| 66 | static public function getTarType($file) { |
||
|
|
|||
| 67 | if (strpos($file, '.')) { |
||
| 68 | $extension = substr($file, strrpos($file, '.')); |
||
| 69 | switch ($extension) { |
||
| 70 | case '.gz': |
||
| 71 | case '.tgz': |
||
| 72 | return self::GZIP; |
||
| 73 | case '.bz': |
||
| 74 | case '.bz2': |
||
| 75 | return self::BZIP; |
||
| 76 | case '.tar': |
||
| 77 | return self::PLAIN; |
||
| 78 | default: |
||
| 79 | return self::PLAIN; |
||
| 80 | } |
||
| 81 | } else { |
||
| 82 | return self::PLAIN; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * add an empty folder to the archive |
||
| 88 | * |
||
| 89 | * @param string $path |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function addFolder($path) { |
||
| 93 | $tmpBase = \OC::$server->getTempManager()->getTemporaryFolder(); |
||
| 94 | $path = rtrim($path, '/') . '/'; |
||
| 95 | if ($this->fileExists($path)) { |
||
| 96 | return false; |
||
| 97 | } |
||
| 98 | $parts = explode('/', $path); |
||
| 99 | $folder = $tmpBase; |
||
| 100 | foreach ($parts as $part) { |
||
| 101 | $folder .= '/' . $part; |
||
| 102 | if (!is_dir($folder)) { |
||
| 103 | mkdir($folder); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | $result = $this->tar->addModify(array($tmpBase . $path), '', $tmpBase); |
||
| 107 | rmdir($tmpBase . $path); |
||
| 108 | $this->fileList = false; |
||
| 109 | $this->cachedHeaders = false; |
||
| 110 | return $result; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * add a file to the archive |
||
| 115 | * |
||
| 116 | * @param string $path |
||
| 117 | * @param string $source either a local file or string data |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | public function addFile($path, $source = '') { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * rename a file or folder in the archive |
||
| 135 | * |
||
| 136 | * @param string $source |
||
| 137 | * @param string $dest |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public function rename($source, $dest) { |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $file |
||
| 157 | */ |
||
| 158 | private function getHeader($file) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * get the uncompressed size of a file in the archive |
||
| 176 | * |
||
| 177 | * @param string $path |
||
| 178 | * @return int |
||
| 179 | */ |
||
| 180 | public function filesize($path) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * get the last modified time of a file in the archive |
||
| 187 | * |
||
| 188 | * @param string $path |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | public function mtime($path) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * get the files in a folder |
||
| 198 | * |
||
| 199 | * @param string $path |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function getFolder($path) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * get all files in the archive |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function getFiles() { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * get the content of a file |
||
| 245 | * |
||
| 246 | * @param string $path |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getFile($path) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * extract a single file from the archive |
||
| 255 | * |
||
| 256 | * @param string $path |
||
| 257 | * @param string $dest |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | public function extractFile($path, $dest) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * extract the archive |
||
| 279 | * |
||
| 280 | * @param string $dest |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function extract($dest) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * check if a file or folder exists in the archive |
||
| 289 | * |
||
| 290 | * @param string $path |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | public function fileExists($path) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * remove a file or folder from the archive |
||
| 315 | * |
||
| 316 | * @param string $path |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function remove($path) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * get a file handler |
||
| 338 | * |
||
| 339 | * @param string $path |
||
| 340 | * @param string $mode |
||
| 341 | * @return resource |
||
| 342 | */ |
||
| 343 | public function getStream($path, $mode) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * write back temporary files |
||
| 367 | */ |
||
| 368 | public function writeBack($tmpFile, $path) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * reopen the archive to ensure everything is written |
||
| 375 | */ |
||
| 376 | private function reopen() { |
||
| 384 | } |
||
| 385 |