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 |
||
49 | class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage { |
||
50 | |||
51 | /** @var \OCP\Share\IShare */ |
||
52 | private $superShare; |
||
53 | |||
54 | /** @var \OCP\Share\IShare[] */ |
||
55 | private $groupedShares; |
||
56 | |||
57 | /** |
||
58 | * @var \OC\Files\View |
||
59 | */ |
||
60 | private $ownerView; |
||
61 | |||
62 | private $initialized = false; |
||
63 | |||
64 | /** |
||
65 | * @var ICacheEntry |
||
66 | */ |
||
67 | private $sourceRootInfo; |
||
68 | |||
69 | /** @var string */ |
||
70 | private $user; |
||
71 | |||
72 | /** |
||
73 | * @var \OCP\ILogger |
||
74 | */ |
||
75 | private $logger; |
||
76 | |||
77 | /** @var IStorage */ |
||
78 | private $nonMaskedStorage; |
||
79 | |||
80 | private $options; |
||
81 | |||
82 | /** @var boolean */ |
||
83 | private $sharingDisabledForUser; |
||
84 | |||
85 | public function __construct($arguments) { |
||
104 | |||
105 | /** |
||
106 | * @return ICacheEntry |
||
107 | */ |
||
108 | private function getSourceRootInfo() { |
||
119 | |||
120 | private function init() { |
||
154 | |||
155 | /** |
||
156 | * @inheritdoc |
||
157 | */ |
||
158 | public function instanceOfStorage($class) { |
||
167 | |||
168 | /** |
||
169 | * @return string |
||
170 | */ |
||
171 | public function getShareId() { |
||
174 | |||
175 | private function isValid() { |
||
178 | |||
179 | /** |
||
180 | * get id of the mount point |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getId() { |
||
187 | |||
188 | /** |
||
189 | * Get the permissions granted for a shared file |
||
190 | * |
||
191 | * @param string $target Shared target file path |
||
192 | * @return int CRUDS permissions granted |
||
193 | */ |
||
194 | public function getPermissions($target = '') { |
||
211 | |||
212 | public function isCreatable($path) { |
||
215 | |||
216 | public function isReadable($path) { |
||
228 | |||
229 | public function isUpdatable($path) { |
||
232 | |||
233 | public function isDeletable($path) { |
||
236 | |||
237 | public function isSharable($path) { |
||
243 | |||
244 | public function fopen($path, $mode) { |
||
297 | |||
298 | /** |
||
299 | * see http://php.net/manual/en/function.rename.php |
||
300 | * |
||
301 | * @param string $path1 |
||
302 | * @param string $path2 |
||
303 | * @return bool |
||
304 | */ |
||
305 | public function rename($path1, $path2) { |
||
323 | |||
324 | /** |
||
325 | * return mount point of share, relative to data/user/files |
||
326 | * |
||
327 | * @return string |
||
328 | */ |
||
329 | public function getMountPoint() { |
||
332 | |||
333 | /** |
||
334 | * @param string $path |
||
335 | */ |
||
336 | public function setMountPoint($path) { |
||
343 | |||
344 | /** |
||
345 | * get the user who shared the file |
||
346 | * |
||
347 | * @return string |
||
348 | */ |
||
349 | public function getSharedFrom() { |
||
352 | |||
353 | /** |
||
354 | * @return \OCP\Share\IShare |
||
355 | */ |
||
356 | public function getShare() { |
||
359 | |||
360 | /** |
||
361 | * return share type, can be "file" or "folder" |
||
362 | * |
||
363 | * @return string |
||
364 | */ |
||
365 | public function getItemType() { |
||
368 | |||
369 | /** |
||
370 | * @param string $path |
||
371 | * @param null $storage |
||
372 | * @return Cache |
||
373 | */ |
||
374 | public function getCache($path = '', $storage = null) { |
||
387 | |||
388 | public function getScanner($path = '', $storage = null) { |
||
394 | |||
395 | public function getOwner($path) { |
||
398 | |||
399 | /** |
||
400 | * unshare complete storage, also the grouped shares |
||
401 | * |
||
402 | * @return bool |
||
403 | */ |
||
404 | public function unshareStorage() { |
||
410 | |||
411 | /** |
||
412 | * @param string $path |
||
413 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
414 | * @param \OCP\Lock\ILockingProvider $provider |
||
415 | * @throws \OCP\Lock\LockedException |
||
416 | */ |
||
417 | View Code Duplication | public function acquireLock($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 | View Code Duplication | public function releaseLock($path, $type, ILockingProvider $provider) { |
|
443 | |||
444 | /** |
||
445 | * @param string $path |
||
446 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
447 | * @param \OCP\Lock\ILockingProvider $provider |
||
448 | */ |
||
449 | public function changeLock($path, $type, ILockingProvider $provider) { |
||
454 | |||
455 | /** |
||
456 | * @return array [ available, last_checked ] |
||
457 | */ |
||
458 | public function getAvailability() { |
||
465 | |||
466 | /** |
||
467 | * @param bool $available |
||
468 | */ |
||
469 | public function setAvailability($available) { |
||
472 | |||
473 | public function getSourceStorage() { |
||
477 | |||
478 | public function getWrapperStorage() { |
||
482 | |||
483 | View Code Duplication | public function file_get_contents($path) { |
|
491 | |||
492 | View Code Duplication | public function file_put_contents($path, $data) { |
|
500 | |||
501 | public function setMountOptions(array $options) { |
||
504 | } |
||
505 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.