Total Complexity | 69 |
Total Lines | 441 |
Duplicated Lines | 0 % |
Changes | 0 |
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.
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; |
||
36 | class Zip implements ZipInterface, Countable { |
||
37 | |||
38 | use SkipTrait; |
||
39 | use MaskTrait; |
||
40 | use PasswordTrait; |
||
41 | use PathTrait; |
||
42 | use ArchiveTrait; |
||
43 | |||
44 | /** |
||
45 | * zip file name |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $zip_file; |
||
50 | |||
51 | /** |
||
52 | * Class constructor |
||
53 | * |
||
54 | * @param string $zip_file ZIP file name |
||
55 | * |
||
56 | * @throws ZipException |
||
57 | */ |
||
58 | public function __construct(string $zip_file) { |
||
59 | |||
60 | if ( empty($zip_file) ) { |
||
61 | throw new ZipException(StatusCodes::get(ZipArchive::ER_NOENT)); |
||
62 | } |
||
63 | |||
64 | $this->zip_file = $zip_file; |
||
65 | |||
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public static function open(string $zip_file): ZipInterface { |
||
72 | |||
73 | try { |
||
74 | |||
75 | $zip = new Zip($zip_file); |
||
76 | $zip->setArchive(self::openZipFile($zip_file)); |
||
77 | |||
78 | } catch (ZipException $ze) { |
||
79 | throw $ze; |
||
80 | } |
||
81 | |||
82 | return $zip; |
||
83 | |||
84 | } |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | public static function check(string $zip_file): bool { |
||
90 | |||
91 | try { |
||
92 | |||
93 | $zip = self::openZipFile($zip_file, ZipArchive::CHECKCONS); |
||
94 | $zip->close(); |
||
95 | |||
96 | } catch (ZipException $ze) { |
||
97 | throw $ze; |
||
98 | } |
||
99 | |||
100 | return true; |
||
101 | |||
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | */ |
||
107 | public static function create(string $zip_file, bool $overwrite = false): ZipInterface { |
||
133 | |||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Count the number of files in the archive |
||
138 | * |
||
139 | * @return int |
||
140 | */ |
||
141 | public function count(): int { |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Get current zip file |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getZipFile(): string { |
||
153 | |||
154 | } |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function listFiles(): array { |
||
174 | |||
175 | } |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function extract(string $destination, $files = null): bool { |
||
217 | |||
218 | } |
||
219 | |||
220 | /** |
||
221 | * {@inheritdoc} |
||
222 | */ |
||
223 | public function add( |
||
224 | $file_name_or_array, |
||
225 | bool $flatten_root_folder = false, |
||
226 | int $compression = self::CM_DEFAULT, |
||
227 | int $encryption = self::EM_NONE |
||
228 | ): ZipInterface { |
||
229 | |||
230 | if ( empty($file_name_or_array) ) { |
||
231 | throw new ZipException(StatusCodes::get(ZipArchive::ER_NOENT)); |
||
232 | } |
||
233 | |||
234 | if ( $encryption !== self::EM_NONE && $this->getPassword() === null ) { |
||
235 | throw new ZipException("Cannot encrypt resource: no password set"); |
||
236 | } |
||
237 | |||
238 | $flatten_root_folder = DataFilter::filterBoolean($flatten_root_folder); |
||
239 | |||
240 | try { |
||
241 | |||
242 | if ( is_array($file_name_or_array) ) { |
||
243 | foreach ( $file_name_or_array as $file_name ) { |
||
244 | $this->addItem($file_name, $flatten_root_folder, $compression, $encryption); |
||
245 | } |
||
246 | } else { |
||
247 | $this->addItem($file_name_or_array, $flatten_root_folder, $compression, $encryption); |
||
248 | } |
||
249 | |||
250 | } catch (ZipException $ze) { |
||
251 | throw $ze; |
||
252 | } |
||
253 | |||
254 | return $this; |
||
255 | |||
256 | } |
||
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | public function delete($file_name_or_array): ZipInterface { |
||
262 | |||
263 | if ( empty($file_name_or_array) ) { |
||
264 | throw new ZipException(StatusCodes::get(ZipArchive::ER_NOENT)); |
||
265 | } |
||
266 | |||
267 | try { |
||
268 | |||
269 | if ( is_array($file_name_or_array) ) { |
||
270 | foreach ( $file_name_or_array as $file_name ) { |
||
271 | $this->deleteItem($file_name); |
||
272 | } |
||
273 | } else { |
||
274 | $this->deleteItem($file_name_or_array); |
||
275 | } |
||
276 | |||
277 | } catch (ZipException $ze) { |
||
278 | throw $ze; |
||
279 | } |
||
280 | |||
281 | return $this; |
||
282 | |||
283 | } |
||
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | public function close(): bool { |
||
289 | |||
290 | if ( $this->getArchive()->close() === false ) { |
||
291 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
292 | } |
||
293 | |||
294 | return true; |
||
295 | |||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Get a list of file contained in zip archive before extraction |
||
300 | * |
||
301 | * @return array |
||
302 | */ |
||
303 | private function getArchiveFiles(): array { |
||
334 | |||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Add item to zip archive |
||
339 | * |
||
340 | * @param string $file File to add (realpath) |
||
341 | * @param bool $flatroot (optional) If true, source directory will be not included |
||
342 | * @param string $base (optional) Base to record in zip file |
||
343 | * @return void |
||
344 | * @throws ZipException |
||
345 | */ |
||
346 | private function addItem( |
||
347 | string $file, |
||
348 | bool $flatroot = false, |
||
349 | int $compression = self::CM_DEFAULT, |
||
350 | int $encryption = self::EM_NONE, |
||
351 | ?string $base = null |
||
352 | ): void { |
||
353 | |||
354 | $file = is_null($this->getPath()) ? $file : $this->getPath()."/$file"; |
||
355 | $real_file = str_replace('\\', '/', realpath($file)); |
||
356 | $real_name = basename($real_file); |
||
357 | |||
358 | if ( |
||
359 | $base !== null && |
||
360 | ( |
||
361 | ( |
||
362 | $real_name[0] == "." && |
||
363 | in_array($this->getSkipMode(), ["HIDDEN", "ALL"]) |
||
364 | ) || |
||
365 | ( |
||
366 | $real_name[0] == "." && |
||
367 | @$real_name[1] == "_" && |
||
368 | in_array($this->getSkipMode(), ["COMODOJO", "ALL"]) |
||
369 | ) |
||
370 | ) |
||
371 | ) { |
||
372 | return; |
||
373 | } |
||
374 | |||
375 | if ( is_dir($real_file) ) { |
||
376 | $this->addDirectoryItem($real_file, $real_name, $compression, $encryption, $base, $flatroot); |
||
377 | } else { |
||
378 | $this->addFileItem($real_file, $real_name, $compression, $encryption, $base); |
||
379 | } |
||
380 | |||
381 | } |
||
382 | |||
383 | private function addFileItem( |
||
384 | string $real_file, |
||
385 | string $real_name, |
||
386 | int $compression = self::CM_DEFAULT, |
||
387 | int $encryption = self::EM_NONE, |
||
388 | ?string $base = null |
||
389 | ): void { |
||
390 | |||
391 | $file_target = is_null($base) ? $real_name : $base.$real_name; |
||
392 | if ( |
||
393 | $this->getArchive()->addFile($real_file, $file_target) === false || |
||
394 | $this->getArchive()->setCompressionName($file_target, $compression) === false || |
||
395 | $this->getArchive()->setEncryptionName($file_target, $encryption, $this->getPassword()) === false |
||
396 | ) { |
||
397 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
398 | } |
||
399 | |||
400 | } |
||
401 | |||
402 | private function addDirectoryItem( |
||
403 | string $real_file, |
||
404 | string $real_name, |
||
405 | int $compression = self::CM_DEFAULT, |
||
406 | int $encryption = self::EM_NONE, |
||
407 | ?string $base = null, |
||
408 | bool $flatroot = false |
||
409 | ): void { |
||
410 | |||
411 | if ( !$flatroot ) { |
||
412 | $folder_target = $base.$real_name; |
||
413 | $new_base = "$folder_target/"; |
||
414 | if ( $this->getArchive()->addEmptyDir($folder_target) === false ) { |
||
415 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
416 | } |
||
417 | } else { |
||
418 | $new_base = null; |
||
419 | } |
||
420 | |||
421 | foreach ( new DirectoryIterator($real_file) as $path ) { |
||
422 | |||
423 | if ( $path->isDot() ) { |
||
424 | continue; |
||
425 | } |
||
426 | |||
427 | try { |
||
428 | $this->addItem( |
||
429 | $path->getPathname(), |
||
430 | false, |
||
431 | $compression, |
||
432 | $encryption, |
||
433 | $new_base |
||
434 | ); |
||
435 | } catch (ZipException $ze) { |
||
436 | throw $ze; |
||
437 | } |
||
438 | |||
439 | } |
||
440 | |||
441 | } |
||
442 | |||
443 | /** |
||
444 | * Delete item from zip archive |
||
445 | * |
||
446 | * @param string $file File to delete (zippath) |
||
447 | * @return void |
||
448 | * @throws ZipException |
||
449 | */ |
||
450 | private function deleteItem(string $file): void { |
||
454 | } |
||
455 | |||
456 | } |
||
457 | |||
458 | /** |
||
459 | * Open a zip file |
||
460 | * |
||
461 | * @param string $zip_file ZIP file name |
||
462 | * @param int $flags ZipArchive::open flags |
||
463 | * |
||
464 | * @return ZipArchive |
||
465 | * @throws ZipException |
||
466 | */ |
||
467 | private static function openZipFile(string $zip_file, int $flags = null): ZipArchive { |
||
477 | |||
478 | } |
||
479 | |||
480 | } |
||
481 |