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 Shared 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 Shared, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class Shared extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage { |
||
| 46 | /** @var \OCP\Share\IShare */ |
||
| 47 | private $superShare; |
||
| 48 | |||
| 49 | /** @var \OCP\Share\IShare[] */ |
||
| 50 | private $groupedShares; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var \OC\Files\View |
||
| 54 | */ |
||
| 55 | private $ownerView; |
||
| 56 | |||
| 57 | private $initialized = false; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var ICacheEntry |
||
| 61 | */ |
||
| 62 | private $sourceRootInfo; |
||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | private $user; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \OCP\ILogger |
||
| 69 | */ |
||
| 70 | private $logger; |
||
| 71 | |||
| 72 | public function __construct($arguments) { |
||
| 86 | |||
| 87 | private function init() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getShareId() { |
||
| 110 | |||
| 111 | private function isValid() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * get id of the mount point |
||
| 118 | * |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | public function getId() { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get the permissions granted for a shared file |
||
| 127 | * |
||
| 128 | * @param string $target Shared target file path |
||
| 129 | * @return int CRUDS permissions granted |
||
| 130 | */ |
||
| 131 | public function getPermissions($target = '') { |
||
| 147 | |||
| 148 | public function isCreatable($path) { |
||
| 151 | |||
| 152 | public function isReadable($path) { |
||
| 162 | |||
| 163 | public function isUpdatable($path) { |
||
| 166 | |||
| 167 | public function isDeletable($path) { |
||
| 170 | |||
| 171 | public function isSharable($path) { |
||
| 177 | |||
| 178 | public function fopen($path, $mode) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * see http://php.net/manual/en/function.rename.php |
||
| 228 | * |
||
| 229 | * @param string $path1 |
||
| 230 | * @param string $path2 |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function rename($path1, $path2) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * return mount point of share, relative to data/user/files |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function getMountPoint() { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $path |
||
| 262 | */ |
||
| 263 | public function setMountPoint($path) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * get the user who shared the file |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getSharedFrom() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return \OCP\Share\IShare |
||
| 282 | */ |
||
| 283 | public function getShare() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * return share type, can be "file" or "folder" |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function getItemType() { |
||
| 295 | |||
| 296 | public function getCache($path = '', $storage = null) { |
||
| 306 | |||
| 307 | public function getScanner($path = '', $storage = null) { |
||
| 313 | |||
| 314 | View Code Duplication | public function getPropagator($storage = null) { |
|
| 315 | if (isset($this->propagator)) { |
||
| 316 | return $this->propagator; |
||
| 317 | } |
||
| 318 | |||
| 319 | if (!$storage) { |
||
| 320 | $storage = $this; |
||
| 321 | } |
||
| 322 | $this->propagator = new \OCA\Files_Sharing\SharedPropagator($storage, \OC::$server->getDatabaseConnection()); |
||
| 323 | return $this->propagator; |
||
| 324 | } |
||
| 325 | |||
| 326 | public function getOwner($path) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * unshare complete storage, also the grouped shares |
||
| 332 | * |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | public function unshareStorage() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $path |
||
| 344 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 345 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 346 | * @throws \OCP\Lock\LockedException |
||
| 347 | */ |
||
| 348 | View Code Duplication | public function acquireLock($path, $type, ILockingProvider $provider) { |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $path |
||
| 361 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 362 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 363 | */ |
||
| 364 | View Code Duplication | public function releaseLock($path, $type, ILockingProvider $provider) { |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $path |
||
| 377 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 378 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 379 | */ |
||
| 380 | public function changeLock($path, $type, ILockingProvider $provider) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return array [ available, last_checked ] |
||
| 388 | */ |
||
| 389 | public function getAvailability() { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param bool $available |
||
| 399 | */ |
||
| 400 | public function setAvailability($available) { |
||
| 403 | |||
| 404 | public function getSourceStorage() { |
||
| 407 | |||
| 408 | public function getWrapperStorage() { |
||
| 412 | |||
| 413 | View Code Duplication | public function file_get_contents($path) { |
|
| 421 | |||
| 422 | View Code Duplication | public function file_put_contents($path, $data) { |
|
| 430 | |||
| 431 | } |
||
| 432 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.