| Conditions | 58 |
| Paths | > 20000 |
| Total Lines | 256 |
| Code Lines | 148 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 269 | public function extract() |
||
| 270 | { |
||
| 271 | $options = func_get_args(); |
||
| 272 | array_shift($options); |
||
| 273 | |||
| 274 | // parse options |
||
| 275 | if (isset($options[0]) && is_string($options[0])) { |
||
| 276 | $options[PCLZIP_OPT_PATH] = $options[0]; |
||
| 277 | if (isset($options[1]) && is_string($options[1])) { |
||
| 278 | $options[PCLZIP_OPT_REMOVE_PATH] = $options[1]; |
||
| 279 | } |
||
| 280 | } else { |
||
| 281 | $options = array_combine( |
||
| 282 | array_filter($options, function ($v) {return (bool) $v&2;}), |
||
| 283 | array_filter($options, function ($v) {return (bool) ($v-1)&2;}) |
||
| 284 | ); |
||
| 285 | } |
||
| 286 | |||
| 287 | // filters initiation |
||
| 288 | if (isset($options[PCLZIP_OPT_PATH])) |
||
| 289 | $extractPath = rtrim($options[PCLZIP_OPT_PATH], '/'); |
||
| 290 | else $extractPath = rtrim(getcwd(), '/'); |
||
| 291 | |||
| 292 | $filters = array(); |
||
| 293 | if (isset($options[PCLZIP_OPT_REMOVE_PATH]) |
||
| 294 | && !isset($options[PCLZIP_OPT_REMOVE_ALL_PATH])) |
||
| 295 | $filters[] = function (&$key, &$value) use ($options) { |
||
| 296 | $key = str_replace($key, null, $key); |
||
| 297 | }; |
||
| 298 | if (isset($options[PCLZIP_OPT_REMOVE_ALL_PATH])) |
||
| 299 | $filters[] = function (&$key, &$value) { $key = basename($key); }; |
||
| 300 | if (isset($options[PCLZIP_OPT_ADD_PATH])) |
||
| 301 | $filters[] = function (&$key, &$value) use ($options) { |
||
| 302 | $key = rtrim($options[PCLZIP_OPT_ADD_PATH], '/').'/'. |
||
| 303 | ltrim($key, '/'); |
||
| 304 | }; |
||
| 305 | |||
| 306 | if (isset($options[PCLZIP_CB_PRE_EXTRACT]) |
||
| 307 | && is_callable($options[PCLZIP_CB_PRE_EXTRACT])) |
||
| 308 | $preExtractCallback = $options[PCLZIP_CB_PRE_EXTRACT]; |
||
| 309 | else $preExtractCallback = function () { return 1; }; |
||
| 310 | |||
| 311 | if (isset($options[PCLZIP_CB_POST_EXTRACT]) |
||
| 312 | && is_callable($options[PCLZIP_CB_POST_EXTRACT])) |
||
| 313 | $postExtractCallback = $options[PCLZIP_CB_POST_EXTRACT]; |
||
| 314 | else $postExtractCallback = function () { return 1; }; |
||
| 315 | |||
| 316 | // exact matching |
||
| 317 | if (isset($options[PCLZIP_OPT_BY_NAME])) |
||
| 318 | $selectFilter = function ($key, $value) use ($options) { |
||
| 319 | $allowedNames = is_array($options[PCLZIP_OPT_BY_NAME]) |
||
| 320 | ? $options[PCLZIP_OPT_BY_NAME] |
||
| 321 | : explode(',', $options[PCLZIP_OPT_BY_NAME]); |
||
| 322 | foreach ($allowedNames as $name) { |
||
| 323 | // select directory with nested files |
||
| 324 | if (in_array(substr($name, -1), array('/', '\\'))) { |
||
| 325 | if (strncasecmp($name, $key, strlen($name)) === 0) { |
||
| 326 | // that's a file inside a dir or that dir |
||
| 327 | return self::SELECT_FILTER_PASS; |
||
| 328 | } |
||
| 329 | } else { |
||
| 330 | // select exact name only |
||
| 331 | if (strcasecmp($name, $key) === 0) { |
||
| 332 | // that's a file with this name |
||
| 333 | return self::SELECT_FILTER_PASS; |
||
| 334 | } |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | // that file is not in allowed list |
||
| 339 | return self::SELECT_FILTER_REFUSE; |
||
| 340 | }; |
||
| 341 | // <ereg> rule |
||
| 342 | else if (isset($options[PCLZIP_OPT_BY_EREG]) && function_exists('ereg')) |
||
| 343 | $selectFilter = function ($key, $value) use ($options) { |
||
| 344 | return (ereg($options[PCLZIP_OPT_BY_EREG], $key) !== false) |
||
| 345 | ? self::SELECT_FILTER_PASS |
||
| 346 | : self::SELECT_FILTER_REFUSE; |
||
| 347 | }; |
||
| 348 | // <preg_match> rule |
||
| 349 | else if (isset($options[PCLZIP_OPT_BY_PREG])) |
||
| 350 | $selectFilter = function ($key, $value) use ($options) { |
||
| 351 | return preg_match($options[PCLZIP_OPT_BY_PREG], $key) |
||
| 352 | ? self::SELECT_FILTER_PASS |
||
| 353 | : self::SELECT_FILTER_REFUSE; |
||
| 354 | }; |
||
| 355 | // index rule |
||
| 356 | else if (isset($options[PCLZIP_OPT_BY_INDEX])) |
||
| 357 | $selectFilter = function ($key, $value, $index) use ($options) { |
||
| 358 | $allowedIndexes = array(); |
||
| 359 | foreach ($options[PCLZIP_OPT_BY_INDEX] as $rule) { |
||
| 360 | $parts = explode('-', $rule); |
||
| 361 | if (count($parts) == 1) $allowedIndexes[] = $rule; |
||
| 362 | else $allowedIndexes = array_merge( |
||
| 363 | range($parts[0], $parts[1]), $allowedIndexes); |
||
| 364 | } |
||
| 365 | |||
| 366 | return in_array($index, $allowedIndexes) ? self::SELECT_FILTER_PASS |
||
| 367 | : self::SELECT_FILTER_REFUSE; |
||
| 368 | }; |
||
| 369 | // no rule |
||
| 370 | else |
||
| 371 | $selectFilter = function () { return self::SELECT_FILTER_PASS; }; |
||
| 372 | |||
| 373 | if (isset($options[PCLZIP_OPT_EXTRACT_AS_STRING])) |
||
| 374 | $anotherOutputFormat = PCLZIP_OPT_EXTRACT_AS_STRING; |
||
| 375 | else if (isset($options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) |
||
| 376 | $anotherOutputFormat = PCLZIP_OPT_EXTRACT_IN_OUTPUT; |
||
| 377 | else $anotherOutputFormat = false; |
||
| 378 | |||
| 379 | if (isset($options[PCLZIP_OPT_REPLACE_NEWER])) |
||
| 380 | $doNotReplaceNewer = false; |
||
| 381 | else $doNotReplaceNewer = true; |
||
| 382 | |||
| 383 | if (isset($options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) |
||
| 384 | $restrictExtractDir = $options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION]; |
||
| 385 | else $restrictExtractDir = false; |
||
| 386 | |||
| 387 | $report = array(); |
||
| 388 | foreach ($this->listContent() as $file_header) { |
||
| 389 | // add file information to report |
||
| 390 | $report[] = $file_header; |
||
| 391 | // refuse by select rule |
||
| 392 | if (call_user_func($selectFilter, $file_header->stored_filename, |
||
| 393 | $file_header->filename, $file_header->index) |
||
| 394 | === self::SELECT_FILTER_REFUSE) { |
||
| 395 | // |
||
| 396 | // I don't know need to remain this file in report or not, |
||
| 397 | // but for now I remove |
||
| 398 | array_pop($report); |
||
| 399 | // $file_header->status = 'filtered'; |
||
| 400 | // |
||
| 401 | continue; |
||
| 402 | } |
||
| 403 | |||
| 404 | // |
||
| 405 | // add extract path in case of extraction |
||
| 406 | // for some reason need to do it before call pre extract callback |
||
| 407 | // (pclzip.lib.php v2.8.2, line 3670) |
||
| 408 | // so I decided to do it here too |
||
| 409 | // |
||
| 410 | if ($anotherOutputFormat === false) { |
||
| 411 | $file_header->filename = realpath($extractPath.'/'. |
||
| 412 | $file_header->filename); |
||
| 413 | // |
||
| 414 | // check for path correlation with restricted path |
||
| 415 | // |
||
| 416 | if ($restrictExtractDir !== false) { |
||
| 417 | $filename = $file_header->filename; |
||
| 418 | $restrictedDir = realpath($restrictExtractDir); |
||
| 419 | if (strncasecmp($restrictedDir, $filename, |
||
| 420 | strlen($restrictedDir)) !== 0) { |
||
| 421 | // refuse file extraction |
||
| 422 | $file_header->status = 'filtered'; |
||
| 423 | continue; |
||
| 424 | } |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | // apply pre extract callback |
||
| 429 | $callback_result = call_user_func($preExtractCallback, |
||
| 430 | $file_header); |
||
| 431 | if ($callback_result == 1) { |
||
| 432 | // go on ... |
||
| 433 | } elseif ($callback_result == 0) { |
||
| 434 | // skip current file |
||
| 435 | $file_header->status = 'skipped'; |
||
| 436 | continue; |
||
| 437 | } elseif ($callback_result == 2) { |
||
| 438 | // skip & stop extraction |
||
| 439 | $file_header->status = 'aborted'; |
||
| 440 | break; |
||
| 441 | } |
||
| 442 | |||
| 443 | // return content |
||
| 444 | if ($anotherOutputFormat == PCLZIP_OPT_EXTRACT_AS_STRING) { |
||
| 445 | $file_header->content |
||
| 446 | = $this->archive->getFromName($file_header->stored_filename); |
||
| 447 | } |
||
| 448 | // echo content |
||
| 449 | else if ($anotherOutputFormat == PCLZIP_OPT_EXTRACT_IN_OUTPUT) { |
||
| 450 | echo $this->archive->getFromName($file_header->stored_filename); |
||
| 451 | } |
||
| 452 | // extract content |
||
| 453 | else if ($anotherOutputFormat === false) { |
||
| 454 | // apply path filters |
||
| 455 | foreach ($filters as $filter) call_user_func($filter, |
||
| 456 | $file_header->stored_filename, $file_header->filename); |
||
| 457 | // dir extraction process |
||
| 458 | if ($file_header->folder) { |
||
| 459 | // if dir doesn't exist |
||
| 460 | if (!is_dir($file_header->filename)) { |
||
| 461 | // try to create folder |
||
| 462 | if (!mkdir($file_header)) { |
||
| 463 | $file_header->status = 'path_creation_fail'; |
||
| 464 | continue; |
||
| 465 | } |
||
| 466 | } |
||
| 467 | } |
||
| 468 | // file extraction process |
||
| 469 | else { |
||
| 470 | // check if path is already taken by a folder |
||
| 471 | if (is_dir($file_header->filename)) { |
||
| 472 | $file_header->status = 'already_a_directory'; |
||
| 473 | continue; |
||
| 474 | } |
||
| 475 | // check if file path is not writable |
||
| 476 | if (!is_writable($file_header->filename)) { |
||
| 477 | $file_header->status = 'write_protected'; |
||
| 478 | continue; |
||
| 479 | } |
||
| 480 | // check if file exists and it's newer |
||
| 481 | if (is_file($file_header->filename)) { |
||
| 482 | if (filemtime($file_header->filename) |
||
| 483 | > $file_header->mtime) { |
||
| 484 | // skip extraction if option EXTRACT_NEWER isn't set |
||
| 485 | if ($doNotReplaceNewer) { |
||
| 486 | $file_header->status = 'newer_exist'; |
||
| 487 | continue; |
||
| 488 | } |
||
| 489 | } |
||
| 490 | } |
||
| 491 | $directory = dirname($file_header->filename); |
||
| 492 | // check if running process can not create extraction folder |
||
| 493 | if (!is_dir($directory)) { |
||
| 494 | if (!mkdir($directory)) { |
||
| 495 | $file_header->status = 'path_creation_fail'; |
||
| 496 | continue; |
||
| 497 | } |
||
| 498 | } |
||
| 499 | // extraction |
||
| 500 | if (copy("zip://".$this->archive->filename."#" |
||
| 501 | .$file_header->stored_filename |
||
| 502 | , $file_header->filename)) { |
||
| 503 | // ok |
||
| 504 | } |
||
| 505 | // extraction fails |
||
| 506 | else { |
||
| 507 | $file_header->status = 'write_error'; |
||
| 508 | continue; |
||
| 509 | } |
||
| 510 | } |
||
| 511 | } |
||
| 512 | |||
| 513 | // apply post extract callback |
||
| 514 | $callback_result = call_user_func($postExtractCallback, |
||
| 515 | $file_header); |
||
| 516 | if ($callback_result == 1) { |
||
| 517 | // go on |
||
| 518 | } elseif ($callback_result == 2) { |
||
| 519 | // skip & stop extraction |
||
| 520 | break; |
||
| 521 | } |
||
| 522 | } |
||
| 523 | |||
| 524 | return $report; |
||
| 525 | } |
||
| 800 | } |
This check looks for imports that have been defined, but are not used in the scope.