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 |
||
| 48 | class Tar { |
||
| 49 | |||
| 50 | const COMPRESS_AUTO = 0; |
||
| 51 | const COMPRESS_NONE = 1; |
||
| 52 | const COMPRESS_GZIP = 2; |
||
| 53 | const COMPRESS_BZIP = 3; |
||
| 54 | |||
| 55 | protected $file = ''; |
||
| 56 | protected $comptype = Tar::COMPRESS_AUTO; |
||
| 57 | /** @var resource|int */ |
||
| 58 | protected $fh; |
||
| 59 | protected $memory = ''; |
||
| 60 | protected $closed = true; |
||
| 61 | protected $writeaccess = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Open an existing TAR file for reading |
||
| 65 | * |
||
| 66 | * @param string $file |
||
| 67 | * @param int $comptype |
||
| 68 | * @throws TarIOException |
||
| 69 | */ |
||
| 70 | public function open($file, $comptype = Tar::COMPRESS_AUTO) { |
||
| 71 | // determine compression |
||
| 72 | if($comptype == Tar::COMPRESS_AUTO) $comptype = $this->filetype($file); |
||
| 73 | $this->compressioncheck($comptype); |
||
| 74 | |||
| 75 | $this->comptype = $comptype; |
||
| 76 | $this->file = $file; |
||
| 77 | |||
| 78 | if($this->comptype === Tar::COMPRESS_GZIP) { |
||
| 79 | $this->fh = @gzopen($this->file, 'rb'); |
||
| 80 | } elseif($this->comptype === Tar::COMPRESS_BZIP) { |
||
| 81 | $this->fh = @bzopen($this->file, 'r'); |
||
| 82 | } else { |
||
| 83 | $this->fh = @fopen($this->file, 'rb'); |
||
| 84 | } |
||
| 85 | |||
| 86 | if(!$this->fh) throw new TarIOException('Could not open file for reading: '.$this->file); |
||
| 87 | $this->closed = false; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Read the contents of a TAR archive |
||
| 92 | * |
||
| 93 | * This function lists the files stored in the archive, and returns an indexed array of associative |
||
| 94 | * arrays containing for each file the following information: |
||
| 95 | * |
||
| 96 | * checksum Tar Checksum of the file |
||
| 97 | * filename The full name of the stored file (up to 100 c.) |
||
| 98 | * mode UNIX permissions in DECIMAL, not octal |
||
| 99 | * uid The Owner ID |
||
| 100 | * gid The Group ID |
||
| 101 | * size Uncompressed filesize |
||
| 102 | * mtime Timestamp of last modification |
||
| 103 | * typeflag Empty for files, set for folders |
||
| 104 | * link Is it a symlink? |
||
| 105 | * uname Owner name |
||
| 106 | * gname Group name |
||
| 107 | * |
||
| 108 | * The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams. |
||
| 109 | * Reopen the file with open() again if you want to do additional operations |
||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | * @throws TarIOException |
||
| 113 | */ |
||
| 114 | public function contents() { |
||
| 115 | if($this->closed || !$this->file) throw new TarIOException('Can not read from a closed archive'); |
||
| 116 | |||
| 117 | $result = array(); |
||
| 118 | while($read = $this->readbytes(512)) { |
||
| 119 | $header = $this->parseHeader($read); |
||
| 120 | if(!is_array($header)) continue; |
||
| 121 | |||
| 122 | $this->skipbytes(ceil($header['size'] / 512) * 512); |
||
| 123 | $result[] = $header; |
||
| 124 | } |
||
| 125 | |||
| 126 | $this->close(); |
||
| 127 | return $result; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Extract an existing TAR archive |
||
| 132 | * |
||
| 133 | * The $strip parameter allows you to strip a certain number of path components from the filenames |
||
| 134 | * found in the tar file, similar to the --strip-components feature of GNU tar. This is triggered when |
||
| 135 | * an integer is passed as $strip. |
||
| 136 | * Alternatively a fixed string prefix may be passed in $strip. If the filename matches this prefix, |
||
| 137 | * the prefix will be stripped. It is recommended to give prefixes with a trailing slash. |
||
| 138 | * |
||
| 139 | * By default this will extract all files found in the archive. You can restrict the output using the $include |
||
| 140 | * and $exclude parameter. Both expect a full regular expression (including delimiters and modifiers). If |
||
| 141 | * $include is set only files that match this expression will be extracted. Files that match the $exclude |
||
| 142 | * expression will never be extracted. Both parameters can be used in combination. Expressions are matched against |
||
| 143 | * stripped filenames as described above. |
||
| 144 | * |
||
| 145 | * The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams. |
||
| 146 | * Reopen the file with open() again if you want to do additional operations |
||
| 147 | * |
||
| 148 | * @param string $outdir the target directory for extracting |
||
| 149 | * @param int|string $strip either the number of path components or a fixed prefix to strip |
||
| 150 | * @param string $exclude a regular expression of files to exclude |
||
| 151 | * @param string $include a regular expression of files to include |
||
| 152 | * @throws TarIOException |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | function extract($outdir, $strip = '', $exclude = '', $include = '') { |
||
|
|
|||
| 156 | if($this->closed || !$this->file) throw new TarIOException('Can not read from a closed archive'); |
||
| 157 | |||
| 158 | $outdir = rtrim($outdir, '/'); |
||
| 159 | io_mkdir_p($outdir); |
||
| 160 | $striplen = strlen($strip); |
||
| 161 | |||
| 162 | $extracted = array(); |
||
| 163 | |||
| 164 | while($dat = $this->readbytes(512)) { |
||
| 165 | // read the file header |
||
| 166 | $header = $this->parseHeader($dat); |
||
| 167 | if(!is_array($header)) continue; |
||
| 168 | if(!$header['filename']) continue; |
||
| 169 | |||
| 170 | // strip prefix |
||
| 171 | $filename = $this->cleanPath($header['filename']); |
||
| 172 | if(is_int($strip)) { |
||
| 173 | // if $strip is an integer we strip this many path components |
||
| 174 | $parts = explode('/', $filename); |
||
| 175 | if(!$header['typeflag']) { |
||
| 176 | $base = array_pop($parts); // keep filename itself |
||
| 177 | } else { |
||
| 178 | $base = ''; |
||
| 179 | } |
||
| 180 | $filename = join('/', array_slice($parts, $strip)); |
||
| 181 | if($base) $filename .= "/$base"; |
||
| 182 | } else { |
||
| 183 | // ifstrip is a string, we strip a prefix here |
||
| 184 | if(substr($filename, 0, $striplen) == $strip) $filename = substr($filename, $striplen); |
||
| 185 | } |
||
| 186 | |||
| 187 | // check if this should be extracted |
||
| 188 | $extract = true; |
||
| 189 | if(!$filename) { |
||
| 190 | $extract = false; |
||
| 191 | } else { |
||
| 192 | if($include) { |
||
| 193 | if(preg_match($include, $filename)) { |
||
| 194 | $extract = true; |
||
| 195 | } else { |
||
| 196 | $extract = false; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | if($exclude && preg_match($exclude, $filename)) { |
||
| 200 | $extract = false; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | // Now do the extraction (or not) |
||
| 205 | if($extract) { |
||
| 206 | $extracted[] = $header; |
||
| 207 | |||
| 208 | $output = "$outdir/$filename"; |
||
| 209 | $directory = ($header['typeflag']) ? $output : dirname($output); |
||
| 210 | io_mkdir_p($directory); |
||
| 211 | |||
| 212 | // is this a file? |
||
| 213 | if(!$header['typeflag']) { |
||
| 214 | $fp = fopen($output, "wb"); |
||
| 215 | if(!$fp) throw new TarIOException('Could not open file for writing: '.$output); |
||
| 216 | |||
| 217 | $size = floor($header['size'] / 512); |
||
| 218 | for($i = 0; $i < $size; $i++) { |
||
| 219 | fwrite($fp, $this->readbytes(512), 512); |
||
| 220 | } |
||
| 221 | if(($header['size'] % 512) != 0) fwrite($fp, $this->readbytes(512), $header['size'] % 512); |
||
| 222 | |||
| 223 | fclose($fp); |
||
| 224 | touch($output, $header['mtime']); |
||
| 225 | chmod($output, $header['perm']); |
||
| 226 | } else { |
||
| 227 | $this->skipbytes(ceil($header['size'] / 512) * 512); // the size is usually 0 for directories |
||
| 228 | } |
||
| 229 | } else { |
||
| 230 | $this->skipbytes(ceil($header['size'] / 512) * 512); |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | $this->close(); |
||
| 235 | return $extracted; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Create a new TAR file |
||
| 240 | * |
||
| 241 | * If $file is empty, the tar file will be created in memory |
||
| 242 | * |
||
| 243 | * @param string $file |
||
| 244 | * @param int $comptype |
||
| 245 | * @param int $complevel |
||
| 246 | * @throws TarIOException |
||
| 247 | * @throws TarIllegalCompressionException |
||
| 248 | */ |
||
| 249 | public function create($file = '', $comptype = Tar::COMPRESS_AUTO, $complevel = 9) { |
||
| 250 | // determine compression |
||
| 251 | if($comptype == Tar::COMPRESS_AUTO) $comptype = $this->filetype($file); |
||
| 252 | $this->compressioncheck($comptype); |
||
| 253 | |||
| 254 | $this->comptype = $comptype; |
||
| 255 | $this->file = $file; |
||
| 256 | $this->memory = ''; |
||
| 257 | $this->fh = 0; |
||
| 258 | |||
| 259 | if($this->file) { |
||
| 260 | if($this->comptype === Tar::COMPRESS_GZIP) { |
||
| 261 | $this->fh = @gzopen($this->file, 'wb'.$complevel); |
||
| 262 | } elseif($this->comptype === Tar::COMPRESS_BZIP) { |
||
| 263 | $this->fh = @bzopen($this->file, 'w'); |
||
| 264 | } else { |
||
| 265 | $this->fh = @fopen($this->file, 'wb'); |
||
| 266 | } |
||
| 267 | |||
| 268 | if(!$this->fh) throw new TarIOException('Could not open file for writing: '.$this->file); |
||
| 269 | } |
||
| 270 | $this->writeaccess = true; |
||
| 271 | $this->closed = false; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Add a file to the current TAR archive using an existing file in the filesystem |
||
| 276 | * |
||
| 277 | * @todo handle directory adding |
||
| 278 | * |
||
| 279 | * @param string $file the original file |
||
| 280 | * @param string $name the name to use for the file in the archive |
||
| 281 | * @throws TarIOException |
||
| 282 | */ |
||
| 283 | public function addFile($file, $name = '') { |
||
| 284 | if($this->closed) throw new TarIOException('Archive has been closed, files can no longer be added'); |
||
| 285 | |||
| 286 | if(!$name) $name = $file; |
||
| 287 | $name = $this->cleanPath($name); |
||
| 288 | |||
| 289 | $fp = fopen($file, 'rb'); |
||
| 290 | if(!$fp) throw new TarIOException('Could not open file for reading: '.$file); |
||
| 291 | |||
| 292 | // create file header and copy all stat info from the original file |
||
| 293 | clearstatcache(false, $file); |
||
| 294 | $stat = stat($file); |
||
| 295 | $this->writeFileHeader( |
||
| 296 | $name, |
||
| 297 | $stat[4], |
||
| 298 | $stat[5], |
||
| 299 | fileperms($file), |
||
| 300 | filesize($file), |
||
| 301 | filemtime($file) |
||
| 302 | ); |
||
| 303 | |||
| 304 | while(!feof($fp)) { |
||
| 305 | $data = fread($fp, 512); |
||
| 306 | if($data === false) break; |
||
| 307 | if($data === '') break; |
||
| 308 | $packed = pack("a512", $data); |
||
| 309 | $this->writebytes($packed); |
||
| 310 | } |
||
| 311 | fclose($fp); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Add a file to the current TAR archive using the given $data as content |
||
| 316 | * |
||
| 317 | * @param string $name |
||
| 318 | * @param string $data |
||
| 319 | * @param int $uid |
||
| 320 | * @param int $gid |
||
| 321 | * @param int $perm |
||
| 322 | * @param int $mtime |
||
| 323 | * @throws TarIOException |
||
| 324 | */ |
||
| 325 | public function addData($name, $data, $uid = 0, $gid = 0, $perm = 0666, $mtime = 0) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Add the closing footer to the archive if in write mode, close all file handles |
||
| 347 | * |
||
| 348 | * After a call to this function no more data can be added to the archive, for |
||
| 349 | * read access no reading is allowed anymore |
||
| 350 | * |
||
| 351 | * "Physically, an archive consists of a series of file entries terminated by an end-of-archive entry, which |
||
| 352 | * consists of two 512 blocks of zero bytes" |
||
| 353 | * |
||
| 354 | * @link http://www.gnu.org/software/tar/manual/html_chapter/tar_8.html#SEC134 |
||
| 355 | */ |
||
| 356 | public function close() { |
||
| 357 | if($this->closed) return; // we did this already |
||
| 358 | |||
| 359 | // write footer |
||
| 360 | if($this->writeaccess) { |
||
| 361 | $this->writebytes(pack("a512", "")); |
||
| 362 | $this->writebytes(pack("a512", "")); |
||
| 363 | } |
||
| 364 | |||
| 365 | // close file handles |
||
| 366 | if($this->file) { |
||
| 367 | if($this->comptype === Tar::COMPRESS_GZIP) { |
||
| 368 | gzclose($this->fh); |
||
| 369 | } elseif($this->comptype === Tar::COMPRESS_BZIP) { |
||
| 370 | bzclose($this->fh); |
||
| 371 | } else { |
||
| 372 | fclose($this->fh); |
||
| 373 | } |
||
| 374 | |||
| 375 | $this->file = ''; |
||
| 376 | $this->fh = 0; |
||
| 377 | } |
||
| 378 | |||
| 379 | $this->closed = true; |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns the created in-memory archive data |
||
| 384 | * |
||
| 385 | * This implicitly calls close() on the Archive |
||
| 386 | * |
||
| 387 | * @param int $comptype |
||
| 388 | * @param int $complevel |
||
| 389 | * @return mixed|string |
||
| 390 | */ |
||
| 391 | public function getArchive($comptype = Tar::COMPRESS_AUTO, $complevel = 9) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Save the created in-memory archive data |
||
| 404 | * |
||
| 405 | * Note: It more memory effective to specify the filename in the create() function and |
||
| 406 | * let the library work on the new file directly. |
||
| 407 | * |
||
| 408 | * @param string $file |
||
| 409 | * @param int $comptype |
||
| 410 | * @param int $complevel |
||
| 411 | * @throws TarIOException |
||
| 412 | */ |
||
| 413 | public function save($file, $comptype = Tar::COMPRESS_AUTO, $complevel = 9) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Read from the open file pointer |
||
| 423 | * |
||
| 424 | * @param int $length bytes to read |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | protected function readbytes($length) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Write to the open filepointer or memory |
||
| 439 | * |
||
| 440 | * @param string $data |
||
| 441 | * @throws TarIOException |
||
| 442 | * @return int number of bytes written |
||
| 443 | */ |
||
| 444 | protected function writebytes($data) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Skip forward in the open file pointer |
||
| 461 | * |
||
| 462 | * This is basically a wrapper around seek() (and a workaround for bzip2) |
||
| 463 | * |
||
| 464 | * @param int $bytes seek to this position |
||
| 465 | */ |
||
| 466 | function skipbytes($bytes) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Write a file header |
||
| 479 | * |
||
| 480 | * @param string $name |
||
| 481 | * @param int $uid |
||
| 482 | * @param int $gid |
||
| 483 | * @param int $perm |
||
| 484 | * @param int $size |
||
| 485 | * @param int $mtime |
||
| 486 | * @param string $typeflag Set to '5' for directories |
||
| 487 | */ |
||
| 488 | protected function writeFileHeader($name, $uid, $gid, $perm, $size, $mtime, $typeflag = '') { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Decode the given tar file header |
||
| 533 | * |
||
| 534 | * @param string $block a 512 byte block containign the header data |
||
| 535 | * @return false|array |
||
| 536 | */ |
||
| 537 | protected function parseHeader($block) { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Cleans up a path and removes relative parts, also strips leading slashes |
||
| 583 | * |
||
| 584 | * @param string $path |
||
| 585 | * @return string |
||
| 586 | */ |
||
| 587 | public function cleanPath($path) { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Checks if the given compression type is available and throws an exception if not |
||
| 603 | * |
||
| 604 | * @param int $comptype |
||
| 605 | * @throws TarIllegalCompressionException |
||
| 606 | */ |
||
| 607 | protected function compressioncheck($comptype) { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Guesses the wanted compression from the given filename extension |
||
| 619 | * |
||
| 620 | * You don't need to call this yourself. It's used when you pass Tar::COMPRESS_AUTO somewhere |
||
| 621 | * |
||
| 622 | * @param string $file |
||
| 623 | * @return int |
||
| 624 | */ |
||
| 625 | public function filetype($file) { |
||
| 636 | } |
||
| 637 | |||
| 649 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.