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() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Configure after successfull mount. |
||
| 92 | * |
||
| 93 | * @return void |
||
| 94 | * @author Dmitry (dio) Levashov |
||
| 95 | **/ |
||
| 96 | protected function configure() { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Long pooling sync checker |
||
| 161 | * This function require server command `inotifywait` |
||
| 162 | * If `inotifywait` need full path, Please add `define('ELFINER_INOTIFYWAIT_PATH', '/PATH_TO/inotifywait');` into connector.php |
||
| 163 | * |
||
| 164 | * @param string $path |
||
| 165 | * @param int $standby |
||
| 166 | * @param number $compare |
||
| 167 | * @return number|bool |
||
| 168 | */ |
||
| 169 | public function localFileSystemInotify($path, $standby, $compare) { |
||
| 208 | |||
| 209 | /*********************************************************************/ |
||
| 210 | /* FS API */ |
||
| 211 | /*********************************************************************/ |
||
| 212 | |||
| 213 | /*********************** paths/urls *************************/ |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Return parent directory path |
||
| 217 | * |
||
| 218 | * @param string $path file path |
||
| 219 | * @return string |
||
| 220 | * @author Dmitry (dio) Levashov |
||
| 221 | **/ |
||
| 222 | protected function _dirname($path) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Return file name |
||
| 228 | * |
||
| 229 | * @param string $path file path |
||
| 230 | * @return string |
||
| 231 | * @author Dmitry (dio) Levashov |
||
| 232 | **/ |
||
| 233 | protected function _basename($path) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Join dir name and file name and retur full path |
||
| 239 | * |
||
| 240 | * @param string $dir |
||
| 241 | * @param string $name |
||
| 242 | * @return string |
||
| 243 | * @author Dmitry (dio) Levashov |
||
| 244 | **/ |
||
| 245 | protected function _joinPath($dir, $name) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Return normalized path, this works the same as os.path.normpath() in Python |
||
| 251 | * |
||
| 252 | * @param string $path path |
||
| 253 | * @return string |
||
| 254 | * @author Troex Nevelin |
||
| 255 | **/ |
||
| 256 | protected function _normpath($path) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Return file path related to root dir |
||
| 310 | * |
||
| 311 | * @param string $path file path |
||
| 312 | * @return string |
||
| 313 | * @author Dmitry (dio) Levashov |
||
| 314 | **/ |
||
| 315 | View Code Duplication | protected function _relpath($path) { |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Convert path related to root dir into real path |
||
| 330 | * |
||
| 331 | * @param string $path file path |
||
| 332 | * @return string |
||
| 333 | * @author Dmitry (dio) Levashov |
||
| 334 | **/ |
||
| 335 | protected function _abspath($path) { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Return fake path started from root dir |
||
| 350 | * |
||
| 351 | * @param string $path file path |
||
| 352 | * @return string |
||
| 353 | * @author Dmitry (dio) Levashov |
||
| 354 | **/ |
||
| 355 | protected function _path($path) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Return true if $path is children of $parent |
||
| 361 | * |
||
| 362 | * @param string $path path to check |
||
| 363 | * @param string $parent parent path |
||
| 364 | * @return bool |
||
| 365 | * @author Dmitry (dio) Levashov |
||
| 366 | **/ |
||
| 367 | protected function _inpath($path, $parent) { |
||
| 376 | |||
| 377 | |||
| 378 | |||
| 379 | /***************** file stat ********************/ |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Return stat for given path. |
||
| 383 | * Stat contains following fields: |
||
| 384 | * - (int) size file size in b. required |
||
| 385 | * - (int) ts file modification time in unix time. required |
||
| 386 | * - (string) mime mimetype. required for folders, others - optionally |
||
| 387 | * - (bool) read read permissions. required |
||
| 388 | * - (bool) write write permissions. required |
||
| 389 | * - (bool) locked is object locked. optionally |
||
| 390 | * - (bool) hidden is object hidden. optionally |
||
| 391 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
| 392 | * - (string) target for symlinks - link target path. optionally |
||
| 393 | * |
||
| 394 | * If file does not exists - returns empty array or false. |
||
| 395 | * |
||
| 396 | * @param string $path file path |
||
| 397 | * @return array|false |
||
| 398 | * @author Dmitry (dio) Levashov |
||
| 399 | **/ |
||
| 400 | protected function _stat($path) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Get stat `owner`, `group` and `isowner` by `uid` and `gid` |
||
| 466 | * Sub-fuction of _stat() and _scandir() |
||
| 467 | * |
||
| 468 | * @param integer $uid |
||
| 469 | * @param integer $gid |
||
| 470 | * @return array stat |
||
| 471 | */ |
||
| 472 | protected function getOwnerStat($uid, $gid) { |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Return true if path is dir and has at least one childs directory |
||
| 516 | * |
||
| 517 | * @param string $path dir path |
||
| 518 | * @return bool |
||
| 519 | * @author Dmitry (dio) Levashov |
||
| 520 | **/ |
||
| 521 | protected function _subdirs($path) { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Return object width and height |
||
| 532 | * Usualy used for images, but can be realize for video etc... |
||
| 533 | * |
||
| 534 | * @param string $path file path |
||
| 535 | * @param string $mime file mime type |
||
| 536 | * @return string |
||
| 537 | * @author Dmitry (dio) Levashov |
||
| 538 | **/ |
||
| 539 | protected function _dimensions($path, $mime) { |
||
| 545 | /******************** file/dir content *********************/ |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Return symlink target file |
||
| 549 | * |
||
| 550 | * @param string $path link path |
||
| 551 | * @return string |
||
| 552 | * @author Dmitry (dio) Levashov |
||
| 553 | **/ |
||
| 554 | protected function readlink($path) { |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Return files list in directory. |
||
| 572 | * |
||
| 573 | * @param string $path dir path |
||
| 574 | * @return array |
||
| 575 | * @author Dmitry (dio) Levashov |
||
| 576 | **/ |
||
| 577 | protected function _scandir($path) { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Open file and return file pointer |
||
| 663 | * |
||
| 664 | * @param string $path file path |
||
| 665 | * @param bool $write open file for writing |
||
| 666 | * @return resource|false |
||
| 667 | * @author Dmitry (dio) Levashov |
||
| 668 | **/ |
||
| 669 | protected function _fopen($path, $mode='rb') { |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Close opened file |
||
| 675 | * |
||
| 676 | * @param resource $fp file pointer |
||
| 677 | * @return bool |
||
| 678 | * @author Dmitry (dio) Levashov |
||
| 679 | **/ |
||
| 680 | protected function _fclose($fp, $path='') { |
||
| 683 | |||
| 684 | /******************** file/dir manipulations *************************/ |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Create dir and return created dir path or false on failed |
||
| 688 | * |
||
| 689 | * @param string $path parent dir path |
||
| 690 | * @param string $name new directory name |
||
| 691 | * @return string|bool |
||
| 692 | * @author Dmitry (dio) Levashov |
||
| 693 | **/ |
||
| 694 | View Code Duplication | protected function _mkdir($path, $name) { |
|
| 705 | |||
| 706 | /** |
||
| 707 | * Create file and return it's path or false on failed |
||
| 708 | * |
||
| 709 | * @param string $path parent dir path |
||
| 710 | * @param string $name new file name |
||
| 711 | * @return string|bool |
||
| 712 | * @author Dmitry (dio) Levashov |
||
| 713 | **/ |
||
| 714 | View Code Duplication | protected function _mkfile($path, $name) { |
|
| 725 | |||
| 726 | /** |
||
| 727 | * Create symlink |
||
| 728 | * |
||
| 729 | * @param string $source file to link to |
||
| 730 | * @param string $targetDir folder to create link in |
||
| 731 | * @param string $name symlink name |
||
| 732 | * @return bool |
||
| 733 | * @author Dmitry (dio) Levashov |
||
| 734 | **/ |
||
| 735 | protected function _symlink($source, $targetDir, $name) { |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Copy file into another file |
||
| 741 | * |
||
| 742 | * @param string $source source file path |
||
| 743 | * @param string $targetDir target directory path |
||
| 744 | * @param string $name new file name |
||
| 745 | * @return bool |
||
| 746 | * @author Dmitry (dio) Levashov |
||
| 747 | **/ |
||
| 748 | protected function _copy($source, $targetDir, $name) { |
||
| 753 | |||
| 754 | /** |
||
| 755 | * Move file into another parent dir. |
||
| 756 | * Return new file path or false. |
||
| 757 | * |
||
| 758 | * @param string $source source file path |
||
| 759 | * @param string $target target dir path |
||
| 760 | * @param string $name file name |
||
| 761 | * @return string|bool |
||
| 762 | * @author Dmitry (dio) Levashov |
||
| 763 | **/ |
||
| 764 | protected function _move($source, $targetDir, $name) { |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Remove file |
||
| 773 | * |
||
| 774 | * @param string $path file path |
||
| 775 | * @return bool |
||
| 776 | * @author Dmitry (dio) Levashov |
||
| 777 | **/ |
||
| 778 | protected function _unlink($path) { |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Remove dir |
||
| 786 | * |
||
| 787 | * @param string $path dir path |
||
| 788 | * @return bool |
||
| 789 | * @author Dmitry (dio) Levashov |
||
| 790 | **/ |
||
| 791 | protected function _rmdir($path) { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Create new file and write into it from file pointer. |
||
| 799 | * Return new file path or false on error. |
||
| 800 | * |
||
| 801 | * @param resource $fp file pointer |
||
| 802 | * @param string $dir target dir path |
||
| 803 | * @param string $name file name |
||
| 804 | * @param array $stat file stat (required by some virtual fs) |
||
| 805 | * @return bool|string |
||
| 806 | * @author Dmitry (dio) Levashov |
||
| 807 | **/ |
||
| 808 | protected function _save($fp, $dir, $name, $stat) { |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Get file contents |
||
| 835 | * |
||
| 836 | * @param string $path file path |
||
| 837 | * @return string|false |
||
| 838 | * @author Dmitry (dio) Levashov |
||
| 839 | **/ |
||
| 840 | protected function _getContents($path) { |
||
| 843 | |||
| 844 | /** |
||
| 845 | * Write a string to a file |
||
| 846 | * |
||
| 847 | * @param string $path file path |
||
| 848 | * @param string $content new file content |
||
| 849 | * @return bool |
||
| 850 | * @author Dmitry (dio) Levashov |
||
| 851 | **/ |
||
| 852 | protected function _filePutContents($path, $content) { |
||
| 859 | |||
| 860 | /** |
||
| 861 | * Detect available archivers |
||
| 862 | * |
||
| 863 | * @return void |
||
| 864 | **/ |
||
| 865 | protected function _checkArchivers() { |
||
| 869 | |||
| 870 | /** |
||
| 871 | * chmod availability |
||
| 872 | * |
||
| 873 | * @return bool |
||
| 874 | **/ |
||
| 875 | protected function _chmod($path, $mode) { |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Recursive symlinks search |
||
| 884 | * |
||
| 885 | * @param string $path file/dir path |
||
| 886 | * @return bool |
||
| 887 | * @author Dmitry (dio) Levashov |
||
| 888 | **/ |
||
| 889 | protected function _findSymlinks($path) { |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Extract files from archive |
||
| 921 | * |
||
| 922 | * @param string $path archive path |
||
| 923 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
| 924 | * @return true |
||
| 925 | * @author Dmitry (dio) Levashov, |
||
| 926 | * @author Alexey Sukhotin |
||
| 927 | **/ |
||
| 928 | protected function _extract($path, $arc) { |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Create archive and return its path |
||
| 1036 | * |
||
| 1037 | * @param string $dir target dir |
||
| 1038 | * @param array $files files names list |
||
| 1039 | * @param string $name archive name |
||
| 1040 | * @param array $arc archiver options |
||
| 1041 | * @return string|bool |
||
| 1042 | * @author Dmitry (dio) Levashov, |
||
| 1043 | * @author Alexey Sukhotin |
||
| 1044 | **/ |
||
| 1045 | protected function _archive($dir, $files, $name, $arc) { |
||
| 1048 | |||
| 1049 | /******************** Over write functions *************************/ |
||
| 1050 | |||
| 1051 | /** |
||
| 1052 | * File path of local server side work file path |
||
| 1053 | * |
||
| 1054 | * @param string $path |
||
| 1055 | * @return string |
||
| 1056 | * @author Naoki Sawada |
||
| 1057 | */ |
||
| 1058 | protected function getWorkFile($path) { |
||
| 1061 | |||
| 1062 | /** |
||
| 1063 | * Delete dirctory trees |
||
| 1064 | * |
||
| 1065 | * @param string $localpath path need convert encoding to server encoding |
||
| 1066 | * @return boolean |
||
| 1067 | * @author Naoki Sawada |
||
| 1068 | */ |
||
| 1069 | protected function delTree($localpath) { |
||
| 1072 | |||
| 1073 | /******************** Over write (Optimized) functions *************************/ |
||
| 1074 | |||
| 1075 | /** |
||
| 1076 | * Recursive files search |
||
| 1077 | * |
||
| 1078 | * @param string $path dir path |
||
| 1079 | * @param string $q search string |
||
| 1080 | * @param array $mimes |
||
| 1081 | * @return array |
||
| 1082 | * @author Dmitry (dio) Levashov |
||
| 1083 | * @author Naoki Sawada |
||
| 1084 | **/ |
||
| 1085 | protected function doSearch($path, $q, $mimes) { |
||
| 1144 | |||
| 1145 | } // END class |
||
| 1146 |
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.