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 elFinderVolumeDropbox2 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 elFinderVolumeDropbox2, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class elFinderVolumeDropbox2 extends elFinderVolumeDriver |
||
19 | { |
||
20 | /** |
||
21 | * Net mount key. |
||
22 | * |
||
23 | * @var string |
||
24 | **/ |
||
25 | public $netMountKey = ''; |
||
26 | /** |
||
27 | * Driver id |
||
28 | * Must be started from letter and contains [a-z0-9] |
||
29 | * Used as part of volume id. |
||
30 | * |
||
31 | * @var string |
||
32 | **/ |
||
33 | protected $driverId = 'db'; |
||
34 | |||
35 | /** |
||
36 | * Dropbox service object. |
||
37 | * |
||
38 | * @var object |
||
39 | **/ |
||
40 | protected $service = null; |
||
41 | |||
42 | /** |
||
43 | * Directory for tmp files |
||
44 | * If not set driver will try to use tmbDir as tmpDir. |
||
45 | * |
||
46 | * @var string |
||
47 | **/ |
||
48 | protected $tmp = ''; |
||
49 | |||
50 | /** |
||
51 | * Fetch options. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private $FETCH_OPTIONS = []; |
||
56 | |||
57 | /** |
||
58 | * Constructor |
||
59 | * Extend options with required fields. |
||
60 | * |
||
61 | * @author Naoki Sawada |
||
62 | **/ |
||
63 | View Code Duplication | public function __construct() |
|
84 | |||
85 | /** |
||
86 | * Get OAuth2 access token form OAuth1 tokens. |
||
87 | * |
||
88 | * @param string $app_key |
||
89 | * @param string $app_secret |
||
90 | * @param string $oauth1_token |
||
91 | * @param string $oauth1_secret |
||
92 | * |
||
93 | * @return string|false |
||
94 | */ |
||
95 | public static function getTokenFromOauth1($app_key, $app_secret, $oauth1_token, $oauth1_secret) |
||
118 | |||
119 | /*********************************************************************/ |
||
120 | /* EXTENDED FUNCTIONS */ |
||
121 | /*********************************************************************/ |
||
122 | |||
123 | /** |
||
124 | * Prepare |
||
125 | * Call from elFinder::netmout() before volume->mount(). |
||
126 | * |
||
127 | * @return array |
||
128 | * |
||
129 | * @author Naoki Sawada |
||
130 | **/ |
||
131 | public function netmountPrepare($options) |
||
293 | |||
294 | /** |
||
295 | * process of on netunmount |
||
296 | * Drop `Dropbox` & rm thumbs. |
||
297 | * |
||
298 | * @param array $options |
||
299 | * |
||
300 | * @return bool |
||
301 | */ |
||
302 | public function netunmount($netVolumes, $key) |
||
312 | |||
313 | /*********************************************************************/ |
||
314 | /* FS API */ |
||
315 | /*********************************************************************/ |
||
316 | |||
317 | /** |
||
318 | * Close opened connection. |
||
319 | **/ |
||
320 | public function umount() |
||
323 | |||
324 | /** |
||
325 | * Return content URL (for netmout volume driver) |
||
326 | * If file.url == 1 requests from JavaScript client with XHR. |
||
327 | * |
||
328 | * @param string $hash file hash |
||
329 | * @param array $options options array |
||
330 | * |
||
331 | * @return bool|string |
||
332 | * |
||
333 | * @author Naoki Sawada |
||
334 | */ |
||
335 | public function getContentUrl($hash, $options = []) |
||
379 | |||
380 | /** |
||
381 | * Return debug info for client. |
||
382 | * |
||
383 | * @return array |
||
384 | **/ |
||
385 | public function debug() |
||
395 | |||
396 | /*********************************************************************/ |
||
397 | /* ORIGINAL FUNCTIONS */ |
||
398 | /*********************************************************************/ |
||
399 | |||
400 | /** |
||
401 | * Get Parent ID, Item ID, Parent Path as an array from path. |
||
402 | * |
||
403 | * @param string $path |
||
404 | * |
||
405 | * @return array |
||
406 | */ |
||
407 | protected function _db_splitPath($path) |
||
426 | |||
427 | /** |
||
428 | * Parse line from Dropbox metadata output and return file stat (array). |
||
429 | * |
||
430 | * @param string $raw line from ftp_rawlist() output |
||
431 | * |
||
432 | * @return array |
||
433 | * |
||
434 | * @author Naoki Sawada |
||
435 | **/ |
||
436 | protected function _db_parseRaw($raw) |
||
487 | |||
488 | /** |
||
489 | * Get thumbnail from Dropbox. |
||
490 | * |
||
491 | * @param string $path |
||
492 | * @param string $size |
||
493 | * |
||
494 | * @return string | boolean |
||
495 | */ |
||
496 | protected function _db_getThumbnail($path) |
||
504 | |||
505 | /** |
||
506 | * Join dir name and file name(display name) and retur full path. |
||
507 | * |
||
508 | * @param unknown $dir |
||
509 | * @param unknown $displayName |
||
510 | * |
||
511 | * @return string |
||
512 | */ |
||
513 | protected function _db_joinName($dir, $displayName) |
||
517 | |||
518 | /*********************************************************************/ |
||
519 | /* INIT AND CONFIGURE */ |
||
520 | /*********************************************************************/ |
||
521 | |||
522 | /** |
||
523 | * Prepare FTP connection |
||
524 | * Connect to remote server and check if credentials are correct, if so, store the connection id in $ftp_conn. |
||
525 | * |
||
526 | * @return bool |
||
527 | * |
||
528 | * @author Naoki Sawada |
||
529 | **/ |
||
530 | protected function init() |
||
606 | |||
607 | /** |
||
608 | * Configure after successfull mount. |
||
609 | * |
||
610 | * @author Naoki Sawada |
||
611 | **/ |
||
612 | View Code Duplication | protected function configure() |
|
628 | |||
629 | /** |
||
630 | * Cache dir contents. |
||
631 | * |
||
632 | * @param string $path dir path |
||
633 | * |
||
634 | * @author Naoki Sawada |
||
635 | **/ |
||
636 | protected function cacheDir($path) |
||
665 | |||
666 | /** |
||
667 | * Recursive files search. |
||
668 | * |
||
669 | * @param string $path dir path |
||
670 | * @param string $q search string |
||
671 | * @param array $mimes |
||
672 | * |
||
673 | * @return array |
||
674 | * |
||
675 | * @author Naoki Sawada |
||
676 | **/ |
||
677 | protected function doSearch($path, $q, $mimes) |
||
710 | |||
711 | /** |
||
712 | * Copy file/recursive copy dir only in current volume. |
||
713 | * Return new file path or false. |
||
714 | * |
||
715 | * @param string $src source path |
||
716 | * @param string $dst destination dir path |
||
717 | * @param string $name new file name (optionaly) |
||
718 | * |
||
719 | * @return string|false |
||
720 | * |
||
721 | * @author Naoki Sawada |
||
722 | **/ |
||
723 | protected function copy($src, $dst, $name) |
||
743 | |||
744 | /** |
||
745 | * Remove file/ recursive remove dir. |
||
746 | * |
||
747 | * @param string $path file path |
||
748 | * @param bool $force try to remove even if file locked |
||
749 | * |
||
750 | * @return bool |
||
751 | * |
||
752 | * @author Naoki Sawada |
||
753 | **/ |
||
754 | View Code Duplication | protected function remove($path, $force = false, $recursive = false) |
|
783 | |||
784 | /** |
||
785 | * Create thumnbnail and return it's URL on success. |
||
786 | * |
||
787 | * @param string $path file path |
||
788 | * @param string $mime file mime type |
||
789 | * |
||
790 | * @return string|false |
||
791 | * |
||
792 | * @author Naoki Sawada |
||
793 | **/ |
||
794 | View Code Duplication | protected function createTmb($path, $stat) |
|
850 | |||
851 | /** |
||
852 | * Return thumbnail file name for required file. |
||
853 | * |
||
854 | * @param array $stat file stat |
||
855 | * |
||
856 | * @return string |
||
857 | * |
||
858 | * @author Naoki Sawada |
||
859 | **/ |
||
860 | protected function tmbname($stat) |
||
869 | |||
870 | /*********************** paths/urls *************************/ |
||
871 | |||
872 | /** |
||
873 | * Return parent directory path. |
||
874 | * |
||
875 | * @param string $path file path |
||
876 | * |
||
877 | * @return string |
||
878 | * |
||
879 | * @author Naoki Sawada |
||
880 | **/ |
||
881 | protected function _dirname($path) |
||
887 | |||
888 | /** |
||
889 | * Return file name. |
||
890 | * |
||
891 | * @param string $path file path |
||
892 | * |
||
893 | * @return string |
||
894 | * |
||
895 | * @author Naoki Sawada |
||
896 | **/ |
||
897 | protected function _basename($path) |
||
903 | |||
904 | /** |
||
905 | * Join dir name and file name and retur full path. |
||
906 | * |
||
907 | * @param string $dir |
||
908 | * @param string $name |
||
909 | * |
||
910 | * @return string |
||
911 | * |
||
912 | * @author Dmitry (dio) Levashov |
||
913 | **/ |
||
914 | protected function _joinPath($dir, $name) |
||
918 | |||
919 | /** |
||
920 | * Return normalized path, this works the same as os.path.normpath() in Python. |
||
921 | * |
||
922 | * @param string $path path |
||
923 | * |
||
924 | * @return string |
||
925 | * |
||
926 | * @author Naoki Sawada |
||
927 | **/ |
||
928 | protected function _normpath($path) |
||
932 | |||
933 | /** |
||
934 | * Return file path related to root dir. |
||
935 | * |
||
936 | * @param string $path file path |
||
937 | * |
||
938 | * @return string |
||
939 | * |
||
940 | * @author Dmitry (dio) Levashov |
||
941 | **/ |
||
942 | protected function _relpath($path) |
||
950 | |||
951 | /** |
||
952 | * Convert path related to root dir into real path. |
||
953 | * |
||
954 | * @param string $path file path |
||
955 | * |
||
956 | * @return string |
||
957 | * |
||
958 | * @author Naoki Sawada |
||
959 | **/ |
||
960 | protected function _abspath($path) |
||
968 | |||
969 | /** |
||
970 | * Return fake path started from root dir. |
||
971 | * |
||
972 | * @param string $path file path |
||
973 | * |
||
974 | * @return string |
||
975 | * |
||
976 | * @author Naoki Sawada |
||
977 | **/ |
||
978 | protected function _path($path) |
||
984 | |||
985 | /** |
||
986 | * Return true if $path is children of $parent. |
||
987 | * |
||
988 | * @param string $path path to check |
||
989 | * @param string $parent parent path |
||
990 | * |
||
991 | * @return bool |
||
992 | * |
||
993 | * @author Naoki Sawada |
||
994 | **/ |
||
995 | protected function _inpath($path, $parent) |
||
999 | |||
1000 | /***************** file stat ********************/ |
||
1001 | |||
1002 | /** |
||
1003 | * Return stat for given path. |
||
1004 | * Stat contains following fields: |
||
1005 | * - (int) size file size in b. required |
||
1006 | * - (int) ts file modification time in unix time. required |
||
1007 | * - (string) mime mimetype. required for folders, others - optionally |
||
1008 | * - (bool) read read permissions. required |
||
1009 | * - (bool) write write permissions. required |
||
1010 | * - (bool) locked is object locked. optionally |
||
1011 | * - (bool) hidden is object hidden. optionally |
||
1012 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
1013 | * - (string) target for symlinks - link target path. optionally. |
||
1014 | * |
||
1015 | * If file does not exists - returns empty array or false. |
||
1016 | * |
||
1017 | * @param string $path file path |
||
1018 | * |
||
1019 | * @return array|false |
||
1020 | * |
||
1021 | * @author Dmitry (dio) Levashov |
||
1022 | **/ |
||
1023 | protected function _stat($path) |
||
1031 | |||
1032 | /** |
||
1033 | * Return true if path is dir and has at least one childs directory. |
||
1034 | * |
||
1035 | * @param string $path dir path |
||
1036 | * |
||
1037 | * @return bool |
||
1038 | * |
||
1039 | * @author Naoki Sawada |
||
1040 | **/ |
||
1041 | protected function _subdirs($path) |
||
1062 | |||
1063 | /** |
||
1064 | * Return object width and height |
||
1065 | * Ususaly used for images, but can be realize for video etc... |
||
1066 | * |
||
1067 | * @param string $path file path |
||
1068 | * @param string $mime file mime type |
||
1069 | * |
||
1070 | * @return string |
||
1071 | * |
||
1072 | * @author Naoki Sawada |
||
1073 | **/ |
||
1074 | protected function _dimensions($path, $mime) |
||
1092 | |||
1093 | /******************** file/dir content *********************/ |
||
1094 | |||
1095 | /** |
||
1096 | * Return files list in directory. |
||
1097 | * |
||
1098 | * @param string $path dir path |
||
1099 | * |
||
1100 | * @return array |
||
1101 | * |
||
1102 | * @author Naoki Sawada |
||
1103 | **/ |
||
1104 | protected function _scandir($path) |
||
1110 | |||
1111 | /** |
||
1112 | * Open file and return file pointer. |
||
1113 | * |
||
1114 | * @param string $path file path |
||
1115 | * @param bool $write open file for writing |
||
1116 | * |
||
1117 | * @return resource|false |
||
1118 | * |
||
1119 | * @author Naoki Sawada |
||
1120 | **/ |
||
1121 | protected function _fopen($path, $mode = 'rb') |
||
1139 | |||
1140 | /** |
||
1141 | * Close opened file. |
||
1142 | * |
||
1143 | * @param resource $fp file pointer |
||
1144 | * |
||
1145 | * @return bool |
||
1146 | * |
||
1147 | * @author Naoki Sawada |
||
1148 | **/ |
||
1149 | protected function _fclose($fp, $path = '') |
||
1153 | |||
1154 | /******************** file/dir manipulations *************************/ |
||
1155 | |||
1156 | /** |
||
1157 | * Create dir and return created dir path or false on failed. |
||
1158 | * |
||
1159 | * @param string $path parent dir path |
||
1160 | * @param string $name new directory name |
||
1161 | * |
||
1162 | * @return string|bool |
||
1163 | * |
||
1164 | * @author Naoki Sawada |
||
1165 | **/ |
||
1166 | View Code Duplication | protected function _mkdir($path, $name) |
|
1174 | |||
1175 | /** |
||
1176 | * Create file and return it's path or false on failed. |
||
1177 | * |
||
1178 | * @param string $path parent dir path |
||
1179 | * @param string $name new file name |
||
1180 | * |
||
1181 | * @return string|bool |
||
1182 | * |
||
1183 | * @author Naoki Sawada |
||
1184 | **/ |
||
1185 | protected function _mkfile($path, $name) |
||
1189 | |||
1190 | /** |
||
1191 | * Create symlink. FTP driver does not support symlinks. |
||
1192 | * |
||
1193 | * @param string $target link target |
||
1194 | * @param string $path symlink path |
||
1195 | * |
||
1196 | * @return bool |
||
1197 | * |
||
1198 | * @author Naoki Sawada |
||
1199 | **/ |
||
1200 | protected function _symlink($target, $path, $name) |
||
1204 | |||
1205 | /** |
||
1206 | * Copy file into another file. |
||
1207 | * |
||
1208 | * @param string $source source file path |
||
1209 | * @param string $targetDir target directory path |
||
1210 | * @param string $name new file name |
||
1211 | * |
||
1212 | * @return bool |
||
1213 | * |
||
1214 | * @author Naoki Sawada |
||
1215 | **/ |
||
1216 | View Code Duplication | protected function _copy($source, $targetDir, $name) |
|
1226 | |||
1227 | /** |
||
1228 | * Move file into another parent dir. |
||
1229 | * Return new file path or false. |
||
1230 | * |
||
1231 | * @param string $source source file path |
||
1232 | * @param string $target target dir path |
||
1233 | * @param string $name file name |
||
1234 | * |
||
1235 | * @return string|bool |
||
1236 | * |
||
1237 | * @author Naoki Sawada |
||
1238 | **/ |
||
1239 | View Code Duplication | protected function _move($source, $targetDir, $name) |
|
1247 | |||
1248 | /** |
||
1249 | * Remove file. |
||
1250 | * |
||
1251 | * @param string $path file path |
||
1252 | * |
||
1253 | * @return bool |
||
1254 | * |
||
1255 | * @author Naoki Sawada |
||
1256 | **/ |
||
1257 | protected function _unlink($path) |
||
1269 | |||
1270 | /** |
||
1271 | * Remove dir. |
||
1272 | * |
||
1273 | * @param string $path dir path |
||
1274 | * |
||
1275 | * @return bool |
||
1276 | * |
||
1277 | * @author Naoki Sawada |
||
1278 | **/ |
||
1279 | protected function _rmdir($path) |
||
1283 | |||
1284 | /** |
||
1285 | * Create new file and write into it from file pointer. |
||
1286 | * Return new file path or false on error. |
||
1287 | * |
||
1288 | * @param resource $fp file pointer |
||
1289 | * @param string $dir target dir path |
||
1290 | * @param string $name file name |
||
1291 | * @param array $stat file stat (required by some virtual fs) |
||
1292 | * |
||
1293 | * @return bool|string |
||
1294 | * |
||
1295 | * @author Naoki Sawada |
||
1296 | **/ |
||
1297 | protected function _save($fp, $path, $name, $stat) |
||
1325 | |||
1326 | /** |
||
1327 | * Get file contents. |
||
1328 | * |
||
1329 | * @param string $path file path |
||
1330 | * |
||
1331 | * @return string|false |
||
1332 | * |
||
1333 | * @author Naoki Sawada |
||
1334 | **/ |
||
1335 | protected function _getContents($path) |
||
1348 | |||
1349 | /** |
||
1350 | * Write a string to a file. |
||
1351 | * |
||
1352 | * @param string $path file path |
||
1353 | * @param string $content new file content |
||
1354 | * |
||
1355 | * @return bool |
||
1356 | * |
||
1357 | * @author Naoki Sawada |
||
1358 | **/ |
||
1359 | View Code Duplication | protected function _filePutContents($path, $content) |
|
1375 | |||
1376 | /** |
||
1377 | * Detect available archivers. |
||
1378 | **/ |
||
1379 | protected function _checkArchivers() |
||
1384 | |||
1385 | /** |
||
1386 | * chmod implementation. |
||
1387 | * |
||
1388 | * @return bool |
||
1389 | **/ |
||
1390 | protected function _chmod($path, $mode) |
||
1394 | |||
1395 | /** |
||
1396 | * Unpack archive. |
||
1397 | * |
||
1398 | * @param string $path archive path |
||
1399 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
1400 | * |
||
1401 | * @return true |
||
1402 | * |
||
1403 | * @author Dmitry (dio) Levashov |
||
1404 | * @author Alexey Sukhotin |
||
1405 | **/ |
||
1406 | protected function _unpack($path, $arc) |
||
1411 | |||
1412 | /** |
||
1413 | * Recursive symlinks search. |
||
1414 | * |
||
1415 | * @param string $path file/dir path |
||
1416 | * |
||
1417 | * @return bool |
||
1418 | * |
||
1419 | * @author Dmitry (dio) Levashov |
||
1420 | **/ |
||
1421 | protected function _findSymlinks($path) |
||
1425 | |||
1426 | /** |
||
1427 | * Extract files from archive. |
||
1428 | * |
||
1429 | * @param string $path archive path |
||
1430 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
1431 | * |
||
1432 | * @return true |
||
1433 | * |
||
1434 | * @author Dmitry (dio) Levashov, |
||
1435 | * @author Alexey Sukhotin |
||
1436 | **/ |
||
1437 | protected function _extract($path, $arc) |
||
1441 | |||
1442 | /** |
||
1443 | * Create archive and return its path. |
||
1444 | * |
||
1445 | * @param string $dir target dir |
||
1446 | * @param array $files files names list |
||
1447 | * @param string $name archive name |
||
1448 | * @param array $arc archiver options |
||
1449 | * |
||
1450 | * @return string|bool |
||
1451 | * |
||
1452 | * @author Dmitry (dio) Levashov, |
||
1453 | * @author Alexey Sukhotin |
||
1454 | **/ |
||
1455 | protected function _archive($dir, $files, $name, $arc) |
||
1459 | |||
1460 | /** |
||
1461 | * Get dat(Dropbox metadata) from Dropbox. |
||
1462 | * |
||
1463 | * @param string $path |
||
1464 | * |
||
1465 | * @return object Dropbox metadata |
||
1466 | */ |
||
1467 | private function _db_getFile($path, $fields = '') |
||
1485 | } // END class |
||
1486 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.