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 elFinderVolumeMySQL 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 elFinderVolumeMySQL, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class elFinderVolumeMySQL extends elFinderVolumeDriver { |
||
|
|||
9 | |||
10 | /** |
||
11 | * Driver id |
||
12 | * Must be started from letter and contains [a-z0-9] |
||
13 | * Used as part of volume id |
||
14 | * |
||
15 | * @var string |
||
16 | **/ |
||
17 | protected $driverId = 'm'; |
||
18 | |||
19 | /** |
||
20 | * Database object |
||
21 | * |
||
22 | * @var mysqli |
||
23 | **/ |
||
24 | protected $db = null; |
||
25 | |||
26 | /** |
||
27 | * Tables to store files |
||
28 | * |
||
29 | * @var string |
||
30 | **/ |
||
31 | protected $tbf = ''; |
||
32 | |||
33 | /** |
||
34 | * Directory for tmp files |
||
35 | * If not set driver will try to use tmbDir as tmpDir |
||
36 | * |
||
37 | * @var string |
||
38 | **/ |
||
39 | protected $tmpPath = ''; |
||
40 | |||
41 | /** |
||
42 | * Numbers of sql requests (for debug) |
||
43 | * |
||
44 | * @var int |
||
45 | **/ |
||
46 | protected $sqlCnt = 0; |
||
47 | |||
48 | /** |
||
49 | * Last db error message |
||
50 | * |
||
51 | * @var string |
||
52 | **/ |
||
53 | protected $dbError = ''; |
||
54 | |||
55 | /** |
||
56 | * Constructor |
||
57 | * Extend options with required fields |
||
58 | * |
||
59 | * @return void |
||
60 | * @author Dmitry (dio) Levashov |
||
61 | **/ |
||
62 | public function __construct() { |
||
78 | |||
79 | /*********************************************************************/ |
||
80 | /* INIT AND CONFIGURE */ |
||
81 | /*********************************************************************/ |
||
82 | |||
83 | /** |
||
84 | * Prepare driver before mount volume. |
||
85 | * Connect to db, check required tables and fetch root path |
||
86 | * |
||
87 | * @return bool |
||
88 | * @author Dmitry (dio) Levashov |
||
89 | **/ |
||
90 | protected function init() { |
||
126 | |||
127 | |||
128 | |||
129 | /** |
||
130 | * Set tmp path |
||
131 | * |
||
132 | * @return void |
||
133 | * @author Dmitry (dio) Levashov |
||
134 | **/ |
||
135 | protected function configure() { |
||
154 | |||
155 | /** |
||
156 | * Close connection |
||
157 | * |
||
158 | * @return void |
||
159 | * @author Dmitry (dio) Levashov |
||
160 | **/ |
||
161 | public function umount() { |
||
164 | |||
165 | /** |
||
166 | * Return debug info for client |
||
167 | * |
||
168 | * @return array |
||
169 | * @author Dmitry (dio) Levashov |
||
170 | **/ |
||
171 | public function debug() { |
||
179 | |||
180 | /** |
||
181 | * Perform sql query and return result. |
||
182 | * Increase sqlCnt and save error if occured |
||
183 | * |
||
184 | * @param string $sql query |
||
185 | * @return misc |
||
186 | * @author Dmitry (dio) Levashov |
||
187 | **/ |
||
188 | protected function query($sql) { |
||
196 | |||
197 | /** |
||
198 | * Create empty object with required mimetype |
||
199 | * |
||
200 | * @param string $path parent dir path |
||
201 | * @param string $name object name |
||
202 | * @param string $mime mime type |
||
203 | * @return bool |
||
204 | * @author Dmitry (dio) Levashov |
||
205 | **/ |
||
206 | protected function make($path, $name, $mime) { |
||
212 | |||
213 | /*********************************************************************/ |
||
214 | /* FS API */ |
||
215 | /*********************************************************************/ |
||
216 | |||
217 | /** |
||
218 | * Cache dir contents |
||
219 | * |
||
220 | * @param string $path dir path |
||
221 | * @return void |
||
222 | * @author Dmitry Levashov |
||
223 | **/ |
||
224 | protected function cacheDir($path) { |
||
225 | $this->dirsCache[$path] = array(); |
||
226 | |||
227 | $sql = 'SELECT f.id, f.parent_id, f.name, f.size, f.mtime AS ts, f.mime, f.read, f.write, f.locked, f.hidden, f.width, f.height, IF(ch.id, 1, 0) AS dirs |
||
228 | FROM '.$this->tbf.' AS f |
||
229 | LEFT JOIN '.$this->tbf.' AS ch ON ch.parent_id=f.id AND ch.mime="directory" |
||
230 | WHERE f.parent_id="'.$path.'" |
||
231 | GROUP BY f.id'; |
||
232 | |||
233 | $res = $this->query($sql); |
||
234 | if ($res) { |
||
235 | while ($row = $res->fetch_assoc()) { |
||
236 | // debug($row); |
||
237 | $id = $row['id']; |
||
238 | if ($row['parent_id']) { |
||
239 | $row['phash'] = $this->encode($row['parent_id']); |
||
240 | } |
||
241 | |||
242 | View Code Duplication | if ($row['mime'] == 'directory') { |
|
243 | unset($row['width']); |
||
244 | unset($row['height']); |
||
245 | } else { |
||
246 | unset($row['dirs']); |
||
247 | } |
||
248 | |||
249 | unset($row['id']); |
||
250 | unset($row['parent_id']); |
||
251 | |||
252 | |||
253 | |||
254 | if (($stat = $this->updateCache($id, $row)) && empty($stat['hidden'])) { |
||
255 | $this->dirsCache[$path][] = $id; |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | |||
260 | return $this->dirsCache[$path]; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Return array of parents paths (ids) |
||
265 | * |
||
266 | * @param int $path file path (id) |
||
267 | * @return array |
||
268 | * @author Dmitry (dio) Levashov |
||
269 | **/ |
||
270 | protected function getParents($path) { |
||
285 | |||
286 | /** |
||
287 | * Return correct file path for LOAD_FILE method |
||
288 | * |
||
289 | * @param string $path file path (id) |
||
290 | * @return string |
||
291 | * @author Troex Nevelin |
||
292 | **/ |
||
293 | protected function loadFilePath($path) { |
||
300 | |||
301 | /** |
||
302 | * Recursive files search |
||
303 | * |
||
304 | * @param string $path dir path |
||
305 | * @param string $q search string |
||
306 | * @param array $mimes |
||
307 | * @return array |
||
308 | * @author Dmitry (dio) Levashov |
||
309 | **/ |
||
310 | protected function doSearch($path, $q, $mimes) { |
||
390 | |||
391 | |||
392 | /*********************** paths/urls *************************/ |
||
393 | |||
394 | /** |
||
395 | * Return parent directory path |
||
396 | * |
||
397 | * @param string $path file path |
||
398 | * @return string |
||
399 | * @author Dmitry (dio) Levashov |
||
400 | **/ |
||
401 | protected function _dirname($path) { |
||
404 | |||
405 | /** |
||
406 | * Return file name |
||
407 | * |
||
408 | * @param string $path file path |
||
409 | * @return string |
||
410 | * @author Dmitry (dio) Levashov |
||
411 | **/ |
||
412 | protected function _basename($path) { |
||
415 | |||
416 | /** |
||
417 | * Join dir name and file name and return full path |
||
418 | * |
||
419 | * @param string $dir |
||
420 | * @param string $name |
||
421 | * @return string |
||
422 | * @author Dmitry (dio) Levashov |
||
423 | **/ |
||
424 | protected function _joinPath($dir, $name) { |
||
433 | |||
434 | /** |
||
435 | * Return normalized path, this works the same as os.path.normpath() in Python |
||
436 | * |
||
437 | * @param string $path path |
||
438 | * @return string |
||
439 | * @author Troex Nevelin |
||
440 | **/ |
||
441 | protected function _normpath($path) { |
||
444 | |||
445 | /** |
||
446 | * Return file path related to root dir |
||
447 | * |
||
448 | * @param string $path file path |
||
449 | * @return string |
||
450 | * @author Dmitry (dio) Levashov |
||
451 | **/ |
||
452 | protected function _relpath($path) { |
||
455 | |||
456 | /** |
||
457 | * Convert path related to root dir into real path |
||
458 | * |
||
459 | * @param string $path file path |
||
460 | * @return string |
||
461 | * @author Dmitry (dio) Levashov |
||
462 | **/ |
||
463 | protected function _abspath($path) { |
||
466 | |||
467 | /** |
||
468 | * Return fake path started from root dir |
||
469 | * |
||
470 | * @param string $path file path |
||
471 | * @return string |
||
472 | * @author Dmitry (dio) Levashov |
||
473 | **/ |
||
474 | protected function _path($path) { |
||
487 | |||
488 | /** |
||
489 | * Return true if $path is children of $parent |
||
490 | * |
||
491 | * @param string $path path to check |
||
492 | * @param string $parent parent path |
||
493 | * @return bool |
||
494 | * @author Dmitry (dio) Levashov |
||
495 | **/ |
||
496 | protected function _inpath($path, $parent) { |
||
501 | |||
502 | /***************** file stat ********************/ |
||
503 | /** |
||
504 | * Return stat for given path. |
||
505 | * Stat contains following fields: |
||
506 | * - (int) size file size in b. required |
||
507 | * - (int) ts file modification time in unix time. required |
||
508 | * - (string) mime mimetype. required for folders, others - optionally |
||
509 | * - (bool) read read permissions. required |
||
510 | * - (bool) write write permissions. required |
||
511 | * - (bool) locked is object locked. optionally |
||
512 | * - (bool) hidden is object hidden. optionally |
||
513 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
514 | * - (string) target for symlinks - link target path. optionally |
||
515 | * |
||
516 | * If file does not exists - returns empty array or false. |
||
517 | * |
||
518 | * @param string $path file path |
||
519 | * @return array|false |
||
520 | * @author Dmitry (dio) Levashov |
||
521 | **/ |
||
522 | protected function _stat($path) { |
||
550 | |||
551 | /** |
||
552 | * Return true if path is dir and has at least one childs directory |
||
553 | * |
||
554 | * @param string $path dir path |
||
555 | * @return bool |
||
556 | * @author Dmitry (dio) Levashov |
||
557 | **/ |
||
558 | protected function _subdirs($path) { |
||
561 | |||
562 | /** |
||
563 | * Return object width and height |
||
564 | * Usualy used for images, but can be realize for video etc... |
||
565 | * |
||
566 | * @param string $path file path |
||
567 | * @param string $mime file mime type |
||
568 | * @return string |
||
569 | * @author Dmitry (dio) Levashov |
||
570 | **/ |
||
571 | protected function _dimensions($path, $mime) { |
||
574 | |||
575 | /******************** file/dir content *********************/ |
||
576 | |||
577 | /** |
||
578 | * Return files list in directory. |
||
579 | * |
||
580 | * @param string $path dir path |
||
581 | * @return array |
||
582 | * @author Dmitry (dio) Levashov |
||
583 | **/ |
||
584 | protected function _scandir($path) { |
||
589 | |||
590 | /** |
||
591 | * Open file and return file pointer |
||
592 | * |
||
593 | * @param string $path file path |
||
594 | * @param string $mode open file mode (ignored in this driver) |
||
595 | * @return resource|false |
||
596 | * @author Dmitry (dio) Levashov |
||
597 | **/ |
||
598 | protected function _fopen($path, $mode='rb') { |
||
617 | |||
618 | /** |
||
619 | * Close opened file |
||
620 | * |
||
621 | * @param resource $fp file pointer |
||
622 | * @return bool |
||
623 | * @author Dmitry (dio) Levashov |
||
624 | **/ |
||
625 | protected function _fclose($fp, $path='') { |
||
631 | |||
632 | /******************** file/dir manipulations *************************/ |
||
633 | |||
634 | /** |
||
635 | * Create dir and return created dir path or false on failed |
||
636 | * |
||
637 | * @param string $path parent dir path |
||
638 | * @param string $name new directory name |
||
639 | * @return string|bool |
||
640 | * @author Dmitry (dio) Levashov |
||
641 | **/ |
||
642 | protected function _mkdir($path, $name) { |
||
645 | |||
646 | /** |
||
647 | * Create file and return it's path or false on failed |
||
648 | * |
||
649 | * @param string $path parent dir path |
||
650 | * @param string $name new file name |
||
651 | * @return string|bool |
||
652 | * @author Dmitry (dio) Levashov |
||
653 | **/ |
||
654 | protected function _mkfile($path, $name) { |
||
657 | |||
658 | /** |
||
659 | * Create symlink. FTP driver does not support symlinks. |
||
660 | * |
||
661 | * @param string $target link target |
||
662 | * @param string $path symlink path |
||
663 | * @return bool |
||
664 | * @author Dmitry (dio) Levashov |
||
665 | **/ |
||
666 | protected function _symlink($target, $path, $name) { |
||
669 | |||
670 | /** |
||
671 | * Copy file into another file |
||
672 | * |
||
673 | * @param string $source source file path |
||
674 | * @param string $targetDir target directory path |
||
675 | * @param string $name new file name |
||
676 | * @return bool |
||
677 | * @author Dmitry (dio) Levashov |
||
678 | **/ |
||
679 | protected function _copy($source, $targetDir, $name) { |
||
689 | |||
690 | /** |
||
691 | * Move file into another parent dir. |
||
692 | * Return new file path or false. |
||
693 | * |
||
694 | * @param string $source source file path |
||
695 | * @param string $target target dir path |
||
696 | * @param string $name file name |
||
697 | * @return string|bool |
||
698 | * @author Dmitry (dio) Levashov |
||
699 | **/ |
||
700 | protected function _move($source, $targetDir, $name) { |
||
705 | |||
706 | /** |
||
707 | * Remove file |
||
708 | * |
||
709 | * @param string $path file path |
||
710 | * @return bool |
||
711 | * @author Dmitry (dio) Levashov |
||
712 | **/ |
||
713 | protected function _unlink($path) { |
||
716 | |||
717 | /** |
||
718 | * Remove dir |
||
719 | * |
||
720 | * @param string $path dir path |
||
721 | * @return bool |
||
722 | * @author Dmitry (dio) Levashov |
||
723 | **/ |
||
724 | protected function _rmdir($path) { |
||
727 | |||
728 | /** |
||
729 | * undocumented function |
||
730 | * |
||
731 | * @return void |
||
732 | * @author Dmitry Levashov |
||
733 | **/ |
||
734 | protected function _setContent($path, $fp) { |
||
741 | |||
742 | /** |
||
743 | * Create new file and write into it from file pointer. |
||
744 | * Return new file path or false on error. |
||
745 | * |
||
746 | * @param resource $fp file pointer |
||
747 | * @param string $dir target dir path |
||
748 | * @param string $name file name |
||
749 | * @param array $stat file stat (required by some virtual fs) |
||
750 | * @return bool|string |
||
751 | * @author Dmitry (dio) Levashov |
||
752 | **/ |
||
753 | protected function _save($fp, $dir, $name, $stat) { |
||
809 | |||
810 | /** |
||
811 | * Get file contents |
||
812 | * |
||
813 | * @param string $path file path |
||
814 | * @return string|false |
||
815 | * @author Dmitry (dio) Levashov |
||
816 | **/ |
||
817 | protected function _getContents($path) { |
||
820 | |||
821 | /** |
||
822 | * Write a string to a file |
||
823 | * |
||
824 | * @param string $path file path |
||
825 | * @param string $content new file content |
||
826 | * @return bool |
||
827 | * @author Dmitry (dio) Levashov |
||
828 | **/ |
||
829 | protected function _filePutContents($path, $content) { |
||
832 | |||
833 | /** |
||
834 | * Detect available archivers |
||
835 | * |
||
836 | * @return void |
||
837 | **/ |
||
838 | protected function _checkArchivers() { |
||
841 | |||
842 | /** |
||
843 | * chmod implementation |
||
844 | * |
||
845 | * @return bool |
||
846 | **/ |
||
847 | protected function _chmod($path, $mode) { |
||
850 | |||
851 | /** |
||
852 | * Unpack archive |
||
853 | * |
||
854 | * @param string $path archive path |
||
855 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
856 | * @return void |
||
857 | * @author Dmitry (dio) Levashov |
||
858 | * @author Alexey Sukhotin |
||
859 | **/ |
||
860 | protected function _unpack($path, $arc) { |
||
863 | |||
864 | /** |
||
865 | * Recursive symlinks search |
||
866 | * |
||
867 | * @param string $path file/dir path |
||
868 | * @return bool |
||
869 | * @author Dmitry (dio) Levashov |
||
870 | **/ |
||
871 | protected function _findSymlinks($path) { |
||
874 | |||
875 | /** |
||
876 | * Extract files from archive |
||
877 | * |
||
878 | * @param string $path archive path |
||
879 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
880 | * @return true |
||
881 | * @author Dmitry (dio) Levashov, |
||
882 | * @author Alexey Sukhotin |
||
883 | **/ |
||
884 | protected function _extract($path, $arc) { |
||
887 | |||
888 | /** |
||
889 | * Create archive and return its path |
||
890 | * |
||
891 | * @param string $dir target dir |
||
892 | * @param array $files files names list |
||
893 | * @param string $name archive name |
||
894 | * @param array $arc archiver options |
||
895 | * @return string|bool |
||
896 | * @author Dmitry (dio) Levashov, |
||
897 | * @author Alexey Sukhotin |
||
898 | **/ |
||
899 | protected function _archive($dir, $files, $name, $arc) { |
||
902 | |||
903 | } // END class |
||
904 |
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.