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