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 |
||
| 44 | class Shared extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage { |
||
| 45 | |||
| 46 | private $share; // the shared resource |
||
| 47 | |||
| 48 | /** @var \OCP\Share\IShare */ |
||
| 49 | private $newShare; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \OC\Files\View |
||
| 53 | */ |
||
| 54 | private $ownerView; |
||
| 55 | |||
| 56 | private $initialized = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var ICacheEntry |
||
| 60 | */ |
||
| 61 | private $sourceRootInfo; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var IStorage |
||
| 65 | */ |
||
| 66 | private $sourceStorage; |
||
| 67 | |||
| 68 | /** @var string */ |
||
| 69 | private $user; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \OCP\ILogger |
||
| 73 | */ |
||
| 74 | private $logger; |
||
| 75 | |||
| 76 | public function __construct($arguments) { |
||
| 91 | |||
| 92 | private function init() { |
||
| 106 | |||
| 107 | private function isValid() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * get id of the mount point |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getId() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * get file cache of the shared item source |
||
| 123 | * |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function getSourceId() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get the permissions granted for a shared file |
||
| 132 | * |
||
| 133 | * @param string $target Shared target file path |
||
| 134 | * @return int CRUDS permissions granted |
||
| 135 | */ |
||
| 136 | public function getPermissions($target = '') { |
||
| 152 | |||
| 153 | public function isCreatable($path) { |
||
| 156 | |||
| 157 | public function isReadable($path) { |
||
| 167 | |||
| 168 | public function isUpdatable($path) { |
||
| 171 | |||
| 172 | public function isDeletable($path) { |
||
| 175 | |||
| 176 | public function isSharable($path) { |
||
| 182 | |||
| 183 | public function fopen($path, $mode) { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * see http://php.net/manual/en/function.rename.php |
||
| 233 | * |
||
| 234 | * @param string $path1 |
||
| 235 | * @param string $path2 |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function rename($path1, $path2) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * return mount point of share, relative to data/user/files |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function getMountPoint() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $path |
||
| 267 | */ |
||
| 268 | public function setMountPoint($path) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return int |
||
| 274 | */ |
||
| 275 | public function getShareType() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * get share ID |
||
| 281 | * |
||
| 282 | * @return integer unique share ID |
||
| 283 | */ |
||
| 284 | public function getShareId() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * get the user who shared the file |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function getSharedFrom() { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @return \OCP\Share\IShare |
||
| 299 | */ |
||
| 300 | public function getShare() { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * return share type, can be "file" or "folder" |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function getItemType() { |
||
| 312 | |||
| 313 | public function getCache($path = '', $storage = null) { |
||
| 323 | |||
| 324 | public function getScanner($path = '', $storage = null) { |
||
| 330 | |||
| 331 | public function getPropagator($storage = null) { |
||
| 337 | |||
| 338 | public function getOwner($path) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * unshare complete storage, also the grouped shares |
||
| 344 | * |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | public function unshareStorage() { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $path |
||
| 354 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 355 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 356 | * @throws \OCP\Lock\LockedException |
||
| 357 | */ |
||
| 358 | View Code Duplication | public function acquireLock($path, $type, ILockingProvider $provider) { |
|
| 368 | |||
| 369 | /** |
||
| 370 | * @param string $path |
||
| 371 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 372 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 373 | */ |
||
| 374 | View Code Duplication | public function releaseLock($path, $type, ILockingProvider $provider) { |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $path |
||
| 387 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
| 388 | * @param \OCP\Lock\ILockingProvider $provider |
||
| 389 | */ |
||
| 390 | public function changeLock($path, $type, ILockingProvider $provider) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return array [ available, last_checked ] |
||
| 398 | */ |
||
| 399 | public function getAvailability() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param bool $available |
||
| 409 | */ |
||
| 410 | public function setAvailability($available) { |
||
| 413 | |||
| 414 | public function getSourceStorage() { |
||
| 417 | |||
| 418 | View Code Duplication | public function file_get_contents($path) { |
|
| 426 | |||
| 427 | View Code Duplication | public function file_put_contents($path, $data) { |
|
| 435 | |||
| 436 | } |
||
| 437 |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.