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 = array(); |
||
| 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 = '') { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Get mounts from mount providers that are registered after setup |
||
| 467 | * |
||
| 468 | * @param MountProviderCollection $mountConfigManager |
||
| 469 | * @param IUserManager $userManager |
||
| 470 | */ |
||
| 471 | private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * get the default filesystem view |
||
| 488 | * |
||
| 489 | * @return View |
||
| 490 | */ |
||
| 491 | static public function getView() { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * tear down the filesystem, removing all storage providers |
||
| 497 | */ |
||
| 498 | static public function tearDown() { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * get the relative path of the root data directory for the current user |
||
| 505 | * |
||
| 506 | * @return string |
||
| 507 | * |
||
| 508 | * Returns path like /admin/files |
||
| 509 | */ |
||
| 510 | static public function getRoot() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * clear all mounts and storage backends |
||
| 519 | */ |
||
| 520 | public static function clearMounts() { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * mount an \OC\Files\Storage\Storage in our virtual filesystem |
||
| 529 | * |
||
| 530 | * @param \OC\Files\Storage\Storage|string $class |
||
| 531 | * @param array $arguments |
||
| 532 | * @param string $mountpoint |
||
| 533 | */ |
||
| 534 | static public function mount($class, $arguments, $mountpoint) { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * return the path to a local version of the file |
||
| 544 | * we need this because we can't know if a file is stored local or not from |
||
| 545 | * outside the filestorage and for some purposes a local file is needed |
||
| 546 | * |
||
| 547 | * @param string $path |
||
| 548 | * @return string |
||
| 549 | */ |
||
| 550 | static public function getLocalFile($path) { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param string $path |
||
| 556 | * @return string |
||
| 557 | */ |
||
| 558 | static public function getLocalFolder($path) { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * return path to file which reflects one visible in browser |
||
| 564 | * |
||
| 565 | * @param string $path |
||
| 566 | * @return string |
||
| 567 | */ |
||
| 568 | static public function getLocalPath($path) { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * check if the requested path is valid |
||
| 579 | * |
||
| 580 | * @param string $path |
||
| 581 | * @return bool |
||
| 582 | */ |
||
| 583 | static public function isValidPath($path) { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * checks if a file is blacklisted for storage in the filesystem |
||
| 596 | * Listens to write and rename hooks |
||
| 597 | * |
||
| 598 | * @param array $data from hook |
||
| 599 | */ |
||
| 600 | static public function isBlacklisted($data) { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @param string $filename |
||
| 615 | * @return bool |
||
| 616 | */ |
||
| 617 | static public function isFileBlacklisted($filename) { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * check if the directory should be ignored when scanning |
||
| 627 | * NOTE: the special directories . and .. would cause never ending recursion |
||
| 628 | * |
||
| 629 | * @param String $dir |
||
| 630 | * @return boolean |
||
| 631 | */ |
||
| 632 | static public function isIgnoredDir($dir) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * following functions are equivalent to their php builtin equivalents for arguments/return values. |
||
| 641 | */ |
||
| 642 | static public function mkdir($path) { |
||
| 645 | |||
| 646 | static public function rmdir($path) { |
||
| 649 | |||
| 650 | static public function is_dir($path) { |
||
| 653 | |||
| 654 | static public function is_file($path) { |
||
| 657 | |||
| 658 | static public function stat($path) { |
||
| 661 | |||
| 662 | static public function filetype($path) { |
||
| 665 | |||
| 666 | static public function filesize($path) { |
||
| 669 | |||
| 670 | static public function readfile($path) { |
||
| 673 | |||
| 674 | static public function isCreatable($path) { |
||
| 677 | |||
| 678 | static public function isReadable($path) { |
||
| 681 | |||
| 682 | static public function isUpdatable($path) { |
||
| 685 | |||
| 686 | static public function isDeletable($path) { |
||
| 689 | |||
| 690 | static public function isSharable($path) { |
||
| 693 | |||
| 694 | static public function file_exists($path) { |
||
| 697 | |||
| 698 | static public function filemtime($path) { |
||
| 701 | |||
| 702 | static public function touch($path, $mtime = null) { |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @return string |
||
| 708 | */ |
||
| 709 | static public function file_get_contents($path) { |
||
| 712 | |||
| 713 | static public function file_put_contents($path, $data) { |
||
| 716 | |||
| 717 | static public function unlink($path) { |
||
| 720 | |||
| 721 | static public function rename($path1, $path2) { |
||
| 724 | |||
| 725 | static public function copy($path1, $path2) { |
||
| 728 | |||
| 729 | static public function fopen($path, $mode) { |
||
| 732 | |||
| 733 | /** |
||
| 734 | * @return string |
||
| 735 | */ |
||
| 736 | static public function toTmpFile($path) { |
||
| 739 | |||
| 740 | static public function fromTmpFile($tmpFile, $path) { |
||
| 743 | |||
| 744 | static public function getMimeType($path) { |
||
| 747 | |||
| 748 | static public function hash($type, $path, $raw = false) { |
||
| 751 | |||
| 752 | static public function free_space($path = '/') { |
||
| 755 | |||
| 756 | static public function search($query) { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * @param string $query |
||
| 762 | */ |
||
| 763 | static public function searchByMime($query) { |
||
| 766 | |||
| 767 | /** |
||
| 768 | * @param string|int $tag name or tag id |
||
| 769 | * @param string $userId owner of the tags |
||
| 770 | * @return FileInfo[] array or file info |
||
| 771 | */ |
||
| 772 | static public function searchByTag($tag, $userId) { |
||
| 775 | |||
| 776 | /** |
||
| 777 | * check if a file or folder has been updated since $time |
||
| 778 | * |
||
| 779 | * @param string $path |
||
| 780 | * @param int $time |
||
| 781 | * @return bool |
||
| 782 | */ |
||
| 783 | static public function hasUpdated($path, $time) { |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Fix common problems with a file path |
||
| 789 | * |
||
| 790 | * @param string $path |
||
| 791 | * @param bool $stripTrailingSlash whether to strip the trailing slash |
||
| 792 | * @param bool $isAbsolutePath whether the given path is absolute |
||
| 793 | * @param bool $keepUnicode true to disable unicode normalization |
||
| 794 | * @return string |
||
| 795 | */ |
||
| 796 | public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
||
| 848 | |||
| 849 | /** |
||
| 850 | * get the filesystem info |
||
| 851 | * |
||
| 852 | * @param string $path |
||
| 853 | * @param boolean $includeMountPoints whether to add mountpoint sizes, |
||
| 854 | * defaults to true |
||
| 855 | * @return \OC\Files\FileInfo|bool False if file does not exist |
||
| 856 | */ |
||
| 857 | public static function getFileInfo($path, $includeMountPoints = true) { |
||
| 860 | |||
| 861 | /** |
||
| 862 | * change file metadata |
||
| 863 | * |
||
| 864 | * @param string $path |
||
| 865 | * @param array $data |
||
| 866 | * @return int |
||
| 867 | * |
||
| 868 | * returns the fileid of the updated file |
||
| 869 | */ |
||
| 870 | public static function putFileInfo($path, $data) { |
||
| 873 | |||
| 874 | /** |
||
| 875 | * get the content of a directory |
||
| 876 | * |
||
| 877 | * @param string $directory path under datadirectory |
||
| 878 | * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
||
| 879 | * @return \OC\Files\FileInfo[] |
||
| 880 | */ |
||
| 881 | public static function getDirectoryContent($directory, $mimetype_filter = '') { |
||
| 884 | |||
| 885 | /** |
||
| 886 | * Get the path of a file by id |
||
| 887 | * |
||
| 888 | * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
||
| 889 | * |
||
| 890 | * @param int $id |
||
| 891 | * @throws NotFoundException |
||
| 892 | * @return string |
||
| 893 | */ |
||
| 894 | public static function getPath($id) { |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Get the owner for a file or folder |
||
| 900 | * |
||
| 901 | * @param string $path |
||
| 902 | * @return string |
||
| 903 | */ |
||
| 904 | public static function getOwner($path) { |
||
| 907 | |||
| 908 | /** |
||
| 909 | * get the ETag for a file or folder |
||
| 910 | * |
||
| 911 | * @param string $path |
||
| 912 | * @return string |
||
| 913 | */ |
||
| 914 | static public function getETag($path) { |
||
| 917 | } |
||
| 918 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.