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 |
||
| 57 | class CirclesService { |
||
| 58 | |||
| 59 | /** @var string */ |
||
| 60 | private $userId; |
||
| 61 | |||
| 62 | /** @var IL10N */ |
||
| 63 | private $l10n; |
||
| 64 | |||
| 65 | /** @var IGroupManager */ |
||
| 66 | private $groupManager; |
||
| 67 | |||
| 68 | /** @var ConfigService */ |
||
| 69 | private $configService; |
||
| 70 | |||
| 71 | /** @var CirclesRequest */ |
||
| 72 | private $circlesRequest; |
||
| 73 | |||
| 74 | /** @var MembersRequest */ |
||
| 75 | private $membersRequest; |
||
| 76 | |||
| 77 | /** @var SharesRequest */ |
||
| 78 | private $sharesRequest; |
||
| 79 | |||
| 80 | /** @var FederatedLinksRequest */ |
||
| 81 | private $federatedLinksRequest; |
||
| 82 | |||
| 83 | /** @var GSUpstreamService */ |
||
| 84 | private $gsUpstreamService; |
||
| 85 | |||
| 86 | /** @var EventsService */ |
||
| 87 | private $eventsService; |
||
| 88 | |||
| 89 | /** @var CircleProviderRequest */ |
||
| 90 | private $circleProviderRequest; |
||
| 91 | |||
| 92 | /** @var MiscService */ |
||
| 93 | private $miscService; |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * CirclesService constructor. |
||
| 98 | * |
||
| 99 | * @param string $userId |
||
| 100 | * @param IL10N $l10n |
||
| 101 | * @param IGroupManager $groupManager |
||
| 102 | * @param ConfigService $configService |
||
| 103 | * @param CirclesRequest $circlesRequest |
||
| 104 | * @param MembersRequest $membersRequest |
||
| 105 | * @param SharesRequest $sharesRequest |
||
| 106 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 107 | * @param GSUpstreamService $gsUpstreamService |
||
| 108 | * @param EventsService $eventsService |
||
| 109 | * @param CircleProviderRequest $circleProviderRequest |
||
| 110 | * @param MiscService $miscService |
||
| 111 | */ |
||
| 112 | View Code Duplication | public function __construct( |
|
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * Create circle using this->userId as owner |
||
| 143 | * |
||
| 144 | * @param int|string $type |
||
| 145 | * @param string $name |
||
| 146 | * |
||
| 147 | * @param string $ownerId |
||
| 148 | * |
||
| 149 | * @return Circle |
||
| 150 | * @throws CircleAlreadyExistsException |
||
| 151 | * @throws CircleTypeDisabledException |
||
| 152 | * @throws MemberAlreadyExistsException |
||
| 153 | * @throws Exception |
||
| 154 | */ |
||
| 155 | public function createCircle($type, $name, string $ownerId = '') { |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 215 | * |
||
| 216 | * @param string $userId |
||
| 217 | * @param mixed $type |
||
| 218 | * @param string $name |
||
| 219 | * @param int $level |
||
| 220 | * |
||
| 221 | * @param bool $forceAll |
||
| 222 | * |
||
| 223 | * @return Circle[] |
||
| 224 | * @throws CircleTypeDisabledException |
||
| 225 | * @throws Exception |
||
| 226 | */ |
||
| 227 | public function listCircles($userId, $type, $name = '', $level = 0, $forceAll = false) { |
||
| 248 | |||
| 249 | |||
| 250 | /** |
||
| 251 | * returns details on circle and its members if this->userId is a member itself. |
||
| 252 | * |
||
| 253 | * @param string $circleUniqueId |
||
| 254 | * @param bool $forceAll |
||
| 255 | * |
||
| 256 | * @return Circle |
||
| 257 | * @throws Exception |
||
| 258 | */ |
||
| 259 | public function detailsCircle($circleUniqueId, $forceAll = false) { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * get the Members list and add the result to the Circle. |
||
| 282 | * |
||
| 283 | * @param Circle $circle |
||
| 284 | * |
||
| 285 | * @throws Exception |
||
| 286 | */ |
||
| 287 | private function detailsCircleMembers(Circle &$circle) { |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * get the Linked Group list and add the result to the Circle. |
||
| 302 | * |
||
| 303 | * @param Circle $circle |
||
| 304 | * |
||
| 305 | * @throws MemberDoesNotExistException |
||
| 306 | */ |
||
| 307 | private function detailsCircleLinkedGroups(Circle &$circle) { |
||
| 318 | |||
| 319 | |||
| 320 | /** |
||
| 321 | * get the Federated Circles list and add the result to the Circle. |
||
| 322 | * |
||
| 323 | * @param Circle $circle |
||
| 324 | */ |
||
| 325 | private function detailsCircleFederatedCircles(Circle &$circle) { |
||
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * save new settings if current user is admin. |
||
| 342 | * |
||
| 343 | * @param string $circleUniqueId |
||
| 344 | * @param array $settings |
||
| 345 | * |
||
| 346 | * @return Circle |
||
| 347 | * @throws Exception |
||
| 348 | */ |
||
| 349 | public function settingsCircle($circleUniqueId, $settings) { |
||
| 373 | |||
| 374 | |||
| 375 | /** |
||
| 376 | * Join a circle. |
||
| 377 | * |
||
| 378 | * @param string $circleUniqueId |
||
| 379 | * |
||
| 380 | * @return null|Member |
||
| 381 | * @throws Exception |
||
| 382 | */ |
||
| 383 | public function joinCircle($circleUniqueId) { |
||
| 400 | |||
| 401 | |||
| 402 | /** |
||
| 403 | * Leave a circle. |
||
| 404 | * |
||
| 405 | * @param string $circleUniqueId |
||
| 406 | * |
||
| 407 | * @return null|Member |
||
| 408 | * @throws Exception |
||
| 409 | */ |
||
| 410 | public function leaveCircle($circleUniqueId) { |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * destroy a circle. |
||
| 425 | * |
||
| 426 | * @param string $circleUniqueId |
||
| 427 | * |
||
| 428 | * @param bool $force |
||
| 429 | * |
||
| 430 | * @throws CircleDoesNotExistException |
||
| 431 | * @throws MemberIsNotOwnerException |
||
| 432 | * @throws ConfigNoCircleAvailableException |
||
| 433 | */ |
||
| 434 | public function removeCircle($circleUniqueId, bool $force = false) { |
||
| 448 | |||
| 449 | |||
| 450 | /** |
||
| 451 | * @param $circleName |
||
| 452 | * |
||
| 453 | * @return Circle|null |
||
| 454 | * @throws CircleDoesNotExistException |
||
| 455 | */ |
||
| 456 | public function infoCircleByName($circleName) { |
||
| 459 | |||
| 460 | |||
| 461 | /** |
||
| 462 | * When a user is removed. |
||
| 463 | * Before deleting a user from the cloud, we assign a new owner to his Circles. |
||
| 464 | * Remove the Circle if it has no admin. |
||
| 465 | * |
||
| 466 | * @param string $userId |
||
| 467 | */ |
||
| 468 | public function onUserRemoved($userId) { |
||
| 484 | |||
| 485 | |||
| 486 | /** |
||
| 487 | * switchOlderAdminToOwner(); |
||
| 488 | * |
||
| 489 | * @param Circle $circle |
||
| 490 | * @param Member[] $members |
||
| 491 | */ |
||
| 492 | private function switchOlderAdminToOwner(Circle $circle, $members) { |
||
| 505 | |||
| 506 | |||
| 507 | /** |
||
| 508 | * Convert a Type in String to its Bit Value |
||
| 509 | * |
||
| 510 | * @param string $type |
||
| 511 | * |
||
| 512 | * @return int|mixed |
||
| 513 | */ |
||
| 514 | public function convertTypeStringToBitValue($type) { |
||
| 529 | |||
| 530 | |||
| 531 | /** |
||
| 532 | * getCircleIcon() |
||
| 533 | * |
||
| 534 | * Return the right imagePath for a type of circle. |
||
| 535 | * |
||
| 536 | * @param string $type |
||
| 537 | * @param bool $png |
||
| 538 | * |
||
| 539 | * @return string |
||
| 540 | */ |
||
| 541 | public static function getCircleIcon($type, $png = false) { |
||
| 572 | |||
| 573 | |||
| 574 | /** |
||
| 575 | * @param string $circleUniqueIds |
||
| 576 | * @param int $limit |
||
| 577 | * @param int $offset |
||
| 578 | * |
||
| 579 | * @return array |
||
| 580 | */ |
||
| 581 | public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { |
||
| 592 | |||
| 593 | |||
| 594 | /** |
||
| 595 | * @param Circle $circle |
||
| 596 | * |
||
| 597 | * @throws MembersLimitException |
||
| 598 | */ |
||
| 599 | public function checkThatCircleIsNotFull(Circle $circle) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @return bool |
||
| 623 | */ |
||
| 624 | public function viewerIsAdmin() { |
||
| 631 | |||
| 632 | |||
| 633 | /** |
||
| 634 | * should be moved. |
||
| 635 | * |
||
| 636 | * @param Member $member |
||
| 637 | * |
||
| 638 | * @throws MemberIsNotOwnerException |
||
| 639 | */ |
||
| 640 | View Code Duplication | public function hasToBeOwner(Member $member) { |
|
| 648 | |||
| 649 | |||
| 650 | /** |
||
| 651 | * should be moved. |
||
| 652 | * |
||
| 653 | * @param Member $member |
||
| 654 | * |
||
| 655 | * @throws MemberIsNotOwnerException |
||
| 656 | */ |
||
| 657 | View Code Duplication | public function hasToBeAdmin(Member $member) { |
|
| 665 | } |
||
| 666 |