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 MembersService 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 MembersService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 62 | class MembersService { |
||
| 63 | |||
| 64 | /** @var string */ |
||
| 65 | private $userId; |
||
| 66 | |||
| 67 | /** @var IL10N */ |
||
| 68 | private $l10n; |
||
| 69 | |||
| 70 | /** @var IUserManager */ |
||
| 71 | private $userManager; |
||
| 72 | |||
| 73 | /** @var ConfigService */ |
||
| 74 | private $configService; |
||
| 75 | |||
| 76 | /** @var CirclesRequest */ |
||
| 77 | private $circlesRequest; |
||
| 78 | |||
| 79 | /** @var MembersRequest */ |
||
| 80 | private $membersRequest; |
||
| 81 | |||
| 82 | /** @var SharesRequest */ |
||
| 83 | private $sharesRequest; |
||
| 84 | |||
| 85 | /** @var TokensRequest */ |
||
| 86 | private $tokensRequest; |
||
| 87 | |||
| 88 | /** @var CirclesService */ |
||
| 89 | private $circlesService; |
||
| 90 | |||
| 91 | /** @var EventsService */ |
||
| 92 | private $eventsService; |
||
| 93 | |||
| 94 | /** @var FileSharingBroadcaster */ |
||
| 95 | private $fileSharingBroadcaster; |
||
| 96 | |||
| 97 | /** @var MiscService */ |
||
| 98 | private $miscService; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * MembersService constructor. |
||
| 102 | * |
||
| 103 | * @param string $userId |
||
| 104 | * @param IL10N $l10n |
||
| 105 | * @param IUserManager $userManager |
||
| 106 | * @param ConfigService $configService |
||
| 107 | * @param CirclesRequest $circlesRequest |
||
| 108 | * @param MembersRequest $membersRequest |
||
| 109 | * @param SharesRequest $sharesRequest |
||
| 110 | * @param TokensRequest $tokensRequest |
||
| 111 | * @param CirclesService $circlesService |
||
| 112 | * @param EventsService $eventsService |
||
| 113 | * @param FileSharingBroadcaster $fileSharingBroadcaster |
||
| 114 | * @param MiscService $miscService |
||
| 115 | */ |
||
| 116 | public function __construct( |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * addMember(); |
||
| 139 | * |
||
| 140 | * add a new member to a circle. |
||
| 141 | * |
||
| 142 | * @param string $circleUniqueId |
||
| 143 | * @param $ident |
||
| 144 | * @param int $type |
||
| 145 | * |
||
| 146 | * @param bool $force |
||
| 147 | * |
||
| 148 | * @return array |
||
|
|
|||
| 149 | * @throws Exception |
||
| 150 | */ |
||
| 151 | public function addMember($circleUniqueId, $ident, $type, bool $force = false) { |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * add a single member to a circle. |
||
| 171 | * |
||
| 172 | * @param Circle $circle |
||
| 173 | * @param string $ident |
||
| 174 | * @param int $type |
||
| 175 | * |
||
| 176 | * @throws MemberAlreadyExistsException |
||
| 177 | * @throws Exception |
||
| 178 | */ |
||
| 179 | private function addSingleMember(Circle $circle, $ident, $type) { |
||
| 194 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * add a bunch of users to a circle based on the type of the 'bunch' |
||
| 198 | * |
||
| 199 | * @param Circle $circle |
||
| 200 | * @param string $ident |
||
| 201 | * @param int $type |
||
| 202 | * |
||
| 203 | * @return bool |
||
| 204 | * @throws Exception |
||
| 205 | */ |
||
| 206 | private function addMassiveMembers(Circle $circle, $ident, $type) { |
||
| 218 | |||
| 219 | |||
| 220 | /** |
||
| 221 | * add a new member based on its type. |
||
| 222 | * |
||
| 223 | * @param Circle $circle |
||
| 224 | * @param Member $member |
||
| 225 | * |
||
| 226 | * @throws Exception |
||
| 227 | */ |
||
| 228 | private function addMemberBasedOnItsType(Circle $circle, Member &$member) { |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * @param Circle $circle |
||
| 237 | * @param Member $member |
||
| 238 | * |
||
| 239 | * @throws Exception |
||
| 240 | */ |
||
| 241 | private function addLocalMember(Circle $circle, Member $member) { |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * add mail address as contact. |
||
| 257 | * |
||
| 258 | * @param Member $member |
||
| 259 | * |
||
| 260 | * @throws Exception |
||
| 261 | */ |
||
| 262 | private function addEmailAddress(Member $member) { |
||
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * Add contact as member. |
||
| 274 | * |
||
| 275 | * @param Member $member |
||
| 276 | * |
||
| 277 | * @throws Exception |
||
| 278 | */ |
||
| 279 | private function addContact(Member $member) { |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * Verify the availability of an ident when Group Backend is enabled |
||
| 291 | * |
||
| 292 | * @param Circle $circle |
||
| 293 | * @param string $ident |
||
| 294 | * @param int $type |
||
| 295 | * |
||
| 296 | * @throws Exception |
||
| 297 | */ |
||
| 298 | private function verifyIdentWithGroupBackend(Circle $circle, $ident, $type) { |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * Verify the availability of an ident, based on its type. |
||
| 318 | * |
||
| 319 | * @param string $ident |
||
| 320 | * @param int $type |
||
| 321 | * |
||
| 322 | * @throws Exception |
||
| 323 | */ |
||
| 324 | private function verifyIdentBasedOnItsType(&$ident, $type) { |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * Verify if a local account is valid. |
||
| 333 | * |
||
| 334 | * @param $ident |
||
| 335 | * @param $type |
||
| 336 | * |
||
| 337 | * @throws NoUserException |
||
| 338 | */ |
||
| 339 | private function verifyIdentLocalMember(&$ident, $type) { |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * Verify if a mail have a valid format. |
||
| 354 | * |
||
| 355 | * @param $ident |
||
| 356 | * @param $type |
||
| 357 | * |
||
| 358 | * @throws EmailAccountInvalidFormatException |
||
| 359 | */ |
||
| 360 | private function verifyIdentEmailAddress(&$ident, $type) { |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * Verify if a contact exist in current user address books. |
||
| 382 | * |
||
| 383 | * @param $ident |
||
| 384 | * @param $type |
||
| 385 | * |
||
| 386 | * @throws NoUserException |
||
| 387 | * @throws EmailAccountInvalidFormatException |
||
| 388 | */ |
||
| 389 | private function verifyIdentContact(&$ident, $type) { |
||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * @param Circle $circle |
||
| 412 | * @param string $groupId |
||
| 413 | * |
||
| 414 | * @return bool |
||
| 415 | * @throws Exception |
||
| 416 | */ |
||
| 417 | private function addGroupMembers(Circle $circle, $groupId) { |
||
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * @param Circle $circle |
||
| 450 | * @param string $mails |
||
| 451 | * |
||
| 452 | * @return bool |
||
| 453 | */ |
||
| 454 | private function addMassiveMails(Circle $circle, $mails) { |
||
| 475 | |||
| 476 | |||
| 477 | /** |
||
| 478 | * getMember(); |
||
| 479 | * |
||
| 480 | * Will return any data of a user related to a circle (as a Member). User can be a 'non-member' |
||
| 481 | * Viewer needs to be at least Member of the Circle |
||
| 482 | * |
||
| 483 | * @param $circleId |
||
| 484 | * @param $userId |
||
| 485 | * @param $type |
||
| 486 | * @param bool $forceAll |
||
| 487 | * |
||
| 488 | * @return Member |
||
| 489 | * @throws CircleDoesNotExistException |
||
| 490 | * @throws ConfigNoCircleAvailableException |
||
| 491 | * @throws MemberDoesNotExistException |
||
| 492 | */ |
||
| 493 | public function getMember($circleId, $userId, $type, $forceAll = false) { |
||
| 505 | |||
| 506 | |||
| 507 | /** |
||
| 508 | * @param string $memberId |
||
| 509 | * |
||
| 510 | * @return Member |
||
| 511 | * @throws MemberDoesNotExistException |
||
| 512 | */ |
||
| 513 | public function getMemberById(string $memberId): Member { |
||
| 516 | |||
| 517 | |||
| 518 | /** |
||
| 519 | * @param string $circleUniqueId |
||
| 520 | * @param string $name |
||
| 521 | * @param int $type |
||
| 522 | * @param int $level |
||
| 523 | * @param bool $force |
||
| 524 | * |
||
| 525 | * @return array |
||
| 526 | * @throws CircleDoesNotExistException |
||
| 527 | * @throws CircleTypeNotValidException |
||
| 528 | * @throws ConfigNoCircleAvailableException |
||
| 529 | * @throws MemberDoesNotExistException |
||
| 530 | * @throws MemberTypeCantEditLevelException |
||
| 531 | * @throws Exception |
||
| 532 | */ |
||
| 533 | public function levelMember($circleUniqueId, $name, $type, $level, bool $force = false) { |
||
| 561 | |||
| 562 | |||
| 563 | /** |
||
| 564 | * @param Circle $circle |
||
| 565 | * @param Member $member |
||
| 566 | * @param $level |
||
| 567 | * @param bool $force |
||
| 568 | * |
||
| 569 | * @throws Exception |
||
| 570 | */ |
||
| 571 | private function updateMemberLevel(Circle $circle, Member $member, $level, bool $force = false) { |
||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * @param Circle $circle |
||
| 588 | * @param Member $member |
||
| 589 | * @param $level |
||
| 590 | * @param bool $force |
||
| 591 | * |
||
| 592 | * @throws Exception |
||
| 593 | */ |
||
| 594 | private function editMemberLevel(Circle $circle, Member &$member, $level, bool $force = false) { |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @param Circle $circle |
||
| 612 | * @param Member $member |
||
| 613 | * @param bool $force |
||
| 614 | * |
||
| 615 | * @throws Exception |
||
| 616 | */ |
||
| 617 | private function switchOwner(Circle $circle, Member &$member, bool $force = false) { |
||
| 636 | |||
| 637 | |||
| 638 | /** |
||
| 639 | * @param string $circleUniqueId |
||
| 640 | * @param string $name |
||
| 641 | * @param $type |
||
| 642 | * @param bool $force |
||
| 643 | * |
||
| 644 | * @return array |
||
| 645 | * @throws CircleDoesNotExistException |
||
| 646 | * @throws ConfigNoCircleAvailableException |
||
| 647 | * @throws MemberDoesNotExistException |
||
| 648 | * @throws MemberIsNotModeratorException |
||
| 649 | * @throws MemberIsOwnerException |
||
| 650 | * @throws ModeratorIsNotHighEnoughException |
||
| 651 | */ |
||
| 652 | public function removeMember($circleUniqueId, $name, $type, bool $force = false) { |
||
| 685 | |||
| 686 | |||
| 687 | /** |
||
| 688 | * When a user is removed, remove him from all Circles |
||
| 689 | * |
||
| 690 | * @param $userId |
||
| 691 | */ |
||
| 692 | public function onUserRemoved($userId) { |
||
| 695 | |||
| 696 | |||
| 697 | } |
||
| 698 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.