Complex classes like ZipBase 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 ZipBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Comodojo\Zip; |
||
| 26 | class ZipBase { |
||
| 27 | |||
| 28 | const SKIP_NONE = 'NONE'; |
||
| 29 | |||
| 30 | const SKIP_HIDDEN = 'HIDDEN'; |
||
| 31 | |||
| 32 | const SKIP_ALL = 'ALL'; |
||
| 33 | |||
| 34 | const SKIP_COMODOJO = 'COMODOJO'; |
||
| 35 | |||
| 36 | const DEFAULT_MASK = 0777; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Select files to skip |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private $skip_mode = self::SKIP_NONE; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Supported skip modes |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | private $supported_skip_modes = ['NONE', 'HIDDEN', 'ALL', 'COMODOJO']; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Mask for the extraction folder (if it should be created) |
||
| 54 | * |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | private $mask = self::DEFAULT_MASK; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * ZipArchive internal pointer |
||
| 61 | * |
||
| 62 | * @var object |
||
| 63 | */ |
||
| 64 | private $zip_archive; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * zip file name |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | private $zip_file; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * zip file password (only for extract) |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | private $password; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Current base path |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | private $path; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Class constructor |
||
| 89 | * |
||
| 90 | * @param string $zip_file ZIP file name |
||
| 91 | * |
||
| 92 | * @throws ZipException |
||
| 93 | */ |
||
| 94 | public function __construct(string $zip_file) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set files to skip |
||
| 104 | * |
||
| 105 | * @param string $mode [HIDDEN, COMODOJO, ALL, NONE] |
||
| 106 | * |
||
| 107 | * @return Zip |
||
| 108 | * @throws ZipException |
||
| 109 | */ |
||
| 110 | public function setSkipped(string $mode): self { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get current skip mode (HIDDEN, COMODOJO, ALL, NONE) |
||
| 124 | * |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function getSkipped(): string { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set extraction password |
||
| 135 | * |
||
| 136 | * @param string $password |
||
| 137 | * |
||
| 138 | * @return Zip |
||
| 139 | */ |
||
| 140 | public function setPassword(string $password): self { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get current extraction password |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function getPassword(): ?string { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Set current base path (just to add relative files to zip archive) |
||
| 161 | * |
||
| 162 | * @param string|null $path |
||
| 163 | * |
||
| 164 | * @return Zip |
||
| 165 | * @throws ZipException |
||
| 166 | */ |
||
| 167 | public function setPath(?string $path): self { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get current base path |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getPath(): ?string { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Set extraction folder mask |
||
| 194 | * |
||
| 195 | * @param int $mask |
||
| 196 | * |
||
| 197 | * @return Zip |
||
| 198 | */ |
||
| 199 | public function setMask(int $mask): self { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get current extraction folder mask |
||
| 216 | * |
||
| 217 | * @return int |
||
| 218 | */ |
||
| 219 | public function getMask(): int { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set the current ZipArchive object |
||
| 227 | * |
||
| 228 | * @param ZipArchive $zip |
||
| 229 | * |
||
| 230 | * @return Zip |
||
| 231 | */ |
||
| 232 | public function setArchive(ZipArchive $zip): self { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get current ZipArchive object |
||
| 242 | * |
||
| 243 | * @return ZipArchive|null |
||
| 244 | */ |
||
| 245 | public function getArchive(): ?ZipArchive { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get current zip file |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function getZipFile(): string { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get a list of files in archive (array) |
||
| 264 | * |
||
| 265 | * @return array |
||
| 266 | * @throws ZipException |
||
| 267 | */ |
||
| 268 | public function listFiles(): array { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Extract files from zip archive |
||
| 288 | * |
||
| 289 | * @param string $destination Destination path |
||
| 290 | * @param mixed $files (optional) a filename or an array of filenames |
||
| 291 | * |
||
| 292 | * @return bool |
||
| 293 | * @throws ZipException |
||
| 294 | */ |
||
| 295 | public function extract(string $destination, $files = null): bool { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Add files to zip archive |
||
| 335 | * |
||
| 336 | * @param mixed $file_name_or_array filename to add or an array of filenames |
||
| 337 | * @param bool $flatten_root_folder in case of directory, specify if root folder should be flatten or not |
||
| 338 | * |
||
| 339 | * @return Zip |
||
| 340 | * @throws ZipException |
||
| 341 | */ |
||
| 342 | public function add($file_name_or_array, bool $flatten_root_folder = false): self { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Delete files from zip archive |
||
| 372 | * |
||
| 373 | * @param mixed $file_name_or_array filename to delete or an array of filenames |
||
| 374 | * |
||
| 375 | * @return Zip |
||
| 376 | * @throws ZipException |
||
| 377 | */ |
||
| 378 | public function delete($file_name_or_array): self { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Close the zip archive |
||
| 406 | * |
||
| 407 | * @return bool |
||
| 408 | * @throws ZipException |
||
| 409 | */ |
||
| 410 | public function close(): bool { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Get a list of file contained in zip archive before extraction |
||
| 420 | * |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | private function getArchiveFiles(): array { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Add item to zip archive |
||
| 449 | * |
||
| 450 | * @param string $file File to add (realpath) |
||
| 451 | * @param bool $flatroot (optional) If true, source directory will be not included |
||
| 452 | * @param string $base (optional) Base to record in zip file |
||
| 453 | * |
||
| 454 | * @throws ZipException |
||
| 455 | */ |
||
| 456 | private function addItem(string $file, bool $flatroot = false, ?string $base = null): void { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Delete item from zip archive |
||
| 524 | * |
||
| 525 | * @param string $file File to delete (zippath) |
||
| 526 | * |
||
| 527 | * @throws ZipException |
||
| 528 | */ |
||
| 529 | private function deleteItem(string $file): void { |
||
| 536 | |||
| 537 | } |
||
| 538 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.