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 |
||
| 45 | class MembersService { |
||
| 46 | |||
| 47 | /** @var string */ |
||
| 48 | private $userId; |
||
| 49 | |||
| 50 | /** @var IL10N */ |
||
| 51 | private $l10n; |
||
| 52 | |||
| 53 | /** @var IUserManager */ |
||
| 54 | private $userManager; |
||
| 55 | |||
| 56 | /** @var ConfigService */ |
||
| 57 | private $configService; |
||
| 58 | |||
| 59 | /** @var CirclesRequest */ |
||
| 60 | private $circlesRequest; |
||
| 61 | |||
| 62 | /** @var MembersRequest */ |
||
| 63 | private $membersRequest; |
||
| 64 | |||
| 65 | /** @var SharesRequest */ |
||
| 66 | private $sharesRequest; |
||
| 67 | |||
| 68 | /** @var CirclesService */ |
||
| 69 | private $circlesService; |
||
| 70 | |||
| 71 | /** @var EventsService */ |
||
| 72 | private $eventsService; |
||
| 73 | |||
| 74 | /** @var MiscService */ |
||
| 75 | private $miscService; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * MembersService constructor. |
||
| 79 | * |
||
| 80 | * @param string $userId |
||
| 81 | * @param IL10N $l10n |
||
| 82 | * @param IUserManager $userManager |
||
| 83 | * @param ConfigService $configService |
||
| 84 | * @param CirclesRequest $circlesRequest |
||
| 85 | * @param MembersRequest $membersRequest |
||
| 86 | * @param SharesRequest $sharesRequest |
||
| 87 | * @param CirclesService $circlesService |
||
| 88 | * @param EventsService $eventsService |
||
| 89 | * @param MiscService $miscService |
||
| 90 | */ |
||
| 91 | public function __construct( |
||
| 92 | $userId, IL10N $l10n, IUserManager $userManager, ConfigService $configService, |
||
| 93 | CirclesRequest $circlesRequest, MembersRequest $membersRequest, |
||
| 94 | SharesRequest $sharesRequest, CirclesService $circlesService, EventsService $eventsService, |
||
| 95 | MiscService $miscService |
||
| 96 | ) { |
||
| 97 | $this->userId = $userId; |
||
| 98 | $this->l10n = $l10n; |
||
| 99 | $this->userManager = $userManager; |
||
| 100 | $this->configService = $configService; |
||
| 101 | $this->circlesRequest = $circlesRequest; |
||
| 102 | $this->membersRequest = $membersRequest; |
||
| 103 | $this->sharesRequest = $sharesRequest; |
||
| 104 | $this->circlesService = $circlesService; |
||
| 105 | $this->eventsService = $eventsService; |
||
| 106 | $this->miscService = $miscService; |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * addMember(); |
||
| 112 | * |
||
| 113 | * add a new member to a circle. |
||
| 114 | * |
||
| 115 | * @param string $circleUniqueId |
||
| 116 | * @param $ident |
||
| 117 | * @param int $type |
||
| 118 | * |
||
| 119 | * @return array |
||
|
|
|||
| 120 | * @throws \Exception |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function addMember($circleUniqueId, $ident, $type) { |
|
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * add a single member to a circle. |
||
| 144 | * |
||
| 145 | * @param Circle $circle |
||
| 146 | * @param string $ident |
||
| 147 | * @param int $type |
||
| 148 | * |
||
| 149 | * @throws MemberAlreadyExistsException |
||
| 150 | * @throws Exception |
||
| 151 | */ |
||
| 152 | private function addSingleMember(Circle $circle, $ident, $type) { |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * add a bunch of users to a circle based on the type of the 'bunch' |
||
| 169 | * |
||
| 170 | * @param Circle $circle |
||
| 171 | * @param string $ident |
||
| 172 | * @param int $type |
||
| 173 | * |
||
| 174 | * @return bool |
||
| 175 | * @throws Exception |
||
| 176 | */ |
||
| 177 | private function addMassiveMembers(Circle $circle, $ident, $type) { |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * add a new member based on its type. |
||
| 193 | * |
||
| 194 | * @param Circle $circle |
||
| 195 | * @param Member $member |
||
| 196 | * |
||
| 197 | * @throws Exception |
||
| 198 | */ |
||
| 199 | private function addMemberBasedOnItsType(Circle $circle, Member &$member) { |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * @param Circle $circle |
||
| 208 | * @param Member $member |
||
| 209 | * |
||
| 210 | * @throws \Exception |
||
| 211 | */ |
||
| 212 | private function addLocalMember(Circle $circle, Member $member) { |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * add mail address as contact. |
||
| 224 | * |
||
| 225 | * @param Member $member |
||
| 226 | * |
||
| 227 | * @throws \Exception |
||
| 228 | */ |
||
| 229 | private function addEmailAddress(Member $member) { |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * Add contact as member. |
||
| 241 | * |
||
| 242 | * @param Member $member |
||
| 243 | * |
||
| 244 | * @throws \Exception |
||
| 245 | */ |
||
| 246 | private function addContact(Member $member) { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * Verify the availability of an ident, based on its type. |
||
| 258 | * |
||
| 259 | * @param string $ident |
||
| 260 | * @param int $type |
||
| 261 | * |
||
| 262 | * @throws Exception |
||
| 263 | */ |
||
| 264 | private function verifyIdentBasedOnItsType(&$ident, $type) { |
||
| 269 | |||
| 270 | |||
| 271 | /** |
||
| 272 | * Verify if a local account is valid. |
||
| 273 | * |
||
| 274 | * @param $ident |
||
| 275 | * @param $type |
||
| 276 | * |
||
| 277 | * @throws NoUserException |
||
| 278 | */ |
||
| 279 | private function verifyIdentLocalMember(&$ident, $type) { |
||
| 290 | |||
| 291 | |||
| 292 | /** |
||
| 293 | * Verify if a mail have a valid format. |
||
| 294 | * |
||
| 295 | * @param $ident |
||
| 296 | * @param $type |
||
| 297 | * |
||
| 298 | * @throws EmailAccountInvalidFormatException |
||
| 299 | */ |
||
| 300 | private function verifyIdentEmailAddress(&$ident, $type) { |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Verify if a contact exist in current user address books. |
||
| 315 | * |
||
| 316 | * @param $ident |
||
| 317 | * @param $type |
||
| 318 | * |
||
| 319 | * @throws NoUserException |
||
| 320 | */ |
||
| 321 | private function verifyIdentContact(&$ident, $type) { |
||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * @param Circle $circle |
||
| 338 | * @param string $groupId |
||
| 339 | * |
||
| 340 | * @return bool |
||
| 341 | * @throws \Exception |
||
| 342 | */ |
||
| 343 | private function addGroupMembers(Circle $circle, $groupId) { |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * @param Circle $circle |
||
| 366 | * @param string $mails |
||
| 367 | * |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | private function addMassiveMails(Circle $circle, $mails) { |
||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * getMember(); |
||
| 391 | * |
||
| 392 | * Will return any data of a user related to a circle (as a Member). User can be a 'non-member' |
||
| 393 | * Viewer needs to be at least Member of the Circle |
||
| 394 | * |
||
| 395 | * @param $circleId |
||
| 396 | * @param $userId |
||
| 397 | * @param $type |
||
| 398 | * @param bool $forceAll |
||
| 399 | * |
||
| 400 | * @return Member |
||
| 401 | * @throws Exception |
||
| 402 | */ |
||
| 403 | public function getMember($circleId, $userId, $type, $forceAll = false) { |
||
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * @param string $circleUniqueId |
||
| 424 | * @param string $name |
||
| 425 | * @param int $type |
||
| 426 | * @param int $level |
||
| 427 | * |
||
| 428 | * @return array |
||
| 429 | * @throws \Exception |
||
| 430 | */ |
||
| 431 | public function levelMember($circleUniqueId, $name, $type, $level) { |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * @param Circle $circle |
||
| 458 | * @param Member $member |
||
| 459 | * @param $level |
||
| 460 | * |
||
| 461 | * @throws Exception |
||
| 462 | */ |
||
| 463 | private function updateMemberLevel(Circle $circle, Member $member, $level) { |
||
| 476 | |||
| 477 | |||
| 478 | /** |
||
| 479 | * @param Circle $circle |
||
| 480 | * @param Member $member |
||
| 481 | * @param $level |
||
| 482 | * |
||
| 483 | * @throws \Exception |
||
| 484 | */ |
||
| 485 | View Code Duplication | private function editMemberLevel(Circle $circle, Member &$member, $level) { |
|
| 502 | |||
| 503 | /** |
||
| 504 | * @param Circle $circle |
||
| 505 | * @param Member $member |
||
| 506 | * |
||
| 507 | * @throws \Exception |
||
| 508 | */ |
||
| 509 | private function switchOwner(Circle $circle, Member &$member) { |
||
| 529 | |||
| 530 | |||
| 531 | /** |
||
| 532 | * @param string $circleUniqueId |
||
| 533 | * @param string $name |
||
| 534 | * @param $type |
||
| 535 | * |
||
| 536 | * @return array |
||
| 537 | * @throws \Exception |
||
| 538 | */ |
||
| 539 | public function removeMember($circleUniqueId, $name, $type) { |
||
| 565 | |||
| 566 | |||
| 567 | /** |
||
| 568 | * When a user is removed, remove him from all Circles |
||
| 569 | * |
||
| 570 | * @param $userId |
||
| 571 | */ |
||
| 572 | public function onUserRemoved($userId) { |
||
| 575 | |||
| 576 | |||
| 577 | } |
||
| 578 |
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.