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 ShareByCircleProviderDeprecated 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 ShareByCircleProviderDeprecated, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
81 | class ShareByCircleProviderDeprecated extends CircleProviderRequest implements IShareProvider { |
||
82 | |||
83 | |||
84 | /** @var ILogger */ |
||
85 | private $logger; |
||
86 | |||
87 | /** @var ISecureRandom */ |
||
88 | private $secureRandom; |
||
89 | |||
90 | /** @var IUserManager */ |
||
91 | private $userManager; |
||
92 | |||
93 | /** @var IRootFolder */ |
||
94 | private $rootFolder; |
||
95 | |||
96 | /** @var IURLGenerator */ |
||
97 | private $urlGenerator; |
||
98 | |||
99 | /** @var MembersService */ |
||
100 | private $membersService; |
||
101 | |||
102 | /** @var DeprecatedCirclesRequest */ |
||
103 | private $circlesRequest; |
||
104 | |||
105 | /** @var DeprecatedMembersRequest */ |
||
106 | private $membersRequest; |
||
107 | |||
108 | /** @var TokensRequest */ |
||
109 | private $tokensRequest; |
||
110 | |||
111 | /** @var GSUpstreamService */ |
||
112 | private $gsUpstreamService; |
||
113 | |||
114 | |||
115 | /** |
||
116 | * ShareByCircleProvider constructor. |
||
117 | * |
||
118 | * @param IDBConnection $connection |
||
119 | * @param ISecureRandom $secureRandom |
||
120 | * @param IUserManager $userManager |
||
121 | * @param IRootFolder $rootFolder |
||
122 | * @param IL10N $l10n |
||
123 | * @param ILogger $logger |
||
124 | * @param IURLGenerator $urlGenerator |
||
125 | * |
||
126 | * @throws QueryException |
||
127 | */ |
||
128 | public function __construct( |
||
151 | |||
152 | |||
153 | /** |
||
154 | * Return the identifier of this provider. |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function identifier() { |
||
161 | |||
162 | |||
163 | /** |
||
164 | * Create a share if it does not exist already. |
||
165 | * |
||
166 | * @param IShare $share |
||
167 | * |
||
168 | * @return IShare The share object |
||
169 | * @throws Exception |
||
170 | */ |
||
171 | public function create(IShare $share) { |
||
205 | |||
206 | |||
207 | /** |
||
208 | * Update a share |
||
209 | * permissions right, owner and initiator |
||
210 | * |
||
211 | * @param IShare $share |
||
212 | * |
||
213 | * @return IShare The share object |
||
214 | */ |
||
215 | public function update(IShare $share) { |
||
226 | |||
227 | |||
228 | /** |
||
229 | * Delete a share, and it's children |
||
230 | * |
||
231 | * @param IShare $share |
||
232 | * |
||
233 | * @throws Exception |
||
234 | */ |
||
235 | public function delete(IShare $share) { |
||
259 | |||
260 | |||
261 | /** |
||
262 | * Unshare a file from self as recipient. |
||
263 | * Because every circles share are group shares, we will set permissions to 0 |
||
264 | * |
||
265 | * @param IShare $share |
||
266 | * @param string $userId |
||
267 | */ |
||
268 | View Code Duplication | public function deleteFromSelf(IShare $share, $userId) { |
|
277 | |||
278 | |||
279 | /** |
||
280 | * Move a share as a recipient. |
||
281 | * |
||
282 | * @param IShare $share |
||
283 | * @param string $userId |
||
284 | * |
||
285 | * @return IShare |
||
286 | * |
||
287 | */ |
||
288 | View Code Duplication | public function move(IShare $share, $userId) { |
|
299 | |||
300 | |||
301 | /** |
||
302 | * return the child ID of a share |
||
303 | * |
||
304 | * @param IShare $share |
||
305 | * @param string $userId |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | private function getShareChildId(IShare $share, $userId) { |
||
323 | |||
324 | |||
325 | /** |
||
326 | * Create a child and returns its ID |
||
327 | * |
||
328 | * @param IShare $share |
||
329 | * |
||
330 | * @throws NotFoundException |
||
331 | */ |
||
332 | private function createShare($share) { |
||
353 | |||
354 | |||
355 | /** |
||
356 | * Create a child and returns its ID |
||
357 | * |
||
358 | * @param string $userId |
||
359 | * @param IShare $share |
||
360 | * |
||
361 | * @return int |
||
362 | * @throws NotFoundException |
||
363 | */ |
||
364 | private function createShareChild($userId, $share) { |
||
374 | |||
375 | |||
376 | /** |
||
377 | * Get all shares by the given user in a folder |
||
378 | * |
||
379 | * @param string $userId |
||
380 | * @param Folder $node |
||
381 | * @param bool $reshares Also get the shares where $user is the owner instead of just the |
||
382 | * shares where $user is the initiator |
||
383 | * |
||
384 | * @return Share[] |
||
385 | */ |
||
386 | public function getSharesInFolder($userId, Folder $node, $reshares) { |
||
402 | |||
403 | |||
404 | /** |
||
405 | * Get all shares by the given user |
||
406 | * |
||
407 | * @param string $userId |
||
408 | * @param int $shareType |
||
409 | * @param Node|null $node |
||
410 | * @param bool $reShares |
||
411 | * @param int $limit The maximum number of shares to be returned, -1 for all shares |
||
412 | * @param int $offset |
||
413 | * |
||
414 | * @return Share[] |
||
415 | * @throws NoUserException |
||
416 | * @throws NotFoundException |
||
417 | * @throws InvalidPathException |
||
418 | */ |
||
419 | public function getSharesBy($userId, $shareType, $node, $reShares, $limit, $offset) { |
||
439 | |||
440 | |||
441 | /** |
||
442 | * returns a better formatted string to display more information about |
||
443 | * the circle to the Sharing UI |
||
444 | * |
||
445 | * @param $data |
||
446 | * |
||
447 | * @return array<string,string> |
||
448 | * @throws NoUserException |
||
449 | */ |
||
450 | private function editShareEntry($data) { |
||
466 | |||
467 | |||
468 | /** |
||
469 | * Get share by its id |
||
470 | * |
||
471 | * @param int $shareId |
||
472 | * @param string|null $recipientId |
||
473 | * |
||
474 | * @return Share |
||
475 | * @throws ShareNotFound |
||
476 | */ |
||
477 | public function getShareById($shareId, $recipientId = null) { |
||
493 | |||
494 | |||
495 | /** |
||
496 | * Get shares for a given path |
||
497 | |||
498 | * @param Node $path |
||
499 | * |
||
500 | * @return IShare[]|null |
||
501 | * @throws InvalidPathException |
||
502 | * @throws NotFoundException * |
||
503 | */ |
||
504 | public function getSharesByPath(Node $path) { |
||
520 | |||
521 | |||
522 | /** |
||
523 | * Get shared with the given user |
||
524 | * |
||
525 | * @param string $userId get shares where this user is the recipient |
||
526 | * @param int $shareType |
||
527 | * @param Node|null $node |
||
528 | * @param int $limit The max number of entries returned, -1 for all |
||
529 | * @param int $offset |
||
530 | * |
||
531 | * @return IShare[] |
||
532 | * @throws Exceptions\GSStatusException |
||
533 | * @throws InvalidPathException |
||
534 | * @throws NotFoundException |
||
535 | */ |
||
536 | public function getSharedWith($userId, $shareType, $node, $limit, $offset) { |
||
540 | |||
541 | |||
542 | /** |
||
543 | * @param string $userId |
||
544 | * @param $shareType |
||
545 | * @param Node $node |
||
546 | * @param int $limit |
||
547 | * @param int $offset |
||
548 | * |
||
549 | * @return IShare[] |
||
550 | * @throws Exceptions\GSStatusException |
||
551 | * @throws InvalidPathException |
||
552 | * @throws NotFoundException |
||
553 | */ |
||
554 | private function getSharedWithCircleMembers($userId, $shareType, $node, $limit, $offset) { |
||
578 | |||
579 | |||
580 | /** |
||
581 | * Get a share by token |
||
582 | * |
||
583 | * @param string $token |
||
584 | * |
||
585 | * @return IShare |
||
586 | * @throws ShareNotFound |
||
587 | * @deprecated - use local querybuilder lib instead |
||
588 | */ |
||
589 | public function getShareByToken($token) { |
||
635 | |||
636 | |||
637 | /** |
||
638 | * @param string $token |
||
639 | * |
||
640 | * @return array |
||
641 | * @throws ShareNotFound |
||
642 | */ |
||
643 | private function getShareByPersonalToken($token) { |
||
687 | |||
688 | |||
689 | /** |
||
690 | * We don't return a thing about children. |
||
691 | * The call to this function is deprecated and should be removed in next release of NC. |
||
692 | * Also, we get the children in the delete() method. |
||
693 | * |
||
694 | * @param IShare $parent |
||
695 | * |
||
696 | * @return array |
||
697 | */ |
||
698 | public function getChildren(IShare $parent) { |
||
701 | |||
702 | |||
703 | /** |
||
704 | * A user is deleted from the system |
||
705 | * So clean up the relevant shares. |
||
706 | * |
||
707 | * @param string $uid |
||
708 | * @param int $shareType |
||
709 | */ |
||
710 | public function userDeleted($uid, $shareType) { |
||
713 | |||
714 | |||
715 | /** |
||
716 | * A group is deleted from the system. |
||
717 | * We handle our own groups. |
||
718 | * |
||
719 | * @param string $gid |
||
720 | */ |
||
721 | public function groupDeleted($gid) { |
||
724 | |||
725 | |||
726 | /** |
||
727 | * A user is deleted from a group. |
||
728 | * We handle our own groups. |
||
729 | * |
||
730 | * @param string $uid |
||
731 | * @param string $gid |
||
732 | */ |
||
733 | public function userDeletedFromGroup($uid, $gid) { |
||
736 | |||
737 | |||
738 | /** |
||
739 | * Create a share object |
||
740 | * |
||
741 | * @param array $data |
||
742 | * |
||
743 | * @return IShare |
||
744 | * @throws IllegalIDChangeException |
||
745 | */ |
||
746 | private function createShareObject($data) { |
||
769 | |||
770 | |||
771 | /** |
||
772 | * @param IShare $share |
||
773 | * @param $data |
||
774 | */ |
||
775 | private function assignShareObjectPropertiesFromParent(IShare $share, $data) { |
||
788 | |||
789 | |||
790 | /** |
||
791 | * @param IShare $share |
||
792 | * @param $data |
||
793 | * |
||
794 | * @throws NoUserException |
||
795 | */ |
||
796 | private function assignShareObjectSharesProperties(IShare $share, $data) { |
||
823 | |||
824 | |||
825 | /** |
||
826 | * @param IShare $share |
||
827 | * |
||
828 | * @return Exception |
||
829 | * @throws NotFoundException |
||
830 | */ |
||
831 | private function errorShareAlreadyExist($share) { |
||
843 | |||
844 | |||
845 | /** |
||
846 | * Get the access list to the array of provided nodes. |
||
847 | * |
||
848 | * @param Node[] $nodes The list of nodes to get access for |
||
849 | * @param bool $currentAccess If current access is required (like for removed shares that might |
||
850 | * get revived later) |
||
851 | * |
||
852 | * @return array |
||
853 | * @throws InvalidPathException |
||
854 | * @throws NotFoundException |
||
855 | * @see IManager::getAccessList() for sample docs |
||
856 | * |
||
857 | * @since 12 |
||
858 | */ |
||
859 | public function getAccessList($nodes, $currentAccess) { |
||
876 | |||
877 | |||
878 | /** |
||
879 | * Restore a share for a given recipient. The implementation could be provider independant. |
||
880 | * |
||
881 | * @param IShare $share |
||
882 | * @param string $recipient |
||
883 | * |
||
884 | * @return IShare The restored share object |
||
885 | * |
||
886 | * @since 14.0.0 |
||
887 | */ |
||
888 | public function restore(IShare $share, string $recipient): IShare { |
||
891 | |||
892 | |||
893 | /** |
||
894 | * return array regarding getAccessList format. |
||
895 | * ie. \OC\Share20\Manager::getAccessList() |
||
896 | * |
||
897 | * @param IQueryBuilder $qb |
||
898 | * |
||
899 | * @return array |
||
900 | */ |
||
901 | private function parseAccessListResult(IQueryBuilder $qb) { |
||
920 | |||
921 | |||
922 | /** |
||
923 | * @param IShare $share |
||
924 | * |
||
925 | * @return array |
||
926 | * @throws NotFoundException |
||
927 | */ |
||
928 | private function shareObjectToArray(IShare $share) { |
||
940 | |||
941 | |||
942 | /** |
||
943 | * @param string $k |
||
944 | * @param array $arr |
||
945 | * @param string $default |
||
946 | * |
||
947 | * @return string |
||
948 | */ |
||
949 | protected function get($k, array $arr, string $default = ''): string { |
||
978 | |||
979 | |||
980 | /** |
||
981 | * @inheritDoc |
||
982 | */ |
||
983 | public function getAllShares(): iterable { |
||
1005 | |||
1006 | |||
1007 | /** |
||
1008 | * @param int $length |
||
1009 | * |
||
1010 | * @return string |
||
1011 | */ |
||
1012 | View Code Duplication | protected function token(int $length = 15): string { |
|
1026 | |||
1027 | |||
1028 | } |
||
1029 |
This method has been deprecated.