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 RequestHandlerController 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 RequestHandlerController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class RequestHandlerController extends OCSController { |
||
| 54 | |||
| 55 | /** @var FederatedShareProvider */ |
||
| 56 | private $federatedShareProvider; |
||
| 57 | |||
| 58 | /** @var IDBConnection */ |
||
| 59 | private $connection; |
||
| 60 | |||
| 61 | /** @var Share\IManager */ |
||
| 62 | private $shareManager; |
||
| 63 | |||
| 64 | /** @var Notifications */ |
||
| 65 | private $notifications; |
||
| 66 | |||
| 67 | /** @var AddressHandler */ |
||
| 68 | private $addressHandler; |
||
| 69 | |||
| 70 | /** @var IUserManager */ |
||
| 71 | private $userManager; |
||
| 72 | |||
| 73 | /** @var string */ |
||
| 74 | private $shareTable = 'share'; |
||
|
|
|||
| 75 | |||
| 76 | /** @var ICloudIdManager */ |
||
| 77 | private $cloudIdManager; |
||
| 78 | |||
| 79 | /** @var ILogger */ |
||
| 80 | private $logger; |
||
| 81 | |||
| 82 | /** @var ICloudFederationFactory */ |
||
| 83 | private $cloudFederationFactory; |
||
| 84 | |||
| 85 | /** @var ICloudFederationProviderManager */ |
||
| 86 | private $cloudFederationProviderManager; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Server2Server constructor. |
||
| 90 | * |
||
| 91 | * @param string $appName |
||
| 92 | * @param IRequest $request |
||
| 93 | * @param FederatedShareProvider $federatedShareProvider |
||
| 94 | * @param IDBConnection $connection |
||
| 95 | * @param Share\IManager $shareManager |
||
| 96 | * @param Notifications $notifications |
||
| 97 | * @param AddressHandler $addressHandler |
||
| 98 | * @param IUserManager $userManager |
||
| 99 | * @param ICloudIdManager $cloudIdManager |
||
| 100 | * @param ILogger $logger |
||
| 101 | * @param ICloudFederationFactory $cloudFederationFactory |
||
| 102 | * @param ICloudFederationProviderManager $cloudFederationProviderManager |
||
| 103 | */ |
||
| 104 | View Code Duplication | public function __construct($appName, |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @NoCSRFRequired |
||
| 133 | * @PublicPage |
||
| 134 | * |
||
| 135 | * create a new share |
||
| 136 | * |
||
| 137 | * @return Http\DataResponse |
||
| 138 | * @throws OCSException |
||
| 139 | */ |
||
| 140 | public function createShare() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @NoCSRFRequired |
||
| 191 | * @PublicPage |
||
| 192 | * |
||
| 193 | * create re-share on behalf of another user |
||
| 194 | * |
||
| 195 | * @param int $id |
||
| 196 | * @return Http\DataResponse |
||
| 197 | * @throws OCSBadRequestException |
||
| 198 | * @throws OCSException |
||
| 199 | * @throws OCSForbiddenException |
||
| 200 | */ |
||
| 201 | public function reShare($id) { |
||
| 241 | |||
| 242 | |||
| 243 | /** |
||
| 244 | * @NoCSRFRequired |
||
| 245 | * @PublicPage |
||
| 246 | * |
||
| 247 | * accept server-to-server share |
||
| 248 | * |
||
| 249 | * @param int $id |
||
| 250 | * @return Http\DataResponse |
||
| 251 | * @throws OCSException |
||
| 252 | * @throws ShareNotFound |
||
| 253 | * @throws \OC\HintException |
||
| 254 | */ |
||
| 255 | View Code Duplication | public function acceptShare($id) { |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @NoCSRFRequired |
||
| 280 | * @PublicPage |
||
| 281 | * |
||
| 282 | * decline server-to-server share |
||
| 283 | * |
||
| 284 | * @param int $id |
||
| 285 | * @return Http\DataResponse |
||
| 286 | * @throws OCSException |
||
| 287 | */ |
||
| 288 | View Code Duplication | public function declineShare($id) { |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @NoCSRFRequired |
||
| 313 | * @PublicPage |
||
| 314 | * |
||
| 315 | * remove server-to-server share if it was unshared by the owner |
||
| 316 | * |
||
| 317 | * @param int $id |
||
| 318 | * @return Http\DataResponse |
||
| 319 | * @throws OCSException |
||
| 320 | */ |
||
| 321 | public function unshare($id) { |
||
| 339 | |||
| 340 | private function cleanupRemote($remote) { |
||
| 345 | |||
| 346 | |||
| 347 | /** |
||
| 348 | * @NoCSRFRequired |
||
| 349 | * @PublicPage |
||
| 350 | * |
||
| 351 | * federated share was revoked, either by the owner or the re-sharer |
||
| 352 | * |
||
| 353 | * @param int $id |
||
| 354 | * @return Http\DataResponse |
||
| 355 | * @throws OCSBadRequestException |
||
| 356 | */ |
||
| 357 | public function revoke($id) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * check if server-to-server sharing is enabled |
||
| 374 | * |
||
| 375 | * @param bool $incoming |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | View Code Duplication | private function isS2SEnabled($incoming = false) { |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @NoCSRFRequired |
||
| 393 | * @PublicPage |
||
| 394 | * |
||
| 395 | * update share information to keep federated re-shares in sync |
||
| 396 | * |
||
| 397 | * @param int $id |
||
| 398 | * @return Http\DataResponse |
||
| 399 | * @throws OCSBadRequestException |
||
| 400 | */ |
||
| 401 | public function updatePermissions($id) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * translate Nextcloud permissions to OCM Permissions |
||
| 420 | * |
||
| 421 | * @param $ncPermissions |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | View Code Duplication | protected function ncPermissions2ocmPermissions($ncPermissions) { |
|
| 425 | |||
| 426 | $ocmPermissions = []; |
||
| 427 | |||
| 428 | if ($ncPermissions & Constants::PERMISSION_SHARE) { |
||
| 429 | $ocmPermissions[] = 'share'; |
||
| 430 | } |
||
| 431 | |||
| 432 | if ($ncPermissions & Constants::PERMISSION_READ) { |
||
| 433 | $ocmPermissions[] = 'read'; |
||
| 434 | } |
||
| 435 | |||
| 436 | if (($ncPermissions & Constants::PERMISSION_CREATE) || |
||
| 437 | ($ncPermissions & Constants::PERMISSION_UPDATE)) { |
||
| 438 | $ocmPermissions[] = 'write'; |
||
| 439 | } |
||
| 440 | |||
| 441 | return $ocmPermissions; |
||
| 442 | |||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @NoCSRFRequired |
||
| 447 | * @PublicPage |
||
| 448 | * |
||
| 449 | * change the owner of a server-to-server share |
||
| 450 | * |
||
| 451 | * @param int $id |
||
| 452 | * @return Http\DataResponse |
||
| 453 | * @throws \InvalidArgumentException |
||
| 454 | * @throws OCSException |
||
| 455 | */ |
||
| 456 | public function move($id) { |
||
| 482 | } |
||
| 483 |
This check marks private properties in classes that are never used. Those properties can be removed.