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 |
||
| 57 | class CirclesService { |
||
| 58 | |||
| 59 | /** @var string */ |
||
| 60 | private $userId; |
||
| 61 | |||
| 62 | /** @var IL10N */ |
||
| 63 | private $l10n; |
||
| 64 | |||
| 65 | /** @var IGroupManager */ |
||
| 66 | private $groupManager; |
||
| 67 | |||
| 68 | /** @var ConfigService */ |
||
| 69 | private $configService; |
||
| 70 | |||
| 71 | /** @var CirclesRequest */ |
||
| 72 | private $circlesRequest; |
||
| 73 | |||
| 74 | /** @var MembersRequest */ |
||
| 75 | private $membersRequest; |
||
| 76 | |||
| 77 | /** @var SharesRequest */ |
||
| 78 | private $sharesRequest; |
||
| 79 | |||
| 80 | /** @var FederatedLinksRequest */ |
||
| 81 | private $federatedLinksRequest; |
||
| 82 | |||
| 83 | /** @var GSUpstreamService */ |
||
| 84 | private $gsUpstreamService; |
||
| 85 | |||
| 86 | /** @var EventsService */ |
||
| 87 | private $eventsService; |
||
| 88 | |||
| 89 | /** @var CircleProviderRequest */ |
||
| 90 | private $circleProviderRequest; |
||
| 91 | |||
| 92 | /** @var MiscService */ |
||
| 93 | private $miscService; |
||
| 94 | |||
| 95 | |||
| 96 | /** |
||
| 97 | * CirclesService constructor. |
||
| 98 | * |
||
| 99 | * @param string $userId |
||
| 100 | * @param IL10N $l10n |
||
| 101 | * @param IUserSession $userSession |
||
| 102 | * @param IGroupManager $groupManager |
||
| 103 | * @param ConfigService $configService |
||
| 104 | * @param CirclesRequest $circlesRequest |
||
| 105 | * @param MembersRequest $membersRequest |
||
| 106 | * @param SharesRequest $sharesRequest |
||
| 107 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 108 | * @param GSUpstreamService $gsUpstreamService |
||
| 109 | * @param EventsService $eventsService |
||
| 110 | * @param CircleProviderRequest $circleProviderRequest |
||
| 111 | * @param MiscService $miscService |
||
| 112 | */ |
||
| 113 | public function __construct( |
||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * Create circle using this->userId as owner |
||
| 153 | * |
||
| 154 | * @param int|string $type |
||
| 155 | * @param string $name |
||
| 156 | * |
||
| 157 | * @param string $ownerId |
||
| 158 | * |
||
| 159 | * @return Circle |
||
| 160 | * @throws CircleAlreadyExistsException |
||
| 161 | * @throws CircleTypeDisabledException |
||
| 162 | * @throws MemberAlreadyExistsException |
||
| 163 | * @throws Exception |
||
| 164 | */ |
||
| 165 | public function createCircle($type, $name, string $ownerId = '') { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * list Circles depends on type (or all) and name (parts) and minimum level. |
||
| 212 | * |
||
| 213 | * @param string $userId |
||
| 214 | * @param mixed $type |
||
| 215 | * @param string $name |
||
| 216 | * @param int $level |
||
| 217 | * |
||
| 218 | * @param bool $forceAll |
||
| 219 | * |
||
| 220 | * @return Circle[] |
||
| 221 | * @throws CircleTypeDisabledException |
||
| 222 | * @throws Exception |
||
| 223 | */ |
||
| 224 | public function listCircles($userId, $type, $name = '', $level = 0, $forceAll = false) { |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * returns details on circle and its members if this->userId is a member itself. |
||
| 249 | * |
||
| 250 | * @param string $circleUniqueId |
||
| 251 | * @param bool $forceAll |
||
| 252 | * |
||
| 253 | * @return Circle |
||
| 254 | * @throws Exception |
||
| 255 | */ |
||
| 256 | public function detailsCircle($circleUniqueId, $forceAll = false) { |
||
| 275 | |||
| 276 | |||
| 277 | /** |
||
| 278 | * get the Members list and add the result to the Circle. |
||
| 279 | * |
||
| 280 | * @param Circle $circle |
||
| 281 | * |
||
| 282 | * @throws Exception |
||
| 283 | */ |
||
| 284 | private function detailsCircleMembers(Circle &$circle) { |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * // TODO - check this on GS setup |
||
| 299 | * get the Linked Group list and add the result to the Circle. |
||
| 300 | * |
||
| 301 | * @param Circle $circle |
||
| 302 | * |
||
| 303 | * @throws MemberDoesNotExistException |
||
| 304 | */ |
||
| 305 | private function detailsCircleLinkedGroups(Circle &$circle) { |
||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * get the Federated Circles list and add the result to the Circle. |
||
| 320 | * |
||
| 321 | * @param Circle $circle |
||
| 322 | */ |
||
| 323 | private function detailsCircleFederatedCircles(Circle &$circle) { |
||
| 336 | |||
| 337 | |||
| 338 | /** |
||
| 339 | * save new settings if current user is admin. |
||
| 340 | * |
||
| 341 | * @param string $circleUniqueId |
||
| 342 | * @param array $settings |
||
| 343 | * |
||
| 344 | * @return Circle |
||
| 345 | * @throws Exception |
||
| 346 | */ |
||
| 347 | public function settingsCircle(string $circleUniqueId, array $settings) { |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * Join a circle. |
||
| 370 | * |
||
| 371 | * @param string $circleUniqueId |
||
| 372 | * |
||
| 373 | * @return null|Member |
||
| 374 | * @throws Exception |
||
| 375 | */ |
||
| 376 | public function joinCircle($circleUniqueId) { |
||
| 393 | |||
| 394 | |||
| 395 | /** |
||
| 396 | * Leave a circle. |
||
| 397 | * |
||
| 398 | * @param string $circleUniqueId |
||
| 399 | * |
||
| 400 | * @return null|Member |
||
| 401 | * @throws Exception |
||
| 402 | */ |
||
| 403 | public function leaveCircle($circleUniqueId) { |
||
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * destroy a circle. |
||
| 418 | * |
||
| 419 | * @param string $circleUniqueId |
||
| 420 | * |
||
| 421 | * @param bool $force |
||
| 422 | * |
||
| 423 | * @throws CircleDoesNotExistException |
||
| 424 | * @throws MemberIsNotOwnerException |
||
| 425 | * @throws ConfigNoCircleAvailableException |
||
| 426 | * @throws Exception |
||
| 427 | */ |
||
| 428 | public function removeCircle($circleUniqueId, bool $force = false) { |
||
| 443 | |||
| 444 | |||
| 445 | /** |
||
| 446 | * @param $circleName |
||
| 447 | * |
||
| 448 | * @return Circle|null |
||
| 449 | * @throws CircleDoesNotExistException |
||
| 450 | */ |
||
| 451 | public function infoCircleByName($circleName) { |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Convert a Type in String to its Bit Value |
||
| 458 | * |
||
| 459 | * @param string $type |
||
| 460 | * |
||
| 461 | * @return int|mixed |
||
| 462 | */ |
||
| 463 | public function convertTypeStringToBitValue($type) { |
||
| 478 | |||
| 479 | |||
| 480 | /** |
||
| 481 | * getCircleIcon() |
||
| 482 | * |
||
| 483 | * Return the right imagePath for a type of circle. |
||
| 484 | * |
||
| 485 | * @param string $type |
||
| 486 | * @param bool $png |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | public static function getCircleIcon($type, $png = false) { |
||
| 521 | |||
| 522 | |||
| 523 | /** |
||
| 524 | * @param string $circleUniqueIds |
||
| 525 | * @param int $limit |
||
| 526 | * @param int $offset |
||
| 527 | * |
||
| 528 | * @return array |
||
| 529 | */ |
||
| 530 | public function getFilesForCircles($circleUniqueIds, $limit = -1, $offset = 0) { |
||
| 541 | |||
| 542 | |||
| 543 | /** |
||
| 544 | * @param Circle $circle |
||
| 545 | * |
||
| 546 | * @throws MembersLimitException |
||
| 547 | */ |
||
| 548 | public function checkThatCircleIsNotFull(Circle $circle) { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @return bool |
||
| 570 | */ |
||
| 571 | public function viewerIsAdmin(): bool { |
||
| 578 | |||
| 579 | |||
| 580 | /** |
||
| 581 | * should be moved. |
||
| 582 | * |
||
| 583 | * @param Member $member |
||
| 584 | * |
||
| 585 | * @throws MemberIsNotOwnerException |
||
| 586 | */ |
||
| 587 | View Code Duplication | public function hasToBeOwner(Member $member) { |
|
| 595 | |||
| 596 | |||
| 597 | /** |
||
| 598 | * should be moved. |
||
| 599 | * |
||
| 600 | * @param Member $member |
||
| 601 | * |
||
| 602 | * @throws MemberIsNotOwnerException |
||
| 603 | */ |
||
| 604 | View Code Duplication | public function hasToBeAdmin(Member $member) { |
|
| 612 | } |
||
| 613 |