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() { |
||
77 | |||
78 | /*********************************************************************/ |
||
79 | /* INIT AND CONFIGURE */ |
||
80 | /*********************************************************************/ |
||
81 | |||
82 | /** |
||
83 | * Prepare driver before mount volume. |
||
84 | * Connect to db, check required tables and fetch root path |
||
85 | * |
||
86 | * @return bool |
||
87 | * @author Dmitry (dio) Levashov |
||
88 | **/ |
||
89 | protected function init() { |
||
125 | |||
126 | |||
127 | |||
128 | /** |
||
129 | * Set tmp path |
||
130 | * |
||
131 | * @return void |
||
132 | * @author Dmitry (dio) Levashov |
||
133 | **/ |
||
134 | protected function configure() { |
||
153 | |||
154 | /** |
||
155 | * Close connection |
||
156 | * |
||
157 | * @return void |
||
158 | * @author Dmitry (dio) Levashov |
||
159 | **/ |
||
160 | public function umount() { |
||
163 | |||
164 | /** |
||
165 | * Return debug info for client |
||
166 | * |
||
167 | * @return array |
||
168 | * @author Dmitry (dio) Levashov |
||
169 | **/ |
||
170 | public function debug() { |
||
178 | |||
179 | /** |
||
180 | * Perform sql query and return result. |
||
181 | * Increase sqlCnt and save error if occured |
||
182 | * |
||
183 | * @param string $sql query |
||
184 | * @return misc |
||
185 | * @author Dmitry (dio) Levashov |
||
186 | **/ |
||
187 | protected function query($sql) { |
||
195 | |||
196 | /** |
||
197 | * Create empty object with required mimetype |
||
198 | * |
||
199 | * @param string $path parent dir path |
||
200 | * @param string $name object name |
||
201 | * @param string $mime mime type |
||
202 | * @return bool |
||
203 | * @author Dmitry (dio) Levashov |
||
204 | **/ |
||
205 | protected function make($path, $name, $mime) { |
||
211 | |||
212 | /** |
||
213 | * Search files |
||
214 | * |
||
215 | * @param string $q search string |
||
216 | * @param array $mimes |
||
217 | * @return array |
||
218 | * @author Dmitry (dio) Levashov |
||
219 | **/ |
||
220 | public function search($q, $mimes) { |
||
258 | |||
259 | /** |
||
260 | * Return temporary file path for required file |
||
261 | * |
||
262 | * @param string $path file path |
||
263 | * @return string |
||
264 | * @author Dmitry (dio) Levashov |
||
265 | **/ |
||
266 | protected function tmpname($path) { |
||
269 | |||
270 | /** |
||
271 | * Resize image |
||
272 | * |
||
273 | * @param string $hash image file |
||
274 | * @param int $width new width |
||
275 | * @param int $height new height |
||
276 | * @param bool $crop crop image |
||
277 | * @return array|false |
||
278 | * @author Dmitry (dio) Levashov |
||
279 | * @author Alexey Sukhotin |
||
280 | **/ |
||
281 | public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0) { |
||
355 | |||
356 | |||
357 | /*********************************************************************/ |
||
358 | /* FS API */ |
||
359 | /*********************************************************************/ |
||
360 | |||
361 | /** |
||
362 | * Cache dir contents |
||
363 | * |
||
364 | * @param string $path dir path |
||
365 | * @return void |
||
366 | * @author Dmitry Levashov |
||
367 | **/ |
||
368 | protected function cacheDir($path) { |
||
406 | |||
407 | /** |
||
408 | * Return array of parents paths (ids) |
||
409 | * |
||
410 | * @param int $path file path (id) |
||
411 | * @return array |
||
412 | * @author Dmitry (dio) Levashov |
||
413 | **/ |
||
414 | protected function getParents($path) { |
||
429 | |||
430 | /** |
||
431 | * Return correct file path for LOAD_FILE method |
||
432 | * |
||
433 | * @param string $path file path (id) |
||
434 | * @return string |
||
435 | * @author Troex Nevelin |
||
436 | **/ |
||
437 | protected function loadFilePath($path) { |
||
444 | |||
445 | /** |
||
446 | * Recursive files search |
||
447 | * |
||
448 | * @param string $path dir path |
||
449 | * @param string $q search string |
||
450 | * @param array $mimes |
||
451 | * @return array |
||
452 | * @author Dmitry (dio) Levashov |
||
453 | **/ |
||
454 | protected function doSearch($path, $q, $mimes) { |
||
457 | |||
458 | |||
459 | /*********************** paths/urls *************************/ |
||
460 | |||
461 | /** |
||
462 | * Return parent directory path |
||
463 | * |
||
464 | * @param string $path file path |
||
465 | * @return string |
||
466 | * @author Dmitry (dio) Levashov |
||
467 | **/ |
||
468 | protected function _dirname($path) { |
||
471 | |||
472 | /** |
||
473 | * Return file name |
||
474 | * |
||
475 | * @param string $path file path |
||
476 | * @return string |
||
477 | * @author Dmitry (dio) Levashov |
||
478 | **/ |
||
479 | protected function _basename($path) { |
||
482 | |||
483 | /** |
||
484 | * Join dir name and file name and return full path |
||
485 | * |
||
486 | * @param string $dir |
||
487 | * @param string $name |
||
488 | * @return string |
||
489 | * @author Dmitry (dio) Levashov |
||
490 | **/ |
||
491 | protected function _joinPath($dir, $name) { |
||
500 | |||
501 | /** |
||
502 | * Return normalized path, this works the same as os.path.normpath() in Python |
||
503 | * |
||
504 | * @param string $path path |
||
505 | * @return string |
||
506 | * @author Troex Nevelin |
||
507 | **/ |
||
508 | protected function _normpath($path) { |
||
511 | |||
512 | /** |
||
513 | * Return file path related to root dir |
||
514 | * |
||
515 | * @param string $path file path |
||
516 | * @return string |
||
517 | * @author Dmitry (dio) Levashov |
||
518 | **/ |
||
519 | protected function _relpath($path) { |
||
522 | |||
523 | /** |
||
524 | * Convert path related to root dir into real path |
||
525 | * |
||
526 | * @param string $path file path |
||
527 | * @return string |
||
528 | * @author Dmitry (dio) Levashov |
||
529 | **/ |
||
530 | protected function _abspath($path) { |
||
533 | |||
534 | /** |
||
535 | * Return fake path started from root dir |
||
536 | * |
||
537 | * @param string $path file path |
||
538 | * @return string |
||
539 | * @author Dmitry (dio) Levashov |
||
540 | **/ |
||
541 | protected function _path($path) { |
||
554 | |||
555 | /** |
||
556 | * Return true if $path is children of $parent |
||
557 | * |
||
558 | * @param string $path path to check |
||
559 | * @param string $parent parent path |
||
560 | * @return bool |
||
561 | * @author Dmitry (dio) Levashov |
||
562 | **/ |
||
563 | protected function _inpath($path, $parent) { |
||
568 | |||
569 | /***************** file stat ********************/ |
||
570 | /** |
||
571 | * Return stat for given path. |
||
572 | * Stat contains following fields: |
||
573 | * - (int) size file size in b. required |
||
574 | * - (int) ts file modification time in unix time. required |
||
575 | * - (string) mime mimetype. required for folders, others - optionally |
||
576 | * - (bool) read read permissions. required |
||
577 | * - (bool) write write permissions. required |
||
578 | * - (bool) locked is object locked. optionally |
||
579 | * - (bool) hidden is object hidden. optionally |
||
580 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
581 | * - (string) target for symlinks - link target path. optionally |
||
582 | * |
||
583 | * If file does not exists - returns empty array or false. |
||
584 | * |
||
585 | * @param string $path file path |
||
586 | * @return array|false |
||
587 | * @author Dmitry (dio) Levashov |
||
588 | **/ |
||
589 | protected function _stat($path) { |
||
617 | |||
618 | /** |
||
619 | * Return true if path is dir and has at least one childs directory |
||
620 | * |
||
621 | * @param string $path dir path |
||
622 | * @return bool |
||
623 | * @author Dmitry (dio) Levashov |
||
624 | **/ |
||
625 | protected function _subdirs($path) { |
||
628 | |||
629 | /** |
||
630 | * Return object width and height |
||
631 | * Usualy used for images, but can be realize for video etc... |
||
632 | * |
||
633 | * @param string $path file path |
||
634 | * @param string $mime file mime type |
||
635 | * @return string |
||
636 | * @author Dmitry (dio) Levashov |
||
637 | **/ |
||
638 | protected function _dimensions($path, $mime) { |
||
641 | |||
642 | /******************** file/dir content *********************/ |
||
643 | |||
644 | /** |
||
645 | * Return files list in directory. |
||
646 | * |
||
647 | * @param string $path dir path |
||
648 | * @return array |
||
649 | * @author Dmitry (dio) Levashov |
||
650 | **/ |
||
651 | protected function _scandir($path) { |
||
656 | |||
657 | /** |
||
658 | * Open file and return file pointer |
||
659 | * |
||
660 | * @param string $path file path |
||
661 | * @param string $mode open file mode (ignored in this driver) |
||
662 | * @return resource|false |
||
663 | * @author Dmitry (dio) Levashov |
||
664 | **/ |
||
665 | protected function _fopen($path, $mode='rb') { |
||
684 | |||
685 | /** |
||
686 | * Close opened file |
||
687 | * |
||
688 | * @param resource $fp file pointer |
||
689 | * @return bool |
||
690 | * @author Dmitry (dio) Levashov |
||
691 | **/ |
||
692 | protected function _fclose($fp, $path='') { |
||
698 | |||
699 | /******************** file/dir manipulations *************************/ |
||
700 | |||
701 | /** |
||
702 | * Create dir and return created dir path or false on failed |
||
703 | * |
||
704 | * @param string $path parent dir path |
||
705 | * @param string $name new directory name |
||
706 | * @return string|bool |
||
707 | * @author Dmitry (dio) Levashov |
||
708 | **/ |
||
709 | protected function _mkdir($path, $name) { |
||
712 | |||
713 | /** |
||
714 | * Create file and return it's path or false on failed |
||
715 | * |
||
716 | * @param string $path parent dir path |
||
717 | * @param string $name new file name |
||
718 | * @return string|bool |
||
719 | * @author Dmitry (dio) Levashov |
||
720 | **/ |
||
721 | protected function _mkfile($path, $name) { |
||
724 | |||
725 | /** |
||
726 | * Create symlink. FTP driver does not support symlinks. |
||
727 | * |
||
728 | * @param string $target link target |
||
729 | * @param string $path symlink path |
||
730 | * @return bool |
||
731 | * @author Dmitry (dio) Levashov |
||
732 | **/ |
||
733 | protected function _symlink($target, $path, $name) { |
||
736 | |||
737 | /** |
||
738 | * Copy file into another file |
||
739 | * |
||
740 | * @param string $source source file path |
||
741 | * @param string $targetDir target directory path |
||
742 | * @param string $name new file name |
||
743 | * @return bool |
||
744 | * @author Dmitry (dio) Levashov |
||
745 | **/ |
||
746 | protected function _copy($source, $targetDir, $name) { |
||
756 | |||
757 | /** |
||
758 | * Move file into another parent dir. |
||
759 | * Return new file path or false. |
||
760 | * |
||
761 | * @param string $source source file path |
||
762 | * @param string $target target dir path |
||
763 | * @param string $name file name |
||
764 | * @return string|bool |
||
765 | * @author Dmitry (dio) Levashov |
||
766 | **/ |
||
767 | protected function _move($source, $targetDir, $name) { |
||
772 | |||
773 | /** |
||
774 | * Remove file |
||
775 | * |
||
776 | * @param string $path file path |
||
777 | * @return bool |
||
778 | * @author Dmitry (dio) Levashov |
||
779 | **/ |
||
780 | protected function _unlink($path) { |
||
783 | |||
784 | /** |
||
785 | * Remove dir |
||
786 | * |
||
787 | * @param string $path dir path |
||
788 | * @return bool |
||
789 | * @author Dmitry (dio) Levashov |
||
790 | **/ |
||
791 | protected function _rmdir($path) { |
||
794 | |||
795 | /** |
||
796 | * undocumented function |
||
797 | * |
||
798 | * @return void |
||
799 | * @author Dmitry Levashov |
||
800 | **/ |
||
801 | protected function _setContent($path, $fp) { |
||
808 | |||
809 | /** |
||
810 | * Create new file and write into it from file pointer. |
||
811 | * Return new file path or false on error. |
||
812 | * |
||
813 | * @param resource $fp file pointer |
||
814 | * @param string $dir target dir path |
||
815 | * @param string $name file name |
||
816 | * @param array $stat file stat (required by some virtual fs) |
||
817 | * @return bool|string |
||
818 | * @author Dmitry (dio) Levashov |
||
819 | **/ |
||
820 | protected function _save($fp, $dir, $name, $stat) { |
||
875 | |||
876 | /** |
||
877 | * Get file contents |
||
878 | * |
||
879 | * @param string $path file path |
||
880 | * @return string|false |
||
881 | * @author Dmitry (dio) Levashov |
||
882 | **/ |
||
883 | protected function _getContents($path) { |
||
886 | |||
887 | /** |
||
888 | * Write a string to a file |
||
889 | * |
||
890 | * @param string $path file path |
||
891 | * @param string $content new file content |
||
892 | * @return bool |
||
893 | * @author Dmitry (dio) Levashov |
||
894 | **/ |
||
895 | protected function _filePutContents($path, $content) { |
||
898 | |||
899 | /** |
||
900 | * Detect available archivers |
||
901 | * |
||
902 | * @return void |
||
903 | **/ |
||
904 | protected function _checkArchivers() { |
||
907 | |||
908 | /** |
||
909 | * Unpack archive |
||
910 | * |
||
911 | * @param string $path archive path |
||
912 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
913 | * @return void |
||
914 | * @author Dmitry (dio) Levashov |
||
915 | * @author Alexey Sukhotin |
||
916 | **/ |
||
917 | protected function _unpack($path, $arc) { |
||
920 | |||
921 | /** |
||
922 | * Recursive symlinks search |
||
923 | * |
||
924 | * @param string $path file/dir path |
||
925 | * @return bool |
||
926 | * @author Dmitry (dio) Levashov |
||
927 | **/ |
||
928 | protected function _findSymlinks($path) { |
||
931 | |||
932 | /** |
||
933 | * Extract files from archive |
||
934 | * |
||
935 | * @param string $path archive path |
||
936 | * @param array $arc archiver command and arguments (same as in $this->archivers) |
||
937 | * @return true |
||
938 | * @author Dmitry (dio) Levashov, |
||
939 | * @author Alexey Sukhotin |
||
940 | **/ |
||
941 | protected function _extract($path, $arc) { |
||
944 | |||
945 | /** |
||
946 | * Create archive and return its path |
||
947 | * |
||
948 | * @param string $dir target dir |
||
949 | * @param array $files files names list |
||
950 | * @param string $name archive name |
||
951 | * @param array $arc archiver options |
||
952 | * @return string|bool |
||
953 | * @author Dmitry (dio) Levashov, |
||
954 | * @author Alexey Sukhotin |
||
955 | **/ |
||
956 | protected function _archive($dir, $files, $name, $arc) { |
||
959 | |||
960 | } // END class |
||
961 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.