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 | * Constructor |
||
| 29 | * Extend options with required fields |
||
| 30 | * |
||
| 31 | * @return void |
||
| 32 | * @author Dmitry (dio) Levashov |
||
| 33 | **/ |
||
| 34 | public function __construct() { |
||
| 42 | |||
| 43 | /*********************************************************************/ |
||
| 44 | /* INIT AND CONFIGURE */ |
||
| 45 | /*********************************************************************/ |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Prepare driver before mount volume. |
||
| 49 | * Return true if volume is ready. |
||
| 50 | * |
||
| 51 | * @return bool |
||
| 52 | **/ |
||
| 53 | protected function init() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Configure after successfull mount. |
||
| 84 | * |
||
| 85 | * @return void |
||
| 86 | * @author Dmitry (dio) Levashov |
||
| 87 | **/ |
||
| 88 | protected function configure() { |
||
| 150 | |||
| 151 | /*********************************************************************/ |
||
| 152 | /* FS API */ |
||
| 153 | /*********************************************************************/ |
||
| 154 | |||
| 155 | /*********************** paths/urls *************************/ |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Return parent directory path |
||
| 159 | * |
||
| 160 | * @param string $path file path |
||
| 161 | * @return string |
||
| 162 | * @author Dmitry (dio) Levashov |
||
| 163 | **/ |
||
| 164 | protected function _dirname($path) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Return file name |
||
| 170 | * |
||
| 171 | * @param string $path file path |
||
| 172 | * @return string |
||
| 173 | * @author Dmitry (dio) Levashov |
||
| 174 | **/ |
||
| 175 | protected function _basename($path) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Join dir name and file name and retur full path |
||
| 181 | * |
||
| 182 | * @param string $dir |
||
| 183 | * @param string $name |
||
| 184 | * @return string |
||
| 185 | * @author Dmitry (dio) Levashov |
||
| 186 | **/ |
||
| 187 | protected function _joinPath($dir, $name) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Return normalized path, this works the same as os.path.normpath() in Python |
||
| 193 | * |
||
| 194 | * @param string $path path |
||
| 195 | * @return string |
||
| 196 | * @author Troex Nevelin |
||
| 197 | **/ |
||
| 198 | protected function _normpath($path) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Return file path related to root dir |
||
| 252 | * |
||
| 253 | * @param string $path file path |
||
| 254 | * @return string |
||
| 255 | * @author Dmitry (dio) Levashov |
||
| 256 | **/ |
||
| 257 | View Code Duplication | protected function _relpath($path) { |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Convert path related to root dir into real path |
||
| 272 | * |
||
| 273 | * @param string $path file path |
||
| 274 | * @return string |
||
| 275 | * @author Dmitry (dio) Levashov |
||
| 276 | **/ |
||
| 277 | protected function _abspath($path) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Return fake path started from root dir |
||
| 292 | * |
||
| 293 | * @param string $path file path |
||
| 294 | * @return string |
||
| 295 | * @author Dmitry (dio) Levashov |
||
| 296 | **/ |
||
| 297 | protected function _path($path) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Return true if $path is children of $parent |
||
| 303 | * |
||
| 304 | * @param string $path path to check |
||
| 305 | * @param string $parent parent path |
||
| 306 | * @return bool |
||
| 307 | * @author Dmitry (dio) Levashov |
||
| 308 | **/ |
||
| 309 | protected function _inpath($path, $parent) { |
||
| 318 | |||
| 319 | |||
| 320 | |||
| 321 | /***************** file stat ********************/ |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Return stat for given path. |
||
| 325 | * Stat contains following fields: |
||
| 326 | * - (int) size file size in b. required |
||
| 327 | * - (int) ts file modification time in unix time. required |
||
| 328 | * - (string) mime mimetype. required for folders, others - optionally |
||
| 329 | * - (bool) read read permissions. required |
||
| 330 | * - (bool) write write permissions. required |
||
| 331 | * - (bool) locked is object locked. optionally |
||
| 332 | * - (bool) hidden is object hidden. optionally |
||
| 333 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
| 334 | * - (string) target for symlinks - link target path. optionally |
||
| 335 | * |
||
| 336 | * If file does not exists - returns empty array or false. |
||
| 337 | * |
||
| 338 | * @param string $path file path |
||
| 339 | * @return array|false |
||
| 340 | * @author Dmitry (dio) Levashov |
||
| 341 | **/ |
||
| 342 | protected function _stat($path) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get stat `owner`, `group` and `isowner` by `uid` and `gid` |
||
| 408 | * Sub-fuction of _stat() and _scandir() |
||
| 409 | * |
||
| 410 | * @param integer $uid |
||
| 411 | * @param integer $gid |
||
| 412 | * @return array stat |
||
| 413 | */ |
||
| 414 | protected function getOwnerStat($uid, $gid) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Return true if path is dir and has at least one childs directory |
||
| 458 | * |
||
| 459 | * @param string $path dir path |
||
| 460 | * @return bool |
||
| 461 | * @author Dmitry (dio) Levashov |
||
| 462 | **/ |
||
| 463 | protected function _subdirs($path) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Return object width and height |
||
| 474 | * Usualy used for images, but can be realize for video etc... |
||
| 475 | * |
||
| 476 | * @param string $path file path |
||
| 477 | * @param string $mime file mime type |
||
| 478 | * @return string |
||
| 479 | * @author Dmitry (dio) Levashov |
||
| 480 | **/ |
||
| 481 | protected function _dimensions($path, $mime) { |
||
| 487 | /******************** file/dir content *********************/ |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Return symlink target file |
||
| 491 | * |
||
| 492 | * @param string $path link path |
||
| 493 | * @return string |
||
| 494 | * @author Dmitry (dio) Levashov |
||
| 495 | **/ |
||
| 496 | protected function readlink($path) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Return files list in directory. |
||
| 514 | * |
||
| 515 | * @param string $path dir path |
||
| 516 | * @return array |
||
| 517 | * @author Dmitry (dio) Levashov |
||
| 518 | **/ |
||
| 519 | protected function _scandir($path) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Open file and return file pointer |
||
| 605 | * |
||
| 606 | * @param string $path file path |
||
| 607 | * @param bool $write open file for writing |
||
| 608 | * @return resource|false |
||
| 609 | * @author Dmitry (dio) Levashov |
||
| 610 | **/ |
||
| 611 | protected function _fopen($path, $mode='rb') { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Close opened file |
||
| 617 | * |
||
| 618 | * @param resource $fp file pointer |
||
| 619 | * @return bool |
||
| 620 | * @author Dmitry (dio) Levashov |
||
| 621 | **/ |
||
| 622 | protected function _fclose($fp, $path='') { |
||
| 625 | |||
| 626 | /******************** file/dir manipulations *************************/ |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Create dir and return created dir path or false on failed |
||
| 630 | * |
||
| 631 | * @param string $path parent dir path |
||
| 632 | * @param string $name new directory name |
||
| 633 | * @return string|bool |
||
| 634 | * @author Dmitry (dio) Levashov |
||
| 635 | **/ |
||
| 636 | View Code Duplication | protected function _mkdir($path, $name) { |
|
| 647 | |||
| 648 | /** |
||
| 649 | * Create file and return it's path or false on failed |
||
| 650 | * |
||
| 651 | * @param string $path parent dir path |
||
| 652 | * @param string $name new file name |
||
| 653 | * @return string|bool |
||
| 654 | * @author Dmitry (dio) Levashov |
||
| 655 | **/ |
||
| 656 | View Code Duplication | protected function _mkfile($path, $name) { |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Create symlink |
||
| 670 | * |
||
| 671 | * @param string $source file to link to |
||
| 672 | * @param string $targetDir folder to create link in |
||
| 673 | * @param string $name symlink name |
||
| 674 | * @return bool |
||
| 675 | * @author Dmitry (dio) Levashov |
||
| 676 | **/ |
||
| 677 | protected function _symlink($source, $targetDir, $name) { |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Copy file into another file |
||
| 683 | * |
||
| 684 | * @param string $source source file path |
||
| 685 | * @param string $targetDir target directory path |
||
| 686 | * @param string $name new file name |
||
| 687 | * @return bool |
||
| 688 | * @author Dmitry (dio) Levashov |
||
| 689 | **/ |
||
| 690 | protected function _copy($source, $targetDir, $name) { |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Move file into another parent dir. |
||
| 698 | * Return new file path or false. |
||
| 699 | * |
||
| 700 | * @param string $source source file path |
||
| 701 | * @param string $target target dir path |
||
| 702 | * @param string $name file name |
||
| 703 | * @return string|bool |
||
| 704 | * @author Dmitry (dio) Levashov |
||
| 705 | **/ |
||
| 706 | protected function _move($source, $targetDir, $name) { |
||
| 712 | |||
| 713 | /** |
||
| 714 | * Remove file |
||
| 715 | * |
||
| 716 | * @param string $path file path |
||
| 717 | * @return bool |
||
| 718 | * @author Dmitry (dio) Levashov |
||
| 719 | **/ |
||
| 720 | protected function _unlink($path) { |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Remove dir |
||
| 728 | * |
||
| 729 | * @param string $path dir path |
||
| 730 | * @return bool |
||
| 731 | * @author Dmitry (dio) Levashov |
||
| 732 | **/ |
||
| 733 | protected function _rmdir($path) { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Create new file and write into it from file pointer. |
||
| 741 | * Return new file path or false on error. |
||
| 742 | * |
||
| 743 | * @param resource $fp file pointer |
||
| 744 | * @param string $dir target dir path |
||
| 745 | * @param string $name file name |
||
| 746 | * @param array $stat file stat (required by some virtual fs) |
||
| 747 | * @return bool|string |
||
| 748 | * @author Dmitry (dio) Levashov |
||
| 749 | **/ |
||
| 750 | protected function _save($fp, $dir, $name, $stat) { |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Get file contents |
||
| 777 | * |
||
| 778 | * @param string $path file path |
||
| 779 | * @return string|false |
||
| 780 | * @author Dmitry (dio) Levashov |
||
| 781 | **/ |
||
| 782 | protected function _getContents($path) { |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Write a string to a file |
||
| 788 | * |
||
| 789 | * @param string $path file path |
||
| 790 | * @param string $content new file content |
||
| 791 | * @return bool |
||
| 792 | * @author Dmitry (dio) Levashov |
||
| 793 | **/ |
||
| 794 | protected function _filePutContents($path, $content) { |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Detect available archivers |
||
| 804 | * |
||
| 805 | * @return void |
||
| 806 | **/ |
||
| 807 | protected function _checkArchivers() { |
||
| 811 | |||
| 812 | /** |
||
| 813 | * chmod availability |
||
| 814 | * |
||
| 815 | * @return bool |
||
| 816 | **/ |
||
| 817 | protected function _chmod($path, $mode) { |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Recursive symlinks search |
||
| 826 | * |
||
| 827 | * @param string $path file/dir path |
||
| 828 | * @return bool |
||
| 829 | * @author Dmitry (dio) Levashov |
||
| 830 | **/ |
||
| 831 | protected function _findSymlinks($path) { |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Extract files from archive |
||
| 863 | * |
||
| 864 | * @param string $path archive path |
||
| 865 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
| 866 | * @return true |
||
| 867 | * @author Dmitry (dio) Levashov, |
||
| 868 | * @author Alexey Sukhotin |
||
| 869 | **/ |
||
| 870 | protected function _extract($path, $arc) { |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Create archive and return its path |
||
| 978 | * |
||
| 979 | * @param string $dir target dir |
||
| 980 | * @param array $files files names list |
||
| 981 | * @param string $name archive name |
||
| 982 | * @param array $arc archiver options |
||
| 983 | * @return string|bool |
||
| 984 | * @author Dmitry (dio) Levashov, |
||
| 985 | * @author Alexey Sukhotin |
||
| 986 | **/ |
||
| 987 | protected function _archive($dir, $files, $name, $arc) { |
||
| 990 | |||
| 991 | /******************** Over write functions *************************/ |
||
| 992 | |||
| 993 | /** |
||
| 994 | * File path of local server side work file path |
||
| 995 | * |
||
| 996 | * @param string $path |
||
| 997 | * @return string |
||
| 998 | * @author Naoki Sawada |
||
| 999 | */ |
||
| 1000 | protected function getWorkFile($path) { |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Delete dirctory trees |
||
| 1006 | * |
||
| 1007 | * @param string $localpath path need convert encoding to server encoding |
||
| 1008 | * @return boolean |
||
| 1009 | * @author Naoki Sawada |
||
| 1010 | */ |
||
| 1011 | protected function delTree($localpath) { |
||
| 1014 | |||
| 1015 | /******************** Over write (Optimized) functions *************************/ |
||
| 1016 | |||
| 1017 | /** |
||
| 1018 | * Recursive files search |
||
| 1019 | * |
||
| 1020 | * @param string $path dir path |
||
| 1021 | * @param string $q search string |
||
| 1022 | * @param array $mimes |
||
| 1023 | * @return array |
||
| 1024 | * @author Dmitry (dio) Levashov |
||
| 1025 | * @author Naoki Sawada |
||
| 1026 | **/ |
||
| 1027 | protected function doSearch($path, $q, $mimes) { |
||
| 1086 | |||
| 1087 | } // END class |
||
| 1088 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.