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 |
||
| 43 | class MembersService { |
||
| 44 | |||
| 45 | /** @var string */ |
||
| 46 | private $userId; |
||
| 47 | |||
| 48 | /** @var IL10N */ |
||
| 49 | private $l10n; |
||
| 50 | |||
| 51 | /** @var IUserManager */ |
||
| 52 | private $userManager; |
||
| 53 | |||
| 54 | /** @var ConfigService */ |
||
| 55 | private $configService; |
||
| 56 | |||
| 57 | /** @var CirclesRequest */ |
||
| 58 | private $circlesRequest; |
||
| 59 | |||
| 60 | /** @var MembersRequest */ |
||
| 61 | private $membersRequest; |
||
| 62 | |||
| 63 | /** @var EventsService */ |
||
| 64 | private $eventsService; |
||
| 65 | |||
| 66 | /** @var MiscService */ |
||
| 67 | private $miscService; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * MembersService constructor. |
||
| 71 | * |
||
| 72 | * @param $userId |
||
| 73 | * @param IL10N $l10n |
||
| 74 | * @param IUserManager $userManager |
||
| 75 | * @param ConfigService $configService |
||
| 76 | * @param CirclesRequest $circlesRequest |
||
| 77 | * @param MembersRequest $membersRequest |
||
| 78 | * @param EventsService $eventsService |
||
| 79 | * @param MiscService $miscService |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function __construct( |
|
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $circleUniqueId |
||
| 104 | * @param $ident |
||
| 105 | * @param $type |
||
| 106 | * |
||
| 107 | * @return array |
||
| 108 | * @throws \Exception |
||
| 109 | */ |
||
| 110 | public function addMember($circleUniqueId, $ident, $type) { |
||
| 134 | |||
| 135 | |||
| 136 | private function addMemberMassively(Circle $circle, $type, $ident) { |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * add a new member based on its type. |
||
| 148 | * |
||
| 149 | * @param Circle $circle |
||
| 150 | * @param Member $member |
||
| 151 | */ |
||
| 152 | private function addMemberBasedOnItsType(Circle $circle, Member &$member) { |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * @param Circle $circle |
||
| 161 | * @param Member $member |
||
| 162 | * |
||
| 163 | * @throws \Exception |
||
| 164 | */ |
||
| 165 | public function addLocalMember(Circle $circle, Member $member) { |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * add mail address as contact. |
||
| 177 | * |
||
| 178 | * @param Member $member |
||
| 179 | * |
||
| 180 | * @throws \Exception |
||
| 181 | */ |
||
| 182 | private function addEmailAddress(Member $member) { |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Add contact as member. |
||
| 194 | * |
||
| 195 | * @param Member $member |
||
| 196 | * |
||
| 197 | * @throws \Exception |
||
| 198 | */ |
||
| 199 | private function addContact(Member $member) { |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Verify the availability of an ident, based on its type. |
||
| 211 | * |
||
| 212 | * @param string $ident |
||
| 213 | * @param int $type |
||
| 214 | * |
||
| 215 | * @throws Exception |
||
| 216 | */ |
||
| 217 | private function verifyIdentBasedOnItsType(&$ident, $type) { |
||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * Verify if a local account is valid. |
||
| 230 | * |
||
| 231 | * @param $ident |
||
| 232 | * @param $type |
||
| 233 | * |
||
| 234 | * @throws NoUserException |
||
| 235 | */ |
||
| 236 | private function verifyIdentLocalMember(&$ident, $type) { |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * Verify if a mail have a valid format. |
||
| 251 | * |
||
| 252 | * @param $ident |
||
| 253 | * @param $type |
||
| 254 | * |
||
| 255 | * @throws EmailAccountInvalidFormatException |
||
| 256 | */ |
||
| 257 | private function verifyIdentEmailAddress(&$ident, $type) { |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * Verify if a contact exist in current user address books. |
||
| 272 | * |
||
| 273 | * @param $ident |
||
| 274 | * @param $type |
||
| 275 | * |
||
| 276 | * @throws NoUserException |
||
| 277 | */ |
||
| 278 | private function verifyIdentContact(&$ident, $type) { |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * @param Circle $circle |
||
| 296 | * @param string $groupId |
||
| 297 | * |
||
| 298 | * @return bool |
||
| 299 | * @throws \Exception |
||
| 300 | */ |
||
| 301 | private function addGroupMembers(Circle $circle, $groupId) { |
||
| 329 | |||
| 330 | |||
| 331 | /** |
||
| 332 | * getMember(); |
||
| 333 | * |
||
| 334 | * Will return any data of a user related to a circle (as a Member). User can be a 'non-member' |
||
| 335 | * Viewer needs to be at least Member of the Circle |
||
| 336 | * |
||
| 337 | * @param $circleId |
||
| 338 | * @param $userId |
||
| 339 | * @param $type |
||
| 340 | * |
||
| 341 | * @return Member |
||
| 342 | * @throws \Exception |
||
| 343 | */ |
||
| 344 | public function getMember($circleId, $userId, $type) { |
||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * @param string $circleUniqueId |
||
| 363 | * @param string $name |
||
| 364 | * @param int $type |
||
| 365 | * @param int $level |
||
| 366 | * |
||
| 367 | * @return array |
||
| 368 | * @throws \Exception |
||
| 369 | */ |
||
| 370 | public function levelMember($circleUniqueId, $name, $type, $level) { |
||
| 401 | |||
| 402 | |||
| 403 | /** |
||
| 404 | * @param Circle $circle |
||
| 405 | * @param Member $member |
||
| 406 | * @param $level |
||
| 407 | * |
||
| 408 | * @throws \Exception |
||
| 409 | */ |
||
| 410 | private function editMemberLevel(Circle $circle, Member &$member, $level) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param Circle $circle |
||
| 430 | * @param Member $member |
||
| 431 | * |
||
| 432 | * @throws \Exception |
||
| 433 | */ |
||
| 434 | private function switchOwner(Circle $circle, Member &$member) { |
||
| 452 | |||
| 453 | |||
| 454 | /** |
||
| 455 | * @param string $circleUniqueId |
||
| 456 | * @param string $name |
||
| 457 | * @param $type |
||
| 458 | * |
||
| 459 | * @return array |
||
| 460 | * @throws \Exception |
||
| 461 | */ |
||
| 462 | public function removeMember($circleUniqueId, $name, $type) { |
||
| 489 | |||
| 490 | |||
| 491 | /** |
||
| 492 | * When a user is removed, remove him from all Circles |
||
| 493 | * |
||
| 494 | * @param $userId |
||
| 495 | */ |
||
| 496 | public function onUserRemoved($userId) { |
||
| 499 | |||
| 500 | |||
| 501 | } |
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.