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 |
||
| 68 | class Filesystem { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var Mount\Manager $mounts |
||
| 72 | */ |
||
| 73 | private static $mounts; |
||
| 74 | |||
| 75 | public static $loaded = false; |
||
| 76 | /** |
||
| 77 | * @var \OC\Files\View $defaultInstance |
||
| 78 | */ |
||
| 79 | static private $defaultInstance; |
||
| 80 | |||
| 81 | static private $usersSetup = array(); |
||
| 82 | |||
| 83 | static private $normalizedPathCache = array(); |
||
| 84 | |||
| 85 | static private $listeningForProviders = false; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * classname which used for hooks handling |
||
| 89 | * used as signalclass in OC_Hooks::emit() |
||
| 90 | */ |
||
| 91 | const CLASSNAME = 'OC_Filesystem'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * signalname emitted before file renaming |
||
| 95 | * |
||
| 96 | * @param string $oldpath |
||
| 97 | * @param string $newpath |
||
| 98 | */ |
||
| 99 | const signal_rename = 'rename'; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * signal emitted after file renaming |
||
| 103 | * |
||
| 104 | * @param string $oldpath |
||
| 105 | * @param string $newpath |
||
| 106 | */ |
||
| 107 | const signal_post_rename = 'post_rename'; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * signal emitted before file/dir creation |
||
| 111 | * |
||
| 112 | * @param string $path |
||
| 113 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 114 | */ |
||
| 115 | const signal_create = 'create'; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * signal emitted after file/dir creation |
||
| 119 | * |
||
| 120 | * @param string $path |
||
| 121 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 122 | */ |
||
| 123 | const signal_post_create = 'post_create'; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * signal emits before file/dir copy |
||
| 127 | * |
||
| 128 | * @param string $oldpath |
||
| 129 | * @param string $newpath |
||
| 130 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 131 | */ |
||
| 132 | const signal_copy = 'copy'; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * signal emits after file/dir copy |
||
| 136 | * |
||
| 137 | * @param string $oldpath |
||
| 138 | * @param string $newpath |
||
| 139 | */ |
||
| 140 | const signal_post_copy = 'post_copy'; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * signal emits before file/dir save |
||
| 144 | * |
||
| 145 | * @param string $path |
||
| 146 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 147 | */ |
||
| 148 | const signal_write = 'write'; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * signal emits after file/dir save |
||
| 152 | * |
||
| 153 | * @param string $path |
||
| 154 | */ |
||
| 155 | const signal_post_write = 'post_write'; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * signal emitted before file/dir update |
||
| 159 | * |
||
| 160 | * @param string $path |
||
| 161 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 162 | */ |
||
| 163 | const signal_update = 'update'; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * signal emitted after file/dir update |
||
| 167 | * |
||
| 168 | * @param string $path |
||
| 169 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 170 | */ |
||
| 171 | const signal_post_update = 'post_update'; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * signal emits when reading file/dir |
||
| 175 | * |
||
| 176 | * @param string $path |
||
| 177 | */ |
||
| 178 | const signal_read = 'read'; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * signal emits when removing file/dir |
||
| 182 | * |
||
| 183 | * @param string $path |
||
| 184 | */ |
||
| 185 | const signal_delete = 'delete'; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * parameters definitions for signals |
||
| 189 | */ |
||
| 190 | const signal_param_path = 'path'; |
||
| 191 | const signal_param_oldpath = 'oldpath'; |
||
| 192 | const signal_param_newpath = 'newpath'; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * run - changing this flag to false in hook handler will cancel event |
||
| 196 | */ |
||
| 197 | const signal_param_run = 'run'; |
||
| 198 | |||
| 199 | const signal_create_mount = 'create_mount'; |
||
| 200 | const signal_delete_mount = 'delete_mount'; |
||
| 201 | const signal_param_mount_type = 'mounttype'; |
||
| 202 | const signal_param_users = 'users'; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @var \OC\Files\Storage\StorageFactory $loader |
||
| 206 | */ |
||
| 207 | private static $loader; |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $wrapperName |
||
| 211 | * @param callable $wrapper |
||
| 212 | * @param int $priority |
||
| 213 | */ |
||
| 214 | 1071 | public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) { |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the storage factory |
||
| 224 | * |
||
| 225 | * @return \OCP\Files\Storage\IStorageFactory |
||
| 226 | */ |
||
| 227 | 1086 | public static function getLoader() { |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the mount manager |
||
| 236 | * |
||
| 237 | * @return \OC\Files\Mount\Manager |
||
| 238 | */ |
||
| 239 | 1099 | public static function getMountManager() { |
|
| 245 | |||
| 246 | /** |
||
| 247 | * get the mountpoint of the storage object for a path |
||
| 248 | * ( note: because a storage is not always mounted inside the fakeroot, the |
||
| 249 | * returned mountpoint is relative to the absolute root of the filesystem |
||
| 250 | * and doesn't take the chroot into account ) |
||
| 251 | * |
||
| 252 | * @param string $path |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | 4 | static public function getMountPoint($path) { |
|
| 266 | |||
| 267 | /** |
||
| 268 | * get a list of all mount points in a directory |
||
| 269 | * |
||
| 270 | * @param string $path |
||
| 271 | * @return string[] |
||
| 272 | */ |
||
| 273 | static public function getMountPoints($path) { |
||
| 274 | if (!self::$mounts) { |
||
| 275 | \OC_Util::setupFS(); |
||
| 276 | } |
||
| 277 | $result = array(); |
||
| 278 | $mounts = self::$mounts->findIn($path); |
||
| 279 | foreach ($mounts as $mount) { |
||
| 280 | $result[] = $mount->getMountPoint(); |
||
| 281 | } |
||
| 282 | return $result; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * get the storage mounted at $mountPoint |
||
| 287 | * |
||
| 288 | * @param string $mountPoint |
||
| 289 | * @return \OC\Files\Storage\Storage |
||
| 290 | */ |
||
| 291 | 959 | public static function getStorage($mountPoint) { |
|
| 292 | 959 | if (!self::$mounts) { |
|
| 293 | 1 | \OC_Util::setupFS(); |
|
| 294 | 1 | } |
|
| 295 | 959 | $mount = self::$mounts->find($mountPoint); |
|
| 296 | 959 | return $mount->getStorage(); |
|
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string $id |
||
| 301 | * @return Mount\MountPoint[] |
||
| 302 | */ |
||
| 303 | public static function getMountByStorageId($id) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @param int $id |
||
| 312 | * @return Mount\MountPoint[] |
||
| 313 | */ |
||
| 314 | 108 | public static function getMountByNumericId($id) { |
|
| 320 | |||
| 321 | /** |
||
| 322 | * resolve a path to a storage and internal path |
||
| 323 | * |
||
| 324 | * @param string $path |
||
| 325 | * @return array an array consisting of the storage and the internal path |
||
| 326 | */ |
||
| 327 | 951 | static public function resolvePath($path) { |
|
| 328 | 951 | if (!self::$mounts) { |
|
| 329 | \OC_Util::setupFS(); |
||
| 330 | } |
||
| 331 | 951 | $mount = self::$mounts->find($path); |
|
| 332 | 951 | if ($mount) { |
|
| 333 | 951 | return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/')); |
|
| 334 | } else { |
||
| 335 | return array(null, null); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | 948 | static public function init($user, $root) { |
|
| 357 | |||
| 358 | 1071 | static public function initMountManager() { |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Initialize system and personal mount points for a user |
||
| 366 | * |
||
| 367 | * @param string $user |
||
| 368 | * @throws \OC\User\NoUserException if the user is not available |
||
| 369 | */ |
||
| 370 | 959 | public static function initMountPoints($user = '') { |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Get mounts from mount providers that are registered after setup |
||
| 436 | * |
||
| 437 | * @param MountProviderCollection $mountConfigManager |
||
| 438 | * @param IUserManager $userManager |
||
| 439 | */ |
||
| 440 | 958 | private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Mounts the cache directory |
||
| 457 | * |
||
| 458 | * @param string $user user name |
||
| 459 | */ |
||
| 460 | 958 | private static function mountCacheDir($user) { |
|
| 471 | |||
| 472 | /** |
||
| 473 | * get the default filesystem view |
||
| 474 | * |
||
| 475 | * @return View |
||
| 476 | */ |
||
| 477 | 944 | static public function getView() { |
|
| 480 | |||
| 481 | /** |
||
| 482 | * tear down the filesystem, removing all storage providers |
||
| 483 | */ |
||
| 484 | 1121 | static public function tearDown() { |
|
| 488 | |||
| 489 | /** |
||
| 490 | * get the relative path of the root data directory for the current user |
||
| 491 | * |
||
| 492 | * @return string |
||
| 493 | * |
||
| 494 | * Returns path like /admin/files |
||
| 495 | */ |
||
| 496 | 944 | static public function getRoot() { |
|
| 502 | |||
| 503 | /** |
||
| 504 | * clear all mounts and storage backends |
||
| 505 | */ |
||
| 506 | 1128 | public static function clearMounts() { |
|
| 512 | |||
| 513 | /** |
||
| 514 | * mount an \OC\Files\Storage\Storage in our virtual filesystem |
||
| 515 | * |
||
| 516 | * @param \OC\Files\Storage\Storage|string $class |
||
| 517 | * @param array $arguments |
||
| 518 | * @param string $mountpoint |
||
| 519 | */ |
||
| 520 | 1083 | static public function mount($class, $arguments, $mountpoint) { |
|
| 521 | 1083 | if (!self::$mounts) { |
|
| 522 | \OC_Util::setupFS(); |
||
| 523 | } |
||
| 524 | 1083 | $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader()); |
|
| 525 | 1083 | self::$mounts->addMount($mount); |
|
| 526 | 1083 | } |
|
| 527 | |||
| 528 | /** |
||
| 529 | * return the path to a local version of the file |
||
| 530 | * we need this because we can't know if a file is stored local or not from |
||
| 531 | * outside the filestorage and for some purposes a local file is needed |
||
| 532 | * |
||
| 533 | * @param string $path |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | static public function getLocalFile($path) { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param string $path |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | static public function getLocalFolder($path) { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * return path to file which reflects one visible in browser |
||
| 550 | * |
||
| 551 | * @param string $path |
||
| 552 | * @return string |
||
| 553 | */ |
||
| 554 | static public function getLocalPath($path) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * check if the requested path is valid |
||
| 565 | * |
||
| 566 | * @param string $path |
||
| 567 | * @return bool |
||
| 568 | */ |
||
| 569 | 1413 | static public function isValidPath($path) { |
|
| 579 | |||
| 580 | /** |
||
| 581 | * checks if a file is blacklisted for storage in the filesystem |
||
| 582 | * Listens to write and rename hooks |
||
| 583 | * |
||
| 584 | * @param array $data from hook |
||
| 585 | */ |
||
| 586 | static public function isBlacklisted($data) { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * @param string $filename |
||
| 601 | * @return bool |
||
| 602 | */ |
||
| 603 | 1017 | static public function isFileBlacklisted($filename) { |
|
| 610 | |||
| 611 | /** |
||
| 612 | * check if the directory should be ignored when scanning |
||
| 613 | * NOTE: the special directories . and .. would cause never ending recursion |
||
| 614 | * |
||
| 615 | * @param String $dir |
||
| 616 | * @return boolean |
||
| 617 | */ |
||
| 618 | 1001 | static public function isIgnoredDir($dir) { |
|
| 624 | |||
| 625 | /** |
||
| 626 | * following functions are equivalent to their php builtin equivalents for arguments/return values. |
||
| 627 | */ |
||
| 628 | 24 | static public function mkdir($path) { |
|
| 631 | |||
| 632 | 3 | static public function rmdir($path) { |
|
| 635 | |||
| 636 | static public function opendir($path) { |
||
| 639 | |||
| 640 | static public function readdir($path) { |
||
| 643 | |||
| 644 | 25 | static public function is_dir($path) { |
|
| 647 | |||
| 648 | static public function is_file($path) { |
||
| 651 | |||
| 652 | static public function stat($path) { |
||
| 655 | |||
| 656 | static public function filetype($path) { |
||
| 659 | |||
| 660 | 2 | static public function filesize($path) { |
|
| 663 | |||
| 664 | static public function readfile($path) { |
||
| 667 | |||
| 668 | static public function isCreatable($path) { |
||
| 671 | |||
| 672 | 1 | static public function isReadable($path) { |
|
| 675 | |||
| 676 | static public function isUpdatable($path) { |
||
| 679 | |||
| 680 | static public function isDeletable($path) { |
||
| 683 | |||
| 684 | 149 | static public function isSharable($path) { |
|
| 687 | |||
| 688 | 162 | static public function file_exists($path) { |
|
| 691 | |||
| 692 | static public function filemtime($path) { |
||
| 695 | |||
| 696 | 7 | static public function touch($path, $mtime = null) { |
|
| 699 | |||
| 700 | /** |
||
| 701 | * @return string |
||
| 702 | */ |
||
| 703 | static public function file_get_contents($path) { |
||
| 706 | |||
| 707 | 40 | static public function file_put_contents($path, $data) { |
|
| 710 | |||
| 711 | 15 | static public function unlink($path) { |
|
| 714 | |||
| 715 | 27 | static public function rename($path1, $path2) { |
|
| 718 | |||
| 719 | 3 | static public function copy($path1, $path2) { |
|
| 722 | |||
| 723 | static public function fopen($path, $mode) { |
||
| 726 | |||
| 727 | /** |
||
| 728 | * @return string |
||
| 729 | */ |
||
| 730 | static public function toTmpFile($path) { |
||
| 733 | |||
| 734 | static public function fromTmpFile($tmpFile, $path) { |
||
| 737 | |||
| 738 | 1 | static public function getMimeType($path) { |
|
| 741 | |||
| 742 | static public function hash($type, $path, $raw = false) { |
||
| 745 | |||
| 746 | 47 | static public function free_space($path = '/') { |
|
| 749 | |||
| 750 | static public function search($query) { |
||
| 753 | |||
| 754 | /** |
||
| 755 | * @param string $query |
||
| 756 | */ |
||
| 757 | static public function searchByMime($query) { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * @param string|int $tag name or tag id |
||
| 763 | * @param string $userId owner of the tags |
||
| 764 | * @return FileInfo[] array or file info |
||
| 765 | */ |
||
| 766 | static public function searchByTag($tag, $userId) { |
||
| 769 | |||
| 770 | /** |
||
| 771 | * check if a file or folder has been updated since $time |
||
| 772 | * |
||
| 773 | * @param string $path |
||
| 774 | * @param int $time |
||
| 775 | * @return bool |
||
| 776 | */ |
||
| 777 | static public function hasUpdated($path, $time) { |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Fix common problems with a file path |
||
| 783 | * |
||
| 784 | * @param string $path |
||
| 785 | * @param bool $stripTrailingSlash |
||
| 786 | * @param bool $isAbsolutePath |
||
| 787 | * @return string |
||
| 788 | */ |
||
| 789 | 1648 | public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false) { |
|
| 852 | |||
| 853 | /** |
||
| 854 | * get the filesystem info |
||
| 855 | * |
||
| 856 | * @param string $path |
||
| 857 | * @param boolean $includeMountPoints whether to add mountpoint sizes, |
||
| 858 | * defaults to true |
||
| 859 | * @return \OC\Files\FileInfo|bool False if file does not exist |
||
| 860 | */ |
||
| 861 | 44 | public static function getFileInfo($path, $includeMountPoints = true) { |
|
| 864 | |||
| 865 | /** |
||
| 866 | * change file metadata |
||
| 867 | * |
||
| 868 | * @param string $path |
||
| 869 | * @param array $data |
||
| 870 | * @return int |
||
| 871 | * |
||
| 872 | * returns the fileid of the updated file |
||
| 873 | */ |
||
| 874 | public static function putFileInfo($path, $data) { |
||
| 877 | |||
| 878 | /** |
||
| 879 | * get the content of a directory |
||
| 880 | * |
||
| 881 | * @param string $directory path under datadirectory |
||
| 882 | * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
||
| 883 | * @return \OC\Files\FileInfo[] |
||
| 884 | */ |
||
| 885 | 1 | public static function getDirectoryContent($directory, $mimetype_filter = '') { |
|
| 888 | |||
| 889 | /** |
||
| 890 | * Get the path of a file by id |
||
| 891 | * |
||
| 892 | * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
||
| 893 | * |
||
| 894 | * @param int $id |
||
| 895 | * @throws NotFoundException |
||
| 896 | * @return string |
||
| 897 | */ |
||
| 898 | 152 | public static function getPath($id) { |
|
| 901 | |||
| 902 | /** |
||
| 903 | * Get the owner for a file or folder |
||
| 904 | * |
||
| 905 | * @param string $path |
||
| 906 | * @return string |
||
| 907 | */ |
||
| 908 | 76 | public static function getOwner($path) { |
|
| 911 | |||
| 912 | /** |
||
| 913 | * get the ETag for a file or folder |
||
| 914 | * |
||
| 915 | * @param string $path |
||
| 916 | * @return string |
||
| 917 | */ |
||
| 918 | static public function getETag($path) { |
||
| 921 | } |
||
| 922 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.