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 FederatedShareProvider 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 FederatedShareProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class FederatedShareProvider implements IShareProvider { |
||
| 51 | |||
| 52 | const SHARE_TYPE_REMOTE = 6; |
||
| 53 | |||
| 54 | /** @var IDBConnection */ |
||
| 55 | private $dbConnection; |
||
| 56 | |||
| 57 | /** @var AddressHandler */ |
||
| 58 | private $addressHandler; |
||
| 59 | |||
| 60 | /** @var Notifications */ |
||
| 61 | private $notifications; |
||
| 62 | |||
| 63 | /** @var TokenHandler */ |
||
| 64 | private $tokenHandler; |
||
| 65 | |||
| 66 | /** @var IL10N */ |
||
| 67 | private $l; |
||
| 68 | |||
| 69 | /** @var ILogger */ |
||
| 70 | private $logger; |
||
| 71 | |||
| 72 | /** @var IRootFolder */ |
||
| 73 | private $rootFolder; |
||
| 74 | |||
| 75 | /** @var IConfig */ |
||
| 76 | private $config; |
||
| 77 | |||
| 78 | /** @var string */ |
||
| 79 | private $externalShareTable = 'share_external'; |
||
| 80 | |||
| 81 | /** @var IUserManager */ |
||
| 82 | private $userManager; |
||
| 83 | |||
| 84 | /** @var ICloudIdManager */ |
||
| 85 | private $cloudIdManager; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * DefaultShareProvider constructor. |
||
| 89 | * |
||
| 90 | * @param IDBConnection $connection |
||
| 91 | * @param AddressHandler $addressHandler |
||
| 92 | * @param Notifications $notifications |
||
| 93 | * @param TokenHandler $tokenHandler |
||
| 94 | * @param IL10N $l10n |
||
| 95 | * @param ILogger $logger |
||
| 96 | * @param IRootFolder $rootFolder |
||
| 97 | * @param IConfig $config |
||
| 98 | * @param IUserManager $userManager |
||
| 99 | * @param ICloudIdManager $cloudIdManager |
||
| 100 | */ |
||
| 101 | public function __construct( |
||
| 102 | IDBConnection $connection, |
||
| 103 | AddressHandler $addressHandler, |
||
| 104 | Notifications $notifications, |
||
| 105 | TokenHandler $tokenHandler, |
||
| 106 | IL10N $l10n, |
||
| 107 | ILogger $logger, |
||
| 108 | IRootFolder $rootFolder, |
||
| 109 | IConfig $config, |
||
| 110 | IUserManager $userManager, |
||
| 111 | ICloudIdManager $cloudIdManager |
||
| 112 | ) { |
||
| 113 | $this->dbConnection = $connection; |
||
| 114 | $this->addressHandler = $addressHandler; |
||
| 115 | $this->notifications = $notifications; |
||
| 116 | $this->tokenHandler = $tokenHandler; |
||
| 117 | $this->l = $l10n; |
||
| 118 | $this->logger = $logger; |
||
| 119 | $this->rootFolder = $rootFolder; |
||
| 120 | $this->config = $config; |
||
| 121 | $this->userManager = $userManager; |
||
| 122 | $this->cloudIdManager = $cloudIdManager; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Return the identifier of this provider. |
||
| 127 | * |
||
| 128 | * @return string Containing only [a-zA-Z0-9] |
||
| 129 | */ |
||
| 130 | public function identifier() { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Share a path |
||
| 136 | * |
||
| 137 | * @param IShare $share |
||
| 138 | * @return IShare The share object |
||
| 139 | * @throws ShareNotFound |
||
| 140 | * @throws \Exception |
||
| 141 | */ |
||
| 142 | public function create(IShare $share) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * create federated share and inform the recipient |
||
| 215 | * |
||
| 216 | * @param IShare $share |
||
| 217 | * @return int |
||
| 218 | * @throws ShareNotFound |
||
| 219 | * @throws \Exception |
||
| 220 | */ |
||
| 221 | protected function createFederatedShare(IShare $share) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $shareWith |
||
| 274 | * @param IShare $share |
||
| 275 | * @param string $shareId internal share Id |
||
| 276 | * @return array |
||
| 277 | * @throws \Exception |
||
| 278 | */ |
||
| 279 | protected function askOwnerToReShare($shareWith, IShare $share, $shareId) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * get federated share from the share_external table but exclude mounted link shares |
||
| 300 | * |
||
| 301 | * @param IShare $share |
||
| 302 | * @return array |
||
| 303 | * @throws ShareNotFound |
||
| 304 | */ |
||
| 305 | protected function getShareFromExternalShareTable(IShare $share) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * add share to the database and return the ID |
||
| 321 | * |
||
| 322 | * @param int $itemSource |
||
| 323 | * @param string $itemType |
||
| 324 | * @param string $shareWith |
||
| 325 | * @param string $sharedBy |
||
| 326 | * @param string $uidOwner |
||
| 327 | * @param int $permissions |
||
| 328 | * @param string $token |
||
| 329 | * @return int |
||
| 330 | */ |
||
| 331 | View Code Duplication | private function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token) { |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Update a share |
||
| 359 | * |
||
| 360 | * @param IShare $share |
||
| 361 | * @return IShare The share object |
||
| 362 | */ |
||
| 363 | public function update(IShare $share) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * send the updated permission to the owner/initiator, if they are not the same |
||
| 385 | * |
||
| 386 | * @param IShare $share |
||
| 387 | * @throws ShareNotFound |
||
| 388 | * @throws \OC\HintException |
||
| 389 | */ |
||
| 390 | View Code Duplication | protected function sendPermissionUpdate(IShare $share) { |
|
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * update successful reShare with the correct token |
||
| 404 | * |
||
| 405 | * @param int $shareId |
||
| 406 | * @param string $token |
||
| 407 | */ |
||
| 408 | View Code Duplication | protected function updateSuccessfulReShare($shareId, $token) { |
|
| 415 | |||
| 416 | /** |
||
| 417 | * store remote ID in federated reShare table |
||
| 418 | * |
||
| 419 | * @param $shareId |
||
| 420 | * @param $remoteId |
||
| 421 | */ |
||
| 422 | public function storeRemoteId($shareId, $remoteId) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * get share ID on remote server for federated re-shares |
||
| 436 | * |
||
| 437 | * @param IShare $share |
||
| 438 | * @return int |
||
| 439 | * @throws ShareNotFound |
||
| 440 | */ |
||
| 441 | public function getRemoteId(IShare $share) { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @inheritdoc |
||
| 456 | */ |
||
| 457 | public function move(IShare $share, $recipient) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Get all children of this share |
||
| 467 | * |
||
| 468 | * @param IShare $parent |
||
| 469 | * @return IShare[] |
||
| 470 | */ |
||
| 471 | View Code Duplication | public function getChildren(IShare $parent) { |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Delete a share (owner unShares the file) |
||
| 492 | * |
||
| 493 | * @param IShare $share |
||
| 494 | */ |
||
| 495 | public function delete(IShare $share) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * in case of a re-share we need to send the other use (initiator or owner) |
||
| 528 | * a message that the file was unshared |
||
| 529 | * |
||
| 530 | * @param IShare $share |
||
| 531 | * @param bool $isOwner the user can either be the owner or the user who re-sahred it |
||
| 532 | * @throws ShareNotFound |
||
| 533 | * @throws \OC\HintException |
||
| 534 | */ |
||
| 535 | View Code Duplication | protected function revokeShare($share, $isOwner) { |
|
| 547 | |||
| 548 | /** |
||
| 549 | * remove share from table |
||
| 550 | * |
||
| 551 | * @param IShare $share |
||
| 552 | */ |
||
| 553 | public function removeShareFromTable(IShare $share) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * remove share from table |
||
| 559 | * |
||
| 560 | * @param string $shareId |
||
| 561 | */ |
||
| 562 | private function removeShareFromTableById($shareId) { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @inheritdoc |
||
| 575 | */ |
||
| 576 | public function deleteFromSelf(IShare $share, $recipient) { |
||
| 583 | |||
| 584 | |||
| 585 | View Code Duplication | public function getSharesInFolder($userId, Folder $node, $reshares) { |
|
| 625 | |||
| 626 | /** |
||
| 627 | * @inheritdoc |
||
| 628 | */ |
||
| 629 | View Code Duplication | public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) { |
|
| 681 | |||
| 682 | /** |
||
| 683 | * @inheritdoc |
||
| 684 | */ |
||
| 685 | View Code Duplication | public function getShareById($id, $recipientId = null) { |
|
| 709 | |||
| 710 | /** |
||
| 711 | * Get shares for a given path |
||
| 712 | * |
||
| 713 | * @param \OCP\Files\Node $path |
||
| 714 | * @return IShare[] |
||
| 715 | */ |
||
| 716 | View Code Duplication | public function getSharesByPath(Node $path) { |
|
| 733 | |||
| 734 | /** |
||
| 735 | * @inheritdoc |
||
| 736 | */ |
||
| 737 | View Code Duplication | public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
|
| 773 | |||
| 774 | /** |
||
| 775 | * Get a share by token |
||
| 776 | * |
||
| 777 | * @param string $token |
||
| 778 | * @return IShare |
||
| 779 | * @throws ShareNotFound |
||
| 780 | */ |
||
| 781 | View Code Duplication | public function getShareByToken($token) { |
|
| 804 | |||
| 805 | /** |
||
| 806 | * get database row of a give share |
||
| 807 | * |
||
| 808 | * @param $id |
||
| 809 | * @return array |
||
| 810 | * @throws ShareNotFound |
||
| 811 | */ |
||
| 812 | View Code Duplication | private function getRawShare($id) { |
|
| 830 | |||
| 831 | /** |
||
| 832 | * Create a share object from an database row |
||
| 833 | * |
||
| 834 | * @param array $data |
||
| 835 | * @return IShare |
||
| 836 | * @throws InvalidShare |
||
| 837 | * @throws ShareNotFound |
||
| 838 | */ |
||
| 839 | private function createShareObject($data) { |
||
| 840 | |||
| 841 | $share = new Share($this->rootFolder, $this->userManager); |
||
| 842 | $share->setId((int)$data['id']) |
||
| 843 | ->setShareType((int)$data['share_type']) |
||
| 844 | ->setPermissions((int)$data['permissions']) |
||
| 845 | ->setTarget($data['file_target']) |
||
| 846 | ->setMailSend((bool)$data['mail_send']) |
||
| 847 | ->setToken($data['token']); |
||
| 848 | |||
| 849 | $shareTime = new \DateTime(); |
||
| 850 | $shareTime->setTimestamp((int)$data['stime']); |
||
| 851 | $share->setShareTime($shareTime); |
||
| 852 | $share->setSharedWith($data['share_with']); |
||
| 853 | |||
| 854 | if ($data['uid_initiator'] !== null) { |
||
| 855 | $share->setShareOwner($data['uid_owner']); |
||
| 856 | $share->setSharedBy($data['uid_initiator']); |
||
| 857 | } else { |
||
| 858 | //OLD SHARE |
||
| 859 | $share->setSharedBy($data['uid_owner']); |
||
| 860 | $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']); |
||
| 861 | |||
| 862 | $owner = $path->getOwner(); |
||
| 863 | $share->setShareOwner($owner->getUID()); |
||
| 864 | } |
||
| 865 | |||
| 866 | $share->setNodeId((int)$data['file_source']); |
||
| 867 | $share->setNodeType($data['item_type']); |
||
| 868 | |||
| 869 | $share->setProviderId($this->identifier()); |
||
| 870 | |||
| 871 | return $share; |
||
| 872 | } |
||
| 873 | |||
| 874 | /** |
||
| 875 | * Get the node with file $id for $user |
||
| 876 | * |
||
| 877 | * @param string $userId |
||
| 878 | * @param int $id |
||
| 879 | * @return \OCP\Files\File|\OCP\Files\Folder |
||
| 880 | * @throws InvalidShare |
||
| 881 | */ |
||
| 882 | View Code Duplication | private function getNode($userId, $id) { |
|
| 897 | |||
| 898 | /** |
||
| 899 | * A user is deleted from the system |
||
| 900 | * So clean up the relevant shares. |
||
| 901 | * |
||
| 902 | * @param string $uid |
||
| 903 | * @param int $shareType |
||
| 904 | */ |
||
| 905 | View Code Duplication | public function userDeleted($uid, $shareType) { |
|
| 915 | |||
| 916 | /** |
||
| 917 | * This provider does not handle groups |
||
| 918 | * |
||
| 919 | * @param string $gid |
||
| 920 | */ |
||
| 921 | public function groupDeleted($gid) { |
||
| 925 | |||
| 926 | /** |
||
| 927 | * This provider does not handle groups |
||
| 928 | * |
||
| 929 | * @param string $uid |
||
| 930 | * @param string $gid |
||
| 931 | */ |
||
| 932 | public function userDeletedFromGroup($uid, $gid) { |
||
| 936 | |||
| 937 | /** |
||
| 938 | * check if users from other Nextcloud instances are allowed to mount public links share by this instance |
||
| 939 | * |
||
| 940 | * @return bool |
||
| 941 | */ |
||
| 942 | public function isOutgoingServer2serverShareEnabled() { |
||
| 946 | |||
| 947 | /** |
||
| 948 | * check if users are allowed to mount public links from other ownClouds |
||
| 949 | * |
||
| 950 | * @return bool |
||
| 951 | */ |
||
| 952 | public function isIncomingServer2serverShareEnabled() { |
||
| 956 | |||
| 957 | /** |
||
| 958 | * Check if querying sharees on the lookup server is enabled |
||
| 959 | * |
||
| 960 | * @return bool |
||
| 961 | */ |
||
| 962 | public function isLookupServerQueriesEnabled() { |
||
| 966 | |||
| 967 | |||
| 968 | /** |
||
| 969 | * Check if it is allowed to publish user specific data to the lookup server |
||
| 970 | * |
||
| 971 | * @return bool |
||
| 972 | */ |
||
| 973 | public function isLookupServerUploadEnabled() { |
||
| 977 | } |
||
| 978 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: