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) { |
||
229 | |||
230 | /*********************************************************************/ |
||
231 | /* FS API */ |
||
232 | /*********************************************************************/ |
||
233 | |||
234 | /*********************** paths/urls *************************/ |
||
235 | |||
236 | /** |
||
237 | * Return parent directory path |
||
238 | * |
||
239 | * @param string $path file path |
||
240 | * @return string |
||
241 | * @author Dmitry (dio) Levashov |
||
242 | **/ |
||
243 | protected function _dirname($path) { |
||
246 | |||
247 | /** |
||
248 | * Return file name |
||
249 | * |
||
250 | * @param string $path file path |
||
251 | * @return string |
||
252 | * @author Dmitry (dio) Levashov |
||
253 | **/ |
||
254 | protected function _basename($path) { |
||
257 | |||
258 | /** |
||
259 | * Join dir name and file name and retur full path |
||
260 | * |
||
261 | * @param string $dir |
||
262 | * @param string $name |
||
263 | * @return string |
||
264 | * @author Dmitry (dio) Levashov |
||
265 | **/ |
||
266 | protected function _joinPath($dir, $name) { |
||
269 | |||
270 | /** |
||
271 | * Return normalized path, this works the same as os.path.normpath() in Python |
||
272 | * |
||
273 | * @param string $path path |
||
274 | * @return string |
||
275 | * @author Troex Nevelin |
||
276 | **/ |
||
277 | protected function _normpath($path) { |
||
328 | |||
329 | /** |
||
330 | * Return file path related to root dir |
||
331 | * |
||
332 | * @param string $path file path |
||
333 | * @return string |
||
334 | * @author Dmitry (dio) Levashov |
||
335 | **/ |
||
336 | View Code Duplication | protected function _relpath($path) { |
|
348 | |||
349 | /** |
||
350 | * Convert path related to root dir into real path |
||
351 | * |
||
352 | * @param string $path file path |
||
353 | * @return string |
||
354 | * @author Dmitry (dio) Levashov |
||
355 | **/ |
||
356 | protected function _abspath($path) { |
||
368 | |||
369 | /** |
||
370 | * Return fake path started from root dir |
||
371 | * |
||
372 | * @param string $path file path |
||
373 | * @return string |
||
374 | * @author Dmitry (dio) Levashov |
||
375 | **/ |
||
376 | protected function _path($path) { |
||
379 | |||
380 | /** |
||
381 | * Return true if $path is children of $parent |
||
382 | * |
||
383 | * @param string $path path to check |
||
384 | * @param string $parent parent path |
||
385 | * @return bool |
||
386 | * @author Dmitry (dio) Levashov |
||
387 | **/ |
||
388 | protected function _inpath($path, $parent) { |
||
397 | |||
398 | |||
399 | |||
400 | /***************** file stat ********************/ |
||
401 | |||
402 | /** |
||
403 | * Return stat for given path. |
||
404 | * Stat contains following fields: |
||
405 | * - (int) size file size in b. required |
||
406 | * - (int) ts file modification time in unix time. required |
||
407 | * - (string) mime mimetype. required for folders, others - optionally |
||
408 | * - (bool) read read permissions. required |
||
409 | * - (bool) write write permissions. required |
||
410 | * - (bool) locked is object locked. optionally |
||
411 | * - (bool) hidden is object hidden. optionally |
||
412 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
413 | * - (string) target for symlinks - link target path. optionally |
||
414 | * |
||
415 | * If file does not exists - returns empty array or false. |
||
416 | * |
||
417 | * @param string $path file path |
||
418 | * @return array|false |
||
419 | * @author Dmitry (dio) Levashov |
||
420 | **/ |
||
421 | protected function _stat($path) { |
||
484 | |||
485 | /** |
||
486 | * Get stat `owner`, `group` and `isowner` by `uid` and `gid` |
||
487 | * Sub-fuction of _stat() and _scandir() |
||
488 | * |
||
489 | * @param integer $uid |
||
490 | * @param integer $gid |
||
491 | * @return array stat |
||
492 | */ |
||
493 | protected function getOwnerStat($uid, $gid) { |
||
534 | |||
535 | /** |
||
536 | * Return true if path is dir and has at least one childs directory |
||
537 | * |
||
538 | * @param string $path dir path |
||
539 | * @return bool |
||
540 | * @author Dmitry (dio) Levashov |
||
541 | **/ |
||
542 | protected function _subdirs($path) { |
||
571 | |||
572 | /** |
||
573 | * Return object width and height |
||
574 | * Usualy used for images, but can be realize for video etc... |
||
575 | * |
||
576 | * @param string $path file path |
||
577 | * @param string $mime file mime type |
||
578 | * @return string |
||
579 | * @author Dmitry (dio) Levashov |
||
580 | **/ |
||
581 | protected function _dimensions($path, $mime) { |
||
587 | /******************** file/dir content *********************/ |
||
588 | |||
589 | /** |
||
590 | * Return symlink target file |
||
591 | * |
||
592 | * @param string $path link path |
||
593 | * @return string |
||
594 | * @author Dmitry (dio) Levashov |
||
595 | **/ |
||
596 | protected function readlink($path) { |
||
611 | |||
612 | /** |
||
613 | * Return files list in directory. |
||
614 | * |
||
615 | * @param string $path dir path |
||
616 | * @return array |
||
617 | * @author Dmitry (dio) Levashov |
||
618 | **/ |
||
619 | protected function _scandir($path) { |
||
702 | |||
703 | /** |
||
704 | * Open file and return file pointer |
||
705 | * |
||
706 | * @param string $path file path |
||
707 | * @param bool $write open file for writing |
||
708 | * @return resource|false |
||
709 | * @author Dmitry (dio) Levashov |
||
710 | **/ |
||
711 | protected function _fopen($path, $mode='rb') { |
||
714 | |||
715 | /** |
||
716 | * Close opened file |
||
717 | * |
||
718 | * @param resource $fp file pointer |
||
719 | * @return bool |
||
720 | * @author Dmitry (dio) Levashov |
||
721 | **/ |
||
722 | protected function _fclose($fp, $path='') { |
||
725 | |||
726 | /******************** file/dir manipulations *************************/ |
||
727 | |||
728 | /** |
||
729 | * Create dir and return created dir path or false on failed |
||
730 | * |
||
731 | * @param string $path parent dir path |
||
732 | * @param string $name new directory name |
||
733 | * @return string|bool |
||
734 | * @author Dmitry (dio) Levashov |
||
735 | **/ |
||
736 | View Code Duplication | protected function _mkdir($path, $name) { |
|
747 | |||
748 | /** |
||
749 | * Create file and return it's path or false on failed |
||
750 | * |
||
751 | * @param string $path parent dir path |
||
752 | * @param string $name new file name |
||
753 | * @return string|bool |
||
754 | * @author Dmitry (dio) Levashov |
||
755 | **/ |
||
756 | View Code Duplication | protected function _mkfile($path, $name) { |
|
767 | |||
768 | /** |
||
769 | * Create symlink |
||
770 | * |
||
771 | * @param string $source file to link to |
||
772 | * @param string $targetDir folder to create link in |
||
773 | * @param string $name symlink name |
||
774 | * @return bool |
||
775 | * @author Dmitry (dio) Levashov |
||
776 | **/ |
||
777 | protected function _symlink($source, $targetDir, $name) { |
||
780 | |||
781 | /** |
||
782 | * Copy file into another file |
||
783 | * |
||
784 | * @param string $source source file path |
||
785 | * @param string $targetDir target directory path |
||
786 | * @param string $name new file name |
||
787 | * @return bool |
||
788 | * @author Dmitry (dio) Levashov |
||
789 | **/ |
||
790 | protected function _copy($source, $targetDir, $name) { |
||
795 | |||
796 | /** |
||
797 | * Move file into another parent dir. |
||
798 | * Return new file path or false. |
||
799 | * |
||
800 | * @param string $source source file path |
||
801 | * @param string $target target dir path |
||
802 | * @param string $name file name |
||
803 | * @return string|bool |
||
804 | * @author Dmitry (dio) Levashov |
||
805 | **/ |
||
806 | protected function _move($source, $targetDir, $name) { |
||
812 | |||
813 | /** |
||
814 | * Remove file |
||
815 | * |
||
816 | * @param string $path file path |
||
817 | * @return bool |
||
818 | * @author Dmitry (dio) Levashov |
||
819 | **/ |
||
820 | protected function _unlink($path) { |
||
825 | |||
826 | /** |
||
827 | * Remove dir |
||
828 | * |
||
829 | * @param string $path dir path |
||
830 | * @return bool |
||
831 | * @author Dmitry (dio) Levashov |
||
832 | **/ |
||
833 | protected function _rmdir($path) { |
||
838 | |||
839 | /** |
||
840 | * Create new file and write into it from file pointer. |
||
841 | * Return new file path or false on error. |
||
842 | * |
||
843 | * @param resource $fp file pointer |
||
844 | * @param string $dir target dir path |
||
845 | * @param string $name file name |
||
846 | * @param array $stat file stat (required by some virtual fs) |
||
847 | * @return bool|string |
||
848 | * @author Dmitry (dio) Levashov |
||
849 | **/ |
||
850 | protected function _save($fp, $dir, $name, $stat) { |
||
874 | |||
875 | /** |
||
876 | * Get file contents |
||
877 | * |
||
878 | * @param string $path file path |
||
879 | * @return string|false |
||
880 | * @author Dmitry (dio) Levashov |
||
881 | **/ |
||
882 | protected function _getContents($path) { |
||
885 | |||
886 | /** |
||
887 | * Write a string to a file |
||
888 | * |
||
889 | * @param string $path file path |
||
890 | * @param string $content new file content |
||
891 | * @return bool |
||
892 | * @author Dmitry (dio) Levashov |
||
893 | **/ |
||
894 | protected function _filePutContents($path, $content) { |
||
901 | |||
902 | /** |
||
903 | * Detect available archivers |
||
904 | * |
||
905 | * @return void |
||
906 | **/ |
||
907 | protected function _checkArchivers() { |
||
911 | |||
912 | /** |
||
913 | * chmod availability |
||
914 | * |
||
915 | * @return bool |
||
916 | **/ |
||
917 | protected function _chmod($path, $mode) { |
||
923 | |||
924 | /** |
||
925 | * Recursive symlinks search |
||
926 | * |
||
927 | * @param string $path file/dir path |
||
928 | * @return bool |
||
929 | * @author Dmitry (dio) Levashov |
||
930 | **/ |
||
931 | protected function _findSymlinks($path) { |
||
960 | |||
961 | /** |
||
962 | * Extract files from archive |
||
963 | * |
||
964 | * @param string $path archive path |
||
965 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
966 | * @return true |
||
967 | * @author Dmitry (dio) Levashov, |
||
968 | * @author Alexey Sukhotin |
||
969 | **/ |
||
970 | protected function _extract($path, $arc) { |
||
1075 | |||
1076 | /** |
||
1077 | * Create archive and return its path |
||
1078 | * |
||
1079 | * @param string $dir target dir |
||
1080 | * @param array $files files names list |
||
1081 | * @param string $name archive name |
||
1082 | * @param array $arc archiver options |
||
1083 | * @return string|bool |
||
1084 | * @author Dmitry (dio) Levashov, |
||
1085 | * @author Alexey Sukhotin |
||
1086 | **/ |
||
1087 | protected function _archive($dir, $files, $name, $arc) { |
||
1090 | |||
1091 | /******************** Over write functions *************************/ |
||
1092 | |||
1093 | /** |
||
1094 | * File path of local server side work file path |
||
1095 | * |
||
1096 | * @param string $path |
||
1097 | * @return string |
||
1098 | * @author Naoki Sawada |
||
1099 | */ |
||
1100 | protected function getWorkFile($path) { |
||
1103 | |||
1104 | /** |
||
1105 | * Delete dirctory trees |
||
1106 | * |
||
1107 | * @param string $localpath path need convert encoding to server encoding |
||
1108 | * @return boolean |
||
1109 | * @author Naoki Sawada |
||
1110 | */ |
||
1111 | protected function delTree($localpath) { |
||
1114 | |||
1115 | /******************** Over write (Optimized) functions *************************/ |
||
1116 | |||
1117 | /** |
||
1118 | * Recursive files search |
||
1119 | * |
||
1120 | * @param string $path dir path |
||
1121 | * @param string $q search string |
||
1122 | * @param array $mimes |
||
1123 | * @return array |
||
1124 | * @author Dmitry (dio) Levashov |
||
1125 | * @author Naoki Sawada |
||
1126 | **/ |
||
1127 | protected function doSearch($path, $q, $mimes) { |
||
1190 | |||
1191 | /******************** Original local functions *************************/ |
||
1192 | |||
1193 | public function localFileSystemSearchIteratorFilter($file, $key, $iterator) { |
||
1199 | |||
1200 | } // END class |
||
1201 | |||
1202 |
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.