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 |
||
| 56 | class CirclesService { |
||
| 57 | |||
| 58 | /** @var string */ |
||
| 59 | private $userId; |
||
| 60 | |||
| 61 | /** @var IL10N */ |
||
| 62 | private $l10n; |
||
| 63 | |||
| 64 | /** @var IGroupManager */ |
||
| 65 | private $groupManager; |
||
| 66 | |||
| 67 | /** @var ConfigService */ |
||
| 68 | private $configService; |
||
| 69 | |||
| 70 | /** @var CirclesRequest */ |
||
| 71 | private $circlesRequest; |
||
| 72 | |||
| 73 | /** @var MembersRequest */ |
||
| 74 | private $membersRequest; |
||
| 75 | |||
| 76 | /** @var SharesRequest */ |
||
| 77 | private $sharesRequest; |
||
| 78 | |||
| 79 | /** @var FederatedLinksRequest */ |
||
| 80 | private $federatedLinksRequest; |
||
| 81 | |||
| 82 | /** @var GSUpstreamService */ |
||
| 83 | private $gsUpstreamService; |
||
| 84 | |||
| 85 | /** @var EventsService */ |
||
| 86 | private $eventsService; |
||
| 87 | |||
| 88 | /** @var CircleProviderRequest */ |
||
| 89 | private $circleProviderRequest; |
||
| 90 | |||
| 91 | /** @var MiscService */ |
||
| 92 | private $miscService; |
||
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * CirclesService constructor. |
||
| 97 | * |
||
| 98 | * @param string $userId |
||
| 99 | * @param IL10N $l10n |
||
| 100 | * @param IGroupManager $groupManager |
||
| 101 | * @param ConfigService $configService |
||
| 102 | * @param CirclesRequest $circlesRequest |
||
| 103 | * @param MembersRequest $membersRequest |
||
| 104 | * @param SharesRequest $sharesRequest |
||
| 105 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 106 | * @param GSUpstreamService $gsUpstreamService |
||
| 107 | * @param EventsService $eventsService |
||
| 108 | * @param CircleProviderRequest $circleProviderRequest |
||
| 109 | * @param MiscService $miscService |
||
| 110 | */ |
||
| 111 | View Code Duplication | public function __construct( |
|
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Create circle using this->userId as owner |
||
| 142 | * |
||
| 143 | * @param int|string $type |
||
| 144 | * @param string $name |
||
| 145 | * |
||
| 146 | * @param string $ownerId |
||
| 147 | * |
||
| 148 | * @return Circle |
||
| 149 | * @throws CircleAlreadyExistsException |
||
| 150 | * @throws CircleTypeDisabledException |
||
| 151 | * @throws MemberAlreadyExistsException |
||
| 152 | * @throws Exception |
||
| 153 | */ |
||
| 154 | public function createCircle($type, $name, string $ownerId = '') { |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 201 | * |
||
| 202 | * @param string $userId |
||
| 203 | * @param mixed $type |
||
| 204 | * @param string $name |
||
| 205 | * @param int $level |
||
| 206 | * |
||
| 207 | * @param bool $forceAll |
||
| 208 | * |
||
| 209 | * @return Circle[] |
||
| 210 | * @throws CircleTypeDisabledException |
||
| 211 | * @throws Exception |
||
| 212 | */ |
||
| 213 | public function listCircles($userId, $type, $name = '', $level = 0, $forceAll = false) { |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * returns details on circle and its members if this->userId is a member itself. |
||
| 238 | * |
||
| 239 | * @param string $circleUniqueId |
||
| 240 | * @param bool $forceAll |
||
| 241 | * |
||
| 242 | * @return Circle |
||
| 243 | * @throws Exception |
||
| 244 | */ |
||
| 245 | public function detailsCircle($circleUniqueId, $forceAll = false) { |
||
| 264 | |||
| 265 | |||
| 266 | /** |
||
| 267 | * get the Members list and add the result to the Circle. |
||
| 268 | * |
||
| 269 | * @param Circle $circle |
||
| 270 | * |
||
| 271 | * @throws Exception |
||
| 272 | */ |
||
| 273 | private function detailsCircleMembers(Circle &$circle) { |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * get the Linked Group list and add the result to the Circle. |
||
| 288 | * |
||
| 289 | * @param Circle $circle |
||
| 290 | * |
||
| 291 | * @throws MemberDoesNotExistException |
||
| 292 | */ |
||
| 293 | private function detailsCircleLinkedGroups(Circle &$circle) { |
||
| 304 | |||
| 305 | |||
| 306 | /** |
||
| 307 | * get the Federated Circles list and add the result to the Circle. |
||
| 308 | * |
||
| 309 | * @param Circle $circle |
||
| 310 | */ |
||
| 311 | private function detailsCircleFederatedCircles(Circle &$circle) { |
||
| 324 | |||
| 325 | |||
| 326 | /** |
||
| 327 | * save new settings if current user is admin. |
||
| 328 | * |
||
| 329 | * @param string $circleUniqueId |
||
| 330 | * @param array $settings |
||
| 331 | * |
||
| 332 | * @return Circle |
||
| 333 | * @throws Exception |
||
| 334 | */ |
||
| 335 | public function settingsCircle(string $circleUniqueId, array $settings) { |
||
| 354 | |||
| 355 | |||
| 356 | /** |
||
| 357 | * Join a circle. |
||
| 358 | * |
||
| 359 | * @param string $circleUniqueId |
||
| 360 | * |
||
| 361 | * @return null|Member |
||
| 362 | * @throws Exception |
||
| 363 | */ |
||
| 364 | public function joinCircle($circleUniqueId) { |
||
| 381 | |||
| 382 | |||
| 383 | /** |
||
| 384 | * Leave a circle. |
||
| 385 | * |
||
| 386 | * @param string $circleUniqueId |
||
| 387 | * |
||
| 388 | * @return null|Member |
||
| 389 | * @throws Exception |
||
| 390 | */ |
||
| 391 | public function leaveCircle($circleUniqueId) { |
||
| 402 | |||
| 403 | |||
| 404 | /** |
||
| 405 | * destroy a circle. |
||
| 406 | * |
||
| 407 | * @param string $circleUniqueId |
||
| 408 | * |
||
| 409 | * @param bool $force |
||
| 410 | * |
||
| 411 | * @throws CircleDoesNotExistException |
||
| 412 | * @throws MemberIsNotOwnerException |
||
| 413 | * @throws ConfigNoCircleAvailableException |
||
| 414 | * @throws Exception |
||
| 415 | */ |
||
| 416 | public function removeCircle($circleUniqueId, bool $force = false) { |
||
| 431 | |||
| 432 | |||
| 433 | /** |
||
| 434 | * @param $circleName |
||
| 435 | * |
||
| 436 | * @return Circle|null |
||
| 437 | * @throws CircleDoesNotExistException |
||
| 438 | */ |
||
| 439 | public function infoCircleByName($circleName) { |
||
| 442 | |||
| 443 | |||
| 444 | /** |
||
| 445 | * When a user is removed. |
||
| 446 | * Before deleting a user from the cloud, we assign a new owner to his Circles. |
||
| 447 | * Remove the Circle if it has no admin. |
||
| 448 | * |
||
| 449 | * @param string $userId |
||
| 450 | */ |
||
| 451 | public function onUserRemoved($userId) { |
||
| 467 | |||
| 468 | |||
| 469 | /** |
||
| 470 | * switchOlderAdminToOwner(); |
||
| 471 | * |
||
| 472 | * @param Circle $circle |
||
| 473 | * @param Member[] $members |
||
| 474 | */ |
||
| 475 | private function switchOlderAdminToOwner(Circle $circle, $members) { |
||
| 488 | |||
| 489 | |||
| 490 | /** |
||
| 491 | * Convert a Type in String to its Bit Value |
||
| 492 | * |
||
| 493 | * @param string $type |
||
| 494 | * |
||
| 495 | * @return int|mixed |
||
| 496 | */ |
||
| 497 | public function convertTypeStringToBitValue($type) { |
||
| 512 | |||
| 513 | |||
| 514 | /** |
||
| 515 | * getCircleIcon() |
||
| 516 | * |
||
| 517 | * Return the right imagePath for a type of circle. |
||
| 518 | * |
||
| 519 | * @param string $type |
||
| 520 | * @param bool $png |
||
| 521 | * |
||
| 522 | * @return string |
||
| 523 | */ |
||
| 524 | public static function getCircleIcon($type, $png = false) { |
||
| 555 | |||
| 556 | |||
| 557 | /** |
||
| 558 | * @param string $circleUniqueIds |
||
| 559 | * @param int $limit |
||
| 560 | * @param int $offset |
||
| 561 | * |
||
| 562 | * @return array |
||
| 563 | */ |
||
| 564 | public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { |
||
| 575 | |||
| 576 | |||
| 577 | /** |
||
| 578 | * @param Circle $circle |
||
| 579 | * |
||
| 580 | * @throws MembersLimitException |
||
| 581 | */ |
||
| 582 | public function checkThatCircleIsNotFull(Circle $circle) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @return bool |
||
| 605 | */ |
||
| 606 | public function viewerIsAdmin(): bool { |
||
| 613 | |||
| 614 | |||
| 615 | /** |
||
| 616 | * should be moved. |
||
| 617 | * |
||
| 618 | * @param Member $member |
||
| 619 | * |
||
| 620 | * @throws MemberIsNotOwnerException |
||
| 621 | */ |
||
| 622 | View Code Duplication | public function hasToBeOwner(Member $member) { |
|
| 630 | |||
| 631 | |||
| 632 | /** |
||
| 633 | * should be moved. |
||
| 634 | * |
||
| 635 | * @param Member $member |
||
| 636 | * |
||
| 637 | * @throws MemberIsNotOwnerException |
||
| 638 | */ |
||
| 639 | View Code Duplication | public function hasToBeAdmin(Member $member) { |
|
| 647 | } |
||
| 648 |