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 CirclesService 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 CirclesService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class CirclesService { |
||
| 44 | |||
| 45 | /** @var string */ |
||
| 46 | private $userId; |
||
| 47 | |||
| 48 | /** @var IL10N */ |
||
| 49 | private $l10n; |
||
| 50 | |||
| 51 | /** @var ConfigService */ |
||
| 52 | private $configService; |
||
| 53 | |||
| 54 | /** @var CirclesRequest */ |
||
| 55 | private $circlesRequest; |
||
| 56 | |||
| 57 | /** @var MembersRequest */ |
||
| 58 | private $membersRequest; |
||
| 59 | |||
| 60 | /** @var FederatedLinksRequest */ |
||
| 61 | private $federatedLinksRequest; |
||
| 62 | |||
| 63 | /** @var EventsService */ |
||
| 64 | private $eventsService; |
||
| 65 | |||
| 66 | /** @var MiscService */ |
||
| 67 | private $miscService; |
||
| 68 | |||
| 69 | |||
| 70 | /** |
||
| 71 | * CirclesService constructor. |
||
| 72 | * |
||
| 73 | * @param string $userId |
||
| 74 | * @param IL10N $l10n |
||
| 75 | * @param ConfigService $configService |
||
| 76 | * @param CirclesRequest $circlesRequest |
||
| 77 | * @param MembersRequest $membersRequest |
||
| 78 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 79 | * @param EventsService $eventsService |
||
| 80 | * @param MiscService $miscService |
||
| 81 | */ |
||
| 82 | View Code Duplication | public function __construct( |
|
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Create circle using this->userId as owner |
||
| 105 | * |
||
| 106 | * @param int|string $type |
||
| 107 | * @param string $name |
||
| 108 | * |
||
| 109 | * @return Circle |
||
| 110 | * @throws CircleTypeDisabledException |
||
| 111 | * @throws \Exception |
||
| 112 | */ |
||
| 113 | public function createCircle($type, $name) { |
||
| 114 | self::convertTypeStringToBitValue($type); |
||
| 115 | $type = (int)$type; |
||
| 116 | |||
| 117 | if ($type === '') { |
||
|
|
|||
| 118 | throw new CircleTypeDisabledException( |
||
| 119 | $this->l10n->t('You need a specify a type of circle') |
||
| 120 | ); |
||
| 121 | } |
||
| 122 | |||
| 123 | if (!$this->configService->isCircleAllowed($type)) { |
||
| 124 | throw new CircleTypeDisabledException( |
||
| 125 | $this->l10n->t('You cannot create this type of circle') |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | |||
| 129 | $circle = new Circle($type, $name); |
||
| 130 | |||
| 131 | try { |
||
| 132 | $this->circlesRequest->createCircle($circle, $this->userId); |
||
| 133 | $this->membersRequest->createMember($circle->getOwner()); |
||
| 134 | } catch (CircleAlreadyExistsException $e) { |
||
| 135 | throw $e; |
||
| 136 | } |
||
| 137 | |||
| 138 | $this->eventsService->onCircleCreation($circle); |
||
| 139 | |||
| 140 | return $circle; |
||
| 141 | } |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 146 | * |
||
| 147 | * @param mixed $type |
||
| 148 | * @param string $name |
||
| 149 | * @param int $level |
||
| 150 | * |
||
| 151 | * @return Circle[] |
||
| 152 | * @throws CircleTypeDisabledException |
||
| 153 | */ |
||
| 154 | public function listCircles($type, $name = '', $level = 0) { |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * returns details on circle and its members if this->userId is a member itself. |
||
| 175 | * |
||
| 176 | * @param string $circleUniqueId |
||
| 177 | * |
||
| 178 | * @return Circle |
||
| 179 | * @throws \Exception |
||
| 180 | ] */ |
||
| 181 | public function detailsCircle($circleUniqueId) { |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * get the Members list and add the result to the Circle. |
||
| 202 | * |
||
| 203 | * @param Circle $circle |
||
| 204 | */ |
||
| 205 | private function detailsCircleMembers(Circle &$circle) { |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * get the Linked Group list and add the result to the Circle. |
||
| 215 | * |
||
| 216 | * @param Circle $circle |
||
| 217 | */ |
||
| 218 | private function detailsCircleLinkedGroups(Circle &$circle) { |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * get the Federated Circles list and add the result to the Circle. |
||
| 233 | * |
||
| 234 | * @param Circle $circle |
||
| 235 | */ |
||
| 236 | private function detailsCircleFederatedCircles(Circle &$circle) { |
||
| 249 | |||
| 250 | |||
| 251 | /** |
||
| 252 | * save new settings if current user is admin. |
||
| 253 | * |
||
| 254 | * @param string $circleUniqueId |
||
| 255 | * @param array $settings |
||
| 256 | * |
||
| 257 | * @return Circle |
||
| 258 | * @throws \Exception |
||
| 259 | */ |
||
| 260 | public function settingsCircle($circleUniqueId, $settings) { |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * Join a circle. |
||
| 285 | * |
||
| 286 | * @param string $circleUniqueId |
||
| 287 | * |
||
| 288 | * @return null|Member |
||
| 289 | * @throws \Exception |
||
| 290 | */ |
||
| 291 | View Code Duplication | public function joinCircle($circleUniqueId) { |
|
| 310 | |||
| 311 | |||
| 312 | /** |
||
| 313 | * Leave a circle. |
||
| 314 | * |
||
| 315 | * @param string $circleUniqueId |
||
| 316 | * |
||
| 317 | * @return null|Member |
||
| 318 | * @throws \Exception |
||
| 319 | */ |
||
| 320 | View Code Duplication | public function leaveCircle($circleUniqueId) { |
|
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * destroy a circle. |
||
| 344 | * |
||
| 345 | * @param string $circleUniqueId |
||
| 346 | * |
||
| 347 | * @throws MemberIsNotOwnerException |
||
| 348 | */ |
||
| 349 | public function removeCircle($circleUniqueId) { |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * @param $circleName |
||
| 369 | * |
||
| 370 | * @return Circle|null |
||
| 371 | */ |
||
| 372 | public function infoCircleByName($circleName) { |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * When a user is removed. |
||
| 379 | * |
||
| 380 | * @param $userId |
||
| 381 | */ |
||
| 382 | public function onUserRemoved($userId) { |
||
| 385 | |||
| 386 | |||
| 387 | /** |
||
| 388 | * switchOlderAdminToOwnerInCirclesOwnedByUser(); |
||
| 389 | * |
||
| 390 | * Before deleting a user from the cloud, we assign a new owner to his Circles. |
||
| 391 | * Remove the Circle if it has no admin. |
||
| 392 | * |
||
| 393 | * @param string $userId |
||
| 394 | */ |
||
| 395 | private function switchOlderAdminToOwnerInCirclesOwnedByUser($userId) { |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * Convert a Type in String to its Bit Value |
||
| 421 | * |
||
| 422 | * @param string $type |
||
| 423 | */ |
||
| 424 | public static function convertTypeStringToBitValue(&$type) { |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * getCircleIcon() |
||
| 445 | * |
||
| 446 | * Return the right imagePath for a type of circle. |
||
| 447 | * |
||
| 448 | * @param string $type |
||
| 449 | * @param bool $png |
||
| 450 | * |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public static function getCircleIcon($type, $png = false) { |
||
| 482 | |||
| 483 | } |