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 Filesystem 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 Filesystem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
71 | class Filesystem { |
||
72 | |||
73 | /** |
||
74 | * @var Mount\Manager $mounts |
||
75 | */ |
||
76 | private static $mounts; |
||
77 | |||
78 | public static $loaded = false; |
||
79 | /** |
||
80 | * @var \OC\Files\View $defaultInstance |
||
81 | */ |
||
82 | static private $defaultInstance; |
||
83 | |||
84 | static private $usersSetup = []; |
||
85 | |||
86 | static private $normalizedPathCache = null; |
||
87 | |||
88 | static private $listeningForProviders = false; |
||
89 | |||
90 | /** |
||
91 | * classname which used for hooks handling |
||
92 | * used as signalclass in OC_Hooks::emit() |
||
93 | */ |
||
94 | const CLASSNAME = 'OC_Filesystem'; |
||
95 | |||
96 | /** |
||
97 | * signalname emitted before file renaming |
||
98 | * |
||
99 | * @param string $oldpath |
||
100 | * @param string $newpath |
||
101 | */ |
||
102 | const signal_rename = 'rename'; |
||
103 | |||
104 | /** |
||
105 | * signal emitted after file renaming |
||
106 | * |
||
107 | * @param string $oldpath |
||
108 | * @param string $newpath |
||
109 | */ |
||
110 | const signal_post_rename = 'post_rename'; |
||
111 | |||
112 | /** |
||
113 | * signal emitted before file/dir creation |
||
114 | * |
||
115 | * @param string $path |
||
116 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
117 | */ |
||
118 | const signal_create = 'create'; |
||
119 | |||
120 | /** |
||
121 | * signal emitted after file/dir creation |
||
122 | * |
||
123 | * @param string $path |
||
124 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
125 | */ |
||
126 | const signal_post_create = 'post_create'; |
||
127 | |||
128 | /** |
||
129 | * signal emits before file/dir copy |
||
130 | * |
||
131 | * @param string $oldpath |
||
132 | * @param string $newpath |
||
133 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
134 | */ |
||
135 | const signal_copy = 'copy'; |
||
136 | |||
137 | /** |
||
138 | * signal emits after file/dir copy |
||
139 | * |
||
140 | * @param string $oldpath |
||
141 | * @param string $newpath |
||
142 | */ |
||
143 | const signal_post_copy = 'post_copy'; |
||
144 | |||
145 | /** |
||
146 | * signal emits before file/dir save |
||
147 | * |
||
148 | * @param string $path |
||
149 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
150 | */ |
||
151 | const signal_write = 'write'; |
||
152 | |||
153 | /** |
||
154 | * signal emits after file/dir save |
||
155 | * |
||
156 | * @param string $path |
||
157 | */ |
||
158 | const signal_post_write = 'post_write'; |
||
159 | |||
160 | /** |
||
161 | * signal emitted before file/dir update |
||
162 | * |
||
163 | * @param string $path |
||
164 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
165 | */ |
||
166 | const signal_update = 'update'; |
||
167 | |||
168 | /** |
||
169 | * signal emitted after file/dir update |
||
170 | * |
||
171 | * @param string $path |
||
172 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
173 | */ |
||
174 | const signal_post_update = 'post_update'; |
||
175 | |||
176 | /** |
||
177 | * signal emits when reading file/dir |
||
178 | * |
||
179 | * @param string $path |
||
180 | */ |
||
181 | const signal_read = 'read'; |
||
182 | |||
183 | /** |
||
184 | * signal emits when removing file/dir |
||
185 | * |
||
186 | * @param string $path |
||
187 | */ |
||
188 | const signal_delete = 'delete'; |
||
189 | |||
190 | /** |
||
191 | * parameters definitions for signals |
||
192 | */ |
||
193 | const signal_param_path = 'path'; |
||
194 | const signal_param_oldpath = 'oldpath'; |
||
195 | const signal_param_newpath = 'newpath'; |
||
196 | |||
197 | /** |
||
198 | * run - changing this flag to false in hook handler will cancel event |
||
199 | */ |
||
200 | const signal_param_run = 'run'; |
||
201 | |||
202 | const signal_create_mount = 'create_mount'; |
||
203 | const signal_delete_mount = 'delete_mount'; |
||
204 | const signal_param_mount_type = 'mounttype'; |
||
205 | const signal_param_users = 'users'; |
||
206 | |||
207 | /** |
||
208 | * @var \OC\Files\Storage\StorageFactory $loader |
||
209 | */ |
||
210 | private static $loader; |
||
211 | |||
212 | /** @var bool */ |
||
213 | private static $logWarningWhenAddingStorageWrapper = true; |
||
214 | |||
215 | /** |
||
216 | * @param bool $shouldLog |
||
217 | * @return bool previous value |
||
218 | * @internal |
||
219 | */ |
||
220 | public static function logWarningWhenAddingStorageWrapper($shouldLog) { |
||
225 | |||
226 | /** |
||
227 | * @param string $wrapperName |
||
228 | * @param callable $wrapper |
||
229 | * @param int $priority |
||
230 | */ |
||
231 | public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
||
245 | |||
246 | /** |
||
247 | * Returns the storage factory |
||
248 | * |
||
249 | * @return \OCP\Files\Storage\IStorageFactory |
||
250 | */ |
||
251 | public static function getLoader() { |
||
257 | |||
258 | /** |
||
259 | * Returns the mount manager |
||
260 | * |
||
261 | * @return \OC\Files\Mount\Manager |
||
262 | */ |
||
263 | public static function getMountManager($user = '') { |
||
269 | |||
270 | /** |
||
271 | * get the mountpoint of the storage object for a path |
||
272 | * ( note: because a storage is not always mounted inside the fakeroot, the |
||
273 | * returned mountpoint is relative to the absolute root of the filesystem |
||
274 | * and doesn't take the chroot into account ) |
||
275 | * |
||
276 | * @param string $path |
||
277 | * @return string |
||
278 | */ |
||
279 | static public function getMountPoint($path) { |
||
290 | |||
291 | /** |
||
292 | * get a list of all mount points in a directory |
||
293 | * |
||
294 | * @param string $path |
||
295 | * @return string[] |
||
296 | */ |
||
297 | static public function getMountPoints($path) { |
||
308 | |||
309 | /** |
||
310 | * get the storage mounted at $mountPoint |
||
311 | * |
||
312 | * @param string $mountPoint |
||
313 | * @return \OC\Files\Storage\Storage |
||
314 | */ |
||
315 | public static function getStorage($mountPoint) { |
||
322 | |||
323 | /** |
||
324 | * @param string $id |
||
325 | * @return Mount\MountPoint[] |
||
326 | */ |
||
327 | public static function getMountByStorageId($id) { |
||
333 | |||
334 | /** |
||
335 | * @param int $id |
||
336 | * @return Mount\MountPoint[] |
||
337 | */ |
||
338 | public static function getMountByNumericId($id) { |
||
344 | |||
345 | /** |
||
346 | * resolve a path to a storage and internal path |
||
347 | * |
||
348 | * @param string $path |
||
349 | * @return array an array consisting of the storage and the internal path |
||
350 | */ |
||
351 | static public function resolvePath($path) { |
||
362 | |||
363 | static public function init($user, $root) { |
||
381 | |||
382 | static public function initMountManager() { |
||
387 | |||
388 | /** |
||
389 | * Initialize system and personal mount points for a user |
||
390 | * |
||
391 | * @param string $user |
||
392 | * @throws \OC\User\NoUserException if the user is not available |
||
393 | */ |
||
394 | public static function initMountPoints($user = '') { |
||
454 | |||
455 | /** |
||
456 | * Get mounts from mount providers that are registered after setup |
||
457 | * |
||
458 | * @param MountProviderCollection $mountConfigManager |
||
459 | * @param IUserManager $userManager |
||
460 | */ |
||
461 | private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
||
475 | |||
476 | /** |
||
477 | * get the default filesystem view |
||
478 | * |
||
479 | * @return View |
||
480 | */ |
||
481 | static public function getView() { |
||
484 | |||
485 | /** |
||
486 | * tear down the filesystem, removing all storage providers |
||
487 | */ |
||
488 | static public function tearDown() { |
||
492 | |||
493 | /** |
||
494 | * get the relative path of the root data directory for the current user |
||
495 | * |
||
496 | * @return string |
||
497 | * |
||
498 | * Returns path like /admin/files |
||
499 | */ |
||
500 | static public function getRoot() { |
||
506 | |||
507 | /** |
||
508 | * clear all mounts and storage backends |
||
509 | */ |
||
510 | public static function clearMounts() { |
||
516 | |||
517 | /** |
||
518 | * mount an \OC\Files\Storage\Storage in our virtual filesystem |
||
519 | * |
||
520 | * @param \OC\Files\Storage\Storage|string $class |
||
521 | * @param array $arguments |
||
522 | * @param string $mountpoint |
||
523 | */ |
||
524 | static public function mount($class, $arguments, $mountpoint) { |
||
531 | |||
532 | /** |
||
533 | * return the path to a local version of the file |
||
534 | * we need this because we can't know if a file is stored local or not from |
||
535 | * outside the filestorage and for some purposes a local file is needed |
||
536 | * |
||
537 | * @param string $path |
||
538 | * @return string |
||
539 | */ |
||
540 | static public function getLocalFile($path) { |
||
543 | |||
544 | /** |
||
545 | * @param string $path |
||
546 | * @return string |
||
547 | */ |
||
548 | static public function getLocalFolder($path) { |
||
551 | |||
552 | /** |
||
553 | * return path to file which reflects one visible in browser |
||
554 | * |
||
555 | * @param string $path |
||
556 | * @return string |
||
557 | */ |
||
558 | static public function getLocalPath($path) { |
||
566 | |||
567 | /** |
||
568 | * check if the requested path is valid |
||
569 | * |
||
570 | * @param string $path |
||
571 | * @return bool |
||
572 | */ |
||
573 | static public function isValidPath($path) { |
||
583 | |||
584 | /** |
||
585 | * checks if a file is blacklisted for storage in the filesystem |
||
586 | * Listens to write and rename hooks |
||
587 | * |
||
588 | * @param array $data from hook |
||
589 | */ |
||
590 | static public function isForbiddenFileOrDir_Hook($data) { |
||
602 | |||
603 | /** |
||
604 | * depriciated, replaced by isForbiddenFileOrDir |
||
605 | * @param string $filename |
||
606 | * @return boolean |
||
607 | */ |
||
608 | static public function isFileBlacklisted($filename) { |
||
611 | |||
612 | /** |
||
613 | * check if the directory should be ignored when scanning |
||
614 | * NOTE: the special directories . and .. would cause never ending recursion |
||
615 | * |
||
616 | * @param String $dir |
||
617 | * @return boolean |
||
618 | */ |
||
619 | static public function isIgnoredDir($dir) { |
||
625 | |||
626 | /** |
||
627 | * Check if the directory path / file name contains a Blacklisted or Excluded name |
||
628 | * config.php parameter arrays can contain file names to be blacklisted or directory names to be excluded |
||
629 | * Blacklist ... files that may harm the owncloud environment like a foreign .htaccess file |
||
630 | * Excluded ... directories that are excluded from beeing further processed, like snapshot directories |
||
631 | * The parameter $ed is only used in conjunction with unit tests as we handover here the excluded |
||
632 | * directory name to be tested against. $ed and the query with can be redesigned if filesystem.php will get |
||
633 | * a constructor where it is then possible to define the excluded directory names for unit tests. |
||
634 | * @param string $FileOrDir |
||
635 | * @param array $ed |
||
636 | * @return boolean |
||
637 | */ |
||
638 | static public function isForbiddenFileOrDir($FileOrDir, $ed = []) { |
||
673 | |||
674 | /** |
||
675 | * following functions are equivalent to their php builtin equivalents for arguments/return values. |
||
676 | */ |
||
677 | static public function mkdir($path) { |
||
680 | |||
681 | static public function rmdir($path) { |
||
684 | |||
685 | static public function opendir($path) { |
||
688 | |||
689 | static public function is_dir($path) { |
||
692 | |||
693 | static public function is_file($path) { |
||
696 | |||
697 | static public function stat($path) { |
||
700 | |||
701 | static public function filetype($path) { |
||
704 | |||
705 | static public function filesize($path) { |
||
708 | |||
709 | static public function readfile($path) { |
||
712 | |||
713 | static public function isCreatable($path) { |
||
716 | |||
717 | static public function isReadable($path) { |
||
720 | |||
721 | static public function isUpdatable($path) { |
||
724 | |||
725 | static public function isDeletable($path) { |
||
728 | |||
729 | static public function isSharable($path) { |
||
732 | |||
733 | static public function file_exists($path) { |
||
736 | |||
737 | static public function filemtime($path) { |
||
740 | |||
741 | static public function touch($path, $mtime = null) { |
||
744 | |||
745 | /** |
||
746 | * @return string |
||
747 | */ |
||
748 | static public function file_get_contents($path) { |
||
751 | |||
752 | static public function file_put_contents($path, $data) { |
||
755 | |||
756 | static public function unlink($path) { |
||
759 | |||
760 | static public function rename($path1, $path2) { |
||
763 | |||
764 | static public function copy($path1, $path2) { |
||
767 | |||
768 | static public function fopen($path, $mode) { |
||
771 | |||
772 | /** |
||
773 | * @return string |
||
774 | */ |
||
775 | static public function toTmpFile($path) { |
||
778 | |||
779 | static public function fromTmpFile($tmpFile, $path) { |
||
782 | |||
783 | static public function getMimeType($path) { |
||
786 | |||
787 | static public function hash($type, $path, $raw = false) { |
||
790 | |||
791 | static public function free_space($path = '/') { |
||
794 | |||
795 | static public function search($query) { |
||
798 | |||
799 | /** |
||
800 | * @param string $query |
||
801 | */ |
||
802 | static public function searchByMime($query) { |
||
805 | |||
806 | /** |
||
807 | * @param string|int $tag name or tag id |
||
808 | * @param string $userId owner of the tags |
||
809 | * @return FileInfo[] array or file info |
||
810 | */ |
||
811 | static public function searchByTag($tag, $userId) { |
||
814 | |||
815 | /** |
||
816 | * check if a file or folder has been updated since $time |
||
817 | * |
||
818 | * @param string $path |
||
819 | * @param int $time |
||
820 | * @return bool |
||
821 | */ |
||
822 | static public function hasUpdated($path, $time) { |
||
825 | |||
826 | /** |
||
827 | * Fix common problems with a file path |
||
828 | * |
||
829 | * @param string $path |
||
830 | * @param bool $stripTrailingSlash whether to strip the trailing slash |
||
831 | * @param bool $isAbsolutePath whether the given path is absolute |
||
832 | * @param bool $keepUnicode true to disable unicode normalization |
||
833 | * @return string |
||
834 | */ |
||
835 | public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
||
896 | |||
897 | /** |
||
898 | * get the filesystem info |
||
899 | * |
||
900 | * @param string $path |
||
901 | * @param boolean $includeMountPoints whether to add mountpoint sizes, |
||
902 | * defaults to true |
||
903 | * @return \OC\Files\FileInfo|bool False if file does not exist |
||
904 | */ |
||
905 | public static function getFileInfo($path, $includeMountPoints = true) { |
||
908 | |||
909 | /** |
||
910 | * change file metadata |
||
911 | * |
||
912 | * @param string $path |
||
913 | * @param array $data |
||
914 | * @return int |
||
915 | * |
||
916 | * returns the fileid of the updated file |
||
917 | */ |
||
918 | public static function putFileInfo($path, $data) { |
||
921 | |||
922 | /** |
||
923 | * get the content of a directory |
||
924 | * |
||
925 | * @param string $directory path under datadirectory |
||
926 | * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
||
927 | * @return \OC\Files\FileInfo[] |
||
928 | */ |
||
929 | public static function getDirectoryContent($directory, $mimetype_filter = '') { |
||
932 | |||
933 | /** |
||
934 | * Get the path of a file by id |
||
935 | * |
||
936 | * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
||
937 | * |
||
938 | * @param int $id |
||
939 | * @throws NotFoundException |
||
940 | * @return string |
||
941 | */ |
||
942 | public static function getPath($id) { |
||
948 | |||
949 | /** |
||
950 | * Get the owner for a file or folder |
||
951 | * |
||
952 | * @param string $path |
||
953 | * @return string |
||
954 | */ |
||
955 | public static function getOwner($path) { |
||
958 | |||
959 | /** |
||
960 | * get the ETag for a file or folder |
||
961 | * |
||
962 | * @param string $path |
||
963 | * @return string |
||
964 | */ |
||
965 | static public function getETag($path) { |
||
968 | } |
||
969 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.