| Total Complexity | 40 |
| Total Lines | 360 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ZipArchive 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 ZipArchive, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class ZipArchive |
||
| 6 | { |
||
| 7 | /** @const int Flags for open method */ |
||
| 8 | public const CREATE = 1; // Emulate \ZipArchive::CREATE |
||
| 9 | public const OVERWRITE = 8; // Emulate \ZipArchive::OVERWRITE |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Number of files (emulate ZipArchive::$numFiles) |
||
| 13 | * |
||
| 14 | * @var int |
||
| 15 | */ |
||
| 16 | public $numFiles = 0; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Archive filename (emulate ZipArchive::$filename) |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | public $filename; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Temporary storage directory |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $tempDir; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Internal zip archive object |
||
| 34 | * |
||
| 35 | * @var \ZipArchive|\PclZip |
||
| 36 | */ |
||
| 37 | private $zip; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Use PCLZip (default behaviour) |
||
| 41 | * |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | private $usePclzip = true; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Create new instance |
||
| 48 | */ |
||
| 49 | public function __construct() |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Catch function calls: pass to ZipArchive or PCLZip |
||
| 62 | * |
||
| 63 | * `call_user_func_array` can only used for public function, hence the `public` in all `pcl...` methods |
||
| 64 | * |
||
| 65 | * @param mixed $function |
||
| 66 | * @param mixed $args |
||
| 67 | * @return mixed |
||
| 68 | */ |
||
| 69 | public function __call($function, $args) |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Open a new zip archive |
||
| 91 | * |
||
| 92 | * @param string $filename The file name of the ZIP archive to open |
||
| 93 | * @param int $flags The mode to use to open the archive |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | public function open(string $filename, ?int $flags = null): bool |
||
| 97 | { |
||
| 98 | $result = true; |
||
| 99 | $this->filename = $filename; |
||
| 100 | $this->tempDir = Settings::getTempDir(); |
||
| 101 | |||
| 102 | if (!$this->usePclzip) { |
||
| 103 | $zip = new \ZipArchive(); |
||
| 104 | $result = $zip->open($this->filename, $flags); |
||
| 105 | |||
| 106 | // Scrutizer will report the property numFiles does not exist |
||
| 107 | // See https://github.com/scrutinizer-ci/php-analyzer/issues/190 |
||
| 108 | $this->numFiles = $zip->numFiles; |
||
| 109 | } else { |
||
| 110 | $zip = new \PclZip($this->filename); |
||
| 111 | $zipContent = $zip->listContent(); |
||
| 112 | $this->numFiles = is_array($zipContent) ? count($zipContent) : 0; |
||
|
|
|||
| 113 | } |
||
| 114 | $this->zip = $zip; |
||
| 115 | |||
| 116 | return $result; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Close the active archive |
||
| 121 | * |
||
| 122 | * @throws \Exception |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | * |
||
| 126 | * @codeCoverageIgnore Can't find any test case. Uncomment when found. |
||
| 127 | */ |
||
| 128 | public function close(): bool |
||
| 129 | { |
||
| 130 | if (!$this->usePclzip) { |
||
| 131 | if ($this->zip->close() === false) { |
||
| 132 | throw new \Exception("Could not close zip file {$this->filename}: "); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | return true; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Extract the archive contents (emulate \ZipArchive) |
||
| 141 | * |
||
| 142 | * @param string $destination |
||
| 143 | * @param mixed $entries |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | public function extractTo(string $destination, $entries = null): bool |
||
| 147 | { |
||
| 148 | if (!is_dir($destination)) { |
||
| 149 | return false; |
||
| 150 | } |
||
| 151 | |||
| 152 | if (!$this->usePclzip) { |
||
| 153 | return $this->zip->extractTo($destination, $entries); |
||
| 154 | } |
||
| 155 | |||
| 156 | return $this->pclzipExtractTo($destination, $entries); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Extract file from archive by given file name (emulate \ZipArchive) |
||
| 161 | * |
||
| 162 | * @param string $filename Filename for the file in zip archive |
||
| 163 | * @return string $contents File string contents |
||
| 164 | */ |
||
| 165 | public function getFromName(string $filename): string |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Add a new file to the zip archive (emulate \ZipArchive) |
||
| 182 | * |
||
| 183 | * @param string $filename Directory/Name of the file to add to the zip archive |
||
| 184 | * @param string $localname Directory/Name of the file added to the zip |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | public function pclzipAddFile(string $filename, ?string $localname = null): bool |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Add a new file to the zip archive from a string of raw data (emulate \ZipArchive) |
||
| 233 | * |
||
| 234 | * @param string $localname Directory/Name of the file to add to the zip archive |
||
| 235 | * @param string $contents String of data to add to the zip archive |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function pclzipAddFromString(string $localname, string $contents): bool |
||
| 239 | { |
||
| 240 | /** @var \PclZip $zip Type hint */ |
||
| 241 | $zip = $this->zip; |
||
| 242 | $filenameParts = pathinfo($localname); |
||
| 243 | |||
| 244 | // Write $contents to a temp file |
||
| 245 | $handle = fopen($this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename'], 'wb'); |
||
| 246 | fwrite($handle, $contents); |
||
| 247 | fclose($handle); |
||
| 248 | |||
| 249 | // Add temp file to zip |
||
| 250 | $filename = $this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename']; |
||
| 251 | $pathRemoved = $this->tempDir; |
||
| 252 | $pathAdded = $filenameParts['dirname']; |
||
| 253 | |||
| 254 | $res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded); |
||
| 255 | |||
| 256 | // Remove temp file |
||
| 257 | @unlink($this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename']); |
||
| 258 | |||
| 259 | return $res != 0; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Extract the archive contents (emulate \ZipArchive) |
||
| 264 | * |
||
| 265 | * @param string $destination |
||
| 266 | * @param mixed $entries |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function pclzipExtractTo(string $destination, $entries = null): bool |
||
| 270 | { |
||
| 271 | /** @var \PclZip $zip Type hint */ |
||
| 272 | $zip = $this->zip; |
||
| 273 | |||
| 274 | // Extract all files |
||
| 275 | if (is_null($entries)) { |
||
| 276 | $result = $zip->extract(PCLZIP_OPT_PATH, $destination); |
||
| 277 | |||
| 278 | return $result > 0; |
||
| 279 | } |
||
| 280 | |||
| 281 | // Extract by entries |
||
| 282 | if (!is_array($entries)) { |
||
| 283 | $entries = array($entries); |
||
| 284 | } |
||
| 285 | foreach ($entries as $entry) { |
||
| 286 | $entryIndex = $this->locateName($entry); |
||
| 287 | $result = $zip->extractByIndex($entryIndex, PCLZIP_OPT_PATH, $destination); |
||
| 288 | if ($result <= 0) { |
||
| 289 | return false; |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | return true; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Extract file from archive by given file name (emulate \ZipArchive) |
||
| 298 | * |
||
| 299 | * @param string $filename Filename for the file in zip archive |
||
| 300 | * @return string $contents File string contents |
||
| 301 | */ |
||
| 302 | public function pclzipGetFromName(string $filename): string |
||
| 303 | { |
||
| 304 | /** @var \PclZip $zip Type hint */ |
||
| 305 | $zip = $this->zip; |
||
| 306 | $listIndex = $this->pclzipLocateName($filename); |
||
| 307 | $contents = false; |
||
| 308 | |||
| 309 | if ($listIndex !== false) { |
||
| 310 | $extracted = $zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING); |
||
| 311 | } else { |
||
| 312 | $filename = substr($filename, 1); |
||
| 313 | $listIndex = $this->pclzipLocateName($filename); |
||
| 314 | $extracted = $zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING); |
||
| 315 | } |
||
| 316 | if ((is_array($extracted)) && ($extracted != 0)) { |
||
| 317 | $contents = $extracted[0]['content']; |
||
| 318 | } |
||
| 319 | |||
| 320 | return $contents; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns the name of an entry using its index (emulate \ZipArchive) |
||
| 325 | * |
||
| 326 | * @param int $index |
||
| 327 | * @return string|bool |
||
| 328 | */ |
||
| 329 | public function pclzipGetNameIndex(int $index) |
||
| 330 | { |
||
| 331 | /** @var \PclZip $zip Type hint */ |
||
| 332 | $zip = $this->zip; |
||
| 333 | $list = $zip->listContent(); |
||
| 334 | if (isset($list[$index])) { |
||
| 335 | return $list[$index]['filename']; |
||
| 336 | } |
||
| 337 | |||
| 338 | return false; |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns the index of the entry in the archive (emulate \ZipArchive) |
||
| 343 | * |
||
| 344 | * @param string $filename Filename for the file in zip archive |
||
| 345 | * @return int |
||
| 346 | */ |
||
| 347 | public function pclzipLocateName(string $filename): int |
||
| 365 | } |
||
| 366 | } |
||
| 367 |