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 elFinderVolumeFTP 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 elFinderVolumeFTP, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class elFinderVolumeFTP 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 = 'f'; |
||
21 | |||
22 | /** |
||
23 | * FTP Connection Instance. |
||
24 | * |
||
25 | * @var ftp |
||
26 | **/ |
||
27 | protected $connect = null; |
||
28 | |||
29 | /** |
||
30 | * Directory for tmp files |
||
31 | * If not set driver will try to use tmbDir as tmpDir. |
||
32 | * |
||
33 | * @var string |
||
34 | **/ |
||
35 | protected $tmpPath = ''; |
||
36 | |||
37 | /** |
||
38 | * Last FTP error message. |
||
39 | * |
||
40 | * @var string |
||
41 | **/ |
||
42 | protected $ftpError = ''; |
||
43 | |||
44 | /** |
||
45 | * FTP server output list as ftp on linux. |
||
46 | * |
||
47 | * @var bool |
||
48 | **/ |
||
49 | protected $ftpOsUnix; |
||
50 | |||
51 | /** |
||
52 | * FTP LIST command option. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $ftpListOption = '-al'; |
||
57 | |||
58 | /** |
||
59 | * Is connected server Pure FTPd? |
||
60 | * |
||
61 | * @var bool |
||
62 | */ |
||
63 | protected $isPureFtpd = false; |
||
64 | |||
65 | /** |
||
66 | * Tmp folder path. |
||
67 | * |
||
68 | * @var string |
||
69 | **/ |
||
70 | protected $tmp = ''; |
||
71 | |||
72 | /** |
||
73 | * FTP command `MLST` support. |
||
74 | * |
||
75 | * @var bool |
||
76 | */ |
||
77 | private $MLSTsupprt = false; |
||
78 | |||
79 | /** |
||
80 | * Calling cacheDir() target path with non-MLST. |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | private $cacheDirTarget = ''; |
||
85 | |||
86 | /** |
||
87 | * Constructor |
||
88 | * Extend options with required fields. |
||
89 | * |
||
90 | * @author Dmitry (dio) Levashov |
||
91 | * @author Cem (DiscoFever) |
||
92 | */ |
||
93 | public function __construct() |
||
115 | |||
116 | /** |
||
117 | * Prepare |
||
118 | * Call from elFinder::netmout() before volume->mount(). |
||
119 | * |
||
120 | * @param $options |
||
121 | * @return array |
||
122 | * @author Naoki Sawada |
||
123 | */ |
||
124 | public function netmountPrepare($options) |
||
138 | |||
139 | /*********************************************************************/ |
||
140 | /* FS API */ |
||
141 | /*********************************************************************/ |
||
142 | |||
143 | /** |
||
144 | * Close opened connection. |
||
145 | * |
||
146 | * @return void |
||
147 | * @author Dmitry (dio) Levashov |
||
148 | **/ |
||
149 | public function umount() |
||
153 | |||
154 | /*********************************************************************/ |
||
155 | /* INIT AND CONFIGURE */ |
||
156 | /*********************************************************************/ |
||
157 | |||
158 | /** |
||
159 | * Prepare FTP connection |
||
160 | * Connect to remote server and check if credentials are correct, if so, store the connection id in $ftp_conn. |
||
161 | * |
||
162 | * @return bool |
||
163 | * @author Dmitry (dio) Levashov |
||
164 | * @author Cem (DiscoFever) |
||
165 | **/ |
||
166 | protected function init() |
||
217 | |||
218 | /** |
||
219 | * Configure after successfull mount. |
||
220 | * |
||
221 | * @return void |
||
222 | * @author Dmitry (dio) Levashov |
||
223 | **/ |
||
224 | protected function configure() |
||
254 | |||
255 | /** |
||
256 | * Connect to ftp server. |
||
257 | * |
||
258 | * @return bool |
||
259 | * @author Dmitry (dio) Levashov |
||
260 | **/ |
||
261 | protected function connect() |
||
317 | |||
318 | /** |
||
319 | * Call ftp_rawlist with option prefix. |
||
320 | * |
||
321 | * @param string $path |
||
322 | * @return array |
||
323 | */ |
||
324 | protected function ftpRawList($path) |
||
339 | |||
340 | /** |
||
341 | * Parse line from ftp_rawlist() output and return file stat (array). |
||
342 | * |
||
343 | * @param string $raw line from ftp_rawlist() output |
||
344 | * @param $base |
||
345 | * @param bool $nameOnly |
||
346 | * @return array |
||
347 | * @author Dmitry Levashov |
||
348 | */ |
||
349 | protected function parseRaw($raw, $base, $nameOnly = false) |
||
431 | |||
432 | /** |
||
433 | * Normalize MS-DOS style FTP LIST Raw line. |
||
434 | * |
||
435 | * @param string $raw line from FTP LIST (MS-DOS style) |
||
436 | * @return array |
||
437 | * @author Naoki Sawada |
||
438 | **/ |
||
439 | protected function normalizeRawWindows($raw) |
||
458 | |||
459 | /** |
||
460 | * Parse permissions string. Return array(read => true/false, write => true/false). |
||
461 | * |
||
462 | * @param string $perm permissions string |
||
463 | * @param string $user |
||
464 | * @return string |
||
465 | * @author Dmitry (dio) Levashov |
||
466 | */ |
||
467 | protected function parsePermissions($perm, $user = '') |
||
483 | |||
484 | /** |
||
485 | * Cache dir contents. |
||
486 | * |
||
487 | * @param string $path dir path |
||
488 | * @return void |
||
489 | * @author Dmitry Levashov |
||
490 | **/ |
||
491 | protected function cacheDir($path) |
||
571 | |||
572 | /** |
||
573 | * Return ftp transfer mode for file. |
||
574 | * |
||
575 | * @param string $path file path |
||
576 | * @return string |
||
577 | * @author Dmitry (dio) Levashov |
||
578 | **/ |
||
579 | protected function ftpMode($path) |
||
583 | |||
584 | /*********************** paths/urls *************************/ |
||
585 | |||
586 | /** |
||
587 | * Return parent directory path. |
||
588 | * |
||
589 | * @param string $path file path |
||
590 | * @return string |
||
591 | * @author Naoki Sawada |
||
592 | **/ |
||
593 | protected function _dirname($path) |
||
600 | |||
601 | /** |
||
602 | * Return file name. |
||
603 | * |
||
604 | * @param string $path file path |
||
605 | * @return string |
||
606 | * @author Naoki Sawada |
||
607 | **/ |
||
608 | protected function _basename($path) |
||
614 | |||
615 | /** |
||
616 | * Join dir name and file name and retur full path. |
||
617 | * |
||
618 | * @param string $dir |
||
619 | * @param string $name |
||
620 | * @return string |
||
621 | * @author Dmitry (dio) Levashov |
||
622 | **/ |
||
623 | protected function _joinPath($dir, $name) |
||
627 | |||
628 | /** |
||
629 | * Return normalized path, this works the same as os.path.normpath() in Python. |
||
630 | * |
||
631 | * @param string $path path |
||
632 | * @return string |
||
633 | * @author Troex Nevelin |
||
634 | **/ |
||
635 | protected function _normpath($path) |
||
681 | |||
682 | /** |
||
683 | * Return file path related to root dir. |
||
684 | * |
||
685 | * @param string $path file path |
||
686 | * @return string |
||
687 | * @author Dmitry (dio) Levashov |
||
688 | **/ |
||
689 | View Code Duplication | protected function _relpath($path) |
|
702 | |||
703 | /** |
||
704 | * Convert path related to root dir into real path. |
||
705 | * |
||
706 | * @param string $path file path |
||
707 | * @return string |
||
708 | * @author Dmitry (dio) Levashov |
||
709 | **/ |
||
710 | protected function _abspath($path) |
||
723 | |||
724 | /** |
||
725 | * Return fake path started from root dir. |
||
726 | * |
||
727 | * @param string $path file path |
||
728 | * @return string |
||
729 | * @author Dmitry (dio) Levashov |
||
730 | **/ |
||
731 | protected function _path($path) |
||
735 | |||
736 | /** |
||
737 | * Return true if $path is children of $parent. |
||
738 | * |
||
739 | * @param string $path path to check |
||
740 | * @param string $parent parent path |
||
741 | * @return bool |
||
742 | * @author Dmitry (dio) Levashov |
||
743 | **/ |
||
744 | protected function _inpath($path, $parent) |
||
748 | |||
749 | /***************** file stat ********************/ |
||
750 | |||
751 | /** |
||
752 | * Return stat for given path. |
||
753 | * Stat contains following fields: |
||
754 | * - (int) size file size in b. required |
||
755 | * - (int) ts file modification time in unix time. required |
||
756 | * - (string) mime mimetype. required for folders, others - optionally |
||
757 | * - (bool) read read permissions. required |
||
758 | * - (bool) write write permissions. required |
||
759 | * - (bool) locked is object locked. optionally |
||
760 | * - (bool) hidden is object hidden. optionally |
||
761 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
762 | * - (string) target for symlinks - link target path. optionally. |
||
763 | * |
||
764 | * If file does not exists - returns empty array or false. |
||
765 | * |
||
766 | * @param string $path file path |
||
767 | * @return array|false |
||
768 | * @author Dmitry (dio) Levashov |
||
769 | **/ |
||
770 | protected function _stat($path) |
||
936 | |||
937 | /** |
||
938 | * Return true if path is dir and has at least one childs directory. |
||
939 | * |
||
940 | * @param string $path dir path |
||
941 | * @return bool |
||
942 | * @author Dmitry (dio) Levashov |
||
943 | **/ |
||
944 | protected function _subdirs($path) |
||
962 | |||
963 | /** |
||
964 | * Return object width and height |
||
965 | * Ususaly used for images, but can be realize for video etc... |
||
966 | * |
||
967 | * @param string $path file path |
||
968 | * @param string $mime file mime type |
||
969 | * @return string|false |
||
970 | * @author Dmitry (dio) Levashov |
||
971 | **/ |
||
972 | protected function _dimensions($path, $mime) |
||
981 | |||
982 | /******************** file/dir content *********************/ |
||
983 | |||
984 | /** |
||
985 | * Return files list in directory. |
||
986 | * |
||
987 | * @param string $path dir path |
||
988 | * @return array |
||
989 | * @author Dmitry (dio) Levashov |
||
990 | * @author Cem (DiscoFever) |
||
991 | **/ |
||
992 | protected function _scandir($path) |
||
1004 | |||
1005 | /** |
||
1006 | * Open file and return file pointer. |
||
1007 | * |
||
1008 | * @param string $path file path |
||
1009 | * @param string $mode |
||
1010 | * @return false|resource |
||
1011 | * @internal param bool $write open file for writing |
||
1012 | * @author Dmitry (dio) Levashov |
||
1013 | */ |
||
1014 | protected function _fopen($path, $mode = 'rb') |
||
1045 | |||
1046 | /** |
||
1047 | * Close opened file. |
||
1048 | * |
||
1049 | * @param resource $fp file pointer |
||
1050 | * @param string $path |
||
1051 | * @return bool |
||
1052 | * @author Dmitry (dio) Levashov |
||
1053 | */ |
||
1054 | protected function _fclose($fp, $path = '') |
||
1061 | |||
1062 | /******************** file/dir manipulations *************************/ |
||
1063 | |||
1064 | /** |
||
1065 | * Create dir and return created dir path or false on failed. |
||
1066 | * |
||
1067 | * @param string $path parent dir path |
||
1068 | * @param string $name new directory name |
||
1069 | * @return string|bool |
||
1070 | * @author Dmitry (dio) Levashov |
||
1071 | **/ |
||
1072 | protected function _mkdir($path, $name) |
||
1083 | |||
1084 | /** |
||
1085 | * Create file and return it's path or false on failed. |
||
1086 | * |
||
1087 | * @param string $path parent dir path |
||
1088 | * @param string $name new file name |
||
1089 | * @return string|bool |
||
1090 | * @author Dmitry (dio) Levashov |
||
1091 | **/ |
||
1092 | protected function _mkfile($path, $name) |
||
1105 | |||
1106 | /** |
||
1107 | * Create symlink. FTP driver does not support symlinks. |
||
1108 | * |
||
1109 | * @param string $target link target |
||
1110 | * @param string $path symlink path |
||
1111 | * @param string $name |
||
1112 | * @return bool |
||
1113 | * @author Dmitry (dio) Levashov |
||
1114 | */ |
||
1115 | protected function _symlink($target, $path, $name) |
||
1119 | |||
1120 | /** |
||
1121 | * Copy file into another file. |
||
1122 | * |
||
1123 | * @param string $source source file path |
||
1124 | * @param string $targetDir target directory path |
||
1125 | * @param string $name new file name |
||
1126 | * @return bool |
||
1127 | * @author Dmitry (dio) Levashov |
||
1128 | **/ |
||
1129 | protected function _copy($source, $targetDir, $name) |
||
1146 | |||
1147 | /** |
||
1148 | * Move file into another parent dir. |
||
1149 | * Return new file path or false. |
||
1150 | * |
||
1151 | * @param string $source source file path |
||
1152 | * @param $targetDir |
||
1153 | * @param string $name file name |
||
1154 | * @return bool|string |
||
1155 | * @internal param string $target target dir path |
||
1156 | * @author Dmitry (dio) Levashov |
||
1157 | */ |
||
1158 | protected function _move($source, $targetDir, $name) |
||
1164 | |||
1165 | /** |
||
1166 | * Remove file. |
||
1167 | * |
||
1168 | * @param string $path file path |
||
1169 | * @return bool |
||
1170 | * @author Dmitry (dio) Levashov |
||
1171 | **/ |
||
1172 | protected function _unlink($path) |
||
1176 | |||
1177 | /** |
||
1178 | * Remove dir. |
||
1179 | * |
||
1180 | * @param string $path dir path |
||
1181 | * @return bool |
||
1182 | * @author Dmitry (dio) Levashov |
||
1183 | **/ |
||
1184 | protected function _rmdir($path) |
||
1188 | |||
1189 | /** |
||
1190 | * Create new file and write into it from file pointer. |
||
1191 | * Return new file path or false on error. |
||
1192 | * |
||
1193 | * @param resource $fp file pointer |
||
1194 | * @param string $dir target dir path |
||
1195 | * @param string $name file name |
||
1196 | * @param array $stat file stat (required by some virtual fs) |
||
1197 | * @return bool|string |
||
1198 | * @author Dmitry (dio) Levashov |
||
1199 | **/ |
||
1200 | protected function _save($fp, $dir, $name, $stat) |
||
1208 | |||
1209 | /** |
||
1210 | * Get file contents. |
||
1211 | * |
||
1212 | * @param string $path file path |
||
1213 | * @return string|false |
||
1214 | * @author Dmitry (dio) Levashov |
||
1215 | **/ |
||
1216 | protected function _getContents($path) |
||
1230 | |||
1231 | /** |
||
1232 | * Write a string to a file. |
||
1233 | * |
||
1234 | * @param string $path file path |
||
1235 | * @param string $content new file content |
||
1236 | * @return bool |
||
1237 | * @author Dmitry (dio) Levashov |
||
1238 | **/ |
||
1239 | protected function _filePutContents($path, $content) |
||
1257 | |||
1258 | /** |
||
1259 | * Detect available archivers. |
||
1260 | * |
||
1261 | * @return void |
||
1262 | **/ |
||
1263 | protected function _checkArchivers() |
||
1267 | |||
1268 | /** |
||
1269 | * chmod availability. |
||
1270 | * |
||
1271 | * @param string $path |
||
1272 | * @param string $mode |
||
1273 | * @return bool |
||
1274 | */ |
||
1275 | protected function _chmod($path, $mode) |
||
1281 | |||
1282 | /** |
||
1283 | * Recursive symlinks search. |
||
1284 | * |
||
1285 | * @param string $path file/dir path |
||
1286 | * @return bool |
||
1287 | * @author Dmitry (dio) Levashov |
||
1288 | **/ |
||
1289 | protected function _findSymlinks($path) |
||
1293 | |||
1294 | /** |
||
1295 | * Extract files from archive. |
||
1296 | * |
||
1297 | * @param string $path archive path |
||
1298 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
1299 | * @return true |
||
1300 | * @author Dmitry (dio) Levashov, |
||
1301 | * @author Alexey Sukhotin |
||
1302 | **/ |
||
1303 | protected function _extract($path, $arc) |
||
1457 | |||
1458 | /** |
||
1459 | * Create archive and return its path. |
||
1460 | * |
||
1461 | * @param string $dir target dir |
||
1462 | * @param array $files files names list |
||
1463 | * @param string $name archive name |
||
1464 | * @param array $arc archiver options |
||
1465 | * @return string|bool |
||
1466 | * @author Dmitry (dio) Levashov, |
||
1467 | * @author Alexey Sukhotin |
||
1468 | **/ |
||
1469 | protected function _archive($dir, $files, $name, $arc) |
||
1502 | |||
1503 | /** |
||
1504 | * Gets an array of absolute remote FTP paths of files and |
||
1505 | * folders in $remote_directory omitting symbolic links. |
||
1506 | * |
||
1507 | * @param $remote_directory string remote FTP path to scan for file and folders recursively |
||
1508 | * @param $targets array Array of target item. `null` is to get all of items |
||
1509 | * @return array of elements each of which is an array of two elements: |
||
1510 | * <ul> |
||
1511 | * <li>$item['path'] - absolute remote FTP path</li> |
||
1512 | * <li>$item['type'] - either 'f' for file or 'd' for directory</li> |
||
1513 | * </ul> |
||
1514 | */ |
||
1515 | protected function ftp_scan_dir($remote_directory, $targets = null) |
||
1557 | |||
1558 | /** |
||
1559 | * Create writable temporary directory and return path to it. |
||
1560 | * @return string path to the new temporary directory or false in case of error. |
||
1561 | */ |
||
1562 | private function tempDir() |
||
1585 | |||
1586 | /** |
||
1587 | * Downloads specified files from remote directory |
||
1588 | * if there is a directory among files it is downloaded recursively (omitting symbolic links). |
||
1589 | * |
||
1590 | * @param $remote_directory string remote FTP path to a source directory to download from. |
||
1591 | * @param array $files list of files to download from remote directory. |
||
1592 | * @param $dest_local_directory string destination folder to store downloaded files. |
||
1593 | * @return bool true on success and false on failure. |
||
1594 | */ |
||
1595 | private function ftp_download_files($remote_directory, array $files, $dest_local_directory) |
||
1626 | |||
1627 | /** |
||
1628 | * Delete local directory recursively. |
||
1629 | * @param $dirPath string to directory to be erased. |
||
1630 | * @return bool true on success and false on failure. |
||
1631 | */ |
||
1632 | private function deleteDir($dirPath) |
||
1663 | |||
1664 | /** |
||
1665 | * Returns array of strings containing all files and folders in the specified local directory. |
||
1666 | * @param $dir |
||
1667 | * @param $omitSymlinks |
||
1668 | * @param string $prefix |
||
1669 | * @return array array of files and folders names relative to the $path |
||
1670 | * or an empty array if the directory $path is empty, |
||
1671 | * <br /> |
||
1672 | * false if $path is not a directory or does not exist. |
||
1673 | * @throws Exception |
||
1674 | * @internal param string $path path to directory to scan. |
||
1675 | */ |
||
1676 | private static function listFilesInDirectory($dir, $omitSymlinks, $prefix = '') |
||
1710 | } // END class |
||
1711 |
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.