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 { |
||
|
1 ignored issue
–
show
|
|||
| 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() { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Configure after successfull mount. |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | * @author Dmitry (dio) Levashov |
||
| 84 | **/ |
||
| 85 | protected function configure() { |
||
| 140 | |||
| 141 | /*********************************************************************/ |
||
| 142 | /* FS API */ |
||
| 143 | /*********************************************************************/ |
||
| 144 | |||
| 145 | /*********************** paths/urls *************************/ |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Return parent directory path |
||
| 149 | * |
||
| 150 | * @param string $path file path |
||
| 151 | * @return string |
||
| 152 | * @author Dmitry (dio) Levashov |
||
| 153 | **/ |
||
| 154 | protected function _dirname($path) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Return file name |
||
| 160 | * |
||
| 161 | * @param string $path file path |
||
| 162 | * @return string |
||
| 163 | * @author Dmitry (dio) Levashov |
||
| 164 | **/ |
||
| 165 | protected function _basename($path) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Join dir name and file name and retur full path |
||
| 171 | * |
||
| 172 | * @param string $dir |
||
| 173 | * @param string $name |
||
| 174 | * @return string |
||
| 175 | * @author Dmitry (dio) Levashov |
||
| 176 | **/ |
||
| 177 | protected function _joinPath($dir, $name) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Return normalized path, this works the same as os.path.normpath() in Python |
||
| 183 | * |
||
| 184 | * @param string $path path |
||
| 185 | * @return string |
||
| 186 | * @author Troex Nevelin |
||
| 187 | **/ |
||
| 188 | protected function _normpath($path) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Return file path related to root dir |
||
| 242 | * |
||
| 243 | * @param string $path file path |
||
| 244 | * @return string |
||
| 245 | * @author Dmitry (dio) Levashov |
||
| 246 | **/ |
||
| 247 | View Code Duplication | protected function _relpath($path) { |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Convert path related to root dir into real path |
||
| 262 | * |
||
| 263 | * @param string $path file path |
||
| 264 | * @return string |
||
| 265 | * @author Dmitry (dio) Levashov |
||
| 266 | **/ |
||
| 267 | protected function _abspath($path) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Return fake path started from root dir |
||
| 282 | * |
||
| 283 | * @param string $path file path |
||
| 284 | * @return string |
||
| 285 | * @author Dmitry (dio) Levashov |
||
| 286 | **/ |
||
| 287 | protected function _path($path) { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Return true if $path is children of $parent |
||
| 293 | * |
||
| 294 | * @param string $path path to check |
||
| 295 | * @param string $parent parent path |
||
| 296 | * @return bool |
||
| 297 | * @author Dmitry (dio) Levashov |
||
| 298 | **/ |
||
| 299 | protected function _inpath($path, $parent) { |
||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | /***************** file stat ********************/ |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Return stat for given path. |
||
| 315 | * Stat contains following fields: |
||
| 316 | * - (int) size file size in b. required |
||
| 317 | * - (int) ts file modification time in unix time. required |
||
| 318 | * - (string) mime mimetype. required for folders, others - optionally |
||
| 319 | * - (bool) read read permissions. required |
||
| 320 | * - (bool) write write permissions. required |
||
| 321 | * - (bool) locked is object locked. optionally |
||
| 322 | * - (bool) hidden is object hidden. optionally |
||
| 323 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
| 324 | * - (string) target for symlinks - link target path. optionally |
||
| 325 | * |
||
| 326 | * If file does not exists - returns empty array or false. |
||
| 327 | * |
||
| 328 | * @param string $path file path |
||
| 329 | * @return array|false |
||
| 330 | * @author Dmitry (dio) Levashov |
||
| 331 | **/ |
||
| 332 | protected function _stat($path) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get stat `owner`, `group` and `isowner` by `uid` and `gid` |
||
| 398 | * Sub-fuction of _stat() and _scandir() |
||
| 399 | * |
||
| 400 | * @param integer $uid |
||
| 401 | * @param integer $gid |
||
| 402 | * @return array stat |
||
| 403 | */ |
||
| 404 | protected function getOwnerStat($uid, $gid) { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Return true if path is dir and has at least one childs directory |
||
| 448 | * |
||
| 449 | * @param string $path dir path |
||
| 450 | * @return bool |
||
| 451 | * @author Dmitry (dio) Levashov |
||
| 452 | **/ |
||
| 453 | protected function _subdirs($path) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Return object width and height |
||
| 463 | * Usualy used for images, but can be realize for video etc... |
||
| 464 | * |
||
| 465 | * @param string $path file path |
||
| 466 | * @param string $mime file mime type |
||
| 467 | * @return string |
||
| 468 | * @author Dmitry (dio) Levashov |
||
| 469 | **/ |
||
| 470 | protected function _dimensions($path, $mime) { |
||
| 476 | /******************** file/dir content *********************/ |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Return symlink target file |
||
| 480 | * |
||
| 481 | * @param string $path link path |
||
| 482 | * @return string |
||
| 483 | * @author Dmitry (dio) Levashov |
||
| 484 | **/ |
||
| 485 | protected function readlink($path) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Return files list in directory. |
||
| 503 | * |
||
| 504 | * @param string $path dir path |
||
| 505 | * @return array |
||
| 506 | * @author Dmitry (dio) Levashov |
||
| 507 | **/ |
||
| 508 | protected function _scandir($path) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Open file and return file pointer |
||
| 594 | * |
||
| 595 | * @param string $path file path |
||
| 596 | * @param bool $write open file for writing |
||
| 597 | * @return resource|false |
||
| 598 | * @author Dmitry (dio) Levashov |
||
| 599 | **/ |
||
| 600 | protected function _fopen($path, $mode='rb') { |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Close opened file |
||
| 606 | * |
||
| 607 | * @param resource $fp file pointer |
||
| 608 | * @return bool |
||
| 609 | * @author Dmitry (dio) Levashov |
||
| 610 | **/ |
||
| 611 | protected function _fclose($fp, $path='') { |
||
| 614 | |||
| 615 | /******************** file/dir manipulations *************************/ |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Create dir and return created dir path or false on failed |
||
| 619 | * |
||
| 620 | * @param string $path parent dir path |
||
| 621 | * @param string $name new directory name |
||
| 622 | * @return string|bool |
||
| 623 | * @author Dmitry (dio) Levashov |
||
| 624 | **/ |
||
| 625 | View Code Duplication | protected function _mkdir($path, $name) { |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Create file and return it's path or false on failed |
||
| 639 | * |
||
| 640 | * @param string $path parent dir path |
||
| 641 | * @param string $name new file name |
||
| 642 | * @return string|bool |
||
| 643 | * @author Dmitry (dio) Levashov |
||
| 644 | **/ |
||
| 645 | View Code Duplication | protected function _mkfile($path, $name) { |
|
| 656 | |||
| 657 | /** |
||
| 658 | * Create symlink |
||
| 659 | * |
||
| 660 | * @param string $source file to link to |
||
| 661 | * @param string $targetDir folder to create link in |
||
| 662 | * @param string $name symlink name |
||
| 663 | * @return bool |
||
| 664 | * @author Dmitry (dio) Levashov |
||
| 665 | **/ |
||
| 666 | protected function _symlink($source, $targetDir, $name) { |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Copy file into another file |
||
| 672 | * |
||
| 673 | * @param string $source source file path |
||
| 674 | * @param string $targetDir target directory path |
||
| 675 | * @param string $name new file name |
||
| 676 | * @return bool |
||
| 677 | * @author Dmitry (dio) Levashov |
||
| 678 | **/ |
||
| 679 | protected function _copy($source, $targetDir, $name) { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Move file into another parent dir. |
||
| 687 | * Return new file path or false. |
||
| 688 | * |
||
| 689 | * @param string $source source file path |
||
| 690 | * @param string $target target dir path |
||
| 691 | * @param string $name file name |
||
| 692 | * @return string|bool |
||
| 693 | * @author Dmitry (dio) Levashov |
||
| 694 | **/ |
||
| 695 | protected function _move($source, $targetDir, $name) { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Remove file |
||
| 704 | * |
||
| 705 | * @param string $path file path |
||
| 706 | * @return bool |
||
| 707 | * @author Dmitry (dio) Levashov |
||
| 708 | **/ |
||
| 709 | protected function _unlink($path) { |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Remove dir |
||
| 717 | * |
||
| 718 | * @param string $path dir path |
||
| 719 | * @return bool |
||
| 720 | * @author Dmitry (dio) Levashov |
||
| 721 | **/ |
||
| 722 | protected function _rmdir($path) { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Create new file and write into it from file pointer. |
||
| 730 | * Return new file path or false on error. |
||
| 731 | * |
||
| 732 | * @param resource $fp file pointer |
||
| 733 | * @param string $dir target dir path |
||
| 734 | * @param string $name file name |
||
| 735 | * @param array $stat file stat (required by some virtual fs) |
||
| 736 | * @return bool|string |
||
| 737 | * @author Dmitry (dio) Levashov |
||
| 738 | **/ |
||
| 739 | protected function _save($fp, $dir, $name, $stat) { |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Get file contents |
||
| 766 | * |
||
| 767 | * @param string $path file path |
||
| 768 | * @return string|false |
||
| 769 | * @author Dmitry (dio) Levashov |
||
| 770 | **/ |
||
| 771 | protected function _getContents($path) { |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Write a string to a file |
||
| 777 | * |
||
| 778 | * @param string $path file path |
||
| 779 | * @param string $content new file content |
||
| 780 | * @return bool |
||
| 781 | * @author Dmitry (dio) Levashov |
||
| 782 | **/ |
||
| 783 | protected function _filePutContents($path, $content) { |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Detect available archivers |
||
| 793 | * |
||
| 794 | * @return void |
||
| 795 | **/ |
||
| 796 | protected function _checkArchivers() { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * chmod availability |
||
| 803 | * |
||
| 804 | * @return bool |
||
| 805 | **/ |
||
| 806 | protected function _chmod($path, $mode) { |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Recursive symlinks search |
||
| 815 | * |
||
| 816 | * @param string $path file/dir path |
||
| 817 | * @return bool |
||
| 818 | * @author Dmitry (dio) Levashov |
||
| 819 | **/ |
||
| 820 | protected function _findSymlinks($path) { |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Extract files from archive |
||
| 852 | * |
||
| 853 | * @param string $path archive path |
||
| 854 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
| 855 | * @return true |
||
| 856 | * @author Dmitry (dio) Levashov, |
||
| 857 | * @author Alexey Sukhotin |
||
| 858 | **/ |
||
| 859 | protected function _extract($path, $arc) { |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Create archive and return its path |
||
| 967 | * |
||
| 968 | * @param string $dir target dir |
||
| 969 | * @param array $files files names list |
||
| 970 | * @param string $name archive name |
||
| 971 | * @param array $arc archiver options |
||
| 972 | * @return string|bool |
||
| 973 | * @author Dmitry (dio) Levashov, |
||
| 974 | * @author Alexey Sukhotin |
||
| 975 | **/ |
||
| 976 | protected function _archive($dir, $files, $name, $arc) { |
||
| 979 | |||
| 980 | /******************** Over write functions *************************/ |
||
| 981 | |||
| 982 | /** |
||
| 983 | * File path of local server side work file path |
||
| 984 | * |
||
| 985 | * @param string $path |
||
| 986 | * @return string |
||
| 987 | * @author Naoki Sawada |
||
| 988 | */ |
||
| 989 | protected function getWorkFile($path) { |
||
| 992 | |||
| 993 | /** |
||
| 994 | * Delete dirctory trees |
||
| 995 | * |
||
| 996 | * @param string $localpath path need convert encoding to server encoding |
||
| 997 | * @return boolean |
||
| 998 | * @author Naoki Sawada |
||
| 999 | */ |
||
| 1000 | protected function delTree($localpath) { |
||
| 1003 | |||
| 1004 | /******************** Over write (Optimized) functions *************************/ |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Recursive files search |
||
| 1008 | * |
||
| 1009 | * @param string $path dir path |
||
| 1010 | * @param string $q search string |
||
| 1011 | * @param array $mimes |
||
| 1012 | * @return array |
||
| 1013 | * @author Dmitry (dio) Levashov |
||
| 1014 | * @author Naoki Sawada |
||
| 1015 | **/ |
||
| 1016 | protected function doSearch($path, $q, $mimes) { |
||
| 1066 | |||
| 1067 | } // END class |
||
|
2 ignored issues
–
show
|
|||
| 1068 |
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.