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 elFinder 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 elFinder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class elFinder { |
||
13 | |||
14 | /** |
||
15 | * API version number |
||
16 | * |
||
17 | * @var string |
||
18 | **/ |
||
19 | protected $version = '2.0'; |
||
20 | |||
21 | /** |
||
22 | * Storages (root dirs) |
||
23 | * |
||
24 | * @var array |
||
25 | **/ |
||
26 | protected $volumes = array(); |
||
27 | |||
28 | public static $netDrivers = array(); |
||
29 | |||
30 | /** |
||
31 | * Mounted volumes count |
||
32 | * Required to create unique volume id |
||
33 | * |
||
34 | * @var int |
||
35 | **/ |
||
36 | public static $volumesCnt = 1; |
||
37 | |||
38 | /** |
||
39 | * Default root (storage) |
||
40 | * |
||
41 | * @var elFinderStorageDriver |
||
42 | **/ |
||
43 | protected $default = null; |
||
44 | |||
45 | /** |
||
46 | * Commands and required arguments list |
||
47 | * |
||
48 | * @var array |
||
49 | **/ |
||
50 | protected $commands = array( |
||
51 | 'open' => array('target' => false, 'tree' => false, 'init' => false, 'mimes' => false), |
||
52 | 'ls' => array('target' => true, 'mimes' => false), |
||
53 | 'tree' => array('target' => true), |
||
54 | 'parents' => array('target' => true), |
||
55 | 'tmb' => array('targets' => true), |
||
56 | 'file' => array('target' => true, 'download' => false), |
||
57 | 'size' => array('targets' => true), |
||
58 | 'mkdir' => array('target' => true, 'name' => true), |
||
59 | 'mkfile' => array('target' => true, 'name' => true, 'mimes' => false), |
||
60 | 'rm' => array('targets' => true), |
||
61 | 'rename' => array('target' => true, 'name' => true, 'mimes' => false), |
||
62 | 'duplicate' => array('targets' => true, 'suffix' => false), |
||
63 | 'paste' => array('dst' => true, 'targets' => true, 'cut' => false, 'mimes' => false), |
||
64 | 'upload' => array('target' => true, 'FILES' => true, 'mimes' => false, 'html' => false), |
||
65 | 'get' => array('target' => true), |
||
66 | 'put' => array('target' => true, 'content' => '', 'mimes' => false), |
||
67 | 'archive' => array('targets' => true, 'type' => true, 'mimes' => false), |
||
68 | 'extract' => array('target' => true, 'mimes' => false), |
||
69 | 'search' => array('q' => true, 'mimes' => false), |
||
70 | 'info' => array('targets' => true), |
||
71 | 'dim' => array('target' => true), |
||
72 | 'resize' => array('target' => true, 'width' => true, 'height' => true, 'mode' => false, 'x' => false, 'y' => false, 'degree' => false), |
||
73 | 'netmount' => array('protocol' => true, 'host' => true, 'path' => false, 'port' => false, 'user' => true, 'pass' => true, 'alias' => false, 'options' => false) |
||
74 | ); |
||
75 | |||
76 | /** |
||
77 | * Commands listeners |
||
78 | * |
||
79 | * @var array |
||
80 | **/ |
||
81 | protected $listeners = array(); |
||
82 | |||
83 | /** |
||
84 | * script work time for debug |
||
85 | * |
||
86 | * @var string |
||
87 | **/ |
||
88 | protected $time = 0; |
||
89 | /** |
||
90 | * Is elFinder init correctly? |
||
91 | * |
||
92 | * @var bool |
||
93 | **/ |
||
94 | protected $loaded = false; |
||
95 | /** |
||
96 | * Send debug to client? |
||
97 | * |
||
98 | * @var string |
||
99 | **/ |
||
100 | protected $debug = false; |
||
101 | |||
102 | /** |
||
103 | * session expires timeout |
||
104 | * |
||
105 | * @var int |
||
106 | **/ |
||
107 | protected $timeout = 0; |
||
108 | |||
109 | /** |
||
110 | * undocumented class variable |
||
111 | * |
||
112 | * @var string |
||
113 | **/ |
||
114 | protected $uploadDebug = ''; |
||
115 | |||
116 | /** |
||
117 | * Errors from not mounted volumes |
||
118 | * |
||
119 | * @var array |
||
120 | **/ |
||
121 | public $mountErrors = array(); |
||
122 | |||
123 | // Errors messages |
||
124 | const ERROR_UNKNOWN = 'errUnknown'; |
||
125 | const ERROR_UNKNOWN_CMD = 'errUnknownCmd'; |
||
126 | const ERROR_CONF = 'errConf'; |
||
127 | const ERROR_CONF_NO_JSON = 'errJSON'; |
||
128 | const ERROR_CONF_NO_VOL = 'errNoVolumes'; |
||
129 | const ERROR_INV_PARAMS = 'errCmdParams'; |
||
130 | const ERROR_OPEN = 'errOpen'; |
||
131 | const ERROR_DIR_NOT_FOUND = 'errFolderNotFound'; |
||
132 | const ERROR_FILE_NOT_FOUND = 'errFileNotFound'; // 'File not found.' |
||
133 | const ERROR_TRGDIR_NOT_FOUND = 'errTrgFolderNotFound'; // 'Target folder "$1" not found.' |
||
134 | const ERROR_NOT_DIR = 'errNotFolder'; |
||
135 | const ERROR_NOT_FILE = 'errNotFile'; |
||
136 | const ERROR_PERM_DENIED = 'errPerm'; |
||
137 | const ERROR_LOCKED = 'errLocked'; // '"$1" is locked and can not be renamed, moved or removed.' |
||
138 | const ERROR_EXISTS = 'errExists'; // 'File named "$1" already exists.' |
||
139 | const ERROR_INVALID_NAME = 'errInvName'; // 'Invalid file name.' |
||
140 | const ERROR_MKDIR = 'errMkdir'; |
||
141 | const ERROR_MKFILE = 'errMkfile'; |
||
142 | const ERROR_RENAME = 'errRename'; |
||
143 | const ERROR_COPY = 'errCopy'; |
||
144 | const ERROR_MOVE = 'errMove'; |
||
145 | const ERROR_COPY_FROM = 'errCopyFrom'; |
||
146 | const ERROR_COPY_TO = 'errCopyTo'; |
||
147 | const ERROR_COPY_ITSELF = 'errCopyInItself'; |
||
148 | const ERROR_REPLACE = 'errReplace'; // 'Unable to replace "$1".' |
||
149 | const ERROR_RM = 'errRm'; // 'Unable to remove "$1".' |
||
150 | const ERROR_RM_SRC = 'errRmSrc'; // 'Unable remove source file(s)' |
||
151 | const ERROR_UPLOAD = 'errUpload'; // 'Upload error.' |
||
152 | const ERROR_UPLOAD_FILE = 'errUploadFile'; // 'Unable to upload "$1".' |
||
153 | const ERROR_UPLOAD_NO_FILES = 'errUploadNoFiles'; // 'No files found for upload.' |
||
154 | const ERROR_UPLOAD_TOTAL_SIZE = 'errUploadTotalSize'; // 'Data exceeds the maximum allowed size.' |
||
155 | const ERROR_UPLOAD_FILE_SIZE = 'errUploadFileSize'; // 'File exceeds maximum allowed size.' |
||
156 | const ERROR_UPLOAD_FILE_MIME = 'errUploadMime'; // 'File type not allowed.' |
||
157 | const ERROR_UPLOAD_TRANSFER = 'errUploadTransfer'; // '"$1" transfer error.' |
||
158 | // const ERROR_ACCESS_DENIED = 'errAccess'; |
||
159 | const ERROR_NOT_REPLACE = 'errNotReplace'; // Object "$1" already exists at this location and can not be replaced with object of another type. |
||
160 | const ERROR_SAVE = 'errSave'; |
||
161 | const ERROR_EXTRACT = 'errExtract'; |
||
162 | const ERROR_ARCHIVE = 'errArchive'; |
||
163 | const ERROR_NOT_ARCHIVE = 'errNoArchive'; |
||
164 | const ERROR_ARCHIVE_TYPE = 'errArcType'; |
||
165 | const ERROR_ARC_SYMLINKS = 'errArcSymlinks'; |
||
166 | const ERROR_ARC_MAXSIZE = 'errArcMaxSize'; |
||
167 | const ERROR_RESIZE = 'errResize'; |
||
168 | const ERROR_UNSUPPORT_TYPE = 'errUsupportType'; |
||
169 | const ERROR_NOT_UTF8_CONTENT = 'errNotUTF8Content'; |
||
170 | const ERROR_NETMOUNT = 'errNetMount'; |
||
171 | const ERROR_NETMOUNT_NO_DRIVER = 'errNetMountNoDriver'; |
||
172 | const ERROR_NETMOUNT_FAILED = 'errNetMountFailed'; |
||
173 | |||
174 | const ERROR_SESSION_EXPIRES = 'errSessionExpires'; |
||
175 | |||
176 | const ERROR_CREATING_TEMP_DIR = 'errCreatingTempDir'; |
||
177 | const ERROR_FTP_DOWNLOAD_FILE = 'errFtpDownloadFile'; |
||
178 | const ERROR_FTP_UPLOAD_FILE = 'errFtpUploadFile'; |
||
179 | const ERROR_FTP_MKDIR = 'errFtpMkdir'; |
||
180 | const ERROR_ARCHIVE_EXEC = 'errArchiveExec'; |
||
181 | const ERROR_EXTRACT_EXEC = 'errExtractExec'; |
||
182 | |||
183 | /** |
||
184 | * Constructor |
||
185 | * |
||
186 | * @param array elFinder and roots configurations |
||
187 | * @return void |
||
|
|||
188 | * @author Dmitry (dio) Levashov |
||
189 | **/ |
||
190 | public function __construct($opts) { |
||
243 | |||
244 | /** |
||
245 | * Return true if fm init correctly |
||
246 | * |
||
247 | * @return bool |
||
248 | * @author Dmitry (dio) Levashov |
||
249 | **/ |
||
250 | public function loaded() { |
||
253 | |||
254 | /** |
||
255 | * Return version (api) number |
||
256 | * |
||
257 | * @return string |
||
258 | * @author Dmitry (dio) Levashov |
||
259 | **/ |
||
260 | public function version() { |
||
263 | |||
264 | /** |
||
265 | * Add handler to elFinder command |
||
266 | * |
||
267 | * @param string command name |
||
268 | * @param string|array callback name or array(object, method) |
||
269 | * @return elFinder |
||
270 | * @author Dmitry (dio) Levashov |
||
271 | **/ |
||
272 | public function bind($cmd, $handler) { |
||
291 | |||
292 | /** |
||
293 | * Remove event (command exec) handler |
||
294 | * |
||
295 | * @param string command name |
||
296 | * @param string|array callback name or array(object, method) |
||
297 | * @return elFinder |
||
298 | * @author Dmitry (dio) Levashov |
||
299 | **/ |
||
300 | public function unbind($cmd, $handler) { |
||
311 | |||
312 | /** |
||
313 | * Return true if command exists |
||
314 | * |
||
315 | * @param string command name |
||
316 | * @return bool |
||
317 | * @author Dmitry (dio) Levashov |
||
318 | **/ |
||
319 | public function commandExists($cmd) { |
||
322 | |||
323 | /** |
||
324 | * Return root - file's owner (public func of volume()) |
||
325 | * |
||
326 | * @param string file hash |
||
327 | * @return elFinderStorageDriver |
||
328 | * @author Naoki Sawada |
||
329 | */ |
||
330 | public function getVolume($hash) { |
||
333 | |||
334 | /** |
||
335 | * Return command required arguments info |
||
336 | * |
||
337 | * @param string command name |
||
338 | * @return array |
||
339 | * @author Dmitry (dio) Levashov |
||
340 | **/ |
||
341 | public function commandArgsList($cmd) { |
||
344 | |||
345 | private function session_expires() { |
||
359 | |||
360 | /** |
||
361 | * Exec command and return result |
||
362 | * |
||
363 | * @param string $cmd command name |
||
364 | * @param array $args command arguments |
||
365 | * @return array |
||
366 | * @author Dmitry (dio) Levashov |
||
367 | **/ |
||
368 | public function exec($cmd, $args) { |
||
446 | |||
447 | /** |
||
448 | * Return file real path |
||
449 | * |
||
450 | * @param string $hash file hash |
||
451 | * @return string |
||
452 | * @author Dmitry (dio) Levashov |
||
453 | **/ |
||
454 | public function realpath($hash) { |
||
460 | |||
461 | /** |
||
462 | * Return network volumes config. |
||
463 | * |
||
464 | * @return array |
||
465 | * @author Dmitry (dio) Levashov |
||
466 | */ |
||
467 | protected function getNetVolumes() { |
||
470 | |||
471 | /** |
||
472 | * Save network volumes config. |
||
473 | * |
||
474 | * @param array $volumes volumes config |
||
475 | * @return void |
||
476 | * @author Dmitry (dio) Levashov |
||
477 | */ |
||
478 | protected function saveNetVolumes($volumes) { |
||
481 | |||
482 | /***************************************************************************/ |
||
483 | /* commands */ |
||
484 | /***************************************************************************/ |
||
485 | |||
486 | /** |
||
487 | * Normalize error messages |
||
488 | * |
||
489 | * @return array |
||
490 | * @author Dmitry (dio) Levashov |
||
491 | **/ |
||
492 | public function error() { |
||
505 | |||
506 | protected function netmount($args) { |
||
546 | |||
547 | /** |
||
548 | * "Open" directory |
||
549 | * Return array with following elements |
||
550 | * - cwd - opened dir info |
||
551 | * - files - opened dir content [and dirs tree if $args[tree]] |
||
552 | * - api - api version (if $args[init]) |
||
553 | * - uplMaxSize - if $args[init] |
||
554 | * - error - on failed |
||
555 | * |
||
556 | * @param array command arguments |
||
557 | * @return array |
||
558 | * @author Dmitry (dio) Levashov |
||
559 | **/ |
||
560 | protected function open($args) { |
||
619 | |||
620 | /** |
||
621 | * Return dir files names list |
||
622 | * |
||
623 | * @param array command arguments |
||
624 | * @return array |
||
625 | * @author Dmitry (dio) Levashov |
||
626 | **/ |
||
627 | View Code Duplication | protected function ls($args) { |
|
636 | |||
637 | /** |
||
638 | * Return subdirs for required directory |
||
639 | * |
||
640 | * @param array command arguments |
||
641 | * @return array |
||
642 | * @author Dmitry (dio) Levashov |
||
643 | **/ |
||
644 | View Code Duplication | protected function tree($args) { |
|
654 | |||
655 | /** |
||
656 | * Return parents dir for required directory |
||
657 | * |
||
658 | * @param array command arguments |
||
659 | * @return array |
||
660 | * @author Dmitry (dio) Levashov |
||
661 | **/ |
||
662 | View Code Duplication | protected function parents($args) { |
|
672 | |||
673 | /** |
||
674 | * Return new created thumbnails list |
||
675 | * |
||
676 | * @param array command arguments |
||
677 | * @return array |
||
678 | * @author Dmitry (dio) Levashov |
||
679 | **/ |
||
680 | protected function tmb($args) { |
||
693 | |||
694 | /** |
||
695 | * Required to output file in browser when volume URL is not set |
||
696 | * Return array contains opened file pointer, root itself and required headers |
||
697 | * |
||
698 | * @param array command arguments |
||
699 | * @return array |
||
700 | * @author Dmitry (dio) Levashov |
||
701 | **/ |
||
702 | protected function file($args) { |
||
763 | |||
764 | /** |
||
765 | * Count total files size |
||
766 | * |
||
767 | * @param array command arguments |
||
768 | * @return array |
||
769 | * @author Dmitry (dio) Levashov |
||
770 | **/ |
||
771 | protected function size($args) { |
||
785 | |||
786 | /** |
||
787 | * Create directory |
||
788 | * |
||
789 | * @param array command arguments |
||
790 | * @return array |
||
791 | * @author Dmitry (dio) Levashov |
||
792 | **/ |
||
793 | View Code Duplication | protected function mkdir($args) { |
|
805 | |||
806 | /** |
||
807 | * Create empty file |
||
808 | * |
||
809 | * @param array command arguments |
||
810 | * @return array |
||
811 | * @author Dmitry (dio) Levashov |
||
812 | **/ |
||
813 | View Code Duplication | protected function mkfile($args) { |
|
825 | |||
826 | /** |
||
827 | * Rename file |
||
828 | * |
||
829 | * @param array $args |
||
830 | * @return array |
||
831 | * @author Dmitry (dio) Levashov |
||
832 | **/ |
||
833 | protected function rename($args) { |
||
847 | |||
848 | /** |
||
849 | * Duplicate file - create copy with "copy %d" suffix |
||
850 | * |
||
851 | * @param array $args command arguments |
||
852 | * @return array |
||
853 | * @author Dmitry (dio) Levashov |
||
854 | **/ |
||
855 | protected function duplicate($args) { |
||
877 | |||
878 | /** |
||
879 | * Remove dirs/files |
||
880 | * |
||
881 | * @param array command arguments |
||
882 | * @return array |
||
883 | * @author Dmitry (dio) Levashov |
||
884 | **/ |
||
885 | protected function rm($args) { |
||
902 | |||
903 | /** |
||
904 | * Save uploaded files |
||
905 | * |
||
906 | * @param array |
||
907 | * @return array |
||
908 | * @author Dmitry (dio) Levashov |
||
909 | **/ |
||
910 | protected function upload($args) { |
||
951 | |||
952 | /** |
||
953 | * Copy/move files into new destination |
||
954 | * |
||
955 | * @param array command arguments |
||
956 | * @return array |
||
957 | * @author Dmitry (dio) Levashov |
||
958 | **/ |
||
959 | protected function paste($args) { |
||
985 | |||
986 | /** |
||
987 | * Return file content |
||
988 | * |
||
989 | * @param array $args command arguments |
||
990 | * @return array |
||
991 | * @author Dmitry (dio) Levashov |
||
992 | **/ |
||
993 | protected function get($args) { |
||
1013 | |||
1014 | /** |
||
1015 | * Save content into text file |
||
1016 | * |
||
1017 | * @return array |
||
1018 | * @author Dmitry (dio) Levashov |
||
1019 | **/ |
||
1020 | protected function put($args) { |
||
1034 | |||
1035 | /** |
||
1036 | * Extract files from archive |
||
1037 | * |
||
1038 | * @param array $args command arguments |
||
1039 | * @return array |
||
1040 | * @author Dmitry (dio) Levashov, |
||
1041 | * @author Alexey Sukhotin |
||
1042 | **/ |
||
1043 | protected function extract($args) { |
||
1057 | |||
1058 | /** |
||
1059 | * Create archive |
||
1060 | * |
||
1061 | * @param array $args command arguments |
||
1062 | * @return array |
||
1063 | * @author Dmitry (dio) Levashov, |
||
1064 | * @author Alexey Sukhotin |
||
1065 | **/ |
||
1066 | protected function archive($args) { |
||
1078 | |||
1079 | /** |
||
1080 | * Search files |
||
1081 | * |
||
1082 | * @param array $args command arguments |
||
1083 | * @return array |
||
1084 | * @author Dmitry Levashov |
||
1085 | **/ |
||
1086 | protected function search($args) { |
||
1097 | |||
1098 | /** |
||
1099 | * Return file info (used by client "places" ui) |
||
1100 | * |
||
1101 | * @param array $args command arguments |
||
1102 | * @return array |
||
1103 | * @author Dmitry Levashov |
||
1104 | **/ |
||
1105 | protected function info($args) { |
||
1117 | |||
1118 | /** |
||
1119 | * Return image dimmensions |
||
1120 | * |
||
1121 | * @param array $args command arguments |
||
1122 | * @return array |
||
1123 | * @author Dmitry (dio) Levashov |
||
1124 | **/ |
||
1125 | protected function dim($args) { |
||
1134 | |||
1135 | /** |
||
1136 | * Resize image |
||
1137 | * |
||
1138 | * @param array command arguments |
||
1139 | * @return array |
||
1140 | * @author Dmitry (dio) Levashov |
||
1141 | * @author Alexey Sukhotin |
||
1142 | **/ |
||
1143 | protected function resize($args) { |
||
1162 | |||
1163 | /***************************************************************************/ |
||
1164 | /* utils */ |
||
1165 | /***************************************************************************/ |
||
1166 | |||
1167 | /** |
||
1168 | * Return root - file's owner |
||
1169 | * |
||
1170 | * @param string file hash |
||
1171 | * @return elFinderStorageDriver |
||
1172 | * @author Dmitry (dio) Levashov |
||
1173 | **/ |
||
1174 | protected function volume($hash) { |
||
1182 | |||
1183 | /** |
||
1184 | * Return files info array |
||
1185 | * |
||
1186 | * @param array $data one file info or files info |
||
1187 | * @return array |
||
1188 | * @author Dmitry (dio) Levashov |
||
1189 | **/ |
||
1190 | protected function toArray($data) { |
||
1193 | |||
1194 | /** |
||
1195 | * Return fils hashes list |
||
1196 | * |
||
1197 | * @param array $files files info |
||
1198 | * @return array |
||
1199 | * @author Dmitry (dio) Levashov |
||
1200 | **/ |
||
1201 | protected function hashes($files) { |
||
1208 | |||
1209 | /** |
||
1210 | * Remove from files list hidden files and files with required mime types |
||
1211 | * |
||
1212 | * @param array $files files info |
||
1213 | * @return array |
||
1214 | * @author Dmitry (dio) Levashov |
||
1215 | **/ |
||
1216 | protected function filter($files) { |
||
1224 | |||
1225 | protected function utime() { |
||
1229 | |||
1230 | } // END class |
||
1231 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.