Total Complexity | 103 |
Total Lines | 612 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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.
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 |
||
19 | class Tar extends BasicFormat |
||
20 | { |
||
21 | const TAR = 'tar'; |
||
22 | const TAR_GZIP = 'tgz'; |
||
23 | const TAR_BZIP = 'tbz2'; |
||
24 | const TAR_LZMA = 'txz'; |
||
25 | const TAR_LZW = 'tar.z'; |
||
26 | |||
27 | /** @var bool */ |
||
28 | static protected $enabledPearTar; |
||
29 | |||
30 | /** @var bool */ |
||
31 | static protected $enabledPharData; |
||
32 | /** |
||
33 | * Checks system configuration for available Tar-manipulation libraries |
||
34 | */ |
||
35 | protected static function checkRequirements() |
||
40 | } |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Checks whether archive can be opened with current system configuration |
||
45 | * @param $archiveFileName |
||
46 | * @return boolean |
||
47 | */ |
||
48 | // public static function canOpenArchive($archiveFileName) |
||
49 | // { |
||
50 | // self::checkRequirements(); |
||
51 | // |
||
52 | // $type = self::detectArchiveType($archiveFileName); |
||
53 | // if ($type !== false) { |
||
54 | // return self::canOpenType($type); |
||
55 | // } |
||
56 | // |
||
57 | // return false; |
||
58 | // } |
||
59 | |||
60 | /** |
||
61 | * Detect archive type by its filename or content. |
||
62 | * @param $archiveFileName |
||
63 | * @param bool $contentCheck |
||
64 | * @return string|boolean One of TarArchive type constants OR false if type is not detected |
||
65 | */ |
||
66 | public static function detectArchiveType($archiveFileName, $contentCheck = true) |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Checks whether specific archive type can be opened with current system configuration |
||
107 | * @param $type |
||
108 | * @return boolean |
||
109 | */ |
||
110 | // public static function canOpenType($type) |
||
111 | // { |
||
112 | // self::checkRequirements(); |
||
113 | // switch ($type) { |
||
114 | // case self::TAR: |
||
115 | // return self::$enabledPearTar || self::$enabledPharData; |
||
116 | // |
||
117 | // case self::TAR_GZIP: |
||
118 | // return (self::$enabledPearTar || self::$enabledPharData) && extension_loaded('zlib'); |
||
119 | // |
||
120 | // case self::TAR_BZIP: |
||
121 | // return (self::$enabledPearTar || self::$enabledPharData) && extension_loaded('bz2'); |
||
122 | // |
||
123 | // |
||
124 | // case self::TAR_LZMA: |
||
125 | // return self::$enabledPearTar && extension_loaded('lzma2'); |
||
126 | // |
||
127 | // case self::TAR_LZW: |
||
128 | // return self::$enabledPearTar && LzwStreamWrapper::isBinaryAvailable(); |
||
129 | // } |
||
130 | // |
||
131 | // return false; |
||
132 | // } |
||
133 | |||
134 | /** |
||
135 | * @param array $files |
||
136 | * @param string $archiveFileName |
||
137 | * @return false|int |
||
138 | * @throws Exception |
||
139 | */ |
||
140 | public static function createArchive(array $files, $archiveFileName) |
||
141 | { |
||
142 | static::checkRequirements(); |
||
143 | |||
144 | if (static::$enabledPharData) |
||
145 | return static::createArchiveForPhar($files, $archiveFileName); |
||
146 | |||
147 | if (static::$enabledPearTar) |
||
148 | return static::createArchiveForPear($files, $archiveFileName); |
||
149 | |||
150 | throw new Exception('Archive_Tar nor PharData not available'); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Creates an archive via Pear library |
||
155 | * @param array $files |
||
156 | * @param $archiveFileName |
||
157 | * @return bool|int |
||
158 | * @throws Exception |
||
159 | */ |
||
160 | protected static function createArchiveForPear(array $files, $archiveFileName) |
||
161 | { |
||
162 | $compression = null; |
||
163 | switch (strtolower(pathinfo($archiveFileName, PATHINFO_EXTENSION))) { |
||
164 | case 'gz': |
||
165 | case 'tgz': |
||
166 | $compression = 'gz'; |
||
167 | break; |
||
168 | case 'bz2': |
||
169 | case 'tbz2': |
||
170 | $compression = 'bz2'; |
||
171 | break; |
||
172 | case 'xz': |
||
173 | $compression = 'lzma2'; |
||
174 | break; |
||
175 | case 'z': |
||
176 | $tar_aname = 'compress.lzw://' . $archiveFileName; |
||
177 | break; |
||
178 | } |
||
179 | |||
180 | if (isset($tar_aname)) |
||
181 | $tar = new Archive_Tar($tar_aname, $compression); |
||
182 | else |
||
183 | $tar = new Archive_Tar($archiveFileName, $compression); |
||
184 | |||
185 | foreach ($files as $localName => $filename) { |
||
186 | $remove_dir = dirname($filename); |
||
187 | $add_dir = dirname($localName); |
||
188 | |||
189 | if (is_null($filename)) { |
||
190 | if ($tar->addString($localName, '') === false) |
||
191 | throw new Exception('Error when adding directory '.$localName.' to archive'); |
||
192 | } else { |
||
193 | if ($tar->addModify($filename, $add_dir, $remove_dir) === false) |
||
194 | throw new Exception('Error when adding file '.$filename.' to archive'); |
||
195 | } |
||
196 | } |
||
197 | $tar = null; |
||
198 | |||
199 | return count($files); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Creates an archive via Phar library |
||
204 | * @param array $files |
||
205 | * @param $archiveFileName |
||
206 | * @return bool |
||
207 | */ |
||
208 | protected static function createArchiveForPhar(array $files, $archiveFileName) |
||
209 | { |
||
210 | if (preg_match('~^(.+)\.(tar\.(gz|bz2))$~i', $archiveFileName, $match)) { |
||
211 | $ext = $match[2]; |
||
212 | $basename = $match[1]; |
||
213 | } else { |
||
214 | $ext = pathinfo($archiveFileName, PATHINFO_EXTENSION); |
||
215 | $basename = dirname($archiveFileName).'/'.basename($archiveFileName, '.'.$ext); |
||
216 | } |
||
217 | $tar = new PharData($basename.'.tar', 0, null, Phar::TAR); |
||
218 | |||
219 | try { |
||
220 | foreach ($files as $localName => $filename) { |
||
221 | if (is_null($filename)) { |
||
222 | if (!in_array($localName, ['/', ''], true)) { |
||
223 | if ($tar->addEmptyDir($localName) === false) { |
||
224 | return false; |
||
225 | } |
||
226 | } |
||
227 | } else { |
||
228 | if ($tar->addFile($filename, $localName) === false) { |
||
229 | return false; |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | } catch (Exception $e) { |
||
234 | return false; |
||
235 | } |
||
236 | |||
237 | switch (strtolower(pathinfo($archiveFileName, PATHINFO_EXTENSION))) { |
||
238 | case 'gz': |
||
239 | case 'tgz': |
||
240 | $tar->compress(Phar::GZ, $ext); |
||
241 | break; |
||
242 | case 'bz2': |
||
243 | case 'tbz2': |
||
244 | $tar->compress(Phar::BZ2, $ext); |
||
245 | break; |
||
246 | } |
||
247 | $tar = null; |
||
248 | |||
249 | return count($files); |
||
250 | } |
||
251 | |||
252 | /** @var string Full path to archive */ |
||
253 | protected $archiveFileName; |
||
254 | |||
255 | /** @var string Full path to archive */ |
||
256 | protected $archiveType; |
||
257 | |||
258 | /** @var Archive_Tar|PharData */ |
||
259 | protected $tar; |
||
260 | |||
261 | /** @var float Overall compression ratio of Tar archive when Archive_Tar is used */ |
||
262 | protected $pearCompressionRatio; |
||
263 | |||
264 | /** @var array<string, integer> List of files and their index in listContent() result */ |
||
265 | protected $pearFilesIndex; |
||
266 | |||
267 | /** @var int Flags for iterator */ |
||
268 | const PHAR_FLAGS = FilesystemIterator::UNIX_PATHS; |
||
269 | |||
270 | /** |
||
271 | * Tar format constructor. |
||
272 | * |
||
273 | * @param string $archiveFileName |
||
274 | * @throws \Exception |
||
275 | */ |
||
276 | public function __construct($archiveFileName) |
||
277 | { |
||
278 | static::checkRequirements(); |
||
279 | |||
280 | $this->archiveFileName = realpath($archiveFileName); |
||
281 | $this->archiveType = static::detectArchiveType($this->archiveFileName); |
||
282 | if ($this->archiveType === false) |
||
283 | throw new \Exception('Could not detect type for archive '.$this->archiveFileName); |
||
284 | $this->open($this->archiveType); |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Tar destructor |
||
289 | */ |
||
290 | public function __destruct() |
||
291 | { |
||
292 | $this->tar = null; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * @param $archiveType |
||
297 | * @throws Exception |
||
298 | */ |
||
299 | protected function open($archiveType) |
||
300 | { |
||
301 | switch ($archiveType) { |
||
302 | case self::TAR_GZIP: |
||
303 | if (self::$enabledPharData) { |
||
304 | $this->tar = new PharData($this->archiveFileName, self::PHAR_FLAGS); |
||
305 | } else { |
||
306 | $this->tar = new Archive_Tar($this->archiveFileName, 'gz'); |
||
307 | } |
||
308 | break; |
||
309 | |||
310 | case self::TAR_BZIP: |
||
311 | if (self::$enabledPharData) { |
||
312 | $this->tar = new PharData($this->archiveFileName, self::PHAR_FLAGS); |
||
313 | } else { |
||
314 | $this->tar = new Archive_Tar($this->archiveFileName, 'bz2'); |
||
315 | } |
||
316 | break; |
||
317 | |||
318 | case self::TAR_LZMA: |
||
319 | if (!self::$enabledPharData) { |
||
320 | throw new Exception('Archive_Tar not available'); |
||
321 | } |
||
322 | $this->tar = new Archive_Tar($this->archiveFileName, 'lzma2'); |
||
323 | break; |
||
324 | |||
325 | case self::TAR_LZW: |
||
326 | if (!self::$enabledPharData) { |
||
327 | throw new Exception('Archive_Tar not available'); |
||
328 | } |
||
329 | |||
330 | LzwStreamWrapper::registerWrapper(); |
||
331 | $this->tar = new Archive_Tar('compress.lzw://' . $this->archiveFileName); |
||
332 | break; |
||
333 | |||
334 | default: |
||
335 | if (self::$enabledPharData) { |
||
336 | $this->tar = new PharData($this->archiveFileName, self::PHAR_FLAGS); |
||
337 | } else { |
||
338 | $this->tar = new Archive_Tar($this->archiveFileName); |
||
339 | } |
||
340 | break; |
||
341 | } |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * @return ArchiveInformation |
||
346 | */ |
||
347 | public function getArchiveInformation() |
||
348 | { |
||
349 | $information = new ArchiveInformation(); |
||
350 | if ($this->tar instanceof Archive_Tar) { |
||
351 | $this->pearFilesIndex = []; |
||
352 | |||
353 | foreach ($this->tar->listContent() as $i => $file) { |
||
354 | // BUG workaround: http://pear.php.net/bugs/bug.php?id=20275 |
||
355 | if ($file['filename'] === 'pax_global_header') { |
||
356 | continue; |
||
357 | } |
||
358 | $information->files[] = $file['filename']; |
||
359 | $information->uncompressedFilesSize += $file['size']; |
||
360 | $this->pearFilesIndex[$file['filename']] = $i; |
||
361 | } |
||
362 | |||
363 | $information->uncompressedFilesSize = filesize($this->archiveFileName); |
||
364 | $this->pearCompressionRatio = $information->uncompressedFilesSize != 0 |
||
365 | ? ceil($information->compressedFilesSize / $information->uncompressedFilesSize) |
||
366 | : 1; |
||
367 | } else { |
||
368 | $stream_path_length = strlen('phar://'.$this->archiveFileName.'/'); |
||
369 | foreach (new RecursiveIteratorIterator($this->tar) as $i => $file) { |
||
370 | $information->files[] = substr($file->getPathname(), $stream_path_length); |
||
371 | $information->compressedFilesSize += $file->getCompressedSize(); |
||
372 | $information->uncompressedFilesSize += filesize($file->getPathname()); |
||
373 | } |
||
374 | } |
||
375 | |||
376 | return $information; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @return array |
||
381 | */ |
||
382 | public function getFileNames() |
||
383 | { |
||
384 | return $this->tar instanceof Archive_Tar |
||
385 | ? $this->getFileNamesForPear() |
||
386 | : $this->getFileNamesForPhar(); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @param string $fileName |
||
391 | * @return bool |
||
392 | */ |
||
393 | public function isFileExists($fileName) |
||
403 | } |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * @param string $fileName |
||
408 | * @return ArchiveEntry|false |
||
409 | * @throws Exception |
||
410 | */ |
||
411 | public function getFileData($fileName) |
||
412 | { |
||
413 | if ($this->tar instanceof Archive_Tar) { |
||
414 | if (!isset($this->pearFilesIndex[$fileName])) |
||
415 | throw new Exception('File '.$fileName.' is not found in archive files list'); |
||
416 | |||
417 | $index = $this->pearFilesIndex[$fileName]; |
||
418 | |||
419 | $files_list = $this->tar->listContent(); |
||
420 | if (!isset($files_list[$index])) |
||
421 | throw new Exception('File '.$fileName.' is not found in Tar archive'); |
||
422 | |||
423 | $data = $files_list[$index]; |
||
424 | unset($files_list); |
||
425 | |||
426 | return new ArchiveEntry($fileName, $data['size'] / $this->pearCompressionRatio, |
||
427 | $data['size'], $data['mtime'], in_array(strtolower(pathinfo($this->archiveFileName, |
||
428 | PATHINFO_EXTENSION)), array('gz', 'bz2', 'xz', 'Z'))); |
||
429 | } |
||
430 | |||
431 | /** @var \PharFileInfo $entry_info */ |
||
432 | $entry_info = $this->tar->offsetGet($fileName); |
||
433 | return new ArchiveEntry($fileName, $entry_info->getSize(), filesize($entry_info->getPathname()), |
||
434 | 0, $entry_info->isCompressed()); |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * @param string $fileName |
||
439 | * @return string|false |
||
440 | * @throws Exception |
||
441 | */ |
||
442 | public function getFileContent($fileName) |
||
443 | { |
||
444 | if ($this->tar instanceof Archive_Tar) { |
||
445 | if (!isset($this->pearFilesIndex[$fileName])) |
||
446 | throw new Exception('File '.$fileName.' is not found in archive files list'); |
||
447 | |||
448 | return $this->tar->extractInString($fileName); |
||
449 | } |
||
450 | |||
451 | return $this->tar->offsetGet($fileName)->getContent(); |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * @param string $fileName |
||
456 | * @return bool|resource|string |
||
457 | * @throws Exception |
||
458 | */ |
||
459 | public function getFileResource($fileName) |
||
460 | { |
||
461 | $resource = fopen('php://temp', 'r+'); |
||
462 | if ($this->tar instanceof Archive_Tar) { |
||
463 | if (!isset($this->pearFilesIndex[$fileName])) |
||
464 | throw new Exception('File '.$fileName.' is not found in archive files list'); |
||
465 | |||
466 | fwrite($resource, $this->tar->extractInString($fileName)); |
||
467 | } else |
||
468 | fwrite($resource, $this->tar->offsetGet($fileName)->getContent()); |
||
469 | |||
470 | rewind($resource); |
||
471 | return $resource; |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * @param string $outputFolder |
||
476 | * @param array $files |
||
477 | * @return false|int |
||
478 | * @throws Exception |
||
479 | */ |
||
480 | public function extractFiles($outputFolder, array $files) |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * @param string $outputFolder |
||
497 | * @return false|int |
||
498 | * @throws Exception |
||
499 | */ |
||
500 | public function extractArchive($outputFolder) |
||
501 | { |
||
502 | if ($this->tar instanceof Archive_Tar) { |
||
503 | $result = $this->tar->extract($outputFolder); |
||
504 | } else { |
||
505 | $result = $this->tar->extractTo($outputFolder, null, true); |
||
506 | } |
||
507 | |||
508 | if ($result === false) { |
||
509 | throw new Exception('Error when extracting from '.$this->archiveFileName); |
||
510 | } |
||
511 | |||
512 | return 1; |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * @param array $files |
||
517 | * @return false|int |
||
518 | * @throws UnsupportedOperationException |
||
519 | * @throws Exception |
||
520 | */ |
||
521 | public function deleteFiles(array $files) |
||
522 | { |
||
523 | if ($this->tar instanceof Archive_Tar) |
||
524 | throw new UnsupportedOperationException(); |
||
525 | |||
526 | $deleted = 0; |
||
527 | |||
528 | foreach ($files as $i => $file) { |
||
529 | if ($this->tar->delete($file)) |
||
530 | $deleted++; |
||
531 | } |
||
532 | |||
533 | $this->tar = null; |
||
534 | $this->open($this->archiveType); |
||
535 | |||
536 | return $deleted; |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * @param array $files |
||
541 | * @return false|int |
||
542 | * @throws Exception |
||
543 | */ |
||
544 | public function addFiles(array $files) |
||
545 | { |
||
546 | $added = 0; |
||
547 | |||
548 | if ($this->tar instanceof Archive_Tar) { |
||
549 | foreach ($files as $localName => $filename) { |
||
550 | $remove_dir = dirname($filename); |
||
551 | $add_dir = dirname($localName); |
||
552 | if (is_null($filename)) { |
||
553 | if ($this->tar->addString($localName, "") === false) |
||
554 | return false; |
||
555 | } else { |
||
556 | if ($this->tar->addModify($filename, $add_dir, $remove_dir) === false) |
||
557 | return false; |
||
558 | $added++; |
||
559 | } |
||
560 | } |
||
561 | } else { |
||
562 | try { |
||
563 | foreach ($files as $localName => $filename) { |
||
564 | if (is_null($filename)) { |
||
565 | $this->tar->addEmptyDir($localName); |
||
566 | } else { |
||
567 | $this->tar->addFile($filename, $localName); |
||
568 | $added++; |
||
569 | } |
||
570 | } |
||
571 | } catch (Exception $e) { |
||
572 | return false; |
||
573 | } |
||
574 | $this->tar = null; |
||
575 | // reopen to refresh files list properly |
||
576 | $this->open($this->archiveType); |
||
577 | } |
||
578 | |||
579 | return $added; |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * @return array |
||
584 | */ |
||
585 | protected function getFileNamesForPear() |
||
599 | } |
||
600 | |||
601 | /** |
||
602 | * @return array |
||
603 | */ |
||
604 | protected function getFileNamesForPhar() |
||
605 | { |
||
606 | $files = []; |
||
607 | |||
608 | $stream_path_length = strlen('phar://'.$this->archiveFileName.'/'); |
||
614 | } |
||
615 | |||
616 | /** |
||
617 | * @return bool |
||
618 | */ |
||
619 | public static function canAddFiles() |
||
620 | { |
||
621 | return true; |
||
622 | } |
||
623 | |||
624 | /** |
||
625 | * @return bool |
||
626 | */ |
||
627 | public static function canDeleteFiles() |
||
631 | } |
||
632 | } |