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:
| 1 | <?php |
||
| 49 | class ShareByCircleProvider extends CircleProviderRequestBuilder implements IShareProvider { |
||
| 50 | |||
| 51 | private $misc; |
||
| 52 | |||
| 53 | |||
| 54 | /** @var ILogger */ |
||
| 55 | private $logger; |
||
| 56 | |||
| 57 | /** @var ISecureRandom */ |
||
| 58 | private $secureRandom; |
||
| 59 | |||
| 60 | /** @var IUserManager */ |
||
| 61 | private $userManager; |
||
| 62 | |||
| 63 | /** @var IRootFolder */ |
||
| 64 | private $rootFolder; |
||
| 65 | |||
| 66 | /** @var IL10N */ |
||
| 67 | private $l; |
||
| 68 | |||
| 69 | /** @var IURLGenerator */ |
||
| 70 | private $urlGenerator; |
||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * DefaultShareProvider constructor. |
||
| 75 | * |
||
| 76 | * @param IDBConnection $connection |
||
| 77 | * @param ISecureRandom $secureRandom |
||
| 78 | * @param IUserManager $userManager |
||
| 79 | * @param IRootFolder $rootFolder |
||
| 80 | * @param IL10N $l |
||
| 81 | * @param ILogger $logger |
||
| 82 | * @param IURLGenerator $urlGenerator |
||
| 83 | */ |
||
| 84 | public function __construct( |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Return the identifier of this provider. |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function identifier() { |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * Create a share if it does not exist already. |
||
| 115 | * |
||
| 116 | * @param IShare $share |
||
| 117 | * |
||
| 118 | * @return IShare The share object |
||
| 119 | * @throws \Exception |
||
| 120 | */ |
||
| 121 | // TODO: check if user can create a share in this circle ! |
||
| 122 | public function create(IShare $share) { |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * Update a share |
||
| 143 | * permissions right, owner and initiator |
||
| 144 | * |
||
| 145 | * @param IShare $share |
||
| 146 | * |
||
| 147 | * @return IShare The share object |
||
| 148 | */ |
||
| 149 | public function update(IShare $share) { |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * Delete a share, and it's children |
||
| 164 | * |
||
| 165 | * @param IShare $share |
||
| 166 | */ |
||
| 167 | public function delete(IShare $share) { |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * Unshare a file from self as recipient. |
||
| 178 | * Because every circles share are group shares, we will set permissions to 0 |
||
| 179 | * |
||
| 180 | * @param IShare $share |
||
| 181 | * @param string $userId |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function deleteFromSelf(IShare $share, $userId) { |
|
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Move a share as a recipient. |
||
| 196 | * |
||
| 197 | * @param IShare $share |
||
| 198 | * @param string $userId |
||
| 199 | * |
||
| 200 | * @return IShare |
||
| 201 | * |
||
| 202 | */ |
||
| 203 | View Code Duplication | public function move(IShare $share, $userId) { |
|
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * return the child ID of a share |
||
| 218 | * |
||
| 219 | * @param IShare $share |
||
| 220 | * @param $userId |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | private function getShareChildId(IShare $share, $userId) { |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * Create a child and returns its ID |
||
| 242 | * |
||
| 243 | * @param IShare $share |
||
| 244 | * |
||
| 245 | * @return int |
||
| 246 | */ |
||
| 247 | private function createShare($share) { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Create a child and returns its ID |
||
| 258 | * |
||
| 259 | * @param string $userId |
||
| 260 | * @param IShare $share |
||
| 261 | * |
||
| 262 | * @return int |
||
| 263 | */ |
||
| 264 | private function createShareChild($userId, $share) { |
||
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * Get all shares by the given user in a folder |
||
| 278 | * |
||
| 279 | * @param string $userId |
||
| 280 | * @param Folder $node |
||
| 281 | * @param bool $reshares Also get the shares where $user is the owner instead of just the |
||
| 282 | * shares where $user is the initiator |
||
| 283 | * |
||
| 284 | * @return IShare[] |
||
| 285 | */ |
||
| 286 | public function getSharesInFolder($userId, Folder $node, $reshares) { |
||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * Get all shares by the given user |
||
| 305 | * |
||
| 306 | * @param string $userId |
||
| 307 | * @param int $shareType |
||
| 308 | * @param Node|null $node |
||
| 309 | * @param bool $reShares |
||
| 310 | * @param int $limit The maximum number of shares to be returned, -1 for all shares |
||
| 311 | * @param int $offset |
||
| 312 | * |
||
| 313 | * @return IShare[] |
||
| 314 | * @internal param bool $reshares Also get the shares where $user is the owner instead of just |
||
| 315 | * the shares where $user is the initiator |
||
| 316 | */ |
||
| 317 | public function getSharesBy($userId, $shareType, $node, $reShares, $limit, $offset) { |
||
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * returns a better formatted string to display more information about |
||
| 340 | * the circle to the Sharing UI |
||
| 341 | * |
||
| 342 | * @param $data |
||
| 343 | * |
||
| 344 | * @return mixed |
||
| 345 | */ |
||
| 346 | private function editShareEntry($data) { |
||
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * Get share by its id |
||
| 356 | * |
||
| 357 | * @param int $shareId |
||
| 358 | * @param string|null $recipientId |
||
| 359 | * |
||
| 360 | * @return IShare |
||
| 361 | * @throws ShareNotFound |
||
| 362 | */ |
||
| 363 | public function getShareById($shareId, $recipientId = null) { |
||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * Get shares for a given path |
||
| 381 | * |
||
| 382 | * @param Node $path |
||
| 383 | * |
||
| 384 | * @return IShare[] |
||
| 385 | */ |
||
| 386 | public function getSharesByPath(Node $path) { |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * Get shared with the given user |
||
| 393 | * |
||
| 394 | * @param string $userId get shares where this user is the recipient |
||
| 395 | * @param int $shareType |
||
| 396 | * @param Node|null $node |
||
| 397 | * @param int $limit The max number of entries returned, -1 for all |
||
| 398 | * @param int $offset |
||
| 399 | * |
||
| 400 | * @return IShare[] |
||
| 401 | */ |
||
| 402 | public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
||
| 422 | |||
| 423 | |||
| 424 | private static function editShareFromParentEntry(& $data) { |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * Get a share by token |
||
| 437 | * |
||
| 438 | * @param string $token |
||
| 439 | * |
||
| 440 | * @return IShare |
||
| 441 | * @throws ShareNotFound |
||
| 442 | */ |
||
| 443 | public function getShareByToken($token) { |
||
| 446 | /** @noinspection PhpUnusedParameterInspection */ |
||
| 447 | |||
| 448 | |||
| 449 | /** |
||
| 450 | * We don't return a thing about children. |
||
| 451 | * The call to this function is deprecated and should be removed in next release of NC. |
||
| 452 | * Also, we get the children in the delete() method. |
||
| 453 | * |
||
| 454 | * @param IShare $parent |
||
| 455 | * |
||
| 456 | * @return array |
||
| 457 | */ |
||
| 458 | public function getChildren(IShare $parent) { |
||
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * A user is deleted from the system |
||
| 465 | * So clean up the relevant shares. |
||
| 466 | * |
||
| 467 | * @param string $uid |
||
| 468 | * @param int $shareType |
||
| 469 | */ |
||
| 470 | public function userDeleted($uid, $shareType) { |
||
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * A group is deleted from the system. |
||
| 478 | * We handle our own groups. |
||
| 479 | * |
||
| 480 | * @param string $gid |
||
| 481 | */ |
||
| 482 | public function groupDeleted($gid) { |
||
| 485 | |||
| 486 | |||
| 487 | /** |
||
| 488 | * A user is deleted from a group. |
||
| 489 | * We handle our own groups. |
||
| 490 | * |
||
| 491 | * @param string $uid |
||
| 492 | * @param string $gid |
||
| 493 | */ |
||
| 494 | public function userDeletedFromGroup($uid, $gid) { |
||
| 497 | |||
| 498 | |||
| 499 | /** |
||
| 500 | * Create a share object |
||
| 501 | * |
||
| 502 | * @param array $data |
||
| 503 | * |
||
| 504 | * @return IShare |
||
| 505 | */ |
||
| 506 | private function createShareObject($data) { |
||
| 542 | |||
| 543 | |||
| 544 | /** |
||
| 545 | * Returns whether the given database result can be interpreted as |
||
| 546 | * a share with accessible file (not trashed, not deleted) |
||
| 547 | * |
||
| 548 | * Complete copy/paste from others ShareProvider |
||
| 549 | * |
||
| 550 | * @param $data |
||
| 551 | * |
||
| 552 | * @return bool |
||
| 553 | */ |
||
| 554 | private static function isAccessibleResult($data) { |
||
| 568 | |||
| 569 | |||
| 570 | /** |
||
| 571 | * @param IShare $share |
||
| 572 | * |
||
| 573 | * @return \Exception |
||
| 574 | */ |
||
| 575 | private function errorShareAlreadyExist($share) { |
||
| 587 | |||
| 588 | } |
||
| 589 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.