Total Complexity | 66 |
Total Lines | 431 |
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 { |
||
160 | |||
161 | $list = []; |
||
162 | |||
163 | for ( $i = 0; $i < $this->getArchive()->numFiles; $i++ ) { |
||
164 | |||
165 | $name = $this->getArchive()->getNameIndex($i); |
||
166 | if ( $name === false ) { |
||
167 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
168 | } |
||
169 | $list[] = $name; |
||
170 | |||
171 | } |
||
172 | |||
173 | return $list; |
||
174 | |||
175 | } |
||
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | public function extract(string $destination, $files = null): bool { |
||
181 | |||
182 | if ( empty($destination) ) { |
||
183 | throw new ZipException("Invalid destination path: $destination"); |
||
184 | } |
||
185 | |||
186 | if ( !file_exists($destination) ) { |
||
187 | |||
188 | $omask = umask(0); |
||
189 | $action = mkdir($destination, $this->getMask(), true); |
||
190 | umask($omask); |
||
191 | |||
192 | if ( $action === false ) { |
||
193 | throw new ZipException("Error creating folder: $destination"); |
||
194 | } |
||
195 | |||
196 | } |
||
197 | |||
198 | if ( !is_writable($destination) ) { |
||
199 | throw new ZipException("Destination path $destination not writable"); |
||
200 | } |
||
201 | |||
202 | if ( is_array($files) && @sizeof($files) != 0 ) { |
||
203 | $file_matrix = $files; |
||
204 | } else { |
||
205 | $file_matrix = $this->getArchiveFiles(); |
||
206 | } |
||
207 | |||
208 | if ( !empty($this->getPassword()) ) { |
||
209 | $this->getArchive()->setPassword($this->getPassword()); |
||
210 | } |
||
211 | |||
212 | if ( $this->getArchive()->extractTo($destination, $file_matrix) === false ) { |
||
213 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
214 | } |
||
215 | |||
216 | return true; |
||
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 | ): ZipInterface { |
||
228 | |||
229 | if ( empty($file_name_or_array) ) { |
||
230 | throw new ZipException(StatusCodes::get(ZipArchive::ER_NOENT)); |
||
231 | } |
||
232 | |||
233 | $flatten_root_folder = DataFilter::filterBoolean($flatten_root_folder); |
||
234 | |||
235 | try { |
||
236 | |||
237 | if ( is_array($file_name_or_array) ) { |
||
238 | foreach ( $file_name_or_array as $file_name ) { |
||
239 | $this->addItem($file_name, $flatten_root_folder, $compression); |
||
240 | } |
||
241 | } else { |
||
242 | $this->addItem($file_name_or_array, $flatten_root_folder, $compression); |
||
243 | } |
||
244 | |||
245 | } catch (ZipException $ze) { |
||
246 | throw $ze; |
||
247 | } |
||
248 | |||
249 | return $this; |
||
250 | |||
251 | } |
||
252 | |||
253 | /** |
||
254 | * {@inheritdoc} |
||
255 | */ |
||
256 | public function delete($file_name_or_array): ZipInterface { |
||
257 | |||
258 | if ( empty($file_name_or_array) ) { |
||
259 | throw new ZipException(StatusCodes::get(ZipArchive::ER_NOENT)); |
||
260 | } |
||
261 | |||
262 | try { |
||
263 | |||
264 | if ( is_array($file_name_or_array) ) { |
||
265 | foreach ( $file_name_or_array as $file_name ) { |
||
266 | $this->deleteItem($file_name); |
||
267 | } |
||
268 | } else { |
||
269 | $this->deleteItem($file_name_or_array); |
||
270 | } |
||
271 | |||
272 | } catch (ZipException $ze) { |
||
273 | throw $ze; |
||
274 | } |
||
275 | |||
276 | return $this; |
||
277 | |||
278 | } |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function close(): bool { |
||
284 | |||
285 | if ( $this->getArchive()->close() === false ) { |
||
286 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
287 | } |
||
288 | |||
289 | return true; |
||
290 | |||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Get a list of file contained in zip archive before extraction |
||
295 | * |
||
296 | * @return array |
||
297 | */ |
||
298 | private function getArchiveFiles(): array { |
||
329 | |||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Add item to zip archive |
||
334 | * |
||
335 | * @param string $file File to add (realpath) |
||
336 | * @param bool $flatroot (optional) If true, source directory will be not included |
||
337 | * @param string $base (optional) Base to record in zip file |
||
338 | * @return void |
||
339 | * @throws ZipException |
||
340 | */ |
||
341 | private function addItem( |
||
342 | string $file, |
||
343 | bool $flatroot = false, |
||
344 | int $compression = self::CM_DEFAULT, |
||
345 | ?string $base = null |
||
346 | ): void { |
||
347 | |||
348 | $file = is_null($this->getPath()) ? $file : $this->getPath()."/$file"; |
||
349 | $real_file = str_replace('\\', '/', realpath($file)); |
||
350 | $real_name = basename($real_file); |
||
351 | |||
352 | if ( |
||
353 | $base !== null && |
||
354 | ( |
||
355 | ( |
||
356 | $real_name[0] == "." && |
||
357 | in_array($this->getSkipMode(), ["HIDDEN", "ALL"]) |
||
358 | ) || |
||
359 | ( |
||
360 | $real_name[0] == "." && |
||
361 | @$real_name[1] == "_" && |
||
362 | in_array($this->getSkipMode(), ["COMODOJO", "ALL"]) |
||
363 | ) |
||
364 | ) |
||
365 | ) { |
||
366 | return; |
||
367 | } |
||
368 | |||
369 | if ( is_dir($real_file) ) { |
||
370 | $this->addDirectoryItem($real_file, $real_name, $compression, $base, $flatroot); |
||
371 | } else { |
||
372 | $this->addFileItem($real_file, $real_name, $compression, $base); |
||
373 | } |
||
374 | |||
375 | } |
||
376 | |||
377 | private function addFileItem( |
||
378 | string $real_file, |
||
379 | string $real_name, |
||
380 | int $compression = self::CM_DEFAULT, |
||
381 | ?string $base = null |
||
382 | ): void { |
||
383 | |||
384 | $file_target = is_null($base) ? $real_name : $base.$real_name; |
||
385 | if ( |
||
386 | $this->getArchive()->addFile($real_file, $file_target) === false || |
||
387 | $this->getArchive()->setCompressionName($file_target, $compression) === false |
||
388 | ) { |
||
389 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
390 | } |
||
391 | |||
392 | } |
||
393 | |||
394 | private function addDirectoryItem( |
||
395 | string $real_file, |
||
396 | string $real_name, |
||
397 | int $compression = self::CM_DEFAULT, |
||
398 | ?string $base = null, |
||
399 | bool $flatroot = false |
||
400 | ): void { |
||
401 | |||
402 | if ( !$flatroot ) { |
||
403 | $folder_target = $base.$real_name; |
||
404 | $new_base = "$folder_target/"; |
||
405 | if ( $this->getArchive()->addEmptyDir($folder_target) === false ) { |
||
406 | throw new ZipException(StatusCodes::get($this->getArchive()->status)); |
||
407 | } |
||
408 | } else { |
||
409 | $new_base = null; |
||
410 | } |
||
411 | |||
412 | foreach ( new DirectoryIterator($real_file) as $path ) { |
||
413 | |||
414 | if ( $path->isDot() ) { |
||
415 | continue; |
||
416 | } |
||
417 | |||
418 | try { |
||
419 | $this->addItem( |
||
420 | $path->getPathname(), |
||
421 | false, |
||
422 | $compression, |
||
423 | $new_base |
||
424 | ); |
||
425 | } catch (ZipException $ze) { |
||
426 | throw $ze; |
||
427 | } |
||
428 | |||
429 | } |
||
430 | |||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Delete item from zip archive |
||
435 | * |
||
436 | * @param string $file File to delete (zippath) |
||
437 | * @return void |
||
438 | * @throws ZipException |
||
439 | */ |
||
440 | private function deleteItem(string $file): void { |
||
444 | } |
||
445 | |||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Open a zip file |
||
450 | * |
||
451 | * @param string $zip_file ZIP file name |
||
452 | * @param int $flags ZipArchive::open flags |
||
453 | * |
||
454 | * @return ZipArchive |
||
455 | * @throws ZipException |
||
456 | */ |
||
457 | private static function openZipFile(string $zip_file, int $flags = null): ZipArchive { |
||
467 | |||
468 | } |
||
469 | |||
470 | } |
||
471 |