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 |
||
| 30 | class elFinderVolumeLocalFileSystem extends elFinderVolumeDriver { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Driver id |
||
| 34 | * Must be started from letter and contains [a-z0-9] |
||
| 35 | * Used as part of volume id |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | **/ |
||
| 39 | protected $driverId = 'l'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Required to count total archive files size |
||
| 43 | * |
||
| 44 | * @var int |
||
| 45 | **/ |
||
| 46 | protected $archiveSize = 0; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Current query word on doSearch |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | **/ |
||
| 53 | private $doSearchCurrentQuery = ''; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Constructor |
||
| 57 | * Extend options with required fields |
||
| 58 | * |
||
| 59 | * @return void |
||
| 60 | * @author Dmitry (dio) Levashov |
||
| 61 | **/ |
||
| 62 | public function __construct() { |
||
| 70 | |||
| 71 | /*********************************************************************/ |
||
| 72 | /* INIT AND CONFIGURE */ |
||
| 73 | /*********************************************************************/ |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Prepare driver before mount volume. |
||
| 77 | * Return true if volume is ready. |
||
| 78 | * |
||
| 79 | * @return bool |
||
| 80 | **/ |
||
| 81 | protected function init() { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Configure after successfull mount. |
||
| 120 | * |
||
| 121 | * @return void |
||
| 122 | * @author Dmitry (dio) Levashov |
||
| 123 | **/ |
||
| 124 | protected function configure() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Long pooling sync checker |
||
| 189 | * This function require server command `inotifywait` |
||
| 190 | * If `inotifywait` need full path, Please add `define('ELFINER_INOTIFYWAIT_PATH', '/PATH_TO/inotifywait');` into connector.php |
||
| 191 | * |
||
| 192 | * @param string $path |
||
| 193 | * @param int $standby |
||
| 194 | * @param number $compare |
||
| 195 | * @return number|bool |
||
| 196 | */ |
||
| 197 | public function localFileSystemInotify($path, $standby, $compare) { |
||
| 231 | |||
| 232 | /*********************************************************************/ |
||
| 233 | /* FS API */ |
||
| 234 | /*********************************************************************/ |
||
| 235 | |||
| 236 | /*********************** paths/urls *************************/ |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Return parent directory path |
||
| 240 | * |
||
| 241 | * @param string $path file path |
||
| 242 | * @return string |
||
| 243 | * @author Dmitry (dio) Levashov |
||
| 244 | **/ |
||
| 245 | protected function _dirname($path) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Return file name |
||
| 251 | * |
||
| 252 | * @param string $path file path |
||
| 253 | * @return string |
||
| 254 | * @author Dmitry (dio) Levashov |
||
| 255 | **/ |
||
| 256 | protected function _basename($path) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Join dir name and file name and retur full path |
||
| 262 | * |
||
| 263 | * @param string $dir |
||
| 264 | * @param string $name |
||
| 265 | * @return string |
||
| 266 | * @author Dmitry (dio) Levashov |
||
| 267 | **/ |
||
| 268 | protected function _joinPath($dir, $name) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Return normalized path, this works the same as os.path.normpath() in Python |
||
| 274 | * |
||
| 275 | * @param string $path path |
||
| 276 | * @return string |
||
| 277 | * @author Troex Nevelin |
||
| 278 | **/ |
||
| 279 | protected function _normpath($path) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Return file path related to root dir |
||
| 333 | * |
||
| 334 | * @param string $path file path |
||
| 335 | * @return string |
||
| 336 | * @author Dmitry (dio) Levashov |
||
| 337 | **/ |
||
| 338 | View Code Duplication | protected function _relpath($path) { |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Convert path related to root dir into real path |
||
| 353 | * |
||
| 354 | * @param string $path file path |
||
| 355 | * @return string |
||
| 356 | * @author Dmitry (dio) Levashov |
||
| 357 | **/ |
||
| 358 | protected function _abspath($path) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Return fake path started from root dir |
||
| 373 | * |
||
| 374 | * @param string $path file path |
||
| 375 | * @return string |
||
| 376 | * @author Dmitry (dio) Levashov |
||
| 377 | **/ |
||
| 378 | protected function _path($path) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Return true if $path is children of $parent |
||
| 384 | * |
||
| 385 | * @param string $path path to check |
||
| 386 | * @param string $parent parent path |
||
| 387 | * @return bool |
||
| 388 | * @author Dmitry (dio) Levashov |
||
| 389 | **/ |
||
| 390 | protected function _inpath($path, $parent) { |
||
| 399 | |||
| 400 | |||
| 401 | |||
| 402 | /***************** file stat ********************/ |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Return stat for given path. |
||
| 406 | * Stat contains following fields: |
||
| 407 | * - (int) size file size in b. required |
||
| 408 | * - (int) ts file modification time in unix time. required |
||
| 409 | * - (string) mime mimetype. required for folders, others - optionally |
||
| 410 | * - (bool) read read permissions. required |
||
| 411 | * - (bool) write write permissions. required |
||
| 412 | * - (bool) locked is object locked. optionally |
||
| 413 | * - (bool) hidden is object hidden. optionally |
||
| 414 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
| 415 | * - (string) target for symlinks - link target path. optionally |
||
| 416 | * |
||
| 417 | * If file does not exists - returns empty array or false. |
||
| 418 | * |
||
| 419 | * @param string $path file path |
||
| 420 | * @return array|false |
||
| 421 | * @author Dmitry (dio) Levashov |
||
| 422 | **/ |
||
| 423 | protected function _stat($path) { |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Get stat `owner`, `group` and `isowner` by `uid` and `gid` |
||
| 489 | * Sub-fuction of _stat() and _scandir() |
||
| 490 | * |
||
| 491 | * @param integer $uid |
||
| 492 | * @param integer $gid |
||
| 493 | * @return array stat |
||
| 494 | */ |
||
| 495 | protected function getOwnerStat($uid, $gid) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Return true if path is dir and has at least one childs directory |
||
| 539 | * |
||
| 540 | * @param string $path dir path |
||
| 541 | * @return bool |
||
| 542 | * @author Dmitry (dio) Levashov |
||
| 543 | **/ |
||
| 544 | protected function _subdirs($path) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Return object width and height |
||
| 562 | * Usualy used for images, but can be realize for video etc... |
||
| 563 | * |
||
| 564 | * @param string $path file path |
||
| 565 | * @param string $mime file mime type |
||
| 566 | * @return string |
||
| 567 | * @author Dmitry (dio) Levashov |
||
| 568 | **/ |
||
| 569 | protected function _dimensions($path, $mime) { |
||
| 575 | /******************** file/dir content *********************/ |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Return symlink target file |
||
| 579 | * |
||
| 580 | * @param string $path link path |
||
| 581 | * @return string |
||
| 582 | * @author Dmitry (dio) Levashov |
||
| 583 | **/ |
||
| 584 | protected function readlink($path) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Return files list in directory. |
||
| 602 | * |
||
| 603 | * @param string $path dir path |
||
| 604 | * @return array |
||
| 605 | * @author Dmitry (dio) Levashov |
||
| 606 | **/ |
||
| 607 | protected function _scandir($path) { |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Open file and return file pointer |
||
| 693 | * |
||
| 694 | * @param string $path file path |
||
| 695 | * @param bool $write open file for writing |
||
| 696 | * @return resource|false |
||
| 697 | * @author Dmitry (dio) Levashov |
||
| 698 | **/ |
||
| 699 | protected function _fopen($path, $mode='rb') { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Close opened file |
||
| 705 | * |
||
| 706 | * @param resource $fp file pointer |
||
| 707 | * @return bool |
||
| 708 | * @author Dmitry (dio) Levashov |
||
| 709 | **/ |
||
| 710 | protected function _fclose($fp, $path='') { |
||
| 713 | |||
| 714 | /******************** file/dir manipulations *************************/ |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Create dir and return created dir path or false on failed |
||
| 718 | * |
||
| 719 | * @param string $path parent dir path |
||
| 720 | * @param string $name new directory name |
||
| 721 | * @return string|bool |
||
| 722 | * @author Dmitry (dio) Levashov |
||
| 723 | **/ |
||
| 724 | View Code Duplication | protected function _mkdir($path, $name) { |
|
| 735 | |||
| 736 | /** |
||
| 737 | * Create file and return it's path or false on failed |
||
| 738 | * |
||
| 739 | * @param string $path parent dir path |
||
| 740 | * @param string $name new file name |
||
| 741 | * @return string|bool |
||
| 742 | * @author Dmitry (dio) Levashov |
||
| 743 | **/ |
||
| 744 | View Code Duplication | protected function _mkfile($path, $name) { |
|
| 755 | |||
| 756 | /** |
||
| 757 | * Create symlink |
||
| 758 | * |
||
| 759 | * @param string $source file to link to |
||
| 760 | * @param string $targetDir folder to create link in |
||
| 761 | * @param string $name symlink name |
||
| 762 | * @return bool |
||
| 763 | * @author Dmitry (dio) Levashov |
||
| 764 | **/ |
||
| 765 | protected function _symlink($source, $targetDir, $name) { |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Copy file into another file |
||
| 771 | * |
||
| 772 | * @param string $source source file path |
||
| 773 | * @param string $targetDir target directory path |
||
| 774 | * @param string $name new file name |
||
| 775 | * @return bool |
||
| 776 | * @author Dmitry (dio) Levashov |
||
| 777 | **/ |
||
| 778 | protected function _copy($source, $targetDir, $name) { |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Move file into another parent dir. |
||
| 786 | * Return new file path or false. |
||
| 787 | * |
||
| 788 | * @param string $source source file path |
||
| 789 | * @param string $target target dir path |
||
| 790 | * @param string $name file name |
||
| 791 | * @return string|bool |
||
| 792 | * @author Dmitry (dio) Levashov |
||
| 793 | **/ |
||
| 794 | protected function _move($source, $targetDir, $name) { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Remove file |
||
| 803 | * |
||
| 804 | * @param string $path file path |
||
| 805 | * @return bool |
||
| 806 | * @author Dmitry (dio) Levashov |
||
| 807 | **/ |
||
| 808 | protected function _unlink($path) { |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Remove dir |
||
| 816 | * |
||
| 817 | * @param string $path dir path |
||
| 818 | * @return bool |
||
| 819 | * @author Dmitry (dio) Levashov |
||
| 820 | **/ |
||
| 821 | protected function _rmdir($path) { |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Create new file and write into it from file pointer. |
||
| 829 | * Return new file path or false on error. |
||
| 830 | * |
||
| 831 | * @param resource $fp file pointer |
||
| 832 | * @param string $dir target dir path |
||
| 833 | * @param string $name file name |
||
| 834 | * @param array $stat file stat (required by some virtual fs) |
||
| 835 | * @return bool|string |
||
| 836 | * @author Dmitry (dio) Levashov |
||
| 837 | **/ |
||
| 838 | protected function _save($fp, $dir, $name, $stat) { |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Get file contents |
||
| 865 | * |
||
| 866 | * @param string $path file path |
||
| 867 | * @return string|false |
||
| 868 | * @author Dmitry (dio) Levashov |
||
| 869 | **/ |
||
| 870 | protected function _getContents($path) { |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Write a string to a file |
||
| 876 | * |
||
| 877 | * @param string $path file path |
||
| 878 | * @param string $content new file content |
||
| 879 | * @return bool |
||
| 880 | * @author Dmitry (dio) Levashov |
||
| 881 | **/ |
||
| 882 | protected function _filePutContents($path, $content) { |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Detect available archivers |
||
| 892 | * |
||
| 893 | * @return void |
||
| 894 | **/ |
||
| 895 | protected function _checkArchivers() { |
||
| 899 | |||
| 900 | /** |
||
| 901 | * chmod availability |
||
| 902 | * |
||
| 903 | * @return bool |
||
| 904 | **/ |
||
| 905 | protected function _chmod($path, $mode) { |
||
| 911 | |||
| 912 | /** |
||
| 913 | * Recursive symlinks search |
||
| 914 | * |
||
| 915 | * @param string $path file/dir path |
||
| 916 | * @return bool |
||
| 917 | * @author Dmitry (dio) Levashov |
||
| 918 | **/ |
||
| 919 | protected function _findSymlinks($path) { |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Extract files from archive |
||
| 951 | * |
||
| 952 | * @param string $path archive path |
||
| 953 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
| 954 | * @return true |
||
| 955 | * @author Dmitry (dio) Levashov, |
||
| 956 | * @author Alexey Sukhotin |
||
| 957 | **/ |
||
| 958 | protected function _extract($path, $arc) { |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * Create archive and return its path |
||
| 1066 | * |
||
| 1067 | * @param string $dir target dir |
||
| 1068 | * @param array $files files names list |
||
| 1069 | * @param string $name archive name |
||
| 1070 | * @param array $arc archiver options |
||
| 1071 | * @return string|bool |
||
| 1072 | * @author Dmitry (dio) Levashov, |
||
| 1073 | * @author Alexey Sukhotin |
||
| 1074 | **/ |
||
| 1075 | protected function _archive($dir, $files, $name, $arc) { |
||
| 1078 | |||
| 1079 | /******************** Over write functions *************************/ |
||
| 1080 | |||
| 1081 | /** |
||
| 1082 | * File path of local server side work file path |
||
| 1083 | * |
||
| 1084 | * @param string $path |
||
| 1085 | * @return string |
||
| 1086 | * @author Naoki Sawada |
||
| 1087 | */ |
||
| 1088 | protected function getWorkFile($path) { |
||
| 1091 | |||
| 1092 | /** |
||
| 1093 | * Delete dirctory trees |
||
| 1094 | * |
||
| 1095 | * @param string $localpath path need convert encoding to server encoding |
||
| 1096 | * @return boolean |
||
| 1097 | * @author Naoki Sawada |
||
| 1098 | */ |
||
| 1099 | protected function delTree($localpath) { |
||
| 1102 | |||
| 1103 | /******************** Over write (Optimized) functions *************************/ |
||
| 1104 | |||
| 1105 | /** |
||
| 1106 | * Recursive files search |
||
| 1107 | * |
||
| 1108 | * @param string $path dir path |
||
| 1109 | * @param string $q search string |
||
| 1110 | * @param array $mimes |
||
| 1111 | * @return array |
||
| 1112 | * @author Dmitry (dio) Levashov |
||
| 1113 | * @author Naoki Sawada |
||
| 1114 | **/ |
||
| 1115 | protected function doSearch($path, $q, $mimes) { |
||
| 1178 | |||
| 1179 | /******************** Original local functions *************************/ |
||
| 1180 | |||
| 1181 | public function localFileSystemSearchIteratorFilter($file, $key, $iterator) { |
||
| 1187 | |||
| 1188 | } // END class |
||
| 1189 | |||
| 1190 |
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.