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 ShareesAPIController 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 ShareesAPIController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class ShareesAPIController extends OCSController { |
||
| 43 | |||
| 44 | /** @var IGroupManager */ |
||
| 45 | protected $groupManager; |
||
| 46 | |||
| 47 | /** @var IUserManager */ |
||
| 48 | protected $userManager; |
||
| 49 | |||
| 50 | /** @var IManager */ |
||
| 51 | protected $contactsManager; |
||
| 52 | |||
| 53 | /** @var IConfig */ |
||
| 54 | protected $config; |
||
| 55 | |||
| 56 | /** @var IUserSession */ |
||
| 57 | protected $userSession; |
||
| 58 | |||
| 59 | /** @var IURLGenerator */ |
||
| 60 | protected $urlGenerator; |
||
| 61 | |||
| 62 | /** @var ILogger */ |
||
| 63 | protected $logger; |
||
| 64 | |||
| 65 | /** @var \OCP\Share\IManager */ |
||
| 66 | protected $shareManager; |
||
| 67 | |||
| 68 | /** @var bool */ |
||
| 69 | protected $shareWithGroupOnly = false; |
||
| 70 | |||
| 71 | /** @var bool */ |
||
| 72 | protected $shareeEnumeration = true; |
||
| 73 | |||
| 74 | /** @var int */ |
||
| 75 | protected $offset = 0; |
||
| 76 | |||
| 77 | /** @var int */ |
||
| 78 | protected $limit = 10; |
||
| 79 | |||
| 80 | /** @var array */ |
||
| 81 | protected $result = [ |
||
| 82 | 'exact' => [ |
||
| 83 | 'users' => [], |
||
| 84 | 'groups' => [], |
||
| 85 | 'remotes' => [], |
||
| 86 | 'emails' => [], |
||
| 87 | ], |
||
| 88 | 'users' => [], |
||
| 89 | 'groups' => [], |
||
| 90 | 'remotes' => [], |
||
| 91 | 'emails' => [], |
||
| 92 | ]; |
||
| 93 | |||
| 94 | protected $reachedEndFor = []; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $appName |
||
| 98 | * @param IRequest $request |
||
| 99 | * @param IGroupManager $groupManager |
||
| 100 | * @param IUserManager $userManager |
||
| 101 | * @param IManager $contactsManager |
||
| 102 | * @param IConfig $config |
||
| 103 | * @param IUserSession $userSession |
||
| 104 | * @param IURLGenerator $urlGenerator |
||
| 105 | * @param ILogger $logger |
||
| 106 | * @param \OCP\Share\IManager $shareManager |
||
| 107 | */ |
||
| 108 | public function __construct($appName, |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $search |
||
| 132 | */ |
||
| 133 | protected function getUsers($search) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $search |
||
| 215 | */ |
||
| 216 | protected function getGroups($search) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $search |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | protected function getRemote($search) { |
||
| 279 | $result = ['results' => [], 'exact' => []]; |
||
| 280 | |||
| 281 | // Search in contacts |
||
| 282 | //@todo Pagination missing |
||
| 283 | $addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN']); |
||
| 284 | $result['exactIdMatch'] = false; |
||
| 285 | foreach ($addressBookContacts as $contact) { |
||
| 286 | if (isset($contact['isLocalSystemBook'])) { |
||
| 287 | continue; |
||
| 288 | } |
||
| 289 | if (isset($contact['CLOUD'])) { |
||
| 290 | $cloudIds = $contact['CLOUD']; |
||
| 291 | if (!is_array($cloudIds)) { |
||
| 292 | $cloudIds = [$cloudIds]; |
||
| 293 | } |
||
| 294 | foreach ($cloudIds as $cloudId) { |
||
| 295 | list(, $serverUrl) = $this->splitUserRemote($cloudId); |
||
| 296 | if (strtolower($contact['FN']) === strtolower($search) || strtolower($cloudId) === strtolower($search)) { |
||
| 297 | if (strtolower($cloudId) === strtolower($search)) { |
||
| 298 | $result['exactIdMatch'] = true; |
||
| 299 | } |
||
| 300 | $result['exact'][] = [ |
||
| 301 | 'label' => $contact['FN'] . " ($cloudId)", |
||
| 302 | 'value' => [ |
||
| 303 | 'shareType' => Share::SHARE_TYPE_REMOTE, |
||
| 304 | 'shareWith' => $cloudId, |
||
| 305 | 'server' => $serverUrl, |
||
| 306 | ], |
||
| 307 | ]; |
||
| 308 | View Code Duplication | } else { |
|
| 309 | $result['results'][] = [ |
||
| 310 | 'label' => $contact['FN'] . " ($cloudId)", |
||
| 311 | 'value' => [ |
||
| 312 | 'shareType' => Share::SHARE_TYPE_REMOTE, |
||
| 313 | 'shareWith' => $cloudId, |
||
| 314 | 'server' => $serverUrl, |
||
| 315 | ], |
||
| 316 | ]; |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | if (!$this->shareeEnumeration) { |
||
| 323 | $result['results'] = []; |
||
| 324 | } |
||
| 325 | |||
| 326 | if (!$result['exactIdMatch'] && substr_count($search, '@') >= 1 && $this->offset === 0) { |
||
| 327 | $result['exact'][] = [ |
||
| 328 | 'label' => $search, |
||
| 329 | 'value' => [ |
||
| 330 | 'shareType' => Share::SHARE_TYPE_REMOTE, |
||
| 331 | 'shareWith' => $search, |
||
| 332 | ], |
||
| 333 | ]; |
||
| 334 | } |
||
| 335 | |||
| 336 | $this->reachedEndFor[] = 'remotes'; |
||
| 337 | |||
| 338 | return $result; |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * split user and remote from federated cloud id |
||
| 343 | * |
||
| 344 | * @param string $address federated share address |
||
| 345 | * @return array [user, remoteURL] |
||
| 346 | * @throws \Exception |
||
| 347 | */ |
||
| 348 | public function splitUserRemote($address) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Strips away a potential file names and trailing slashes: |
||
| 389 | * - http://localhost |
||
| 390 | * - http://localhost/ |
||
| 391 | * - http://localhost/index.php |
||
| 392 | * - http://localhost/index.php/s/{shareToken} |
||
| 393 | * |
||
| 394 | * all return: http://localhost |
||
| 395 | * |
||
| 396 | * @param string $remote |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | View Code Duplication | protected function fixRemoteURL($remote) { |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @NoAdminRequired |
||
| 411 | * |
||
| 412 | * @param string $search |
||
| 413 | * @param string $itemType |
||
| 414 | * @param int $page |
||
| 415 | * @param int $perPage |
||
| 416 | * @param int|int[] $shareType |
||
| 417 | * @return Http\DataResponse |
||
| 418 | * @throws OCSBadRequestException |
||
| 419 | */ |
||
| 420 | public function search($search = '', $itemType = null, $page = 1, $perPage = 200, $shareType = null) { |
||
| 421 | if ($perPage <= 0) { |
||
| 422 | throw new OCSBadRequestException('Invalid perPage argument'); |
||
| 423 | } |
||
| 424 | if ($page <= 0) { |
||
| 425 | throw new OCSBadRequestException('Invalid page'); |
||
| 426 | } |
||
| 427 | |||
| 428 | $shareTypes = [ |
||
| 429 | Share::SHARE_TYPE_USER, |
||
| 430 | ]; |
||
| 431 | |||
| 432 | if ($itemType === 'file' || $itemType === 'folder') { |
||
| 433 | if ($this->shareManager->allowGroupSharing()) { |
||
| 434 | $shareTypes[] = Share::SHARE_TYPE_GROUP; |
||
| 435 | } |
||
| 436 | |||
| 437 | if ($this->isRemoteSharingAllowed($itemType)) { |
||
| 438 | $shareTypes[] = Share::SHARE_TYPE_REMOTE; |
||
| 439 | } |
||
| 440 | |||
| 441 | if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) { |
||
| 442 | $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
||
| 443 | } |
||
| 444 | } else { |
||
| 445 | $shareTypes[] = Share::SHARE_TYPE_GROUP; |
||
| 446 | $shareTypes[] = Share::SHARE_TYPE_EMAIL; |
||
| 447 | } |
||
| 448 | |||
| 449 | if (isset($_GET['shareType']) && is_array($_GET['shareType'])) { |
||
| 450 | $shareTypes = array_intersect($shareTypes, $_GET['shareType']); |
||
| 451 | sort($shareTypes); |
||
| 452 | } else if (is_numeric($shareType)) { |
||
| 453 | $shareTypes = array_intersect($shareTypes, [(int) $shareType]); |
||
| 454 | sort($shareTypes); |
||
| 455 | } |
||
| 456 | |||
| 457 | $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; |
||
| 458 | $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes'; |
||
| 459 | $this->limit = (int) $perPage; |
||
| 460 | $this->offset = $perPage * ($page - 1); |
||
| 461 | |||
| 462 | return $this->searchSharees($search, $itemType, $shareTypes, $page, $perPage); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Method to get out the static call for better testing |
||
| 467 | * |
||
| 468 | * @param string $itemType |
||
| 469 | * @return bool |
||
| 470 | */ |
||
| 471 | protected function isRemoteSharingAllowed($itemType) { |
||
| 472 | try { |
||
| 473 | $backend = Share::getBackend($itemType); |
||
| 474 | return $backend->isShareTypeAllowed(Share::SHARE_TYPE_REMOTE); |
||
| 475 | } catch (\Exception $e) { |
||
| 476 | return false; |
||
| 477 | } |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Testable search function that does not need globals |
||
| 482 | * |
||
| 483 | * @param string $search |
||
| 484 | * @param string $itemType |
||
| 485 | * @param array $shareTypes |
||
| 486 | * @param int $page |
||
| 487 | * @param int $perPage |
||
| 488 | * @return Http\DataResponse |
||
| 489 | * @throws OCSBadRequestException |
||
| 490 | */ |
||
| 491 | protected function searchSharees($search, $itemType, array $shareTypes, $page, $perPage) { |
||
| 492 | // Verify arguments |
||
| 493 | if ($itemType === null) { |
||
| 494 | throw new OCSBadRequestException('Missing itemType'); |
||
| 495 | } |
||
| 496 | |||
| 497 | // Get users |
||
| 498 | if (in_array(Share::SHARE_TYPE_USER, $shareTypes)) { |
||
| 499 | $this->getUsers($search); |
||
| 500 | } |
||
| 501 | |||
| 502 | // Get groups |
||
| 503 | if (in_array(Share::SHARE_TYPE_GROUP, $shareTypes)) { |
||
| 504 | $this->getGroups($search); |
||
| 505 | } |
||
| 506 | |||
| 507 | // Get remote |
||
| 508 | $remoteResults = ['results' => [], 'exact' => [], 'exactIdMatch' => false]; |
||
| 509 | if (in_array(Share::SHARE_TYPE_REMOTE, $shareTypes)) { |
||
| 510 | $remoteResults = $this->getRemote($search); |
||
| 511 | } |
||
| 512 | |||
| 513 | $mailResults = ['results' => [], 'exact' => [], 'exactIdMatch' => false]; |
||
| 514 | if (in_array(Share::SHARE_TYPE_EMAIL, $shareTypes)) { |
||
| 515 | $mailResults = $this->getEmail($search); |
||
| 516 | } |
||
| 517 | |||
| 518 | // if we have a exact match, either for the federated cloud id or for the |
||
| 519 | // email address we only return the exact match. It is highly unlikely |
||
| 520 | // that the exact same email address and federated cloud id exists |
||
| 521 | if ($mailResults['exactIdMatch'] && !$remoteResults['exactIdMatch']) { |
||
| 522 | $this->result['emails'] = $mailResults['results']; |
||
| 523 | $this->result['exact']['emails'] = $mailResults['exact']; |
||
| 524 | } else if (!$mailResults['exactIdMatch'] && $remoteResults['exactIdMatch']) { |
||
| 525 | $this->result['remotes'] = $remoteResults['results']; |
||
| 526 | $this->result['exact']['remotes'] = $remoteResults['exact']; |
||
| 527 | } else { |
||
| 528 | $this->result['remotes'] = $remoteResults['results']; |
||
| 529 | $this->result['exact']['remotes'] = $remoteResults['exact']; |
||
| 530 | $this->result['emails'] = $mailResults['results']; |
||
| 531 | $this->result['exact']['emails'] = $mailResults['exact']; |
||
| 532 | } |
||
| 533 | |||
| 534 | $response = new Http\DataResponse($this->result); |
||
| 535 | |||
| 536 | if (sizeof($this->reachedEndFor) < 3) { |
||
| 537 | $response->addHeader('Link', $this->getPaginationLink($page, [ |
||
| 538 | 'search' => $search, |
||
| 539 | 'itemType' => $itemType, |
||
| 540 | 'shareType' => $shareTypes, |
||
| 541 | 'perPage' => $perPage, |
||
| 542 | ])); |
||
| 543 | } |
||
| 544 | |||
| 545 | return $response; |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @param string $search |
||
| 550 | * @return array |
||
| 551 | */ |
||
| 552 | protected function getEmail($search) { |
||
| 553 | $result = ['results' => [], 'exact' => []]; |
||
| 554 | |||
| 555 | // Search in contacts |
||
| 556 | //@todo Pagination missing |
||
| 557 | $addressBookContacts = $this->contactsManager->search($search, ['EMAIL', 'FN']); |
||
| 558 | $result['exactIdMatch'] = false; |
||
| 559 | foreach ($addressBookContacts as $contact) { |
||
| 560 | if (isset($contact['isLocalSystemBook'])) { |
||
| 561 | continue; |
||
| 562 | } |
||
| 563 | if (isset($contact['EMAIL'])) { |
||
| 564 | $emailAddresses = $contact['EMAIL']; |
||
| 565 | if (!is_array($emailAddresses)) { |
||
| 566 | $emailAddresses = [$emailAddresses]; |
||
| 567 | } |
||
| 568 | foreach ($emailAddresses as $emailAddress) { |
||
| 569 | if (strtolower($contact['FN']) === strtolower($search) || strtolower($emailAddress) === strtolower($search)) { |
||
| 570 | if (strtolower($emailAddress) === strtolower($search)) { |
||
| 571 | $result['exactIdMatch'] = true; |
||
| 572 | } |
||
| 573 | $result['exact'][] = [ |
||
| 574 | 'label' => $contact['FN'] . " ($emailAddress)", |
||
| 575 | 'value' => [ |
||
| 576 | 'shareType' => Share::SHARE_TYPE_EMAIL, |
||
| 577 | 'shareWith' => $emailAddress, |
||
| 578 | ], |
||
| 579 | ]; |
||
| 580 | View Code Duplication | } else { |
|
| 581 | $result['results'][] = [ |
||
| 582 | 'label' => $contact['FN'] . " ($emailAddress)", |
||
| 583 | 'value' => [ |
||
| 584 | 'shareType' => Share::SHARE_TYPE_EMAIL, |
||
| 585 | 'shareWith' => $emailAddress, |
||
| 586 | ], |
||
| 587 | ]; |
||
| 588 | } |
||
| 589 | } |
||
| 590 | } |
||
| 591 | } |
||
| 592 | |||
| 593 | if (!$this->shareeEnumeration) { |
||
| 594 | $result['results'] = []; |
||
| 595 | } |
||
| 596 | |||
| 597 | if (!$result['exactIdMatch'] && filter_var($search, FILTER_VALIDATE_EMAIL)) { |
||
| 598 | $result['exact'][] = [ |
||
| 599 | 'label' => $search, |
||
| 600 | 'value' => [ |
||
| 601 | 'shareType' => Share::SHARE_TYPE_EMAIL, |
||
| 602 | 'shareWith' => $search, |
||
| 603 | ], |
||
| 604 | ]; |
||
| 605 | } |
||
| 606 | |||
| 607 | $this->reachedEndFor[] = 'emails'; |
||
| 608 | |||
| 609 | return $result; |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Generates a bunch of pagination links for the current page |
||
| 614 | * |
||
| 615 | * @param int $page Current page |
||
| 616 | * @param array $params Parameters for the URL |
||
| 617 | * @return string |
||
| 618 | */ |
||
| 619 | protected function getPaginationLink($page, array $params) { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * @return bool |
||
| 633 | */ |
||
| 634 | protected function isV2() { |
||
| 637 | } |
||
| 638 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: