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 | View Code Duplication | public function __construct( |
|
|
|||
117 | $userId, IL10N $l10n, IUserManager $userManager, ConfigService $configService, |
||
118 | CirclesRequest $circlesRequest, MembersRequest $membersRequest, SharesRequest $sharesRequest, |
||
119 | TokensRequest $tokensRequest, CirclesService $circlesService, EventsService $eventsService, |
||
120 | FileSharingBroadcaster $fileSharingBroadcaster, MiscService $miscService |
||
121 | ) { |
||
122 | $this->userId = $userId; |
||
123 | $this->l10n = $l10n; |
||
124 | $this->userManager = $userManager; |
||
125 | $this->configService = $configService; |
||
126 | $this->circlesRequest = $circlesRequest; |
||
127 | $this->membersRequest = $membersRequest; |
||
128 | $this->sharesRequest = $sharesRequest; |
||
129 | $this->tokensRequest = $tokensRequest; |
||
130 | $this->circlesService = $circlesService; |
||
131 | $this->eventsService = $eventsService; |
||
132 | $this->fileSharingBroadcaster = $fileSharingBroadcaster; |
||
133 | $this->miscService = $miscService; |
||
134 | } |
||
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) { |
||
195 | |||
196 | |||
197 | /** |
||
198 | * add a bunch of users to a circle based on the type of the 'bunch' |
||
199 | * |
||
200 | * @param Circle $circle |
||
201 | * @param string $ident |
||
202 | * @param int $type |
||
203 | * |
||
204 | * @return bool |
||
205 | * @throws Exception |
||
206 | */ |
||
207 | private function addMassiveMembers(Circle $circle, $ident, $type) { |
||
219 | |||
220 | |||
221 | /** |
||
222 | * add a new member based on its type. |
||
223 | * |
||
224 | * @param Circle $circle |
||
225 | * @param Member $member |
||
226 | * |
||
227 | * @throws Exception |
||
228 | */ |
||
229 | private function addMemberBasedOnItsType(Circle $circle, Member &$member) { |
||
234 | |||
235 | |||
236 | /** |
||
237 | * @param Circle $circle |
||
238 | * @param Member $member |
||
239 | * |
||
240 | * @throws Exception |
||
241 | */ |
||
242 | private function addLocalMember(Circle $circle, Member $member) { |
||
254 | |||
255 | |||
256 | /** |
||
257 | * add mail address as contact. |
||
258 | * |
||
259 | * @param Member $member |
||
260 | * |
||
261 | * @throws Exception |
||
262 | */ |
||
263 | private function addEmailAddress(Member $member) { |
||
271 | |||
272 | |||
273 | /** |
||
274 | * Add contact as member. |
||
275 | * |
||
276 | * @param Member $member |
||
277 | * |
||
278 | * @throws Exception |
||
279 | */ |
||
280 | private function addContact(Member $member) { |
||
288 | |||
289 | |||
290 | /** |
||
291 | * Verify the availability of an ident when Group Backend is enabled |
||
292 | * |
||
293 | * @param Circle $circle |
||
294 | * @param string $ident |
||
295 | * @param int $type |
||
296 | * |
||
297 | * @throws Exception |
||
298 | */ |
||
299 | private function verifyIdentWithGroupBackend(Circle $circle, $ident, $type) { |
||
315 | |||
316 | |||
317 | /** |
||
318 | * Verify the availability of an ident, based on its type. |
||
319 | * |
||
320 | * @param string $ident |
||
321 | * @param int $type |
||
322 | * |
||
323 | * @throws Exception |
||
324 | */ |
||
325 | private function verifyIdentBasedOnItsType(&$ident, $type) { |
||
330 | |||
331 | |||
332 | /** |
||
333 | * Verify if a local account is valid. |
||
334 | * |
||
335 | * @param $ident |
||
336 | * @param $type |
||
337 | * |
||
338 | * @throws NoUserException |
||
339 | */ |
||
340 | private function verifyIdentLocalMember(&$ident, $type) { |
||
351 | |||
352 | |||
353 | /** |
||
354 | * Verify if a mail have a valid format. |
||
355 | * |
||
356 | * @param $ident |
||
357 | * @param $type |
||
358 | * |
||
359 | * @throws EmailAccountInvalidFormatException |
||
360 | */ |
||
361 | private function verifyIdentEmailAddress(&$ident, $type) { |
||
379 | |||
380 | |||
381 | /** |
||
382 | * Verify if a contact exist in current user address books. |
||
383 | * |
||
384 | * @param $ident |
||
385 | * @param $type |
||
386 | * |
||
387 | * @throws NoUserException |
||
388 | * @throws EmailAccountInvalidFormatException |
||
389 | */ |
||
390 | private function verifyIdentContact(&$ident, $type) { |
||
409 | |||
410 | |||
411 | /** |
||
412 | * @param Circle $circle |
||
413 | * @param string $groupId |
||
414 | * |
||
415 | * @return bool |
||
416 | * @throws Exception |
||
417 | */ |
||
418 | private function addGroupMembers(Circle $circle, $groupId) { |
||
447 | |||
448 | |||
449 | /** |
||
450 | * @param Circle $circle |
||
451 | * @param string $mails |
||
452 | * |
||
453 | * @return bool |
||
454 | */ |
||
455 | private function addMassiveMails(Circle $circle, $mails) { |
||
476 | |||
477 | |||
478 | /** |
||
479 | * getMember(); |
||
480 | * |
||
481 | * Will return any data of a user related to a circle (as a Member). User can be a 'non-member' |
||
482 | * Viewer needs to be at least Member of the Circle |
||
483 | * |
||
484 | * @param $circleId |
||
485 | * @param $userId |
||
486 | * @param $type |
||
487 | * @param bool $forceAll |
||
488 | * |
||
489 | * @return Member |
||
490 | * @throws CircleDoesNotExistException |
||
491 | * @throws ConfigNoCircleAvailableException |
||
492 | * @throws MemberDoesNotExistException |
||
493 | */ |
||
494 | public function getMember($circleId, $userId, $type, $forceAll = false) { |
||
506 | |||
507 | |||
508 | /** |
||
509 | * @param string $memberId |
||
510 | * |
||
511 | * @return Member |
||
512 | * @throws MemberDoesNotExistException |
||
513 | */ |
||
514 | public function getMemberById(string $memberId): Member { |
||
517 | |||
518 | |||
519 | /** |
||
520 | * @param string $circleUniqueId |
||
521 | * @param string $name |
||
522 | * @param int $type |
||
523 | * @param int $level |
||
524 | * @param bool $force |
||
525 | * |
||
526 | * @return array |
||
527 | * @throws CircleDoesNotExistException |
||
528 | * @throws CircleTypeNotValidException |
||
529 | * @throws ConfigNoCircleAvailableException |
||
530 | * @throws MemberDoesNotExistException |
||
531 | * @throws MemberTypeCantEditLevelException |
||
532 | * @throws Exception |
||
533 | */ |
||
534 | public function levelMember($circleUniqueId, $name, $type, $level, bool $force = false) { |
||
562 | |||
563 | |||
564 | /** |
||
565 | * @param Circle $circle |
||
566 | * @param Member $member |
||
567 | * @param $level |
||
568 | * @param bool $force |
||
569 | * |
||
570 | * @throws Exception |
||
571 | */ |
||
572 | private function updateMemberLevel(Circle $circle, Member $member, $level, bool $force = false) { |
||
585 | |||
586 | |||
587 | /** |
||
588 | * @param Circle $circle |
||
589 | * @param Member $member |
||
590 | * @param $level |
||
591 | * @param bool $force |
||
592 | * |
||
593 | * @throws Exception |
||
594 | */ |
||
595 | private function editMemberLevel(Circle $circle, Member &$member, $level, bool $force = false) { |
||
610 | |||
611 | /** |
||
612 | * @param Circle $circle |
||
613 | * @param Member $member |
||
614 | * @param bool $force |
||
615 | * |
||
616 | * @throws Exception |
||
617 | */ |
||
618 | private function switchOwner(Circle $circle, Member &$member, bool $force = false) { |
||
637 | |||
638 | |||
639 | /** |
||
640 | * @param string $circleUniqueId |
||
641 | * @param string $name |
||
642 | * @param $type |
||
643 | * @param bool $force |
||
644 | * |
||
645 | * @return array |
||
646 | * @throws CircleDoesNotExistException |
||
647 | * @throws ConfigNoCircleAvailableException |
||
648 | * @throws MemberDoesNotExistException |
||
649 | * @throws MemberIsNotModeratorException |
||
650 | * @throws MemberIsOwnerException |
||
651 | * @throws ModeratorIsNotHighEnoughException |
||
652 | */ |
||
653 | public function removeMember($circleUniqueId, $name, $type, bool $force = false) { |
||
686 | |||
687 | |||
688 | /** |
||
689 | * When a user is removed, remove him from all Circles |
||
690 | * |
||
691 | * @param $userId |
||
692 | */ |
||
693 | public function onUserRemoved($userId) { |
||
696 | |||
697 | |||
698 | } |
||
699 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.