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 Storage 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 Storage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class Storage extends DAV implements ISharedStorage { |
||
| 45 | /** @var ICloudId */ |
||
| 46 | private $cloudId; |
||
| 47 | /** @var string */ |
||
| 48 | private $mountPoint; |
||
| 49 | /** @var string */ |
||
| 50 | private $token; |
||
| 51 | /** @var \OCP\ICacheFactory */ |
||
| 52 | private $memcacheFactory; |
||
| 53 | /** @var \OCP\Http\Client\IClientService */ |
||
| 54 | private $httpClient; |
||
| 55 | /** @var bool */ |
||
| 56 | private $updateChecked = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \OCA\Files_Sharing\External\Manager |
||
| 60 | */ |
||
| 61 | private $manager; |
||
| 62 | |||
| 63 | public function __construct($options) { |
||
| 64 | $this->memcacheFactory = \OC::$server->getMemCacheFactory(); |
||
| 65 | $this->httpClient = $options['HttpClientService']; |
||
| 66 | |||
| 67 | $this->manager = $options['manager']; |
||
| 68 | $this->cloudId = $options['cloudId']; |
||
| 69 | $discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class); |
||
| 70 | |||
| 71 | list($protocol, $remote) = explode('://', $this->cloudId->getRemote()); |
||
| 72 | if (strpos($remote, '/')) { |
||
| 73 | list($host, $root) = explode('/', $remote, 2); |
||
| 74 | } else { |
||
| 75 | $host = $remote; |
||
| 76 | $root = ''; |
||
| 77 | } |
||
| 78 | $secure = $protocol === 'https'; |
||
| 79 | $federatedSharingEndpoints = $discoveryService->discover($this->cloudId->getRemote(), 'FEDERATED_SHARING'); |
||
| 80 | $webDavEndpoint = isset($federatedSharingEndpoints['webdav']) ? $federatedSharingEndpoints['webdav'] : '/public.php/webdav'; |
||
| 81 | $root = rtrim($root, '/') . $webDavEndpoint; |
||
| 82 | $this->mountPoint = $options['mountpoint']; |
||
| 83 | $this->token = $options['token']; |
||
| 84 | |||
| 85 | parent::__construct(array( |
||
| 86 | 'secure' => $secure, |
||
| 87 | 'host' => $host, |
||
| 88 | 'root' => $root, |
||
| 89 | 'user' => $options['token'], |
||
| 90 | 'password' => (string)$options['password'] |
||
| 91 | )); |
||
| 92 | } |
||
| 93 | |||
| 94 | public function getWatcher($path = '', $storage = null) { |
||
| 95 | if (!$storage) { |
||
| 96 | $storage = $this; |
||
| 97 | } |
||
| 98 | if (!isset($this->watcher)) { |
||
| 99 | $this->watcher = new Watcher($storage); |
||
| 100 | $this->watcher->setPolicy(\OC\Files\Cache\Watcher::CHECK_ONCE); |
||
| 101 | } |
||
| 102 | return $this->watcher; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function getRemoteUser() { |
||
| 108 | |||
| 109 | public function getRemote() { |
||
| 112 | |||
| 113 | public function getMountPoint() { |
||
| 116 | |||
| 117 | public function getToken() { |
||
| 120 | |||
| 121 | public function getPassword() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @brief get id of the mount point |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | public function getId() { |
||
| 132 | |||
| 133 | public function getCache($path = '', $storage = null) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param string $path |
||
| 142 | * @param \OC\Files\Storage\Storage $storage |
||
| 143 | * @return \OCA\Files_Sharing\External\Scanner |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function getScanner($path = '', $storage = null) { |
|
| 154 | |||
| 155 | /** |
||
| 156 | * check if a file or folder has been updated since $time |
||
| 157 | * |
||
| 158 | * @param string $path |
||
| 159 | * @param int $time |
||
| 160 | * @throws \OCP\Files\StorageNotAvailableException |
||
| 161 | * @throws \OCP\Files\StorageInvalidException |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public function hasUpdated($path, $time) { |
||
| 183 | |||
| 184 | public function test() { |
||
| 185 | try { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Check whether this storage is permanently or temporarily |
||
| 200 | * unavailable |
||
| 201 | * |
||
| 202 | * @throws \OCP\Files\StorageNotAvailableException |
||
| 203 | * @throws \OCP\Files\StorageInvalidException |
||
| 204 | */ |
||
| 205 | public function checkStorageAvailability() { |
||
| 235 | |||
| 236 | public function file_exists($path) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * check if the configured remote is a valid federated share provider |
||
| 246 | * |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | protected function testRemote() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $url |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | private function testRemoteUrl($url) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Whether the remote is an ownCloud/Nextcloud, used since some sharing features are not |
||
| 289 | * standardized. Let's use this to detect whether to use it. |
||
| 290 | * |
||
| 291 | * @return bool |
||
| 292 | */ |
||
| 293 | public function remoteIsOwnCloud() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return mixed |
||
| 302 | * @throws ForbiddenException |
||
| 303 | * @throws NotFoundException |
||
| 304 | * @throws \Exception |
||
| 305 | */ |
||
| 306 | public function getShareInfo() { |
||
| 341 | |||
| 342 | public function getOwner($path) { |
||
| 345 | |||
| 346 | public function isSharable($path) { |
||
| 352 | |||
| 353 | public function getPermissions($path) { |
||
| 368 | |||
| 369 | public function needsPartFile() { |
||
| 372 | } |
||
| 373 |
Scrutinizer analyzes your
composer.json/composer.lockfile if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.