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( |
||
| 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) { |
||
| 294 | |||
| 295 | |||
| 296 | /** |
||
| 297 | * get the Members list and add the result to the Circle. |
||
| 298 | * |
||
| 299 | * @param Circle $circle |
||
| 300 | * |
||
| 301 | * @throws Exception |
||
| 302 | */ |
||
| 303 | private function detailsCircleMembers(Circle $circle) { |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * // TODO - check this on GS setup |
||
| 318 | * get the Linked Group list and add the result to the Circle. |
||
| 319 | * |
||
| 320 | * @param Circle $circle |
||
| 321 | * |
||
| 322 | * @throws GSStatusException |
||
| 323 | */ |
||
| 324 | private function detailsCircleLinkedGroups(Circle $circle) { |
||
| 335 | |||
| 336 | |||
| 337 | /** |
||
| 338 | * get the Federated Circles list and add the result to the Circle. |
||
| 339 | * |
||
| 340 | * @param Circle $circle |
||
| 341 | */ |
||
| 342 | private function detailsCircleFederatedCircles(Circle $circle) { |
||
| 355 | |||
| 356 | |||
| 357 | /** |
||
| 358 | * save new settings if current user is admin. |
||
| 359 | * |
||
| 360 | * @param string $circleUniqueId |
||
| 361 | * @param array $settings |
||
| 362 | * |
||
| 363 | * @return Circle |
||
| 364 | * @throws Exception |
||
| 365 | */ |
||
| 366 | public function settingsCircle(string $circleUniqueId, array $settings) { |
||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * @param Circle $circle |
||
| 404 | */ |
||
| 405 | public function updatePasswordOnShares(Circle $circle) { |
||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * Join a circle. |
||
| 412 | * |
||
| 413 | * @param string $circleUniqueId |
||
| 414 | * |
||
| 415 | * @return null|Member |
||
| 416 | * @throws Exception |
||
| 417 | */ |
||
| 418 | public function joinCircle($circleUniqueId): Member { |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * Leave a circle. |
||
| 441 | * |
||
| 442 | * @param string $circleUniqueId |
||
| 443 | * |
||
| 444 | * @return null|Member |
||
| 445 | * @throws Exception |
||
| 446 | */ |
||
| 447 | public function leaveCircle($circleUniqueId) { |
||
| 458 | |||
| 459 | |||
| 460 | /** |
||
| 461 | * destroy a circle. |
||
| 462 | * |
||
| 463 | * @param string $circleUniqueId |
||
| 464 | * |
||
| 465 | * @param bool $force |
||
| 466 | * |
||
| 467 | * @throws CircleDoesNotExistException |
||
| 468 | * @throws MemberIsNotOwnerException |
||
| 469 | * @throws ConfigNoCircleAvailableException |
||
| 470 | * @throws Exception |
||
| 471 | */ |
||
| 472 | public function removeCircle($circleUniqueId, bool $force = false) { |
||
| 487 | |||
| 488 | |||
| 489 | |||
| 490 | |||
| 491 | /** |
||
| 492 | * @return Circle[] |
||
| 493 | */ |
||
| 494 | public function getCirclesToSync(): array { |
||
| 514 | |||
| 515 | |||
| 516 | /** |
||
| 517 | * @param $circleName |
||
| 518 | * |
||
| 519 | * @return Circle|null |
||
| 520 | * @throws CircleDoesNotExistException |
||
| 521 | */ |
||
| 522 | public function infoCircleByName($circleName) { |
||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * Convert a Type in String to its Bit Value |
||
| 529 | * |
||
| 530 | * @param string $type |
||
| 531 | * |
||
| 532 | * @return int|mixed |
||
| 533 | */ |
||
| 534 | public function convertTypeStringToBitValue($type) { |
||
| 549 | |||
| 550 | |||
| 551 | /** |
||
| 552 | * getCircleIcon() |
||
| 553 | * |
||
| 554 | * Return the right imagePath for a type of circle. |
||
| 555 | * |
||
| 556 | * @param string $type |
||
| 557 | * @param bool $png |
||
| 558 | * |
||
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | public static function getCircleIcon($type, $png = false) { |
||
| 592 | |||
| 593 | |||
| 594 | /** |
||
| 595 | * @param string $circleUniqueIds |
||
| 596 | * @param int $limit |
||
| 597 | * @param int $offset |
||
| 598 | * |
||
| 599 | * @return array |
||
| 600 | * @throws GSStatusException |
||
| 601 | */ |
||
| 602 | public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { |
||
| 611 | |||
| 612 | |||
| 613 | /** |
||
| 614 | * @param Circle $circle |
||
| 615 | * |
||
| 616 | * @throws MembersLimitException |
||
| 617 | */ |
||
| 618 | public function checkThatCircleIsNotFull(Circle $circle) { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * @return bool |
||
| 640 | */ |
||
| 641 | public function viewerIsAdmin(): bool { |
||
| 648 | |||
| 649 | |||
| 650 | /** |
||
| 651 | * should be moved. |
||
| 652 | * |
||
| 653 | * @param Member $member |
||
| 654 | * |
||
| 655 | * @throws MemberIsNotOwnerException |
||
| 656 | */ |
||
| 657 | View Code Duplication | public function hasToBeOwner(Member $member) { |
|
| 665 | |||
| 666 | |||
| 667 | /** |
||
| 668 | * should be moved. |
||
| 669 | * |
||
| 670 | * @param Member $member |
||
| 671 | * |
||
| 672 | * @throws MemberIsNotOwnerException |
||
| 673 | */ |
||
| 674 | View Code Duplication | public function hasToBeAdmin(Member $member) { |
|
| 682 | |||
| 683 | } |
||
| 684 |
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.