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 | ||
| 39 | class FedShareManager { | ||
| 40 | const ACTION_URL = 'ocs/v1.php/apps/files_sharing/api/v1/remote_shares/pending/'; | ||
| 41 | |||
| 42 | /** | ||
| 43 | * @var FederatedShareProvider | ||
| 44 | */ | ||
| 45 | private $federatedShareProvider; | ||
| 46 | |||
| 47 | /** | ||
| 48 | * @var Notifications | ||
| 49 | */ | ||
| 50 | private $notifications; | ||
| 51 | |||
| 52 | /** | ||
| 53 | * @var IUserManager | ||
| 54 | */ | ||
| 55 | private $userManager; | ||
| 56 | |||
| 57 | /** | ||
| 58 | * @var ActivityManager | ||
| 59 | */ | ||
| 60 | private $activityManager; | ||
| 61 | |||
| 62 | /** | ||
| 63 | * @var NotificationManager | ||
| 64 | */ | ||
| 65 | private $notificationManager; | ||
| 66 | |||
| 67 | /** | ||
| 68 | * @var AddressHandler | ||
| 69 | */ | ||
| 70 | private $addressHandler; | ||
| 71 | |||
| 72 | /** | ||
| 73 | * @var Permissions | ||
| 74 | */ | ||
| 75 | private $permissions; | ||
| 76 | |||
| 77 | /** | ||
| 78 | * @var EventDispatcherInterface | ||
| 79 | */ | ||
| 80 | private $eventDispatcher; | ||
| 81 | |||
| 82 | /** | ||
| 83 | * FedShareManager constructor. | ||
| 84 | * | ||
| 85 | * @param FederatedShareProvider $federatedShareProvider | ||
| 86 | * @param Notifications $notifications | ||
| 87 | * @param IUserManager $userManager | ||
| 88 | * @param ActivityManager $activityManager | ||
| 89 | * @param NotificationManager $notificationManager | ||
| 90 | * @param AddressHandler $addressHandler | ||
| 91 | * @param Permissions $permissions | ||
| 92 | * @param EventDispatcherInterface $eventDispatcher | ||
| 93 | */ | ||
| 94 | public function __construct(FederatedShareProvider $federatedShareProvider, | ||
| 112 | |||
| 113 | /** | ||
| 114 | * Create an incoming share | ||
| 115 | * | ||
| 116 | * @param Address $ownerAddress | ||
| 117 | * @param Address $sharedByAddress | ||
| 118 | * @param string $shareWith | ||
| 119 | * @param int $remoteId | ||
| 120 | * @param string $name | ||
| 121 | * @param string $token | ||
| 122 | * | ||
| 123 | * @return void | ||
| 124 | */ | ||
| 125 | public function createShare(Address $ownerAddress, | ||
| 181 | |||
| 182 | /** | ||
| 183 | * @param IShare $share | ||
| 184 | * @param int $remoteId | ||
| 185 | * @param string $shareWith | ||
| 186 | * @param int|null $permissions - null for OCM 1.0-proposal1 | ||
| 187 | * | ||
| 188 | * @return IShare | ||
| 189 | * | ||
| 190 | * @throws \OCP\Share\Exceptions\ShareNotFound | ||
| 191 | */ | ||
| 192 | 	public function reShare(IShare $share, $remoteId, $shareWith, $permissions = null) { | ||
| 206 | |||
| 207 | /** | ||
| 208 | * | ||
| 209 | * | ||
| 210 | * @param IShare $share | ||
| 211 | * | ||
| 212 | * @throws \OCP\Files\InvalidPathException | ||
| 213 | * @throws \OCP\Files\NotFoundException | ||
| 214 | */ | ||
| 215 | View Code Duplication | 	public function acceptShare(IShare $share) { | |
| 230 | |||
| 231 | /** | ||
| 232 | * Delete declined share and create a activity | ||
| 233 | * | ||
| 234 | * @param IShare $share | ||
| 235 | * | ||
| 236 | * @throws \OCP\Files\InvalidPathException | ||
| 237 | * @throws \OCP\Files\NotFoundException | ||
| 238 | */ | ||
| 239 | View Code Duplication | 	public function declineShare(IShare $share) { | |
| 255 | |||
| 256 | /** | ||
| 257 | * Unshare an item from self | ||
| 258 | * | ||
| 259 | * @param int $id | ||
| 260 | * @param string $token | ||
| 261 | * | ||
| 262 | * @return void | ||
| 263 | */ | ||
| 264 | 	public function unshare($id, $token) { | ||
| 290 | |||
| 291 | /** | ||
| 292 | * @param IShare $share | ||
| 293 | * | ||
| 294 | * @return void | ||
| 295 | */ | ||
| 296 | 	public function undoReshare(IShare $share) { | ||
| 299 | |||
| 300 | /** | ||
| 301 | * Update permissions | ||
| 302 | * | ||
| 303 | * @param IShare $share | ||
| 304 | * @param string[] $ocmPermissions as ['read', 'write', 'share'] | ||
| 305 | * | ||
| 306 | * @return void | ||
| 307 | */ | ||
| 308 | 	public function updateOcmPermissions(IShare $share, $ocmPermissions) { | ||
| 313 | |||
| 314 | /** | ||
| 315 | * Update permissions | ||
| 316 | * | ||
| 317 | * @param IShare $share | ||
| 318 | * @param int $permissions | ||
| 319 | * | ||
| 320 | * @return void | ||
| 321 | */ | ||
| 322 | 	public function updatePermissions(IShare $share, $permissions) { | ||
| 326 | |||
| 327 | /** | ||
| 328 | * @param IShare $share | ||
| 329 | * @param callable $callback | ||
| 330 | * | ||
| 331 | * @throws \OCP\Share\Exceptions\ShareNotFound | ||
| 332 | * @throws \OC\HintException | ||
| 333 | */ | ||
| 334 | 	protected function notifyRemote($share, $callback) { | ||
| 343 | |||
| 344 | /** | ||
| 345 | * Publish a new activity | ||
| 346 | * | ||
| 347 | * @param string $affectedUser | ||
| 348 | * @param string $subject | ||
| 349 | * @param array $subjectParams | ||
| 350 | * @param string $objectType | ||
| 351 | * @param int $objectId | ||
| 352 | * @param string $objectName | ||
| 353 | * @param string $link | ||
| 354 | * | ||
| 355 | * @return void | ||
| 356 | */ | ||
| 357 | protected function publishActivity($affectedUser, | ||
| 374 | |||
| 375 | /** | ||
| 376 | * Get a new notification | ||
| 377 | * | ||
| 378 | * @param string $uid | ||
| 379 | * | ||
| 380 | * @return \OCP\Notification\INotification | ||
| 381 | */ | ||
| 382 | 	protected function createNotification($uid) { | ||
| 388 | |||
| 389 | /** | ||
| 390 | * @param int $shareId | ||
| 391 | * @return string | ||
| 392 | */ | ||
| 393 | 	protected function getActionLink($shareId) { | ||
| 400 | |||
| 401 | /** | ||
| 402 | * Get file | ||
| 403 | * | ||
| 404 | * @param string $user | ||
| 405 | * @param int $fileSource | ||
| 406 | * | ||
| 407 | * @return array with internal path of the file and a absolute link to it | ||
| 408 | */ | ||
| 409 | 	protected function getFile($user, $fileSource) { | ||
| 425 | |||
| 426 | /** | ||
| 427 | * Check if we are the initiator or the owner of a re-share | ||
| 428 | * and return the correct UID | ||
| 429 | * | ||
| 430 | * @param IShare $share | ||
| 431 | * | ||
| 432 | * @return string | ||
| 433 | */ | ||
| 434 | 	protected function getCorrectUid(IShare $share) { | ||
| 441 | } | ||
| 442 |