Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like elFinderVolumeLocalFileSystem 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 elFinderVolumeLocalFileSystem, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class elFinderVolumeLocalFileSystem extends elFinderVolumeDriver { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Driver id |
||
| 13 | * Must be started from letter and contains [a-z0-9] |
||
| 14 | * Used as part of volume id |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | **/ |
||
| 18 | protected $driverId = 'l'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Required to count total archive files size |
||
| 22 | * |
||
| 23 | * @var int |
||
| 24 | **/ |
||
| 25 | protected $archiveSize = 0; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Canonicalized absolute pathname of $root |
||
| 29 | * |
||
| 30 | * @var strung |
||
| 31 | */ |
||
| 32 | protected $aroot; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Constructor |
||
| 36 | * Extend options with required fields |
||
| 37 | * |
||
| 38 | * @return void |
||
|
|
|||
| 39 | * @author Dmitry (dio) Levashov |
||
| 40 | **/ |
||
| 41 | public function __construct() { |
||
| 48 | |||
| 49 | /*********************************************************************/ |
||
| 50 | /* INIT AND CONFIGURE */ |
||
| 51 | /*********************************************************************/ |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Configure after successfull mount. |
||
| 55 | * |
||
| 56 | * @return void |
||
| 57 | * @author Dmitry (dio) Levashov |
||
| 58 | **/ |
||
| 59 | protected function configure() { |
||
| 107 | |||
| 108 | /*********************************************************************/ |
||
| 109 | /* FS API */ |
||
| 110 | /*********************************************************************/ |
||
| 111 | |||
| 112 | /*********************** paths/urls *************************/ |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Return parent directory path |
||
| 116 | * |
||
| 117 | * @param string $path file path |
||
| 118 | * @return string |
||
| 119 | * @author Dmitry (dio) Levashov |
||
| 120 | **/ |
||
| 121 | protected function _dirname($path) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Return file name |
||
| 127 | * |
||
| 128 | * @param string $path file path |
||
| 129 | * @return string |
||
| 130 | * @author Dmitry (dio) Levashov |
||
| 131 | **/ |
||
| 132 | protected function _basename($path) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Join dir name and file name and retur full path |
||
| 138 | * |
||
| 139 | * @param string $dir |
||
| 140 | * @param string $name |
||
| 141 | * @return string |
||
| 142 | * @author Dmitry (dio) Levashov |
||
| 143 | **/ |
||
| 144 | protected function _joinPath($dir, $name) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Return normalized path, this works the same as os.path.normpath() in Python |
||
| 150 | * |
||
| 151 | * @param string $path path |
||
| 152 | * @return string |
||
| 153 | * @author Troex Nevelin |
||
| 154 | **/ |
||
| 155 | protected function _normpath($path) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return file path related to root dir |
||
| 200 | * |
||
| 201 | * @param string $path file path |
||
| 202 | * @return string |
||
| 203 | * @author Dmitry (dio) Levashov |
||
| 204 | **/ |
||
| 205 | protected function _relpath($path) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Convert path related to root dir into real path |
||
| 211 | * |
||
| 212 | * @param string $path file path |
||
| 213 | * @return string |
||
| 214 | * @author Dmitry (dio) Levashov |
||
| 215 | **/ |
||
| 216 | protected function _abspath($path) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Return fake path started from root dir |
||
| 222 | * |
||
| 223 | * @param string $path file path |
||
| 224 | * @return string |
||
| 225 | * @author Dmitry (dio) Levashov |
||
| 226 | **/ |
||
| 227 | protected function _path($path) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Return true if $path is children of $parent |
||
| 233 | * |
||
| 234 | * @param string $path path to check |
||
| 235 | * @param string $parent parent path |
||
| 236 | * @return bool |
||
| 237 | * @author Dmitry (dio) Levashov |
||
| 238 | **/ |
||
| 239 | protected function _inpath($path, $parent) { |
||
| 247 | |||
| 248 | |||
| 249 | |||
| 250 | /***************** file stat ********************/ |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Return stat for given path. |
||
| 254 | * Stat contains following fields: |
||
| 255 | * - (int) size file size in b. required |
||
| 256 | * - (int) ts file modification time in unix time. required |
||
| 257 | * - (string) mime mimetype. required for folders, others - optionally |
||
| 258 | * - (bool) read read permissions. required |
||
| 259 | * - (bool) write write permissions. required |
||
| 260 | * - (bool) locked is object locked. optionally |
||
| 261 | * - (bool) hidden is object hidden. optionally |
||
| 262 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
| 263 | * - (string) target for symlinks - link target path. optionally |
||
| 264 | * |
||
| 265 | * If file does not exists - returns empty array or false. |
||
| 266 | * |
||
| 267 | * @param string $path file path |
||
| 268 | * @return array|false |
||
| 269 | * @author Dmitry (dio) Levashov |
||
| 270 | **/ |
||
| 271 | protected function _stat($path) { |
||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * Return true if path is dir and has at least one childs directory |
||
| 321 | * |
||
| 322 | * @param string $path dir path |
||
| 323 | * @return bool |
||
| 324 | * @author Dmitry (dio) Levashov |
||
| 325 | **/ |
||
| 326 | protected function _subdirs($path) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Return object width and height |
||
| 344 | * Usualy used for images, but can be realize for video etc... |
||
| 345 | * |
||
| 346 | * @param string $path file path |
||
| 347 | * @param string $mime file mime type |
||
| 348 | * @return string |
||
| 349 | * @author Dmitry (dio) Levashov |
||
| 350 | **/ |
||
| 351 | protected function _dimensions($path, $mime) { |
||
| 357 | /******************** file/dir content *********************/ |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Return symlink target file |
||
| 361 | * |
||
| 362 | * @param string $path link path |
||
| 363 | * @return string |
||
| 364 | * @author Dmitry (dio) Levashov |
||
| 365 | **/ |
||
| 366 | protected function readlink($path) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Return files list in directory. |
||
| 385 | * |
||
| 386 | * @param string $path dir path |
||
| 387 | * @return array |
||
| 388 | * @author Dmitry (dio) Levashov |
||
| 389 | **/ |
||
| 390 | protected function _scandir($path) { |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Open file and return file pointer |
||
| 403 | * |
||
| 404 | * @param string $path file path |
||
| 405 | * @param bool $write open file for writing |
||
| 406 | * @return resource|false |
||
| 407 | * @author Dmitry (dio) Levashov |
||
| 408 | **/ |
||
| 409 | protected function _fopen($path, $mode='rb') { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Close opened file |
||
| 415 | * |
||
| 416 | * @param resource $fp file pointer |
||
| 417 | * @return bool |
||
| 418 | * @author Dmitry (dio) Levashov |
||
| 419 | **/ |
||
| 420 | protected function _fclose($fp, $path='') { |
||
| 423 | |||
| 424 | /******************** file/dir manipulations *************************/ |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Create dir and return created dir path or false on failed |
||
| 428 | * |
||
| 429 | * @param string $path parent dir path |
||
| 430 | * @param string $name new directory name |
||
| 431 | * @return string|bool |
||
| 432 | * @author Dmitry (dio) Levashov |
||
| 433 | **/ |
||
| 434 | View Code Duplication | protected function _mkdir($path, $name) { |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Create file and return it's path or false on failed |
||
| 447 | * |
||
| 448 | * @param string $path parent dir path |
||
| 449 | * @param string $name new file name |
||
| 450 | * @return string|bool |
||
| 451 | * @author Dmitry (dio) Levashov |
||
| 452 | **/ |
||
| 453 | View Code Duplication | protected function _mkfile($path, $name) { |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Create symlink |
||
| 466 | * |
||
| 467 | * @param string $source file to link to |
||
| 468 | * @param string $targetDir folder to create link in |
||
| 469 | * @param string $name symlink name |
||
| 470 | * @return bool |
||
| 471 | * @author Dmitry (dio) Levashov |
||
| 472 | **/ |
||
| 473 | protected function _symlink($source, $targetDir, $name) { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Copy file into another file |
||
| 479 | * |
||
| 480 | * @param string $source source file path |
||
| 481 | * @param string $targetDir target directory path |
||
| 482 | * @param string $name new file name |
||
| 483 | * @return bool |
||
| 484 | * @author Dmitry (dio) Levashov |
||
| 485 | **/ |
||
| 486 | protected function _copy($source, $targetDir, $name) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Move file into another parent dir. |
||
| 492 | * Return new file path or false. |
||
| 493 | * |
||
| 494 | * @param string $source source file path |
||
| 495 | * @param string $target target dir path |
||
| 496 | * @param string $name file name |
||
| 497 | * @return string|bool |
||
| 498 | * @author Dmitry (dio) Levashov |
||
| 499 | **/ |
||
| 500 | protected function _move($source, $targetDir, $name) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Remove file |
||
| 507 | * |
||
| 508 | * @param string $path file path |
||
| 509 | * @return bool |
||
| 510 | * @author Dmitry (dio) Levashov |
||
| 511 | **/ |
||
| 512 | protected function _unlink($path) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Remove dir |
||
| 518 | * |
||
| 519 | * @param string $path dir path |
||
| 520 | * @return bool |
||
| 521 | * @author Dmitry (dio) Levashov |
||
| 522 | **/ |
||
| 523 | protected function _rmdir($path) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Create new file and write into it from file pointer. |
||
| 529 | * Return new file path or false on error. |
||
| 530 | * |
||
| 531 | * @param resource $fp file pointer |
||
| 532 | * @param string $dir target dir path |
||
| 533 | * @param string $name file name |
||
| 534 | * @param array $stat file stat (required by some virtual fs) |
||
| 535 | * @return bool|string |
||
| 536 | * @author Dmitry (dio) Levashov |
||
| 537 | **/ |
||
| 538 | protected function _save($fp, $dir, $name, $stat) { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Get file contents |
||
| 556 | * |
||
| 557 | * @param string $path file path |
||
| 558 | * @return string|false |
||
| 559 | * @author Dmitry (dio) Levashov |
||
| 560 | **/ |
||
| 561 | protected function _getContents($path) { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Write a string to a file |
||
| 567 | * |
||
| 568 | * @param string $path file path |
||
| 569 | * @param string $content new file content |
||
| 570 | * @return bool |
||
| 571 | * @author Dmitry (dio) Levashov |
||
| 572 | **/ |
||
| 573 | protected function _filePutContents($path, $content) { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Detect available archivers |
||
| 583 | * |
||
| 584 | * @return void |
||
| 585 | **/ |
||
| 586 | protected function _checkArchivers() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Unpack archive |
||
| 593 | * |
||
| 594 | * @param string $path archive path |
||
| 595 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
| 596 | * @return void |
||
| 597 | * @author Dmitry (dio) Levashov |
||
| 598 | * @author Alexey Sukhotin |
||
| 599 | **/ |
||
| 600 | protected function _unpack($path, $arc) { |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Recursive symlinks search |
||
| 611 | * |
||
| 612 | * @param string $path file/dir path |
||
| 613 | * @return bool |
||
| 614 | * @author Dmitry (dio) Levashov |
||
| 615 | **/ |
||
| 616 | View Code Duplication | protected function _findSymlinks($path) { |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Extract files from archive |
||
| 645 | * |
||
| 646 | * @param string $path archive path |
||
| 647 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
| 648 | * @return true |
||
| 649 | * @author Dmitry (dio) Levashov, |
||
| 650 | * @author Alexey Sukhotin |
||
| 651 | **/ |
||
| 652 | protected function _extract($path, $arc) { |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Create archive and return its path |
||
| 739 | * |
||
| 740 | * @param string $dir target dir |
||
| 741 | * @param array $files files names list |
||
| 742 | * @param string $name archive name |
||
| 743 | * @param array $arc archiver options |
||
| 744 | * @return string|bool |
||
| 745 | * @author Dmitry (dio) Levashov, |
||
| 746 | * @author Alexey Sukhotin |
||
| 747 | **/ |
||
| 748 | protected function _archive($dir, $files, $name, $arc) { |
||
| 761 | |||
| 762 | } // END class |
||
| 763 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.