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 FederatedService 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 FederatedService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class FederatedService { |
||
| 51 | |||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | private $userId; |
||
| 55 | |||
| 56 | /** @var IL10N */ |
||
| 57 | private $l10n; |
||
| 58 | |||
| 59 | /** @var CirclesRequest */ |
||
| 60 | private $circlesRequest; |
||
| 61 | |||
| 62 | /** @var ConfigService */ |
||
| 63 | private $configService; |
||
| 64 | |||
| 65 | /** @var CirclesService */ |
||
| 66 | private $circlesService; |
||
| 67 | |||
| 68 | /** @var BroadcastService */ |
||
| 69 | private $broadcastService; |
||
| 70 | |||
| 71 | /** @var FederatedLinksRequest */ |
||
| 72 | private $federatedLinksRequest; |
||
| 73 | |||
| 74 | /** @var EventsService */ |
||
| 75 | private $eventsService; |
||
| 76 | |||
| 77 | /** @var string */ |
||
| 78 | private $serverHost; |
||
| 79 | |||
| 80 | /** @var ClientService */ |
||
| 81 | private $clientService; |
||
| 82 | |||
| 83 | /** @var MiscService */ |
||
| 84 | private $miscService; |
||
| 85 | |||
| 86 | /** @var bool */ |
||
| 87 | private $localTest = false; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * CirclesService constructor. |
||
| 91 | * |
||
| 92 | * @param $userId |
||
| 93 | * @param IL10N $l10n |
||
| 94 | * @param CirclesRequest $circlesRequest |
||
| 95 | * @param ConfigService $configService |
||
| 96 | * @param CirclesService $circlesService |
||
| 97 | * @param BroadcastService $broadcastService |
||
| 98 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 99 | * @param EventsService $eventsService |
||
| 100 | * @param string $serverHost |
||
| 101 | * @param ClientService $clientService |
||
| 102 | * @param MiscService $miscService |
||
| 103 | */ |
||
| 104 | public function __construct( |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * linkCircle() |
||
| 132 | * |
||
| 133 | * link to a circle. |
||
| 134 | * Function will check if settings allow Federated links between circles, and the format of |
||
| 135 | * the link ($remote). If no exception, a request to the remote circle will be initiated |
||
| 136 | * using requestLinkWithCircle() |
||
| 137 | * |
||
| 138 | * $remote format: <circle_name>@<remote_host> |
||
| 139 | * |
||
| 140 | * @param int $circleId |
||
| 141 | * @param string $remote |
||
| 142 | * |
||
| 143 | * @throws Exception |
||
| 144 | * @throws FederatedCircleLinkFormatException |
||
| 145 | * @throws CircleTypeNotValid |
||
| 146 | * @throws MemberIsNotAdminException |
||
| 147 | * |
||
| 148 | * @return FederatedLink |
||
| 149 | */ |
||
| 150 | public function linkCircle($circleId, $remote) { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * linkStatus() |
||
| 174 | * |
||
| 175 | * Update the status of a link. |
||
| 176 | * Function will check if user can edit the status, will update it and send the update to |
||
| 177 | * remote |
||
| 178 | * |
||
| 179 | * @param int $linkId |
||
| 180 | * @param int $status |
||
| 181 | * |
||
| 182 | * @throws Exception |
||
| 183 | * @throws FederatedCircleLinkFormatException |
||
| 184 | * @throws CircleTypeNotValid |
||
| 185 | * @throws MemberIsNotAdminException |
||
| 186 | * |
||
| 187 | * @return FederatedLink[] |
||
| 188 | */ |
||
| 189 | public function linkStatus($linkId, $status) { |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * requestLinkWithCircle() |
||
| 234 | * |
||
| 235 | * Using CircleId, function will get more infos from the database. |
||
| 236 | * Will check if author is at least admin and initiate a FederatedLink, save it |
||
| 237 | * in the database and send a request to the remote circle using requestLink() |
||
| 238 | * If any issue, entry is removed from the database. |
||
| 239 | * |
||
| 240 | * @param integer $circleId |
||
| 241 | * @param string $remote |
||
| 242 | * |
||
| 243 | * @return FederatedLink |
||
| 244 | * @throws Exception |
||
| 245 | */ |
||
| 246 | private function requestLinkWithCircle($circleId, $remote) { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $remote |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | View Code Duplication | private function generateLinkRemoteURL($remote) { |
|
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $remote |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | View Code Duplication | private function generatePayloadDeliveryURL($remote) { |
|
| 306 | |||
| 307 | |||
| 308 | public function allowNonSSLLink() { |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * requestLink() |
||
| 315 | * |
||
| 316 | * |
||
| 317 | * @param Circle $circle |
||
| 318 | * @param FederatedLink $link |
||
| 319 | * |
||
| 320 | * @return boolean |
||
| 321 | * @throws Exception |
||
| 322 | */ |
||
| 323 | private function requestLink(Circle $circle, FederatedLink & $link) { |
||
| 365 | |||
| 366 | |||
| 367 | /** |
||
| 368 | * @param $token |
||
| 369 | * @param $uniqueId |
||
| 370 | * @param $status |
||
| 371 | * |
||
| 372 | * @return FederatedLink |
||
| 373 | * @throws Exception |
||
| 374 | */ |
||
| 375 | public function updateLinkFromRemote($token, $uniqueId, $status) { |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param FederatedLink $link |
||
| 397 | * @param $status |
||
| 398 | * |
||
| 399 | * @throws FederatedCircleStatusUpdateException |
||
| 400 | */ |
||
| 401 | private function checkUpdateLinkFromRemote(FederatedLink $link, $status) { |
||
| 413 | |||
| 414 | |||
| 415 | /** |
||
| 416 | * @param Circle $circle |
||
| 417 | * @param FederatedLink $link |
||
| 418 | * @param $status |
||
| 419 | * |
||
| 420 | * @throws FederatedCircleStatusUpdateException |
||
| 421 | */ |
||
| 422 | private function checkUpdateLinkFromRemoteLinkUp(Circle $circle, FederatedLink $link, $status) { |
||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * @param Circle $circle |
||
| 441 | * @param FederatedLink $link |
||
| 442 | * @param $status |
||
| 443 | * |
||
| 444 | * @throws FederatedCircleStatusUpdateException |
||
| 445 | */ |
||
| 446 | private function checkUpdateLinkFromRemoteLinkRemove( |
||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * updateLinkRemote() |
||
| 477 | * |
||
| 478 | * Send a request to the remote of the link to update its status. |
||
| 479 | * |
||
| 480 | * @param FederatedLink $link |
||
| 481 | * |
||
| 482 | * @return boolean |
||
| 483 | * @throws Exception |
||
| 484 | */ |
||
| 485 | public function updateLinkRemote(FederatedLink & $link) { |
||
| 509 | |||
| 510 | |||
| 511 | private function parsingRequestLinkResult($result) { |
||
| 545 | |||
| 546 | |||
| 547 | /** |
||
| 548 | * Create a new link into database and assign the correct status. |
||
| 549 | * |
||
| 550 | * @param Circle $circle |
||
| 551 | * @param FederatedLink $link |
||
| 552 | * |
||
| 553 | * @throws Exception |
||
| 554 | */ |
||
| 555 | public function initiateLink(Circle $circle, FederatedLink & $link) { |
||
| 574 | |||
| 575 | |||
| 576 | /** |
||
| 577 | * @param Circle $circle |
||
| 578 | * @param FederatedLink $link |
||
| 579 | * |
||
| 580 | * @throws LinkCreationException |
||
| 581 | */ |
||
| 582 | private function checkLinkRequestValidity($circle, $link) { |
||
| 595 | |||
| 596 | |||
| 597 | /** |
||
| 598 | * @param string $token |
||
| 599 | * @param string $uniqueId |
||
| 600 | * @param SharingFrame $frame |
||
| 601 | * |
||
| 602 | * @return bool |
||
| 603 | * @throws Exception |
||
| 604 | */ |
||
| 605 | public function receiveFrame($token, $uniqueId, SharingFrame & $frame) { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * @param integer $circleId |
||
| 635 | * @param string $uniqueId |
||
| 636 | * |
||
| 637 | * @return FederatedLink |
||
| 638 | */ |
||
| 639 | public function getLink($circleId, $uniqueId) { |
||
| 642 | |||
| 643 | |||
| 644 | /** |
||
| 645 | * @param integer $circleId |
||
| 646 | * |
||
| 647 | * @return FederatedLink[] |
||
| 648 | */ |
||
| 649 | public function getLinks($circleId) { |
||
| 652 | |||
| 653 | |||
| 654 | /** |
||
| 655 | * @param int $circleId |
||
| 656 | * @param string $uniqueId |
||
| 657 | * |
||
| 658 | * @return bool |
||
| 659 | * @throws Exception |
||
| 660 | */ |
||
| 661 | public function initiateRemoteShare($circleId, $uniqueId) { |
||
| 688 | |||
| 689 | |||
| 690 | /** |
||
| 691 | * @param SharingFrame $frame |
||
| 692 | * |
||
| 693 | * @throws Exception |
||
| 694 | */ |
||
| 695 | public function sendRemoteShare(SharingFrame $frame) { |
||
| 726 | |||
| 727 | |||
| 728 | /** |
||
| 729 | * generateHeaders() |
||
| 730 | * |
||
| 731 | * Generate new headers for the current Payload, and save them in the SharingFrame. |
||
| 732 | * |
||
| 733 | * @param SharingFrame $frame |
||
| 734 | */ |
||
| 735 | public function updateFrameWithCloudId(SharingFrame $frame) { |
||
| 739 | |||
| 740 | } |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: