Complex classes like Zip 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 Zip, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Comodojo\Zip; |
||
| 27 | class Zip implements Countable { |
||
| 28 | |||
| 29 | public const SKIP_NONE = 'NONE'; |
||
| 30 | |||
| 31 | public const SKIP_HIDDEN = 'HIDDEN'; |
||
| 32 | |||
| 33 | public const SKIP_ALL = 'ALL'; |
||
| 34 | |||
| 35 | public const SKIP_COMODOJO = 'COMODOJO'; |
||
| 36 | |||
| 37 | protected const DEFAULT_MASK = 0777; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Select files to skip |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $skip_mode = self::SKIP_NONE; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Supported skip modes |
||
| 48 | * |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | private $supported_skip_modes = ['NONE', 'HIDDEN', 'ALL', 'COMODOJO']; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Mask for the extraction folder (if it should be created) |
||
| 55 | * |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | private $mask = self::DEFAULT_MASK; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * ZipArchive internal pointer |
||
| 62 | * |
||
| 63 | * @var ZipArchive |
||
| 64 | */ |
||
| 65 | private $zip_archive; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * zip file name |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $zip_file; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * zip file password (only for extract) |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | private $password; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Current base path |
||
| 83 | * |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $path; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Class constructor |
||
| 90 | * |
||
| 91 | * @param string $zip_file ZIP file name |
||
| 92 | * |
||
| 93 | * @throws ZipException |
||
| 94 | */ |
||
| 95 | public function __construct(string $zip_file) { |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Open a zip archive (static constructor) |
||
| 107 | * |
||
| 108 | * @param string $zip_file File name |
||
| 109 | * |
||
| 110 | * @return Zip |
||
| 111 | * @throws ZipException |
||
| 112 | */ |
||
| 113 | public static function open(string $zip_file): Zip { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Check a zip archive (static constructor) |
||
| 130 | * |
||
| 131 | * @param string $zip_file ZIP file name |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | * @throws ZipException |
||
| 135 | */ |
||
| 136 | public static function check(string $zip_file): bool { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Create a new zip archive (static constructor) |
||
| 153 | * |
||
| 154 | * @param string $zip_file ZIP file name |
||
| 155 | * @param bool $overwrite Overwrite existing file (if any) |
||
| 156 | * |
||
| 157 | * @return Zip |
||
| 158 | * @throws ZipException |
||
| 159 | */ |
||
| 160 | public static function create(string $zip_file, bool $overwrite = false): Zip { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Count the number of files in the archive |
||
| 191 | * |
||
| 192 | * @return int |
||
| 193 | */ |
||
| 194 | public function count(): int { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Set files to skip |
||
| 200 | * |
||
| 201 | * Supported skip modes: |
||
| 202 | * Zip::SKIP_NONE - skip no files |
||
| 203 | * Zip::SKIP_HIDDEN - skip hidden files |
||
| 204 | * Zip::SKIP_ALL - skip HIDDEN + COMODOJO ghost files |
||
| 205 | * Zip::SKIP_COMODOJO - skip comodojo ghost files |
||
| 206 | * |
||
| 207 | * @param string $mode Skip file mode |
||
| 208 | * |
||
| 209 | * @return Zip |
||
| 210 | * @throws ZipException |
||
| 211 | */ |
||
| 212 | public function setSkipped(string $mode): Zip { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get current skip mode |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public function getSkipped(): string { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Set zip password |
||
| 239 | * |
||
| 240 | * @param string $password |
||
| 241 | * |
||
| 242 | * @return Zip |
||
| 243 | */ |
||
| 244 | public function setPassword(string $password): Zip { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get current zip password |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function getPassword(): ?string { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Set current base path (to add relative files to zip archive) |
||
| 265 | * |
||
| 266 | * @param string|null $path |
||
| 267 | * |
||
| 268 | * @return Zip |
||
| 269 | * @throws ZipException |
||
| 270 | */ |
||
| 271 | public function setPath(?string $path = null): Zip { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get current base path |
||
| 287 | * |
||
| 288 | * @return string|null |
||
| 289 | */ |
||
| 290 | public function getPath(): ?string { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Set the mask of the extraction folder |
||
| 298 | * |
||
| 299 | * @param int $mask Integer representation of the file mask |
||
| 300 | * |
||
| 301 | * @return Zip |
||
| 302 | */ |
||
| 303 | public function setMask(int $mask): Zip { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get current mask of the extraction folder |
||
| 320 | * |
||
| 321 | * @return int |
||
| 322 | */ |
||
| 323 | public function getMask(): int { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Set the current ZipArchive object |
||
| 331 | * |
||
| 332 | * @param ZipArchive $zip |
||
| 333 | * |
||
| 334 | * @return Zip |
||
| 335 | */ |
||
| 336 | public function setArchive(ZipArchive $zip): Zip { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get current ZipArchive object |
||
| 346 | * |
||
| 347 | * @return ZipArchive|null |
||
| 348 | */ |
||
| 349 | public function getArchive(): ?ZipArchive { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get current zip file |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | public function getZipFile(): string { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get the list of files in the archive as an array |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | * @throws ZipException |
||
| 371 | */ |
||
| 372 | public function listFiles(): array { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Extract files from zip archive |
||
| 392 | * |
||
| 393 | * @param string $destination Destination path |
||
| 394 | * @param mixed $files (optional) a filename or an array of filenames |
||
| 395 | * |
||
| 396 | * @return bool |
||
| 397 | * @throws ZipException |
||
| 398 | */ |
||
| 399 | public function extract(string $destination, $files = null): bool { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Add files to zip archive |
||
| 443 | * |
||
| 444 | * @param mixed $file_name_or_array Filename to add or an array of filenames |
||
| 445 | * @param bool $flatten_root_folder In case of directory, specify if root folder should be flatten or not |
||
| 446 | * |
||
| 447 | * @return Zip |
||
| 448 | * @throws ZipException |
||
| 449 | */ |
||
| 450 | public function add($file_name_or_array, bool $flatten_root_folder = false): Zip { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Delete files from zip archive |
||
| 478 | * |
||
| 479 | * @param mixed $file_name_or_array Filename to delete or an array of filenames |
||
| 480 | * |
||
| 481 | * @return Zip |
||
| 482 | * @throws ZipException |
||
| 483 | */ |
||
| 484 | public function delete($file_name_or_array): Zip { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Close the zip archive |
||
| 510 | * |
||
| 511 | * @return bool |
||
| 512 | * @throws ZipException |
||
| 513 | */ |
||
| 514 | public function close(): bool { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get a list of file contained in zip archive before extraction |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | private function getArchiveFiles(): array { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Add item to zip archive |
||
| 566 | * |
||
| 567 | * @param string $file File to add (realpath) |
||
| 568 | * @param bool $flatroot (optional) If true, source directory will be not included |
||
| 569 | * @param string $base (optional) Base to record in zip file |
||
| 570 | * @return void |
||
| 571 | * @throws ZipException |
||
| 572 | */ |
||
| 573 | private function addItem( |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Delete item from zip archive |
||
| 649 | * |
||
| 650 | * @param string $file File to delete (zippath) |
||
| 651 | * @return void |
||
| 652 | * @throws ZipException |
||
| 653 | */ |
||
| 654 | private function deleteItem(string $file): void { |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Open a zip file |
||
| 665 | * |
||
| 666 | * @param string $zip_file ZIP file name |
||
| 667 | * @param int $flags ZipArchive::open flags |
||
| 668 | * |
||
| 669 | * @return ZipArchive |
||
| 670 | * @throws ZipException |
||
| 671 | */ |
||
| 672 | private static function openZipFile(string $zip_file, int $flags = null): ZipArchive { |
||
| 684 | |||
| 685 | } |
||
| 686 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: