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 | /** |
||
70 | * @var IStorage |
||
71 | */ |
||
72 | private $sourceStorage; |
||
73 | |||
74 | /** @var string */ |
||
75 | private $user; |
||
76 | |||
77 | /** |
||
78 | * @var \OCP\ILogger |
||
79 | */ |
||
80 | private $logger; |
||
81 | |||
82 | public function __construct($arguments) { |
||
96 | |||
97 | private function init() { |
||
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | public function instanceOfStorage($class) { |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getShareId() { |
||
139 | |||
140 | private function isValid() { |
||
144 | |||
145 | /** |
||
146 | * get id of the mount point |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getId() { |
||
153 | |||
154 | /** |
||
155 | * Get the permissions granted for a shared file |
||
156 | * |
||
157 | * @param string $target Shared target file path |
||
158 | * @return int CRUDS permissions granted |
||
159 | */ |
||
160 | public function getPermissions($target = '') { |
||
176 | |||
177 | public function isCreatable($path) { |
||
180 | |||
181 | public function isReadable($path) { |
||
191 | |||
192 | public function isUpdatable($path) { |
||
195 | |||
196 | public function isDeletable($path) { |
||
199 | |||
200 | public function isSharable($path) { |
||
206 | |||
207 | public function fopen($path, $mode) { |
||
254 | |||
255 | /** |
||
256 | * see http://php.net/manual/en/function.rename.php |
||
257 | * |
||
258 | * @param string $path1 |
||
259 | * @param string $path2 |
||
260 | * @return bool |
||
261 | */ |
||
262 | public function rename($path1, $path2) { |
||
279 | |||
280 | /** |
||
281 | * return mount point of share, relative to data/user/files |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | public function getMountPoint() { |
||
288 | |||
289 | /** |
||
290 | * @param string $path |
||
291 | */ |
||
292 | public function setMountPoint($path) { |
||
299 | |||
300 | /** |
||
301 | * get the user who shared the file |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | public function getSharedFrom() { |
||
308 | |||
309 | /** |
||
310 | * @return \OCP\Share\IShare |
||
311 | */ |
||
312 | public function getShare() { |
||
315 | |||
316 | /** |
||
317 | * return share type, can be "file" or "folder" |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | public function getItemType() { |
||
324 | |||
325 | public function getCache($path = '', $storage = null) { |
||
335 | |||
336 | public function getScanner($path = '', $storage = null) { |
||
342 | |||
343 | View Code Duplication | public function getPropagator($storage = null) { |
|
354 | |||
355 | public function getOwner($path) { |
||
358 | |||
359 | /** |
||
360 | * unshare complete storage, also the grouped shares |
||
361 | * |
||
362 | * @return bool |
||
363 | */ |
||
364 | public function unshareStorage() { |
||
370 | |||
371 | /** |
||
372 | * @param string $path |
||
373 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
374 | * @param \OCP\Lock\ILockingProvider $provider |
||
375 | * @throws \OCP\Lock\LockedException |
||
376 | */ |
||
377 | View Code Duplication | public function acquireLock($path, $type, ILockingProvider $provider) { |
|
387 | |||
388 | /** |
||
389 | * @param string $path |
||
390 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
391 | * @param \OCP\Lock\ILockingProvider $provider |
||
392 | */ |
||
393 | View Code Duplication | public function releaseLock($path, $type, ILockingProvider $provider) { |
|
403 | |||
404 | /** |
||
405 | * @param string $path |
||
406 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
407 | * @param \OCP\Lock\ILockingProvider $provider |
||
408 | */ |
||
409 | public function changeLock($path, $type, ILockingProvider $provider) { |
||
414 | |||
415 | /** |
||
416 | * @return array [ available, last_checked ] |
||
417 | */ |
||
418 | public function getAvailability() { |
||
425 | |||
426 | /** |
||
427 | * @param bool $available |
||
428 | */ |
||
429 | public function setAvailability($available) { |
||
432 | |||
433 | public function getSourceStorage() { |
||
436 | |||
437 | View Code Duplication | public function file_get_contents($path) { |
|
445 | |||
446 | View Code Duplication | public function file_put_contents($path, $data) { |
|
454 | |||
455 | public function getWrapperStorage() { |
||
460 | |||
461 | public function getLocks(string $internalPath, bool $returnChildLocks = false): array { |
||
471 | |||
472 | public function lockNodePersistent(string $internalPath, array $lockInfo) : bool { |
||
475 | |||
476 | public function unlockNodePersistent(string $internalPath, array $lockInfo) { |
||
479 | } |
||
480 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.