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 |
||
| 35 | class Filesystem { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Mount\Manager $mounts |
||
| 39 | */ |
||
| 40 | private static $mounts; |
||
| 41 | |||
| 42 | public static $loaded = false; |
||
| 43 | /** |
||
| 44 | * @var \OC\Files\View $defaultInstance |
||
| 45 | */ |
||
| 46 | static private $defaultInstance; |
||
| 47 | |||
| 48 | static private $usersSetup = array(); |
||
| 49 | |||
| 50 | static private $normalizedPathCache = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * classname which used for hooks handling |
||
| 54 | * used as signalclass in OC_Hooks::emit() |
||
| 55 | */ |
||
| 56 | const CLASSNAME = 'OC_Filesystem'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * signalname emitted before file renaming |
||
| 60 | * |
||
| 61 | * @param string $oldpath |
||
| 62 | * @param string $newpath |
||
| 63 | */ |
||
| 64 | const signal_rename = 'rename'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * signal emitted after file renaming |
||
| 68 | * |
||
| 69 | * @param string $oldpath |
||
| 70 | * @param string $newpath |
||
| 71 | */ |
||
| 72 | const signal_post_rename = 'post_rename'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * signal emitted before file/dir creation |
||
| 76 | * |
||
| 77 | * @param string $path |
||
| 78 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 79 | */ |
||
| 80 | const signal_create = 'create'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * signal emitted after file/dir creation |
||
| 84 | * |
||
| 85 | * @param string $path |
||
| 86 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 87 | */ |
||
| 88 | const signal_post_create = 'post_create'; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * signal emits before file/dir copy |
||
| 92 | * |
||
| 93 | * @param string $oldpath |
||
| 94 | * @param string $newpath |
||
| 95 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 96 | */ |
||
| 97 | const signal_copy = 'copy'; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * signal emits after file/dir copy |
||
| 101 | * |
||
| 102 | * @param string $oldpath |
||
| 103 | * @param string $newpath |
||
| 104 | */ |
||
| 105 | const signal_post_copy = 'post_copy'; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * signal emits before file/dir save |
||
| 109 | * |
||
| 110 | * @param string $path |
||
| 111 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 112 | */ |
||
| 113 | const signal_write = 'write'; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * signal emits after file/dir save |
||
| 117 | * |
||
| 118 | * @param string $path |
||
| 119 | */ |
||
| 120 | const signal_post_write = 'post_write'; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * signal emitted before file/dir update |
||
| 124 | * |
||
| 125 | * @param string $path |
||
| 126 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 127 | */ |
||
| 128 | const signal_update = 'update'; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * signal emitted after file/dir update |
||
| 132 | * |
||
| 133 | * @param string $path |
||
| 134 | * @param bool $run changing this flag to false in hook handler will cancel event |
||
| 135 | */ |
||
| 136 | const signal_post_update = 'post_update'; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * signal emits when reading file/dir |
||
| 140 | * |
||
| 141 | * @param string $path |
||
| 142 | */ |
||
| 143 | const signal_read = 'read'; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * signal emits when removing file/dir |
||
| 147 | * |
||
| 148 | * @param string $path |
||
| 149 | */ |
||
| 150 | const signal_delete = 'delete'; |
||
| 151 | |||
| 152 | /** |
||
| 153 | * parameters definitions for signals |
||
| 154 | */ |
||
| 155 | const signal_param_path = 'path'; |
||
| 156 | const signal_param_oldpath = 'oldpath'; |
||
| 157 | const signal_param_newpath = 'newpath'; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * run - changing this flag to false in hook handler will cancel event |
||
| 161 | */ |
||
| 162 | const signal_param_run = 'run'; |
||
| 163 | |||
| 164 | const signal_create_mount = 'create_mount'; |
||
| 165 | const signal_delete_mount = 'delete_mount'; |
||
| 166 | const signal_param_mount_type = 'mounttype'; |
||
| 167 | const signal_param_users = 'users'; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @var \OC\Files\Storage\StorageFactory $loader |
||
| 171 | */ |
||
| 172 | private static $loader; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param callable $wrapper |
||
| 176 | */ |
||
| 177 | public static function addStorageWrapper($wrapperName, $wrapper) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns the storage factory |
||
| 187 | * |
||
| 188 | * @return \OCP\Files\Storage\IStorageFactory |
||
| 189 | */ |
||
| 190 | public static function getLoader() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns the mount manager |
||
| 199 | * |
||
| 200 | * @return \OC\Files\Mount\Manager |
||
| 201 | */ |
||
| 202 | public static function getMountManager() { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * get the mountpoint of the storage object for a path |
||
| 211 | * ( note: because a storage is not always mounted inside the fakeroot, the |
||
| 212 | * returned mountpoint is relative to the absolute root of the filesystem |
||
| 213 | * and doesn't take the chroot into account ) |
||
| 214 | * |
||
| 215 | * @param string $path |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | static public function getMountPoint($path) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * get a list of all mount points in a directory |
||
| 232 | * |
||
| 233 | * @param string $path |
||
| 234 | * @return string[] |
||
| 235 | */ |
||
| 236 | static public function getMountPoints($path) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * get the storage mounted at $mountPoint |
||
| 250 | * |
||
| 251 | * @param string $mountPoint |
||
| 252 | * @return \OC\Files\Storage\Storage |
||
| 253 | */ |
||
| 254 | public static function getStorage($mountPoint) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $id |
||
| 264 | * @return Mount\MountPoint[] |
||
| 265 | */ |
||
| 266 | public static function getMountByStorageId($id) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param int $id |
||
| 275 | * @return Mount\MountPoint[] |
||
| 276 | */ |
||
| 277 | public static function getMountByNumericId($id) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * resolve a path to a storage and internal path |
||
| 286 | * |
||
| 287 | * @param string $path |
||
| 288 | * @return array an array consisting of the storage and the internal path |
||
| 289 | */ |
||
| 290 | static public function resolvePath($path) { |
||
| 301 | |||
| 302 | static public function init($user, $root) { |
||
| 320 | |||
| 321 | static public function initMounts() { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Initialize system and personal mount points for a user |
||
| 329 | * |
||
| 330 | * @param string $user |
||
| 331 | */ |
||
| 332 | public static function initMountPoints($user = '') { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Mounts the cache directory |
||
| 394 | * @param string $user user name |
||
| 395 | */ |
||
| 396 | private static function mountCacheDir($user) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * get the default filesystem view |
||
| 417 | * |
||
| 418 | * @return View |
||
| 419 | */ |
||
| 420 | static public function getView() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * tear down the filesystem, removing all storage providers |
||
| 426 | */ |
||
| 427 | static public function tearDown() { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * get the relative path of the root data directory for the current user |
||
| 434 | * @return string |
||
| 435 | * |
||
| 436 | * Returns path like /admin/files |
||
| 437 | */ |
||
| 438 | static public function getRoot() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * clear all mounts and storage backends |
||
| 447 | */ |
||
| 448 | public static function clearMounts() { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * mount an \OC\Files\Storage\Storage in our virtual filesystem |
||
| 457 | * |
||
| 458 | * @param \OC\Files\Storage\Storage|string $class |
||
| 459 | * @param array $arguments |
||
| 460 | * @param string $mountpoint |
||
| 461 | */ |
||
| 462 | static public function mount($class, $arguments, $mountpoint) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * return the path to a local version of the file |
||
| 472 | * we need this because we can't know if a file is stored local or not from |
||
| 473 | * outside the filestorage and for some purposes a local file is needed |
||
| 474 | * |
||
| 475 | * @param string $path |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | static public function getLocalFile($path) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $path |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | static public function getLocalFolder($path) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * return path to file which reflects one visible in browser |
||
| 492 | * |
||
| 493 | * @param string $path |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | static public function getLocalPath($path) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * check if the requested path is valid |
||
| 507 | * |
||
| 508 | * @param string $path |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | static public function isValidPath($path) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * checks if a file is blacklisted for storage in the filesystem |
||
| 524 | * Listens to write and rename hooks |
||
| 525 | * |
||
| 526 | * @param array $data from hook |
||
| 527 | */ |
||
| 528 | static public function isBlacklisted($data) { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @param string $filename |
||
| 543 | * @return bool |
||
| 544 | */ |
||
| 545 | static public function isFileBlacklisted($filename) { |
||
| 552 | |||
| 553 | /** |
||
| 554 | * check if the directory should be ignored when scanning |
||
| 555 | * NOTE: the special directories . and .. would cause never ending recursion |
||
| 556 | * @param String $dir |
||
| 557 | * @return boolean |
||
| 558 | */ |
||
| 559 | static public function isIgnoredDir($dir) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * following functions are equivalent to their php builtin equivalents for arguments/return values. |
||
| 568 | */ |
||
| 569 | static public function mkdir($path) { |
||
| 572 | |||
| 573 | static public function rmdir($path) { |
||
| 576 | |||
| 577 | static public function opendir($path) { |
||
| 580 | |||
| 581 | static public function readdir($path) { |
||
| 584 | |||
| 585 | static public function is_dir($path) { |
||
| 588 | |||
| 589 | static public function is_file($path) { |
||
| 592 | |||
| 593 | static public function stat($path) { |
||
| 596 | |||
| 597 | static public function filetype($path) { |
||
| 600 | |||
| 601 | static public function filesize($path) { |
||
| 604 | |||
| 605 | static public function readfile($path) { |
||
| 608 | |||
| 609 | static public function isCreatable($path) { |
||
| 612 | |||
| 613 | static public function isReadable($path) { |
||
| 616 | |||
| 617 | static public function isUpdatable($path) { |
||
| 620 | |||
| 621 | static public function isDeletable($path) { |
||
| 624 | |||
| 625 | static public function isSharable($path) { |
||
| 628 | |||
| 629 | static public function file_exists($path) { |
||
| 632 | |||
| 633 | static public function filemtime($path) { |
||
| 636 | |||
| 637 | static public function touch($path, $mtime = null) { |
||
| 640 | |||
| 641 | /** |
||
| 642 | * @return string |
||
| 643 | */ |
||
| 644 | static public function file_get_contents($path) { |
||
| 647 | |||
| 648 | static public function file_put_contents($path, $data) { |
||
| 651 | |||
| 652 | static public function unlink($path) { |
||
| 655 | |||
| 656 | static public function rename($path1, $path2) { |
||
| 659 | |||
| 660 | static public function copy($path1, $path2) { |
||
| 663 | |||
| 664 | static public function fopen($path, $mode) { |
||
| 667 | |||
| 668 | /** |
||
| 669 | * @return string |
||
| 670 | */ |
||
| 671 | static public function toTmpFile($path) { |
||
| 674 | |||
| 675 | static public function fromTmpFile($tmpFile, $path) { |
||
| 678 | |||
| 679 | static public function getMimeType($path) { |
||
| 682 | |||
| 683 | static public function hash($type, $path, $raw = false) { |
||
| 686 | |||
| 687 | static public function free_space($path = '/') { |
||
| 690 | |||
| 691 | static public function search($query) { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @param string $query |
||
| 697 | */ |
||
| 698 | static public function searchByMime($query) { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * @param string|int $tag name or tag id |
||
| 704 | * @param string $userId owner of the tags |
||
| 705 | * @return FileInfo[] array or file info |
||
| 706 | */ |
||
| 707 | static public function searchByTag($tag, $userId) { |
||
| 710 | |||
| 711 | /** |
||
| 712 | * check if a file or folder has been updated since $time |
||
| 713 | * |
||
| 714 | * @param string $path |
||
| 715 | * @param int $time |
||
| 716 | * @return bool |
||
| 717 | */ |
||
| 718 | static public function hasUpdated($path, $time) { |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Fix common problems with a file path |
||
| 724 | * @param string $path |
||
| 725 | * @param bool $stripTrailingSlash |
||
| 726 | * @param bool $isAbsolutePath |
||
| 727 | * @return string |
||
| 728 | */ |
||
| 729 | public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false) { |
||
| 792 | |||
| 793 | /** |
||
| 794 | * get the filesystem info |
||
| 795 | * |
||
| 796 | * @param string $path |
||
| 797 | * @param boolean $includeMountPoints whether to add mountpoint sizes, |
||
| 798 | * defaults to true |
||
| 799 | * @return \OC\Files\FileInfo |
||
| 800 | */ |
||
| 801 | public static function getFileInfo($path, $includeMountPoints = true) { |
||
| 804 | |||
| 805 | /** |
||
| 806 | * change file metadata |
||
| 807 | * |
||
| 808 | * @param string $path |
||
| 809 | * @param array $data |
||
| 810 | * @return int |
||
| 811 | * |
||
| 812 | * returns the fileid of the updated file |
||
| 813 | */ |
||
| 814 | public static function putFileInfo($path, $data) { |
||
| 817 | |||
| 818 | /** |
||
| 819 | * get the content of a directory |
||
| 820 | * |
||
| 821 | * @param string $directory path under datadirectory |
||
| 822 | * @param string $mimetype_filter limit returned content to this mimetype or mimepart |
||
| 823 | * @return \OC\Files\FileInfo[] |
||
| 824 | */ |
||
| 825 | public static function getDirectoryContent($directory, $mimetype_filter = '') { |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Get the path of a file by id |
||
| 831 | * |
||
| 832 | * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
||
| 833 | * |
||
| 834 | * @param int $id |
||
| 835 | * @return string |
||
| 836 | */ |
||
| 837 | public static function getPath($id) { |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Get the owner for a file or folder |
||
| 843 | * |
||
| 844 | * @param string $path |
||
| 845 | * @return string |
||
| 846 | */ |
||
| 847 | public static function getOwner($path) { |
||
| 850 | |||
| 851 | /** |
||
| 852 | * get the ETag for a file or folder |
||
| 853 | * |
||
| 854 | * @param string $path |
||
| 855 | * @return string |
||
| 856 | */ |
||
| 857 | static public function getETag($path) { |
||
| 860 | } |
||
| 861 |
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.