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 DavService 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 DavService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class DavService { |
||
| 53 | |||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $userId; |
||
| 57 | |||
| 58 | /** @var IUserManager */ |
||
| 59 | private $userManager; |
||
| 60 | |||
| 61 | /** @var CirclesRequest */ |
||
| 62 | private $circlesRequest; |
||
| 63 | |||
| 64 | /** @var MembersRequest */ |
||
| 65 | private $membersRequest; |
||
| 66 | |||
| 67 | /** @var ConfigService */ |
||
| 68 | private $configService; |
||
| 69 | |||
| 70 | /** @var MiscService */ |
||
| 71 | private $miscService; |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * TimezoneService constructor. |
||
| 76 | * |
||
| 77 | * @param string $userId |
||
| 78 | * @param IUserManager $userManager |
||
| 79 | * @param CirclesRequest $circlesRequest |
||
| 80 | * @param MembersRequest $membersRequest |
||
| 81 | * @param ConfigService $configService |
||
| 82 | * @param MiscService $miscService |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function __construct( |
|
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * @param GenericEvent $event |
||
| 99 | */ |
||
| 100 | public function onCreateCard(GenericEvent $event) { |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * @param GenericEvent $event |
||
| 108 | */ |
||
| 109 | public function onUpdateCard(GenericEvent $event) { |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * @param GenericEvent $event |
||
| 117 | */ |
||
| 118 | public function onDeleteCard(GenericEvent $event) { |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @param GenericEvent $event |
||
| 126 | * |
||
| 127 | * @return DavCard |
||
| 128 | */ |
||
| 129 | private function generateDavCard(GenericEvent $event): DavCard { |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @param DavCard $davCard |
||
| 145 | */ |
||
| 146 | private function updateDavCard(DavCard $davCard) { |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * @param DavCard $davCard |
||
| 155 | * |
||
| 156 | * @return Member[] |
||
| 157 | */ |
||
| 158 | private function manageContactMembers(DavCard $davCard): array { |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * @param array $circles |
||
| 177 | * @param DavCard $davCard |
||
| 178 | * @param string $userId |
||
| 179 | * |
||
| 180 | * @return Member[] |
||
| 181 | */ |
||
| 182 | private function manageLocalMembers(array $circles, DavCard $davCard, string $userId): array { |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * @param array $circles |
||
| 205 | * @param DavCard $davCard |
||
| 206 | * |
||
| 207 | * @return Member[] |
||
| 208 | */ |
||
| 209 | private function manageRemoteMembers(array $circles, DavCard $davCard): array { |
||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $contactId |
||
| 236 | * @param Circle $circle |
||
| 237 | * @param string $userId |
||
| 238 | * @param int $type |
||
| 239 | * |
||
| 240 | * @return Member |
||
| 241 | */ |
||
| 242 | private function manageMember(string $contactId, Circle $circle, string $userId, int $type) { |
||
| 264 | |||
| 265 | |||
| 266 | /** |
||
| 267 | * @param DavCard $davCard |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | * @throws NotLocalMemberException |
||
| 271 | */ |
||
| 272 | private function isLocalMember(DavCard $davCard): string { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @param DavCard $davCard |
||
| 293 | */ |
||
| 294 | private function manageCircles(DavCard $davCard) { |
||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * @param int $bookId |
||
| 311 | * @param array $fromCard |
||
| 312 | * @param array $current |
||
| 313 | */ |
||
| 314 | private function manageNewCircles(int $bookId, array $fromCard, array $current) { |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * // TODO: Get all group from an addressbook |
||
| 343 | * // TODO: remove deprecated circles |
||
| 344 | * |
||
| 345 | * @param array $fromCard |
||
| 346 | * @param array $current |
||
| 347 | */ |
||
| 348 | private function manageDeprecatedCircles(array $fromCard, array $current) { |
||
| 350 | |||
| 351 | |||
| 352 | /** |
||
| 353 | * @param DavCard $davCard |
||
| 354 | */ |
||
| 355 | private function assignCirclesToCard(DavCard $davCard) { |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * @param int $addressBookId |
||
| 369 | * |
||
| 370 | * @return Circle[] |
||
| 371 | */ |
||
| 372 | private function getCirclesFromBook(int $addressBookId): array { |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * @param int $length |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | * @deprecated |
||
| 382 | */ |
||
| 383 | protected function uuid(int $length = 0): string { |
||
| 400 | |||
| 401 | |||
| 402 | } |
||
| 403 | |||
| 405 |
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.