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 SharedStorage 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 SharedStorage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 48 | class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage { |
||
| 49 | |||
| 50 | /** @var \OCP\Share\IShare */ |
||
| 51 | private $superShare; |
||
| 52 | |||
| 53 | /** @var \OCP\Share\IShare[] */ |
||
| 54 | private $groupedShares; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \OC\Files\View |
||
| 58 | */ |
||
| 59 | private $ownerView; |
||
| 60 | |||
| 61 | private $initialized = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var ICacheEntry |
||
| 65 | */ |
||
| 66 | private $sourceRootInfo; |
||
| 67 | |||
| 68 | /** @var string */ |
||
| 69 | private $user; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \OCP\ILogger |
||
| 73 | */ |
||
| 74 | private $logger; |
||
| 75 | |||
| 76 | /** @var IStorage */ |
||
| 77 | private $nonMaskedStorage; |
||
| 78 | |||
| 79 | private $options; |
||
|
|
|||
| 80 | |||
| 81 | public function __construct($arguments) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return ICacheEntry |
||
| 98 | */ |
||
| 99 | private function getSourceRootInfo() { |
||
| 110 | |||
| 111 | private function init() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @inheritdoc |
||
| 148 | */ |
||
| 149 | public function instanceOfStorage($class) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function getShareId() { |
||
| 165 | |||
| 166 | private function isValid() { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * get id of the mount point |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function getId() { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get the permissions granted for a shared file |
||
| 181 | * |
||
| 182 | * @param string $target Shared target file path |
||
| 183 | * @return int CRUDS permissions granted |
||
| 184 | */ |
||
| 185 | public function getPermissions($target = '') { |
||
| 201 | |||
| 202 | public function isCreatable($path) { |
||
| 205 | |||
| 206 | public function isReadable($path) { |
||
| 218 | |||
| 219 | public function isUpdatable($path) { |
||
| 222 | |||
| 223 | public function isDeletable($path) { |
||
| 226 | |||
| 227 | public function isSharable($path) { |
||
| 233 | |||
| 234 | public function fopen($path, $mode) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * see http://php.net/manual/en/function.rename.php |
||
| 284 | * |
||
| 285 | * @param string $path1 |
||
| 286 | * @param string $path2 |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | public function rename($path1, $path2) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * return mount point of share, relative to data/user/files |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | public function getMountPoint() { |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $path |
||
| 319 | */ |
||
| 320 | public function setMountPoint($path) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * get the user who shared the file |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function getSharedFrom() { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return \OCP\Share\IShare |
||
| 339 | */ |
||
| 340 | public function getShare() { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * return share type, can be "file" or "folder" |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function getItemType() { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $path |
||
| 355 | * @param null $storage |
||
| 356 | * @return Cache |
||
| 357 | */ |
||
| 358 | public function getCache($path = '', $storage = null) { |
||
| 371 | |||
| 372 | public function getScanner($path = '', $storage = null) { |
||
| 378 | |||
| 379 | public function getOwner($path) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * unshare complete storage, also the grouped shares |
||
| 385 | * |
||
| 386 | * @return bool |
||
| 387 | */ |
||
| 388 | public function unshareStorage() { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param string $path |
||
| 397 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 398 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 399 | * @throws \OCP\Lock\LockedException |
||
| 400 | */ |
||
| 401 | View Code Duplication | public function acquireLock($path, $type, ILockingProvider $provider) { |
|
| 411 | |||
| 412 | /** |
||
| 413 | * @param string $path |
||
| 414 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 415 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 416 | */ |
||
| 417 | View Code Duplication | public function releaseLock($path, $type, ILockingProvider $provider) { |
|
| 427 | |||
| 428 | /** |
||
| 429 | * @param string $path |
||
| 430 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 431 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 432 | */ |
||
| 433 | public function changeLock($path, $type, ILockingProvider $provider) { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @return array [ available, last_checked ] |
||
| 441 | */ |
||
| 442 | public function getAvailability() { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param bool $available |
||
| 452 | */ |
||
| 453 | public function setAvailability($available) { |
||
| 456 | |||
| 457 | public function getSourceStorage() { |
||
| 461 | |||
| 462 | public function getWrapperStorage() { |
||
| 466 | |||
| 467 | View Code Duplication | public function file_get_contents($path) { |
|
| 475 | |||
| 476 | View Code Duplication | public function file_put_contents($path, $data) { |
|
| 484 | |||
| 485 | public function setMountOptions(array $options) { |
||
| 488 | } |
||
| 489 |
This check marks private properties in classes that are never used. Those properties can be removed.