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 |
||
| 53 | class CirclesService { |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $userId; |
||
| 57 | |||
| 58 | /** @var IL10N */ |
||
| 59 | private $l10n; |
||
| 60 | |||
| 61 | /** @var IGroupManager */ |
||
| 62 | private $groupManager; |
||
| 63 | |||
| 64 | /** @var ConfigService */ |
||
| 65 | private $configService; |
||
| 66 | |||
| 67 | /** @var CirclesRequest */ |
||
| 68 | private $circlesRequest; |
||
| 69 | |||
| 70 | /** @var MembersRequest */ |
||
| 71 | private $membersRequest; |
||
| 72 | |||
| 73 | /** @var SharesRequest */ |
||
| 74 | private $sharesRequest; |
||
| 75 | |||
| 76 | /** @var FederatedLinksRequest */ |
||
| 77 | private $federatedLinksRequest; |
||
| 78 | |||
| 79 | /** @var EventsService */ |
||
| 80 | private $eventsService; |
||
| 81 | |||
| 82 | /** @var CircleProviderRequest */ |
||
| 83 | private $circleProviderRequest; |
||
| 84 | |||
| 85 | /** @var MiscService */ |
||
| 86 | private $miscService; |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * CirclesService constructor. |
||
| 91 | * |
||
| 92 | * @param string $userId |
||
| 93 | * @param IL10N $l10n |
||
| 94 | * @param IGroupManager $groupManager |
||
| 95 | * @param ConfigService $configService |
||
| 96 | * @param CirclesRequest $circlesRequest |
||
| 97 | * @param MembersRequest $membersRequest |
||
| 98 | * @param SharesRequest $sharesRequest |
||
| 99 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 100 | * @param EventsService $eventsService |
||
| 101 | * @param CircleProviderRequest $circleProviderRequest |
||
| 102 | * @param MiscService $miscService |
||
| 103 | */ |
||
| 104 | public function __construct( |
||
| 105 | $userId, |
||
| 106 | IL10N $l10n, |
||
| 107 | IGroupManager $groupManager, |
||
| 108 | ConfigService $configService, |
||
| 109 | CirclesRequest $circlesRequest, |
||
| 110 | MembersRequest $membersRequest, |
||
| 111 | SharesRequest $sharesRequest, |
||
| 112 | FederatedLinksRequest $federatedLinksRequest, |
||
| 113 | EventsService $eventsService, |
||
| 114 | CircleProviderRequest $circleProviderRequest, |
||
| 115 | MiscService $miscService |
||
| 116 | ) { |
||
| 117 | $this->userId = $userId; |
||
| 118 | $this->l10n = $l10n; |
||
| 119 | $this->groupManager = $groupManager; |
||
| 120 | $this->configService = $configService; |
||
| 121 | $this->circlesRequest = $circlesRequest; |
||
| 122 | $this->membersRequest = $membersRequest; |
||
| 123 | $this->sharesRequest = $sharesRequest; |
||
| 124 | $this->federatedLinksRequest = $federatedLinksRequest; |
||
| 125 | $this->eventsService = $eventsService; |
||
| 126 | $this->circleProviderRequest = $circleProviderRequest; |
||
| 127 | $this->miscService = $miscService; |
||
| 128 | } |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * Create circle using this->userId as owner |
||
| 133 | * |
||
| 134 | * @param int|string $type |
||
| 135 | * @param string $name |
||
| 136 | * |
||
| 137 | * @return Circle |
||
| 138 | * @throws CircleTypeDisabledException |
||
| 139 | * @throws \Exception |
||
| 140 | */ |
||
| 141 | public function createCircle($type, $name) { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 174 | * |
||
| 175 | * @param string $userId |
||
| 176 | * @param mixed $type |
||
| 177 | * @param string $name |
||
| 178 | * @param int $level |
||
| 179 | * |
||
| 180 | * @return Circle[] |
||
| 181 | * @throws CircleTypeDisabledException |
||
| 182 | * @throws Exception |
||
| 183 | */ |
||
| 184 | public function listCircles($userId, $type, $name = '', $level = 0) { |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * returns details on circle and its members if this->userId is a member itself. |
||
| 209 | * |
||
| 210 | * @param string $circleUniqueId |
||
| 211 | * |
||
| 212 | * @return Circle |
||
| 213 | * @throws \Exception |
||
| 214 | */ |
||
| 215 | public function detailsCircle($circleUniqueId) { |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * get the Members list and add the result to the Circle. |
||
| 237 | * |
||
| 238 | * @param Circle $circle |
||
| 239 | * |
||
| 240 | * @throws Exception |
||
| 241 | */ |
||
| 242 | private function detailsCircleMembers(Circle &$circle) { |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * get the Linked Group list and add the result to the Circle. |
||
| 257 | * |
||
| 258 | * @param Circle $circle |
||
| 259 | * |
||
| 260 | * @throws MemberDoesNotExistException |
||
| 261 | */ |
||
| 262 | private function detailsCircleLinkedGroups(Circle &$circle) { |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * get the Federated Circles list and add the result to the Circle. |
||
| 277 | * |
||
| 278 | * @param Circle $circle |
||
| 279 | */ |
||
| 280 | private function detailsCircleFederatedCircles(Circle &$circle) { |
||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * save new settings if current user is admin. |
||
| 297 | * |
||
| 298 | * @param string $circleUniqueId |
||
| 299 | * @param array $settings |
||
| 300 | * |
||
| 301 | * @return Circle |
||
| 302 | * @throws \Exception |
||
| 303 | */ |
||
| 304 | public function settingsCircle($circleUniqueId, $settings) { |
||
| 328 | |||
| 329 | |||
| 330 | /** |
||
| 331 | * Join a circle. |
||
| 332 | * |
||
| 333 | * @param string $circleUniqueId |
||
| 334 | * |
||
| 335 | * @return null|Member |
||
| 336 | * @throws \Exception |
||
| 337 | */ |
||
| 338 | View Code Duplication | public function joinCircle($circleUniqueId) { |
|
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * Leave a circle. |
||
| 363 | * |
||
| 364 | * @param string $circleUniqueId |
||
| 365 | * |
||
| 366 | * @return null|Member |
||
| 367 | * @throws \Exception |
||
| 368 | */ |
||
| 369 | public function leaveCircle($circleUniqueId) { |
||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * destroy a circle. |
||
| 388 | * |
||
| 389 | * @param string $circleUniqueId |
||
| 390 | * |
||
| 391 | * @throws CircleDoesNotExistException |
||
| 392 | * @throws MemberIsNotModeratorException |
||
| 393 | * @throws MemberIsNotOwnerException |
||
| 394 | */ |
||
| 395 | public function removeCircle($circleUniqueId) { |
||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * @param $circleName |
||
| 410 | * |
||
| 411 | * @return Circle|null |
||
| 412 | * @throws CircleDoesNotExistException |
||
| 413 | */ |
||
| 414 | public function infoCircleByName($circleName) { |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * When a user is removed. |
||
| 421 | * Before deleting a user from the cloud, we assign a new owner to his Circles. |
||
| 422 | * Remove the Circle if it has no admin. |
||
| 423 | * |
||
| 424 | * @param string $userId |
||
| 425 | */ |
||
| 426 | public function onUserRemoved($userId) { |
||
| 442 | |||
| 443 | |||
| 444 | /** |
||
| 445 | * switchOlderAdminToOwner(); |
||
| 446 | * |
||
| 447 | * @param Circle $circle |
||
| 448 | * @param Member[] $members |
||
| 449 | */ |
||
| 450 | private function switchOlderAdminToOwner(Circle $circle, $members) { |
||
| 463 | |||
| 464 | |||
| 465 | /** |
||
| 466 | * Convert a Type in String to its Bit Value |
||
| 467 | * |
||
| 468 | * @param string $type |
||
| 469 | * |
||
| 470 | * @return int|mixed |
||
| 471 | */ |
||
| 472 | public function convertTypeStringToBitValue($type) { |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * getCircleIcon() |
||
| 491 | * |
||
| 492 | * Return the right imagePath for a type of circle. |
||
| 493 | * |
||
| 494 | * @param string $type |
||
| 495 | * @param bool $png |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public static function getCircleIcon($type, $png = false) { |
||
| 530 | |||
| 531 | |||
| 532 | /** |
||
| 533 | * @param string $circleUniqueIds |
||
| 534 | * @param int $limit |
||
| 535 | * @param int $offset |
||
| 536 | * |
||
| 537 | * @return array |
||
| 538 | */ |
||
| 539 | public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { |
||
| 550 | |||
| 551 | |||
| 552 | /** |
||
| 553 | * @param Circle $circle |
||
| 554 | * |
||
| 555 | * @throws MembersLimitException |
||
| 556 | */ |
||
| 557 | public function checkThatCircleIsNotFull(Circle $circle) { |
||
| 558 | |||
| 559 | $members = $this->membersRequest->forceGetMembers( |
||
| 560 | $circle->getUniqueId(), Member::STATUS_MEMBER, true |
||
| 561 | ); |
||
| 562 | |||
| 563 | $limit = $circle->getSetting('members_limit'); |
||
| 564 | if ($limit === -1) { |
||
| 565 | return; |
||
| 566 | } |
||
| 567 | if ($limit === 0 || $limit === '' || $limit === null) { |
||
| 568 | $limit = $this->configService->getAppValue(ConfigService::CIRCLES_MEMBERS_LIMIT); |
||
| 569 | } |
||
| 570 | |||
| 571 | if (sizeof($members) >= $limit) { |
||
| 572 | throw new MembersLimitException( |
||
| 573 | 'This circle already reach its limit on the number of members' |
||
| 574 | ); |
||
| 575 | } |
||
| 576 | |||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @return bool |
||
| 581 | */ |
||
| 582 | public function viewerIsAdmin() { |
||
| 589 | |||
| 590 | |||
| 591 | /** |
||
| 592 | * should be moved. |
||
| 593 | * |
||
| 594 | * @param Member $member |
||
| 595 | * |
||
| 596 | * @throws MemberIsNotOwnerException |
||
| 597 | */ |
||
| 598 | View Code Duplication | public function hasToBeOwner(Member $member) { |
|
| 606 | |||
| 607 | |||
| 608 | /** |
||
| 609 | * should be moved. |
||
| 610 | * |
||
| 611 | * @param Member $member |
||
| 612 | * |
||
| 613 | * @throws MemberIsNotOwnerException |
||
| 614 | */ |
||
| 615 | View Code Duplication | public function hasToBeAdmin(Member $member) { |
|
| 623 | } |