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 elFinderVolumeDropbox 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 elFinderVolumeDropbox, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class elFinderVolumeDropbox extends elFinderVolumeDriver |
||
12 | { |
||
13 | /** |
||
14 | * Driver id |
||
15 | * Must be started from letter and contains [a-z0-9] |
||
16 | * Used as part of volume id. |
||
17 | * |
||
18 | * @var string |
||
19 | **/ |
||
20 | protected $driverId = 'd'; |
||
21 | |||
22 | /** |
||
23 | * OAuth object. |
||
24 | * |
||
25 | * @var oauth |
||
26 | **/ |
||
27 | protected $oauth = null; |
||
28 | |||
29 | /** |
||
30 | * Dropbox object. |
||
31 | * |
||
32 | * @var dropbox |
||
33 | **/ |
||
34 | protected $dropbox = null; |
||
35 | |||
36 | /** |
||
37 | * Directory for meta data caches |
||
38 | * If not set driver not cache meta data. |
||
39 | * |
||
40 | * @var string |
||
41 | **/ |
||
42 | protected $metaCache = ''; |
||
43 | |||
44 | /** |
||
45 | * Last API error message. |
||
46 | * |
||
47 | * @var string |
||
48 | **/ |
||
49 | protected $apiError = ''; |
||
50 | |||
51 | /** |
||
52 | * Directory for tmp files |
||
53 | * If not set driver will try to use tmbDir as tmpDir. |
||
54 | * |
||
55 | * @var string |
||
56 | **/ |
||
57 | protected $tmp = ''; |
||
58 | |||
59 | /** |
||
60 | * Dropbox.com uid. |
||
61 | * |
||
62 | * @var string |
||
63 | **/ |
||
64 | protected $dropboxUid = ''; |
||
65 | |||
66 | /** |
||
67 | * Dropbox download host, replaces 'www.dropbox.com' of shares URL. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | private $dropbox_dlhost = 'dl.dropboxusercontent.com'; |
||
72 | |||
73 | private $dropbox_phpFound = false; |
||
74 | |||
75 | private $DB_TableName = ''; |
||
76 | |||
77 | private $tmbPrefix = ''; |
||
78 | |||
79 | /** |
||
80 | * Constructor |
||
81 | * Extend options with required fields. |
||
82 | * |
||
83 | * @author Dmitry (dio) Levashov |
||
84 | * @author Cem (DiscoFever) |
||
85 | */ |
||
86 | public function __construct() |
||
126 | |||
127 | /** |
||
128 | * Prepare |
||
129 | * Call from elFinder::netmout() before volume->mount(). |
||
130 | * |
||
131 | * @param $options |
||
132 | * @return array |
||
133 | * @author Naoki Sawada |
||
134 | */ |
||
135 | public function netmountPrepare($options) |
||
239 | |||
240 | /** |
||
241 | * process of on netunmount |
||
242 | * Drop table `dropbox` & rm thumbs. |
||
243 | * |
||
244 | * @param $netVolumes |
||
245 | * @param $key |
||
246 | * @return bool |
||
247 | * @internal param array $options |
||
248 | */ |
||
249 | public function netunmount($netVolumes, $key) |
||
270 | |||
271 | /*********************************************************************/ |
||
272 | /* FS API */ |
||
273 | /*********************************************************************/ |
||
274 | |||
275 | /** |
||
276 | * Close opened connection. |
||
277 | * |
||
278 | * @return void |
||
279 | * @author Dmitry (dio) Levashov |
||
280 | **/ |
||
281 | public function umount() |
||
284 | |||
285 | /** |
||
286 | * Return content URL. |
||
287 | * |
||
288 | * @param string $hash file hash |
||
289 | * @param array $options options |
||
290 | * @return array |
||
291 | * @author Naoki Sawada |
||
292 | **/ |
||
293 | public function getContentUrl($hash, $options = []) |
||
333 | |||
334 | /*********************************************************************/ |
||
335 | /* INIT AND CONFIGURE */ |
||
336 | /*********************************************************************/ |
||
337 | |||
338 | /** |
||
339 | * Prepare FTP connection |
||
340 | * Connect to remote server and check if credentials are correct, if so, store the connection id in $ftp_conn. |
||
341 | * |
||
342 | * @return bool |
||
343 | * @author Dmitry (dio) Levashov |
||
344 | * @author Cem (DiscoFever) |
||
345 | **/ |
||
346 | protected function init() |
||
484 | |||
485 | /** |
||
486 | * Configure after successful mount. |
||
487 | * |
||
488 | * @return string |
||
489 | * @author Dmitry (dio) Levashov |
||
490 | **/ |
||
491 | protected function configure() |
||
498 | |||
499 | /** |
||
500 | * Get delta data and DB update. |
||
501 | * |
||
502 | * @param bool $refresh force refresh |
||
503 | * @return true|string error message |
||
504 | */ |
||
505 | protected function deltaCheck($refresh = true) |
||
605 | |||
606 | /** |
||
607 | * Parse line from dropbox metadata output and return file stat (array). |
||
608 | * |
||
609 | * @param string $raw line from ftp_rawlist() output |
||
610 | * @return array |
||
611 | * @author Dmitry Levashov |
||
612 | **/ |
||
613 | protected function parseRaw($raw) |
||
642 | |||
643 | /** |
||
644 | * Cache dir contents. |
||
645 | * |
||
646 | * @param string $path dir path |
||
647 | * @return string |
||
648 | * @author Dmitry Levashov |
||
649 | **/ |
||
650 | protected function cacheDir($path) |
||
678 | |||
679 | /** |
||
680 | * Recursive files search. |
||
681 | * |
||
682 | * @param string $path dir path |
||
683 | * @param string $q search string |
||
684 | * @param array $mimes |
||
685 | * @return array |
||
686 | * @author Naoki Sawada |
||
687 | **/ |
||
688 | protected function doSearch($path, $q, $mimes) |
||
720 | |||
721 | /** |
||
722 | * Copy file/recursive copy dir only in current volume. |
||
723 | * Return new file path or false. |
||
724 | * |
||
725 | * @param string $src source path |
||
726 | * @param string $dst destination dir path |
||
727 | * @param string $name new file name (optionaly) |
||
728 | * @return string|false |
||
729 | * @author Dmitry (dio) Levashov |
||
730 | * @author Naoki Sawada |
||
731 | **/ |
||
732 | protected function copy($src, $dst, $name) |
||
740 | |||
741 | /** |
||
742 | * Remove file/ recursive remove dir. |
||
743 | * |
||
744 | * @param string $path file path |
||
745 | * @param bool $force try to remove even if file locked |
||
746 | * @param bool $recursive |
||
747 | * @return bool |
||
748 | * @author Dmitry (dio) Levashov |
||
749 | * @author Naoki Sawada |
||
750 | */ |
||
751 | View Code Duplication | protected function remove($path, $force = false, $recursive = false) |
|
780 | |||
781 | /** |
||
782 | * Create thumnbnail and return it's URL on success. |
||
783 | * |
||
784 | * @param string $path file path |
||
785 | * @param $stat |
||
786 | * @return false|string |
||
787 | * @internal param string $mime file mime type |
||
788 | * @author Dmitry (dio) Levashov |
||
789 | * @author Naoki Sawada |
||
790 | */ |
||
791 | View Code Duplication | protected function createTmb($path, $stat) |
|
847 | |||
848 | /** |
||
849 | * Return thumbnail file name for required file. |
||
850 | * |
||
851 | * @param array $stat file stat |
||
852 | * @return string |
||
853 | * @author Dmitry (dio) Levashov |
||
854 | **/ |
||
855 | protected function tmbname($stat) |
||
859 | |||
860 | /** |
||
861 | * Get thumbnail from dropbox.com. |
||
862 | * @param string $path |
||
863 | * @param string $size |
||
864 | * @return string | boolean |
||
865 | */ |
||
866 | protected function getThumbnail($path, $size = 'small') |
||
874 | |||
875 | /*********************** paths/urls *************************/ |
||
876 | |||
877 | /** |
||
878 | * Return parent directory path. |
||
879 | * |
||
880 | * @param string $path file path |
||
881 | * @return string |
||
882 | * @author Dmitry (dio) Levashov |
||
883 | **/ |
||
884 | protected function _dirname($path) |
||
888 | |||
889 | /** |
||
890 | * Return file name. |
||
891 | * |
||
892 | * @param string $path file path |
||
893 | * @return string |
||
894 | * @author Dmitry (dio) Levashov |
||
895 | **/ |
||
896 | protected function _basename($path) |
||
900 | |||
901 | /** |
||
902 | * Join dir name and file name and retur full path. |
||
903 | * |
||
904 | * @param string $dir |
||
905 | * @param string $name |
||
906 | * @return string |
||
907 | * @author Dmitry (dio) Levashov |
||
908 | **/ |
||
909 | protected function _joinPath($dir, $name) |
||
913 | |||
914 | /** |
||
915 | * Return normalized path, this works the same as os.path.normpath() in Python. |
||
916 | * |
||
917 | * @param string $path path |
||
918 | * @return string |
||
919 | * @author Troex Nevelin |
||
920 | **/ |
||
921 | protected function _normpath($path) |
||
927 | |||
928 | /** |
||
929 | * Return file path related to root dir. |
||
930 | * |
||
931 | * @param string $path file path |
||
932 | * @return string |
||
933 | * @author Dmitry (dio) Levashov |
||
934 | **/ |
||
935 | protected function _relpath($path) |
||
939 | |||
940 | /** |
||
941 | * Convert path related to root dir into real path. |
||
942 | * |
||
943 | * @param string $path file path |
||
944 | * @return string |
||
945 | * @author Dmitry (dio) Levashov |
||
946 | **/ |
||
947 | protected function _abspath($path) |
||
951 | |||
952 | /** |
||
953 | * Return fake path started from root dir. |
||
954 | * |
||
955 | * @param string $path file path |
||
956 | * @return string |
||
957 | * @author Dmitry (dio) Levashov |
||
958 | **/ |
||
959 | protected function _path($path) |
||
963 | |||
964 | /** |
||
965 | * Return true if $path is children of $parent. |
||
966 | * |
||
967 | * @param string $path path to check |
||
968 | * @param string $parent parent path |
||
969 | * @return bool |
||
970 | * @author Dmitry (dio) Levashov |
||
971 | **/ |
||
972 | protected function _inpath($path, $parent) |
||
976 | |||
977 | /***************** file stat ********************/ |
||
978 | |||
979 | /** |
||
980 | * Return stat for given path. |
||
981 | * Stat contains following fields: |
||
982 | * - (int) size file size in b. required |
||
983 | * - (int) ts file modification time in unix time. required |
||
984 | * - (string) mime mimetype. required for folders, others - optionally |
||
985 | * - (bool) read read permissions. required |
||
986 | * - (bool) write write permissions. required |
||
987 | * - (bool) locked is object locked. optionally |
||
988 | * - (bool) hidden is object hidden. optionally |
||
989 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
990 | * - (string) target for symlinks - link target path. optionally. |
||
991 | * |
||
992 | * If file does not exists - returns empty array or false. |
||
993 | * |
||
994 | * @param string $path file path |
||
995 | * @return array|false |
||
996 | * @author Dmitry (dio) Levashov |
||
997 | **/ |
||
998 | protected function _stat($path) |
||
1010 | |||
1011 | /** |
||
1012 | * Return true if path is dir and has at least one childs directory. |
||
1013 | * |
||
1014 | * @param string $path dir path |
||
1015 | * @return bool |
||
1016 | * @author Dmitry (dio) Levashov |
||
1017 | **/ |
||
1018 | protected function _subdirs($path) |
||
1022 | |||
1023 | /** |
||
1024 | * Return object width and height |
||
1025 | * Ususaly used for images, but can be realize for video etc... |
||
1026 | * |
||
1027 | * @param string $path file path |
||
1028 | * @param string $mime file mime type |
||
1029 | * @return string |
||
1030 | * @author Dmitry (dio) Levashov |
||
1031 | **/ |
||
1032 | protected function _dimensions($path, $mime) |
||
1054 | |||
1055 | /******************** file/dir content *********************/ |
||
1056 | |||
1057 | /** |
||
1058 | * Return files list in directory. |
||
1059 | * |
||
1060 | * @param string $path dir path |
||
1061 | * @return array |
||
1062 | * @author Dmitry (dio) Levashov |
||
1063 | * @author Cem (DiscoFever) |
||
1064 | **/ |
||
1065 | protected function _scandir($path) |
||
1071 | |||
1072 | /** |
||
1073 | * Open file and return file pointer. |
||
1074 | * |
||
1075 | * @param string $path file path |
||
1076 | * @param string $mode |
||
1077 | * @return false|resource |
||
1078 | * @internal param bool $write open file for writing |
||
1079 | * @author Dmitry (dio) Levashov |
||
1080 | */ |
||
1081 | protected function _fopen($path, $mode = 'rb') |
||
1116 | |||
1117 | /** |
||
1118 | * Close opened file. |
||
1119 | * |
||
1120 | * @param resource $fp file pointer |
||
1121 | * @param string $path |
||
1122 | * @return bool |
||
1123 | * @author Dmitry (dio) Levashov |
||
1124 | */ |
||
1125 | protected function _fclose($fp, $path = '') |
||
1132 | |||
1133 | /******************** file/dir manipulations *************************/ |
||
1134 | |||
1135 | /** |
||
1136 | * Create dir and return created dir path or false on failed. |
||
1137 | * |
||
1138 | * @param string $path parent dir path |
||
1139 | * @param string $name new directory name |
||
1140 | * @return string|bool |
||
1141 | * @author Dmitry (dio) Levashov |
||
1142 | **/ |
||
1143 | protected function _mkdir($path, $name) |
||
1160 | |||
1161 | /** |
||
1162 | * Create file and return it's path or false on failed. |
||
1163 | * |
||
1164 | * @param string $path parent dir path |
||
1165 | * @param string $name new file name |
||
1166 | * @return string|bool |
||
1167 | * @author Dmitry (dio) Levashov |
||
1168 | **/ |
||
1169 | protected function _mkfile($path, $name) |
||
1173 | |||
1174 | /** |
||
1175 | * Create symlink. FTP driver does not support symlinks. |
||
1176 | * |
||
1177 | * @param string $target link target |
||
1178 | * @param string $path symlink path |
||
1179 | * @param string $name |
||
1180 | * @return bool |
||
1181 | * @author Dmitry (dio) Levashov |
||
1182 | */ |
||
1183 | protected function _symlink($target, $path, $name) |
||
1187 | |||
1188 | /** |
||
1189 | * Copy file into another file. |
||
1190 | * |
||
1191 | * @param string $source source file path |
||
1192 | * @param string $targetDir target directory path |
||
1193 | * @param string $name new file name |
||
1194 | * @return bool |
||
1195 | * @author Dmitry (dio) Levashov |
||
1196 | **/ |
||
1197 | View Code Duplication | protected function _copy($source, $targetDir, $name) |
|
1209 | |||
1210 | /** |
||
1211 | * Move file into another parent dir. |
||
1212 | * Return new file path or false. |
||
1213 | * |
||
1214 | * @param string $source source file path |
||
1215 | * @param $targetDir |
||
1216 | * @param string $name file name |
||
1217 | * @return bool|string |
||
1218 | * @internal param string $target target dir path |
||
1219 | * @author Dmitry (dio) Levashov |
||
1220 | */ |
||
1221 | View Code Duplication | protected function _move($source, $targetDir, $name) |
|
1233 | |||
1234 | /** |
||
1235 | * Remove file. |
||
1236 | * |
||
1237 | * @param string $path file path |
||
1238 | * @return bool |
||
1239 | * @author Dmitry (dio) Levashov |
||
1240 | **/ |
||
1241 | protected function _unlink($path) |
||
1252 | |||
1253 | /** |
||
1254 | * Remove dir. |
||
1255 | * |
||
1256 | * @param string $path dir path |
||
1257 | * @return bool |
||
1258 | * @author Dmitry (dio) Levashov |
||
1259 | **/ |
||
1260 | protected function _rmdir($path) |
||
1264 | |||
1265 | /** |
||
1266 | * Create new file and write into it from file pointer. |
||
1267 | * Return new file path or false on error. |
||
1268 | * |
||
1269 | * @param resource $fp file pointer |
||
1270 | * @param string $path |
||
1271 | * @param string $name file name |
||
1272 | * @param array $stat file stat (required by some virtual fs) |
||
1273 | * @return bool|string |
||
1274 | * @internal param string $dir target dir path |
||
1275 | * @author Dmitry (dio) Levashov |
||
1276 | */ |
||
1277 | protected function _save($fp, $path, $name, $stat) |
||
1302 | |||
1303 | /** |
||
1304 | * Get file contents. |
||
1305 | * |
||
1306 | * @param string $path file path |
||
1307 | * @return string|false |
||
1308 | * @author Dmitry (dio) Levashov |
||
1309 | **/ |
||
1310 | protected function _getContents($path) |
||
1321 | |||
1322 | /** |
||
1323 | * Write a string to a file. |
||
1324 | * |
||
1325 | * @param string $path file path |
||
1326 | * @param string $content new file content |
||
1327 | * @return bool |
||
1328 | * @author Dmitry (dio) Levashov |
||
1329 | **/ |
||
1330 | View Code Duplication | protected function _filePutContents($path, $content) |
|
1346 | |||
1347 | /** |
||
1348 | * Detect available archivers. |
||
1349 | * |
||
1350 | * @return array |
||
1351 | **/ |
||
1352 | protected function _checkArchivers() |
||
1357 | |||
1358 | /** |
||
1359 | * chmod implementation. |
||
1360 | * |
||
1361 | * @param string $path |
||
1362 | * @param string $mode |
||
1363 | * @return bool |
||
1364 | */ |
||
1365 | protected function _chmod($path, $mode) |
||
1369 | |||
1370 | /** |
||
1371 | * Unpack archive. |
||
1372 | * |
||
1373 | * @param string $path archive path |
||
1374 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
1375 | * @return true |
||
1376 | * @return void |
||
1377 | * @author Dmitry (dio) Levashov |
||
1378 | * @author Alexey Sukhotin |
||
1379 | **/ |
||
1380 | protected function _unpack($path, $arc) |
||
1384 | |||
1385 | /** |
||
1386 | * Recursive symlinks search. |
||
1387 | * |
||
1388 | * @param string $path file/dir path |
||
1389 | * @return bool |
||
1390 | * @author Dmitry (dio) Levashov |
||
1391 | **/ |
||
1392 | protected function _findSymlinks($path) |
||
1396 | |||
1397 | /** |
||
1398 | * Extract files from archive. |
||
1399 | * |
||
1400 | * @param string $path archive path |
||
1401 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
1402 | * @return true |
||
1403 | * @author Dmitry (dio) Levashov, |
||
1404 | * @author Alexey Sukhotin |
||
1405 | **/ |
||
1406 | protected function _extract($path, $arc) |
||
1410 | |||
1411 | /** |
||
1412 | * Create archive and return its path. |
||
1413 | * |
||
1414 | * @param string $dir target dir |
||
1415 | * @param array $files files names list |
||
1416 | * @param string $name archive name |
||
1417 | * @param array $arc archiver options |
||
1418 | * @return string|bool |
||
1419 | * @author Dmitry (dio) Levashov, |
||
1420 | * @author Alexey Sukhotin |
||
1421 | **/ |
||
1422 | protected function _archive($dir, $files, $name, $arc) |
||
1426 | |||
1427 | /** |
||
1428 | * Check DB for delta cache. |
||
1429 | * |
||
1430 | * @return bool |
||
1431 | */ |
||
1432 | private function checkDB() |
||
1456 | |||
1457 | /** |
||
1458 | * DB query and fetchAll. |
||
1459 | * |
||
1460 | * @param string $sql |
||
1461 | * @return bool|array |
||
1462 | */ |
||
1463 | private function query($sql) |
||
1473 | |||
1474 | /** |
||
1475 | * Get dat(dropbox metadata) from DB. |
||
1476 | * |
||
1477 | * @param string $path |
||
1478 | * @return array dropbox metadata |
||
1479 | */ |
||
1480 | private function getDBdat($path) |
||
1488 | |||
1489 | /** |
||
1490 | * Update DB dat(dropbox metadata). |
||
1491 | * |
||
1492 | * @param string $path |
||
1493 | * @param array $dat |
||
1494 | * @return bool|array |
||
1495 | */ |
||
1496 | private function updateDBdat($path, $dat) |
||
1502 | |||
1503 | /** |
||
1504 | * Get HTTP request response header string. |
||
1505 | * |
||
1506 | * @param string $url target URL |
||
1507 | * @return string |
||
1508 | * @author Naoki Sawada |
||
1509 | */ |
||
1510 | private function getHttpResponseHeader($url) |
||
1546 | } // END class |
||
1547 |
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.