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 CirclesService 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 CirclesService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
65 | class CirclesService { |
||
66 | |||
67 | |||
68 | use TArrayTools; |
||
69 | |||
70 | |||
71 | /** @var string */ |
||
72 | private $userId; |
||
73 | |||
74 | /** @var IL10N */ |
||
75 | private $l10n; |
||
76 | |||
77 | /** @var IGroupManager */ |
||
78 | private $groupManager; |
||
79 | |||
80 | /** @var MembersService */ |
||
81 | private $membersService; |
||
82 | |||
83 | /** @var ConfigService */ |
||
84 | private $configService; |
||
85 | |||
86 | /** @var DeprecatedCirclesRequest */ |
||
87 | private $circlesRequest; |
||
88 | |||
89 | /** @var DeprecatedMembersRequest */ |
||
90 | private $membersRequest; |
||
91 | |||
92 | /** @var TokensRequest */ |
||
93 | private $tokensRequest; |
||
94 | |||
95 | /** @var FileSharesRequest */ |
||
96 | private $fileSharesRequest; |
||
97 | |||
98 | /** @var FederatedLinksRequest */ |
||
99 | private $federatedLinksRequest; |
||
100 | |||
101 | /** @var GSUpstreamService */ |
||
102 | private $gsUpstreamService; |
||
103 | |||
104 | /** @var EventsService */ |
||
105 | private $eventsService; |
||
106 | |||
107 | /** @var CircleProviderRequest */ |
||
108 | private $circleProviderRequest; |
||
109 | |||
110 | /** @var MiscService */ |
||
111 | private $miscService; |
||
112 | |||
113 | |||
114 | /** |
||
115 | * CirclesService constructor. |
||
116 | * |
||
117 | * @param string $userId |
||
118 | * @param IL10N $l10n |
||
119 | * @param IUserSession $userSession |
||
120 | * @param IGroupManager $groupManager |
||
121 | * @param MembersService $membersService |
||
122 | * @param ConfigService $configService |
||
123 | * @param DeprecatedCirclesRequest $circlesRequest |
||
124 | * @param DeprecatedMembersRequest $membersRequest |
||
125 | * @param TokensRequest $tokensRequest |
||
126 | * @param FileSharesRequest $fileSharesRequest |
||
127 | * @param FederatedLinksRequest $federatedLinksRequest |
||
128 | * @param GSUpstreamService $gsUpstreamService |
||
129 | * @param EventsService $eventsService |
||
130 | * @param CircleProviderRequest $circleProviderRequest |
||
131 | * @param MiscService $miscService |
||
132 | */ |
||
133 | public function __construct( |
||
173 | |||
174 | |||
175 | /** |
||
176 | * Create circle using this->userId as owner |
||
177 | * |
||
178 | * @param int|string $type |
||
179 | * @param string $name |
||
180 | * |
||
181 | * @param string $ownerId |
||
182 | * |
||
183 | * @return DeprecatedCircle |
||
184 | * @throws CircleAlreadyExistsException |
||
185 | * @throws CircleTypeDisabledException |
||
186 | * @throws Exception |
||
187 | */ |
||
188 | public function createCircle($type, $name, string $ownerId = '') { |
||
232 | |||
233 | |||
234 | /** |
||
235 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
236 | * |
||
237 | * @param string $userId |
||
238 | * @param mixed $type |
||
239 | * @param string $name |
||
240 | * @param int $level |
||
241 | * |
||
242 | * @param bool $forceAll |
||
243 | * |
||
244 | * @return DeprecatedCircle[] |
||
245 | * @throws CircleTypeDisabledException |
||
246 | * @throws Exception |
||
247 | */ |
||
248 | public function listCircles($userId, $type, $name = '', $level = 0, $forceAll = false) { |
||
269 | |||
270 | |||
271 | /** |
||
272 | * returns details on circle and its members if this->userId is a member itself. |
||
273 | * |
||
274 | * @param string $circleUniqueId |
||
275 | * @param bool $forceAll |
||
276 | * |
||
277 | * @return DeprecatedCircle |
||
278 | * @throws Exception |
||
279 | */ |
||
280 | public function detailsCircle($circleUniqueId, $forceAll = false) { |
||
304 | |||
305 | |||
306 | /** |
||
307 | * get the Members list and add the result to the Circle. |
||
308 | * |
||
309 | * @param DeprecatedCircle $circle |
||
310 | * |
||
311 | * @throws Exception |
||
312 | */ |
||
313 | private function detailsCircleMembers(Circle $circle, $forceAll = false) { |
||
324 | |||
325 | |||
326 | /** |
||
327 | * // TODO - check this on GS setup |
||
328 | * get the Linked Group list and add the result to the Circle. |
||
329 | * |
||
330 | * @param DeprecatedCircle $circle |
||
331 | * |
||
332 | * @throws GSStatusException |
||
333 | */ |
||
334 | private function detailsCircleLinkedGroups(DeprecatedCircle $circle) { |
||
345 | |||
346 | |||
347 | /** |
||
348 | * get the Federated Circles list and add the result to the Circle. |
||
349 | * |
||
350 | * @param DeprecatedCircle $circle |
||
351 | */ |
||
352 | private function detailsCircleFederatedCircles(DeprecatedCircle $circle) { |
||
365 | |||
366 | |||
367 | /** |
||
368 | * save new settings if current user is admin. |
||
369 | * |
||
370 | * @param string $circleUniqueId |
||
371 | * @param array $settings |
||
372 | * |
||
373 | * @return DeprecatedCircle |
||
374 | * @throws Exception |
||
375 | */ |
||
376 | public function settingsCircle(string $circleUniqueId, array $settings) { |
||
410 | |||
411 | |||
412 | /** |
||
413 | * @param DeprecatedCircle $circle |
||
414 | */ |
||
415 | public function updatePasswordOnShares(DeprecatedCircle $circle) { |
||
418 | |||
419 | |||
420 | /** |
||
421 | * Join a circle. |
||
422 | * |
||
423 | * @param string $circleUniqueId |
||
424 | * |
||
425 | * @return null|DeprecatedMember |
||
426 | * @throws Exception |
||
427 | */ |
||
428 | public function joinCircle($circleUniqueId): DeprecatedMember { |
||
447 | |||
448 | |||
449 | /** |
||
450 | * Leave a circle. |
||
451 | * |
||
452 | * @param string $circleUniqueId |
||
453 | * |
||
454 | * @return null|DeprecatedMember |
||
455 | * @throws Exception |
||
456 | */ |
||
457 | public function leaveCircle($circleUniqueId) { |
||
468 | |||
469 | |||
470 | /** |
||
471 | * destroy a circle. |
||
472 | * |
||
473 | * @param string $circleUniqueId |
||
474 | * |
||
475 | * @param bool $force |
||
476 | * |
||
477 | * @throws CircleDoesNotExistException |
||
478 | * @throws MemberIsNotOwnerException |
||
479 | * @throws ConfigNoCircleAvailableException |
||
480 | * @throws Exception |
||
481 | */ |
||
482 | public function removeCircle($circleUniqueId, bool $force = false) { |
||
497 | |||
498 | |||
499 | /** |
||
500 | * @return DeprecatedCircle[] |
||
501 | */ |
||
502 | public function getCirclesToSync(): array { |
||
522 | |||
523 | |||
524 | /** |
||
525 | * @param $circleName |
||
526 | * |
||
527 | * @return DeprecatedCircle|null |
||
528 | * @throws CircleDoesNotExistException |
||
529 | */ |
||
530 | public function infoCircleByName($circleName) { |
||
533 | |||
534 | |||
535 | /** |
||
536 | * Convert a Type in String to its Bit Value |
||
537 | * |
||
538 | * @param string $type |
||
539 | * |
||
540 | * @return int|mixed |
||
541 | */ |
||
542 | public function convertTypeStringToBitValue($type) { |
||
557 | |||
558 | |||
559 | /** |
||
560 | * getCircleIcon() |
||
561 | * |
||
562 | * Return the right imagePath for a type of circle. |
||
563 | * |
||
564 | * @param string $type |
||
565 | * @param bool $png |
||
566 | * |
||
567 | * @return string |
||
568 | */ |
||
569 | public static function getCircleIcon($type, $png = false) { |
||
600 | |||
601 | |||
602 | /** |
||
603 | * @param string $circleUniqueIds |
||
604 | * @param int $limit |
||
605 | * @param int $offset |
||
606 | * |
||
607 | * @return array |
||
608 | * @throws GSStatusException |
||
609 | */ |
||
610 | public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { |
||
619 | |||
620 | |||
621 | /** |
||
622 | * @param DeprecatedCircle $circle |
||
623 | * |
||
624 | * @throws MembersLimitException |
||
625 | */ |
||
626 | public function checkThatCircleIsNotFull(DeprecatedCircle $circle) { |
||
647 | |||
648 | /** |
||
649 | * @return bool |
||
650 | */ |
||
651 | public function viewerIsAdmin(): bool { |
||
658 | |||
659 | |||
660 | /** |
||
661 | * should be moved. |
||
662 | * |
||
663 | * @param DeprecatedMember $member |
||
664 | * |
||
665 | * @throws MemberIsNotOwnerException |
||
666 | */ |
||
667 | View Code Duplication | public function hasToBeOwner(DeprecatedMember $member) { |
|
675 | |||
676 | |||
677 | /** |
||
678 | * should be moved. |
||
679 | * |
||
680 | * @param DeprecatedMember $member |
||
681 | * |
||
682 | * @throws MemberIsNotOwnerException |
||
683 | */ |
||
684 | View Code Duplication | public function hasToBeAdmin(DeprecatedMember $member) { |
|
692 | |||
693 | } |
||
694 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.