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 |
||
| 58 | class CirclesService { |
||
| 59 | |||
| 60 | |||
| 61 | use TArrayTools; |
||
| 62 | |||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | private $userId; |
||
| 66 | |||
| 67 | /** @var IL10N */ |
||
| 68 | private $l10n; |
||
| 69 | |||
| 70 | /** @var IGroupManager */ |
||
| 71 | private $groupManager; |
||
| 72 | |||
| 73 | /** @var MembersService */ |
||
| 74 | private $membersService; |
||
| 75 | |||
| 76 | /** @var ConfigService */ |
||
| 77 | private $configService; |
||
| 78 | |||
| 79 | /** @var CirclesRequest */ |
||
| 80 | private $circlesRequest; |
||
| 81 | |||
| 82 | /** @var MembersRequest */ |
||
| 83 | private $membersRequest; |
||
| 84 | |||
| 85 | /** @var TokensRequest */ |
||
| 86 | private $tokensRequest; |
||
| 87 | |||
| 88 | /** @var SharesRequest */ |
||
| 89 | private $sharesRequest; |
||
| 90 | |||
| 91 | /** @var FederatedLinksRequest */ |
||
| 92 | private $federatedLinksRequest; |
||
| 93 | |||
| 94 | /** @var GSUpstreamService */ |
||
| 95 | private $gsUpstreamService; |
||
| 96 | |||
| 97 | /** @var EventsService */ |
||
| 98 | private $eventsService; |
||
| 99 | |||
| 100 | /** @var CircleProviderRequest */ |
||
| 101 | private $circleProviderRequest; |
||
| 102 | |||
| 103 | /** @var MiscService */ |
||
| 104 | private $miscService; |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * CirclesService constructor. |
||
| 109 | * |
||
| 110 | * @param string $userId |
||
| 111 | * @param IL10N $l10n |
||
| 112 | * @param IUserSession $userSession |
||
| 113 | * @param IGroupManager $groupManager |
||
| 114 | * @param MembersService $membersService |
||
| 115 | * @param ConfigService $configService |
||
| 116 | * @param CirclesRequest $circlesRequest |
||
| 117 | * @param MembersRequest $membersRequest |
||
| 118 | * @param TokensRequest $tokensRequest |
||
| 119 | * @param SharesRequest $sharesRequest |
||
| 120 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 121 | * @param GSUpstreamService $gsUpstreamService |
||
| 122 | * @param EventsService $eventsService |
||
| 123 | * @param CircleProviderRequest $circleProviderRequest |
||
| 124 | * @param MiscService $miscService |
||
| 125 | */ |
||
| 126 | public function __construct( |
||
| 127 | $userId, |
||
| 128 | IL10N $l10n, |
||
| 129 | IUserSession $userSession, |
||
| 130 | IGroupManager $groupManager, |
||
| 131 | MembersService $membersService, |
||
| 132 | ConfigService $configService, |
||
| 133 | CirclesRequest $circlesRequest, |
||
| 134 | MembersRequest $membersRequest, |
||
| 135 | TokensRequest $tokensRequest, |
||
| 136 | SharesRequest $sharesRequest, |
||
| 137 | FederatedLinksRequest $federatedLinksRequest, |
||
| 138 | GSUpstreamService $gsUpstreamService, |
||
| 139 | EventsService $eventsService, |
||
| 140 | CircleProviderRequest $circleProviderRequest, |
||
| 141 | MiscService $miscService |
||
| 142 | ) { |
||
| 143 | |||
| 144 | if ($userId === null) { |
||
| 145 | $user = $userSession->getUser(); |
||
| 146 | if ($user !== null) { |
||
| 147 | $userId = $user->getUID(); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | $this->userId = $userId; |
||
| 152 | $this->l10n = $l10n; |
||
| 153 | $this->groupManager = $groupManager; |
||
| 154 | $this->membersService = $membersService; |
||
| 155 | $this->configService = $configService; |
||
| 156 | $this->circlesRequest = $circlesRequest; |
||
| 157 | $this->membersRequest = $membersRequest; |
||
| 158 | $this->tokensRequest = $tokensRequest; |
||
| 159 | $this->sharesRequest = $sharesRequest; |
||
| 160 | $this->federatedLinksRequest = $federatedLinksRequest; |
||
| 161 | $this->gsUpstreamService = $gsUpstreamService; |
||
| 162 | $this->eventsService = $eventsService; |
||
| 163 | $this->circleProviderRequest = $circleProviderRequest; |
||
| 164 | $this->miscService = $miscService; |
||
| 165 | } |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Create circle using this->userId as owner |
||
| 170 | * |
||
| 171 | * @param int|string $type |
||
| 172 | * @param string $name |
||
| 173 | * |
||
| 174 | * @param string $ownerId |
||
| 175 | * |
||
| 176 | * @return Circle |
||
| 177 | * @throws CircleAlreadyExistsException |
||
| 178 | * @throws CircleTypeDisabledException |
||
| 179 | * @throws Exception |
||
| 180 | */ |
||
| 181 | public function createCircle($type, $name, string $ownerId = '') { |
||
| 225 | |||
| 226 | |||
| 227 | /** |
||
| 228 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 229 | * |
||
| 230 | * @param string $userId |
||
| 231 | * @param mixed $type |
||
| 232 | * @param string $name |
||
| 233 | * @param int $level |
||
| 234 | * |
||
| 235 | * @param bool $forceAll |
||
| 236 | * |
||
| 237 | * @return Circle[] |
||
| 238 | * @throws CircleTypeDisabledException |
||
| 239 | * @throws Exception |
||
| 240 | */ |
||
| 241 | public function listCircles($userId, $type, $name = '', $level = 0, $forceAll = false) { |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * returns details on circle and its members if this->userId is a member itself. |
||
| 266 | * |
||
| 267 | * @param string $circleUniqueId |
||
| 268 | * @param bool $forceAll |
||
| 269 | * |
||
| 270 | * @return Circle |
||
| 271 | * @throws Exception |
||
| 272 | */ |
||
| 273 | public function detailsCircle($circleUniqueId, $forceAll = false) { |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * get the Members list and add the result to the Circle. |
||
| 302 | * |
||
| 303 | * @param Circle $circle |
||
| 304 | * |
||
| 305 | * @throws Exception |
||
| 306 | */ |
||
| 307 | private function detailsCircleMembers(Circle $circle) { |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * // TODO - check this on GS setup |
||
| 322 | * get the Linked Group list and add the result to the Circle. |
||
| 323 | * |
||
| 324 | * @param Circle $circle |
||
| 325 | * |
||
| 326 | * @throws GSStatusException |
||
| 327 | */ |
||
| 328 | private function detailsCircleLinkedGroups(Circle $circle) { |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * get the Federated Circles list and add the result to the Circle. |
||
| 343 | * |
||
| 344 | * @param Circle $circle |
||
| 345 | */ |
||
| 346 | private function detailsCircleFederatedCircles(Circle $circle) { |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * save new settings if current user is admin. |
||
| 363 | * |
||
| 364 | * @param string $circleUniqueId |
||
| 365 | * @param array $settings |
||
| 366 | * |
||
| 367 | * @return Circle |
||
| 368 | * @throws Exception |
||
| 369 | */ |
||
| 370 | public function settingsCircle(string $circleUniqueId, array $settings) { |
||
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * @param Circle $circle |
||
| 408 | */ |
||
| 409 | public function updatePasswordOnShares(Circle $circle) { |
||
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * Join a circle. |
||
| 416 | * |
||
| 417 | * @param string $circleUniqueId |
||
| 418 | * |
||
| 419 | * @return null|Member |
||
| 420 | * @throws Exception |
||
| 421 | */ |
||
| 422 | public function joinCircle($circleUniqueId): Member { |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * Leave a circle. |
||
| 445 | * |
||
| 446 | * @param string $circleUniqueId |
||
| 447 | * |
||
| 448 | * @return null|Member |
||
| 449 | * @throws Exception |
||
| 450 | */ |
||
| 451 | public function leaveCircle($circleUniqueId) { |
||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * destroy a circle. |
||
| 466 | * |
||
| 467 | * @param string $circleUniqueId |
||
| 468 | * |
||
| 469 | * @param bool $force |
||
| 470 | * |
||
| 471 | * @throws CircleDoesNotExistException |
||
| 472 | * @throws MemberIsNotOwnerException |
||
| 473 | * @throws ConfigNoCircleAvailableException |
||
| 474 | * @throws Exception |
||
| 475 | */ |
||
| 476 | public function removeCircle($circleUniqueId, bool $force = false) { |
||
| 491 | |||
| 492 | |||
| 493 | |||
| 494 | |||
| 495 | /** |
||
| 496 | * @return Circle[] |
||
| 497 | */ |
||
| 498 | public function getCirclesToSync(): array { |
||
| 518 | |||
| 519 | |||
| 520 | /** |
||
| 521 | * @param $circleName |
||
| 522 | * |
||
| 523 | * @return Circle|null |
||
| 524 | * @throws CircleDoesNotExistException |
||
| 525 | */ |
||
| 526 | public function infoCircleByName($circleName) { |
||
| 529 | |||
| 530 | |||
| 531 | /** |
||
| 532 | * Convert a Type in String to its Bit Value |
||
| 533 | * |
||
| 534 | * @param string $type |
||
| 535 | * |
||
| 536 | * @return int|mixed |
||
| 537 | */ |
||
| 538 | public function convertTypeStringToBitValue($type) { |
||
| 553 | |||
| 554 | |||
| 555 | /** |
||
| 556 | * getCircleIcon() |
||
| 557 | * |
||
| 558 | * Return the right imagePath for a type of circle. |
||
| 559 | * |
||
| 560 | * @param string $type |
||
| 561 | * @param bool $png |
||
| 562 | * |
||
| 563 | * @return string |
||
| 564 | */ |
||
| 565 | public static function getCircleIcon($type, $png = false) { |
||
| 596 | |||
| 597 | |||
| 598 | /** |
||
| 599 | * @param string $circleUniqueIds |
||
| 600 | * @param int $limit |
||
| 601 | * @param int $offset |
||
| 602 | * |
||
| 603 | * @return array |
||
| 604 | * @throws GSStatusException |
||
| 605 | */ |
||
| 606 | public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { |
||
| 615 | |||
| 616 | |||
| 617 | /** |
||
| 618 | * @param Circle $circle |
||
| 619 | * |
||
| 620 | * @throws MembersLimitException |
||
| 621 | */ |
||
| 622 | public function checkThatCircleIsNotFull(Circle $circle) { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * @return bool |
||
| 644 | */ |
||
| 645 | public function viewerIsAdmin(): bool { |
||
| 652 | |||
| 653 | |||
| 654 | /** |
||
| 655 | * should be moved. |
||
| 656 | * |
||
| 657 | * @param Member $member |
||
| 658 | * |
||
| 659 | * @throws MemberIsNotOwnerException |
||
| 660 | */ |
||
| 661 | View Code Duplication | public function hasToBeOwner(Member $member) { |
|
| 669 | |||
| 670 | |||
| 671 | /** |
||
| 672 | * should be moved. |
||
| 673 | * |
||
| 674 | * @param Member $member |
||
| 675 | * |
||
| 676 | * @throws MemberIsNotOwnerException |
||
| 677 | */ |
||
| 678 | View Code Duplication | public function hasToBeAdmin(Member $member) { |
|
| 686 | |||
| 687 | } |
||
| 688 |