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() { |
||
| 143 | |||
| 144 | /*********************************************************************/ |
||
| 145 | /* FS API */ |
||
| 146 | /*********************************************************************/ |
||
| 147 | |||
| 148 | /*********************** paths/urls *************************/ |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Return parent directory path |
||
| 152 | * |
||
| 153 | * @param string $path file path |
||
| 154 | * @return string |
||
| 155 | * @author Dmitry (dio) Levashov |
||
| 156 | **/ |
||
| 157 | protected function _dirname($path) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Return file name |
||
| 163 | * |
||
| 164 | * @param string $path file path |
||
| 165 | * @return string |
||
| 166 | * @author Dmitry (dio) Levashov |
||
| 167 | **/ |
||
| 168 | protected function _basename($path) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Join dir name and file name and retur full path |
||
| 174 | * |
||
| 175 | * @param string $dir |
||
| 176 | * @param string $name |
||
| 177 | * @return string |
||
| 178 | * @author Dmitry (dio) Levashov |
||
| 179 | **/ |
||
| 180 | protected function _joinPath($dir, $name) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Return normalized path, this works the same as os.path.normpath() in Python |
||
| 186 | * |
||
| 187 | * @param string $path path |
||
| 188 | * @return string |
||
| 189 | * @author Troex Nevelin |
||
| 190 | **/ |
||
| 191 | protected function _normpath($path) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Return file path related to root dir |
||
| 245 | * |
||
| 246 | * @param string $path file path |
||
| 247 | * @return string |
||
| 248 | * @author Dmitry (dio) Levashov |
||
| 249 | **/ |
||
| 250 | View Code Duplication | protected function _relpath($path) { |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Convert path related to root dir into real path |
||
| 265 | * |
||
| 266 | * @param string $path file path |
||
| 267 | * @return string |
||
| 268 | * @author Dmitry (dio) Levashov |
||
| 269 | **/ |
||
| 270 | protected function _abspath($path) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Return fake path started from root dir |
||
| 285 | * |
||
| 286 | * @param string $path file path |
||
| 287 | * @return string |
||
| 288 | * @author Dmitry (dio) Levashov |
||
| 289 | **/ |
||
| 290 | protected function _path($path) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Return true if $path is children of $parent |
||
| 296 | * |
||
| 297 | * @param string $path path to check |
||
| 298 | * @param string $parent parent path |
||
| 299 | * @return bool |
||
| 300 | * @author Dmitry (dio) Levashov |
||
| 301 | **/ |
||
| 302 | protected function _inpath($path, $parent) { |
||
| 311 | |||
| 312 | |||
| 313 | |||
| 314 | /***************** file stat ********************/ |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Return stat for given path. |
||
| 318 | * Stat contains following fields: |
||
| 319 | * - (int) size file size in b. required |
||
| 320 | * - (int) ts file modification time in unix time. required |
||
| 321 | * - (string) mime mimetype. required for folders, others - optionally |
||
| 322 | * - (bool) read read permissions. required |
||
| 323 | * - (bool) write write permissions. required |
||
| 324 | * - (bool) locked is object locked. optionally |
||
| 325 | * - (bool) hidden is object hidden. optionally |
||
| 326 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
| 327 | * - (string) target for symlinks - link target path. optionally |
||
| 328 | * |
||
| 329 | * If file does not exists - returns empty array or false. |
||
| 330 | * |
||
| 331 | * @param string $path file path |
||
| 332 | * @return array|false |
||
| 333 | * @author Dmitry (dio) Levashov |
||
| 334 | **/ |
||
| 335 | protected function _stat($path) { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Get stat `owner`, `group` and `isowner` by `uid` and `gid` |
||
| 401 | * Sub-fuction of _stat() and _scandir() |
||
| 402 | * |
||
| 403 | * @param integer $uid |
||
| 404 | * @param integer $gid |
||
| 405 | * @return array stat |
||
| 406 | */ |
||
| 407 | protected function getOwnerStat($uid, $gid) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Return true if path is dir and has at least one childs directory |
||
| 451 | * |
||
| 452 | * @param string $path dir path |
||
| 453 | * @return bool |
||
| 454 | * @author Dmitry (dio) Levashov |
||
| 455 | **/ |
||
| 456 | protected function _subdirs($path) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Return object width and height |
||
| 467 | * Usualy used for images, but can be realize for video etc... |
||
| 468 | * |
||
| 469 | * @param string $path file path |
||
| 470 | * @param string $mime file mime type |
||
| 471 | * @return string |
||
| 472 | * @author Dmitry (dio) Levashov |
||
| 473 | **/ |
||
| 474 | protected function _dimensions($path, $mime) { |
||
| 480 | /******************** file/dir content *********************/ |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Return symlink target file |
||
| 484 | * |
||
| 485 | * @param string $path link path |
||
| 486 | * @return string |
||
| 487 | * @author Dmitry (dio) Levashov |
||
| 488 | **/ |
||
| 489 | protected function readlink($path) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Return files list in directory. |
||
| 507 | * |
||
| 508 | * @param string $path dir path |
||
| 509 | * @return array |
||
| 510 | * @author Dmitry (dio) Levashov |
||
| 511 | **/ |
||
| 512 | protected function _scandir($path) { |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Open file and return file pointer |
||
| 598 | * |
||
| 599 | * @param string $path file path |
||
| 600 | * @param bool $write open file for writing |
||
| 601 | * @return resource|false |
||
| 602 | * @author Dmitry (dio) Levashov |
||
| 603 | **/ |
||
| 604 | protected function _fopen($path, $mode='rb') { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Close opened file |
||
| 610 | * |
||
| 611 | * @param resource $fp file pointer |
||
| 612 | * @return bool |
||
| 613 | * @author Dmitry (dio) Levashov |
||
| 614 | **/ |
||
| 615 | protected function _fclose($fp, $path='') { |
||
| 618 | |||
| 619 | /******************** file/dir manipulations *************************/ |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Create dir and return created dir path or false on failed |
||
| 623 | * |
||
| 624 | * @param string $path parent dir path |
||
| 625 | * @param string $name new directory name |
||
| 626 | * @return string|bool |
||
| 627 | * @author Dmitry (dio) Levashov |
||
| 628 | **/ |
||
| 629 | View Code Duplication | protected function _mkdir($path, $name) { |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Create file and return it's path or false on failed |
||
| 643 | * |
||
| 644 | * @param string $path parent dir path |
||
| 645 | * @param string $name new file name |
||
| 646 | * @return string|bool |
||
| 647 | * @author Dmitry (dio) Levashov |
||
| 648 | **/ |
||
| 649 | View Code Duplication | protected function _mkfile($path, $name) { |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Create symlink |
||
| 663 | * |
||
| 664 | * @param string $source file to link to |
||
| 665 | * @param string $targetDir folder to create link in |
||
| 666 | * @param string $name symlink name |
||
| 667 | * @return bool |
||
| 668 | * @author Dmitry (dio) Levashov |
||
| 669 | **/ |
||
| 670 | protected function _symlink($source, $targetDir, $name) { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Copy file into another file |
||
| 676 | * |
||
| 677 | * @param string $source source file path |
||
| 678 | * @param string $targetDir target directory path |
||
| 679 | * @param string $name new file name |
||
| 680 | * @return bool |
||
| 681 | * @author Dmitry (dio) Levashov |
||
| 682 | **/ |
||
| 683 | protected function _copy($source, $targetDir, $name) { |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Move file into another parent dir. |
||
| 691 | * Return new file path or false. |
||
| 692 | * |
||
| 693 | * @param string $source source file path |
||
| 694 | * @param string $target target dir path |
||
| 695 | * @param string $name file name |
||
| 696 | * @return string|bool |
||
| 697 | * @author Dmitry (dio) Levashov |
||
| 698 | **/ |
||
| 699 | protected function _move($source, $targetDir, $name) { |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Remove file |
||
| 708 | * |
||
| 709 | * @param string $path file path |
||
| 710 | * @return bool |
||
| 711 | * @author Dmitry (dio) Levashov |
||
| 712 | **/ |
||
| 713 | protected function _unlink($path) { |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Remove dir |
||
| 721 | * |
||
| 722 | * @param string $path dir path |
||
| 723 | * @return bool |
||
| 724 | * @author Dmitry (dio) Levashov |
||
| 725 | **/ |
||
| 726 | protected function _rmdir($path) { |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Create new file and write into it from file pointer. |
||
| 734 | * Return new file path or false on error. |
||
| 735 | * |
||
| 736 | * @param resource $fp file pointer |
||
| 737 | * @param string $dir target dir path |
||
| 738 | * @param string $name file name |
||
| 739 | * @param array $stat file stat (required by some virtual fs) |
||
| 740 | * @return bool|string |
||
| 741 | * @author Dmitry (dio) Levashov |
||
| 742 | **/ |
||
| 743 | protected function _save($fp, $dir, $name, $stat) { |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Get file contents |
||
| 770 | * |
||
| 771 | * @param string $path file path |
||
| 772 | * @return string|false |
||
| 773 | * @author Dmitry (dio) Levashov |
||
| 774 | **/ |
||
| 775 | protected function _getContents($path) { |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Write a string to a file |
||
| 781 | * |
||
| 782 | * @param string $path file path |
||
| 783 | * @param string $content new file content |
||
| 784 | * @return bool |
||
| 785 | * @author Dmitry (dio) Levashov |
||
| 786 | **/ |
||
| 787 | protected function _filePutContents($path, $content) { |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Detect available archivers |
||
| 797 | * |
||
| 798 | * @return void |
||
| 799 | **/ |
||
| 800 | protected function _checkArchivers() { |
||
| 804 | |||
| 805 | /** |
||
| 806 | * chmod availability |
||
| 807 | * |
||
| 808 | * @return bool |
||
| 809 | **/ |
||
| 810 | protected function _chmod($path, $mode) { |
||
| 816 | |||
| 817 | /** |
||
| 818 | * Recursive symlinks search |
||
| 819 | * |
||
| 820 | * @param string $path file/dir path |
||
| 821 | * @return bool |
||
| 822 | * @author Dmitry (dio) Levashov |
||
| 823 | **/ |
||
| 824 | protected function _findSymlinks($path) { |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Extract files from archive |
||
| 856 | * |
||
| 857 | * @param string $path archive path |
||
| 858 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
| 859 | * @return true |
||
| 860 | * @author Dmitry (dio) Levashov, |
||
| 861 | * @author Alexey Sukhotin |
||
| 862 | **/ |
||
| 863 | protected function _extract($path, $arc) { |
||
| 968 | |||
| 969 | /** |
||
| 970 | * Create archive and return its path |
||
| 971 | * |
||
| 972 | * @param string $dir target dir |
||
| 973 | * @param array $files files names list |
||
| 974 | * @param string $name archive name |
||
| 975 | * @param array $arc archiver options |
||
| 976 | * @return string|bool |
||
| 977 | * @author Dmitry (dio) Levashov, |
||
| 978 | * @author Alexey Sukhotin |
||
| 979 | **/ |
||
| 980 | protected function _archive($dir, $files, $name, $arc) { |
||
| 983 | |||
| 984 | /******************** Over write functions *************************/ |
||
| 985 | |||
| 986 | /** |
||
| 987 | * File path of local server side work file path |
||
| 988 | * |
||
| 989 | * @param string $path |
||
| 990 | * @return string |
||
| 991 | * @author Naoki Sawada |
||
| 992 | */ |
||
| 993 | protected function getWorkFile($path) { |
||
| 996 | |||
| 997 | /** |
||
| 998 | * Delete dirctory trees |
||
| 999 | * |
||
| 1000 | * @param string $localpath path need convert encoding to server encoding |
||
| 1001 | * @return boolean |
||
| 1002 | * @author Naoki Sawada |
||
| 1003 | */ |
||
| 1004 | protected function delTree($localpath) { |
||
| 1007 | |||
| 1008 | /******************** Over write (Optimized) functions *************************/ |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Recursive files search |
||
| 1012 | * |
||
| 1013 | * @param string $path dir path |
||
| 1014 | * @param string $q search string |
||
| 1015 | * @param array $mimes |
||
| 1016 | * @return array |
||
| 1017 | * @author Dmitry (dio) Levashov |
||
| 1018 | * @author Naoki Sawada |
||
| 1019 | **/ |
||
| 1020 | protected function doSearch($path, $q, $mimes) { |
||
| 1079 | |||
| 1080 | } // END class |
||
| 1081 |
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.