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 | * @author Dmitry (dio) Levashov |
||
60 | */ |
||
61 | public function __construct() |
||
79 | |||
80 | /** |
||
81 | * Close connection. |
||
82 | * |
||
83 | * @return void |
||
84 | * @author Dmitry (dio) Levashov |
||
85 | **/ |
||
86 | public function umount() |
||
90 | |||
91 | /** |
||
92 | * Return debug info for client. |
||
93 | * |
||
94 | * @return array |
||
95 | * @author Dmitry (dio) Levashov |
||
96 | **/ |
||
97 | public function debug() |
||
107 | |||
108 | /*********************************************************************/ |
||
109 | /* INIT AND CONFIGURE */ |
||
110 | /*********************************************************************/ |
||
111 | |||
112 | /** |
||
113 | * Prepare driver before mount volume. |
||
114 | * Connect to db, check required tables and fetch root path. |
||
115 | * |
||
116 | * @return bool |
||
117 | * @author Dmitry (dio) Levashov |
||
118 | **/ |
||
119 | protected function init() |
||
154 | |||
155 | /** |
||
156 | * Set tmp path. |
||
157 | * |
||
158 | * @return void |
||
159 | * @author Dmitry (dio) Levashov |
||
160 | **/ |
||
161 | protected function configure() |
||
185 | |||
186 | /** |
||
187 | * Perform sql query and return result. |
||
188 | * Increase sqlCnt and save error if occured. |
||
189 | * |
||
190 | * @param string $sql query |
||
191 | * @return misc |
||
192 | * @author Dmitry (dio) Levashov |
||
193 | **/ |
||
194 | protected function query($sql) |
||
204 | |||
205 | /** |
||
206 | * Create empty object with required mimetype. |
||
207 | * |
||
208 | * @param string $path parent dir path |
||
209 | * @param string $name object name |
||
210 | * @param string $mime mime type |
||
211 | * @return bool |
||
212 | * @author Dmitry (dio) Levashov |
||
213 | **/ |
||
214 | protected function make($path, $name, $mime) |
||
221 | |||
222 | /*********************************************************************/ |
||
223 | /* FS API */ |
||
224 | /*********************************************************************/ |
||
225 | |||
226 | /** |
||
227 | * Cache dir contents. |
||
228 | * |
||
229 | * @param string $path dir path |
||
230 | * @return string |
||
231 | * @author Dmitry Levashov |
||
232 | **/ |
||
233 | protected function cacheDir($path) |
||
270 | |||
271 | /** |
||
272 | * Return array of parents paths (ids). |
||
273 | * |
||
274 | * @param int $path file path (id) |
||
275 | * @return array |
||
276 | * @author Dmitry (dio) Levashov |
||
277 | **/ |
||
278 | protected function getParents($path) |
||
295 | |||
296 | /** |
||
297 | * Return correct file path for LOAD_FILE method. |
||
298 | * |
||
299 | * @param string $path file path (id) |
||
300 | * @return string |
||
301 | * @author Troex Nevelin |
||
302 | **/ |
||
303 | protected function loadFilePath($path) |
||
312 | |||
313 | /** |
||
314 | * Recursive files search. |
||
315 | * |
||
316 | * @param string $path dir path |
||
317 | * @param string $q search string |
||
318 | * @param array $mimes |
||
319 | * @return array |
||
320 | * @author Dmitry (dio) Levashov |
||
321 | **/ |
||
322 | protected function doSearch($path, $q, $mimes) |
||
403 | |||
404 | /*********************** paths/urls *************************/ |
||
405 | |||
406 | /** |
||
407 | * Return parent directory path. |
||
408 | * |
||
409 | * @param string $path file path |
||
410 | * @return string |
||
411 | * @author Dmitry (dio) Levashov |
||
412 | **/ |
||
413 | protected function _dirname($path) |
||
417 | |||
418 | /** |
||
419 | * Return file name. |
||
420 | * |
||
421 | * @param string $path file path |
||
422 | * @return string |
||
423 | * @author Dmitry (dio) Levashov |
||
424 | **/ |
||
425 | protected function _basename($path) |
||
429 | |||
430 | /** |
||
431 | * Join dir name and file name and return full path. |
||
432 | * |
||
433 | * @param string $dir |
||
434 | * @param string $name |
||
435 | * @return string |
||
436 | * @author Dmitry (dio) Levashov |
||
437 | **/ |
||
438 | protected function _joinPath($dir, $name) |
||
450 | |||
451 | /** |
||
452 | * Return normalized path, this works the same as os.path.normpath() in Python. |
||
453 | * |
||
454 | * @param string $path path |
||
455 | * @return string |
||
456 | * @author Troex Nevelin |
||
457 | **/ |
||
458 | protected function _normpath($path) |
||
462 | |||
463 | /** |
||
464 | * Return file path related to root dir. |
||
465 | * |
||
466 | * @param string $path file path |
||
467 | * @return string |
||
468 | * @author Dmitry (dio) Levashov |
||
469 | **/ |
||
470 | protected function _relpath($path) |
||
474 | |||
475 | /** |
||
476 | * Convert path related to root dir into real path. |
||
477 | * |
||
478 | * @param string $path file path |
||
479 | * @return string |
||
480 | * @author Dmitry (dio) Levashov |
||
481 | **/ |
||
482 | protected function _abspath($path) |
||
486 | |||
487 | /** |
||
488 | * Return fake path started from root dir. |
||
489 | * |
||
490 | * @param string $path file path |
||
491 | * @return string |
||
492 | * @author Dmitry (dio) Levashov |
||
493 | **/ |
||
494 | protected function _path($path) |
||
509 | |||
510 | /** |
||
511 | * Return true if $path is children of $parent. |
||
512 | * |
||
513 | * @param string $path path to check |
||
514 | * @param string $parent parent path |
||
515 | * @return bool |
||
516 | * @author Dmitry (dio) Levashov |
||
517 | **/ |
||
518 | protected function _inpath($path, $parent) |
||
524 | |||
525 | /***************** file stat ********************/ |
||
526 | |||
527 | /** |
||
528 | * Return stat for given path. |
||
529 | * Stat contains following fields: |
||
530 | * - (int) size file size in b. required |
||
531 | * - (int) ts file modification time in unix time. required |
||
532 | * - (string) mime mimetype. required for folders, others - optionally |
||
533 | * - (bool) read read permissions. required |
||
534 | * - (bool) write write permissions. required |
||
535 | * - (bool) locked is object locked. optionally |
||
536 | * - (bool) hidden is object hidden. optionally |
||
537 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
538 | * - (string) target for symlinks - link target path. optionally. |
||
539 | * |
||
540 | * If file does not exists - returns empty array or false. |
||
541 | * |
||
542 | * @param string $path file path |
||
543 | * @return array|false |
||
544 | * @author Dmitry (dio) Levashov |
||
545 | **/ |
||
546 | protected function _stat($path) |
||
580 | |||
581 | /** |
||
582 | * Return true if path is dir and has at least one childs directory. |
||
583 | * |
||
584 | * @param string $path dir path |
||
585 | * @return bool |
||
586 | * @author Dmitry (dio) Levashov |
||
587 | **/ |
||
588 | protected function _subdirs($path) |
||
592 | |||
593 | /** |
||
594 | * Return object width and height |
||
595 | * Usualy used for images, but can be realize for video etc... |
||
596 | * |
||
597 | * @param string $path file path |
||
598 | * @param string $mime file mime type |
||
599 | * @return string |
||
600 | * @author Dmitry (dio) Levashov |
||
601 | **/ |
||
602 | protected function _dimensions($path, $mime) |
||
606 | |||
607 | /******************** file/dir content *********************/ |
||
608 | |||
609 | /** |
||
610 | * Return files list in directory. |
||
611 | * |
||
612 | * @param string $path dir path |
||
613 | * @return array |
||
614 | * @author Dmitry (dio) Levashov |
||
615 | **/ |
||
616 | protected function _scandir($path) |
||
622 | |||
623 | /** |
||
624 | * Open file and return file pointer. |
||
625 | * |
||
626 | * @param string $path file path |
||
627 | * @param string $mode open file mode (ignored in this driver) |
||
628 | * @return resource|false |
||
629 | * @author Dmitry (dio) Levashov |
||
630 | **/ |
||
631 | protected function _fopen($path, $mode = 'rb') |
||
651 | |||
652 | /** |
||
653 | * Close opened file. |
||
654 | * |
||
655 | * @param resource $fp file pointer |
||
656 | * @param string $path |
||
657 | * @return bool |
||
658 | * @author Dmitry (dio) Levashov |
||
659 | */ |
||
660 | protected function _fclose($fp, $path = '') |
||
667 | |||
668 | /******************** file/dir manipulations *************************/ |
||
669 | |||
670 | /** |
||
671 | * Create dir and return created dir path or false on failed. |
||
672 | * |
||
673 | * @param string $path parent dir path |
||
674 | * @param string $name new directory name |
||
675 | * @return string|bool |
||
676 | * @author Dmitry (dio) Levashov |
||
677 | **/ |
||
678 | protected function _mkdir($path, $name) |
||
682 | |||
683 | /** |
||
684 | * Create file and return it's path or false on failed. |
||
685 | * |
||
686 | * @param string $path parent dir path |
||
687 | * @param string $name new file name |
||
688 | * @return string|bool |
||
689 | * @author Dmitry (dio) Levashov |
||
690 | **/ |
||
691 | protected function _mkfile($path, $name) |
||
695 | |||
696 | /** |
||
697 | * Create symlink. FTP driver does not support symlinks. |
||
698 | * |
||
699 | * @param string $target link target |
||
700 | * @param string $path symlink path |
||
701 | * @param string $name |
||
702 | * @return bool |
||
703 | * @author Dmitry (dio) Levashov |
||
704 | */ |
||
705 | protected function _symlink($target, $path, $name) |
||
709 | |||
710 | /** |
||
711 | * Copy file into another file. |
||
712 | * |
||
713 | * @param string $source source file path |
||
714 | * @param string $targetDir target directory path |
||
715 | * @param string $name new file name |
||
716 | * @return bool |
||
717 | * @author Dmitry (dio) Levashov |
||
718 | **/ |
||
719 | protected function _copy($source, $targetDir, $name) |
||
730 | |||
731 | /** |
||
732 | * Move file into another parent dir. |
||
733 | * Return new file path or false. |
||
734 | * |
||
735 | * @param string $source source file path |
||
736 | * @param $targetDir |
||
737 | * @param string $name file name |
||
738 | * @return bool|string |
||
739 | * @internal param string $target target dir path |
||
740 | * @author Dmitry (dio) Levashov |
||
741 | */ |
||
742 | protected function _move($source, $targetDir, $name) |
||
749 | |||
750 | /** |
||
751 | * Remove file. |
||
752 | * |
||
753 | * @param string $path file path |
||
754 | * @return bool |
||
755 | * @author Dmitry (dio) Levashov |
||
756 | **/ |
||
757 | protected function _unlink($path) |
||
761 | |||
762 | /** |
||
763 | * Remove dir. |
||
764 | * |
||
765 | * @param string $path dir path |
||
766 | * @return bool |
||
767 | * @author Dmitry (dio) Levashov |
||
768 | **/ |
||
769 | protected function _rmdir($path) |
||
773 | |||
774 | /** |
||
775 | * undocumented function. |
||
776 | * |
||
777 | * @param $path |
||
778 | * @param $fp |
||
779 | * @author Dmitry Levashov |
||
780 | */ |
||
781 | protected function _setContent($path, $fp) |
||
787 | |||
788 | /** |
||
789 | * Create new file and write into it from file pointer. |
||
790 | * Return new file path or false on error. |
||
791 | * |
||
792 | * @param resource $fp file pointer |
||
793 | * @param string $dir target dir path |
||
794 | * @param string $name file name |
||
795 | * @param array $stat file stat (required by some virtual fs) |
||
796 | * @return bool|string |
||
797 | * @author Dmitry (dio) Levashov |
||
798 | **/ |
||
799 | protected function _save($fp, $dir, $name, $stat) |
||
855 | |||
856 | /** |
||
857 | * Get file contents. |
||
858 | * |
||
859 | * @param string $path file path |
||
860 | * @return string|false |
||
861 | * @author Dmitry (dio) Levashov |
||
862 | **/ |
||
863 | protected function _getContents($path) |
||
867 | |||
868 | /** |
||
869 | * Write a string to a file. |
||
870 | * |
||
871 | * @param string $path file path |
||
872 | * @param string $content new file content |
||
873 | * @return bool |
||
874 | * @author Dmitry (dio) Levashov |
||
875 | **/ |
||
876 | protected function _filePutContents($path, $content) |
||
880 | |||
881 | /** |
||
882 | * Detect available archivers. |
||
883 | * |
||
884 | * @return void |
||
885 | **/ |
||
886 | protected function _checkArchivers() |
||
889 | |||
890 | /** |
||
891 | * chmod implementation. |
||
892 | * |
||
893 | * @param string $path |
||
894 | * @param string $mode |
||
895 | * @return bool |
||
896 | */ |
||
897 | protected function _chmod($path, $mode) |
||
901 | |||
902 | /** |
||
903 | * Unpack archive. |
||
904 | * |
||
905 | * @param string $path archive path |
||
906 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
907 | * @return void |
||
908 | * @author Dmitry (dio) Levashov |
||
909 | * @author Alexey Sukhotin |
||
910 | **/ |
||
911 | protected function _unpack($path, $arc) |
||
914 | |||
915 | /** |
||
916 | * Recursive symlinks search. |
||
917 | * |
||
918 | * @param string $path file/dir path |
||
919 | * @return bool |
||
920 | * @author Dmitry (dio) Levashov |
||
921 | **/ |
||
922 | protected function _findSymlinks($path) |
||
926 | |||
927 | /** |
||
928 | * Extract files from archive. |
||
929 | * |
||
930 | * @param string $path archive path |
||
931 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
932 | * @return true |
||
933 | * @author Dmitry (dio) Levashov, |
||
934 | * @author Alexey Sukhotin |
||
935 | **/ |
||
936 | protected function _extract($path, $arc) |
||
940 | |||
941 | /** |
||
942 | * Create archive and return its path. |
||
943 | * |
||
944 | * @param string $dir target dir |
||
945 | * @param array $files files names list |
||
946 | * @param string $name archive name |
||
947 | * @param array $arc archiver options |
||
948 | * @return string|bool |
||
949 | * @author Dmitry (dio) Levashov, |
||
950 | * @author Alexey Sukhotin |
||
951 | **/ |
||
952 | protected function _archive($dir, $files, $name, $arc) |
||
956 | } // END class |
||
957 |
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.