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 |
||
| 48 | class CirclesService { |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $userId; |
||
| 52 | |||
| 53 | /** @var IL10N */ |
||
| 54 | private $l10n; |
||
| 55 | |||
| 56 | /** @var ConfigService */ |
||
| 57 | private $configService; |
||
| 58 | |||
| 59 | /** @var CirclesRequest */ |
||
| 60 | private $circlesRequest; |
||
| 61 | |||
| 62 | /** @var MembersRequest */ |
||
| 63 | private $membersRequest; |
||
| 64 | |||
| 65 | /** @var FederatedLinksRequest */ |
||
| 66 | private $federatedLinksRequest; |
||
| 67 | |||
| 68 | /** @var EventsService */ |
||
| 69 | private $eventsService; |
||
| 70 | |||
| 71 | /** @var CircleProviderRequest */ |
||
| 72 | private $circleProviderRequest; |
||
| 73 | |||
| 74 | /** @var MiscService */ |
||
| 75 | private $miscService; |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * CirclesService constructor. |
||
| 80 | * |
||
| 81 | * @param string $userId |
||
| 82 | * @param IL10N $l10n |
||
| 83 | * @param ConfigService $configService |
||
| 84 | * @param CirclesRequest $circlesRequest |
||
| 85 | * @param MembersRequest $membersRequest |
||
| 86 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 87 | * @param EventsService $eventsService |
||
| 88 | * @param CircleProviderRequest $circleProviderRequest |
||
| 89 | * @param MiscService $miscService |
||
| 90 | */ |
||
| 91 | View Code Duplication | public function __construct( |
|
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * Create circle using this->userId as owner |
||
| 116 | * |
||
| 117 | * @param int|string $type |
||
| 118 | * @param string $name |
||
| 119 | * |
||
| 120 | * @return Circle |
||
| 121 | * @throws CircleTypeDisabledException |
||
| 122 | * @throws \Exception |
||
| 123 | */ |
||
| 124 | public function createCircle($type, $name) { |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 157 | * |
||
| 158 | * @param string $userId |
||
| 159 | * @param mixed $type |
||
| 160 | * @param string $name |
||
| 161 | * @param int $level |
||
| 162 | * |
||
| 163 | * @return Circle[] |
||
| 164 | * @throws CircleTypeDisabledException |
||
| 165 | * @throws Exception |
||
| 166 | */ |
||
| 167 | public function listCircles($userId, $type, $name = '', $level = 0) { |
||
| 188 | |||
| 189 | |||
| 190 | /** |
||
| 191 | * returns details on circle and its members if this->userId is a member itself. |
||
| 192 | * |
||
| 193 | * @param string $circleUniqueId |
||
| 194 | * |
||
| 195 | * @return Circle |
||
| 196 | * @throws \Exception |
||
| 197 | ] */ |
||
| 198 | public function detailsCircle($circleUniqueId) { |
||
| 215 | |||
| 216 | |||
| 217 | /** |
||
| 218 | * get the Members list and add the result to the Circle. |
||
| 219 | * |
||
| 220 | * @param Circle $circle |
||
| 221 | */ |
||
| 222 | private function detailsCircleMembers(Circle &$circle) { |
||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * get the Linked Group list and add the result to the Circle. |
||
| 232 | * |
||
| 233 | * @param Circle $circle |
||
| 234 | */ |
||
| 235 | private function detailsCircleLinkedGroups(Circle &$circle) { |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * get the Federated Circles list and add the result to the Circle. |
||
| 250 | * |
||
| 251 | * @param Circle $circle |
||
| 252 | */ |
||
| 253 | private function detailsCircleFederatedCircles(Circle &$circle) { |
||
| 266 | |||
| 267 | |||
| 268 | /** |
||
| 269 | * save new settings if current user is admin. |
||
| 270 | * |
||
| 271 | * @param string $circleUniqueId |
||
| 272 | * @param array $settings |
||
| 273 | * |
||
| 274 | * @return Circle |
||
| 275 | * @throws \Exception |
||
| 276 | */ |
||
| 277 | public function settingsCircle($circleUniqueId, $settings) { |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * Join a circle. |
||
| 302 | * |
||
| 303 | * @param string $circleUniqueId |
||
| 304 | * |
||
| 305 | * @return null|Member |
||
| 306 | * @throws \Exception |
||
| 307 | */ |
||
| 308 | View Code Duplication | public function joinCircle($circleUniqueId) { |
|
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * Leave a circle. |
||
| 331 | * |
||
| 332 | * @param string $circleUniqueId |
||
| 333 | * |
||
| 334 | * @return null|Member |
||
| 335 | * @throws \Exception |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function leaveCircle($circleUniqueId) { |
|
| 357 | |||
| 358 | |||
| 359 | /** |
||
| 360 | * destroy a circle. |
||
| 361 | * |
||
| 362 | * @param string $circleUniqueId |
||
| 363 | * |
||
| 364 | * @throws MemberIsNotOwnerException |
||
| 365 | */ |
||
| 366 | public function removeCircle($circleUniqueId) { |
||
| 382 | |||
| 383 | |||
| 384 | /** |
||
| 385 | * @param $circleName |
||
| 386 | * |
||
| 387 | * @return Circle|null |
||
| 388 | */ |
||
| 389 | public function infoCircleByName($circleName) { |
||
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * When a user is removed. |
||
| 396 | * Before deleting a user from the cloud, we assign a new owner to his Circles. |
||
| 397 | * Remove the Circle if it has no admin. |
||
| 398 | * |
||
| 399 | * @param string $userId |
||
| 400 | */ |
||
| 401 | public function onUserRemoved($userId) { |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * switchOlderAdminToOwner(); |
||
| 421 | * |
||
| 422 | * @param Member[] $members |
||
| 423 | */ |
||
| 424 | private function switchOlderAdminToOwner($circle, $members) { |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * Convert a Type in String to its Bit Value |
||
| 441 | * |
||
| 442 | * @param string $type |
||
| 443 | * |
||
| 444 | * @return int|mixed |
||
| 445 | */ |
||
| 446 | public function convertTypeStringToBitValue($type) { |
||
| 461 | |||
| 462 | |||
| 463 | /** |
||
| 464 | * getCircleIcon() |
||
| 465 | * |
||
| 466 | * Return the right imagePath for a type of circle. |
||
| 467 | * |
||
| 468 | * @param string $type |
||
| 469 | * @param bool $png |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | public static function getCircleIcon($type, $png = false) { |
||
| 502 | |||
| 503 | |||
| 504 | /** |
||
| 505 | * @param string $circleUniqueIds |
||
| 506 | * @param int $limit |
||
| 507 | * @param int $offset |
||
| 508 | * |
||
| 509 | * @return array |
||
| 510 | */ |
||
| 511 | public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { |
||
| 522 | |||
| 523 | } |