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 | * |
||
| 147 | * @return FederatedLink |
||
| 148 | */ |
||
| 149 | public function linkCircle($circleId, $remote) { |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * linkStatus() |
||
| 173 | * |
||
| 174 | * Update the status of a link. |
||
| 175 | * Function will check if user can edit the status, will update it and send the update to |
||
| 176 | * remote |
||
| 177 | * |
||
| 178 | * @param int $linkId |
||
| 179 | * @param int $status |
||
| 180 | * |
||
| 181 | * @throws Exception |
||
| 182 | * @throws FederatedCircleLinkFormatException |
||
| 183 | * @throws CircleTypeNotValid |
||
| 184 | * @throws MemberIsNotAdminException |
||
| 185 | * |
||
| 186 | * @return FederatedLink[] |
||
| 187 | */ |
||
| 188 | public function linkStatus($linkId, $status) { |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * eventOnLinkStatus(); |
||
| 224 | * |
||
| 225 | * Called by linkStatus() to manage events when status is changing. |
||
| 226 | * If status does not need update, returns false; |
||
| 227 | * |
||
| 228 | * @param Circle $circle |
||
| 229 | * @param FederatedLink $link |
||
| 230 | * @param $status |
||
| 231 | * |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | private function eventOnLinkStatus(Circle $circle, FederatedLink $link, $status) { |
||
| 250 | |||
| 251 | |||
| 252 | /** |
||
| 253 | * requestLinkWithCircle() |
||
| 254 | * |
||
| 255 | * Using CircleId, function will get more infos from the database. |
||
| 256 | * Will check if author is at least admin and initiate a FederatedLink, save it |
||
| 257 | * in the database and send a request to the remote circle using requestLink() |
||
| 258 | * If any issue, entry is removed from the database. |
||
| 259 | * |
||
| 260 | * @param integer $circleId |
||
| 261 | * @param string $remote |
||
| 262 | * |
||
| 263 | * @return FederatedLink |
||
| 264 | * @throws Exception |
||
| 265 | */ |
||
| 266 | private function requestLinkWithCircle($circleId, $remote) { |
||
| 298 | |||
| 299 | |||
| 300 | /** |
||
| 301 | * @param string $remote |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | View Code Duplication | private function generateLinkRemoteURL($remote) { |
|
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $remote |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | View Code Duplication | private function generatePayloadDeliveryURL($remote) { |
|
| 326 | |||
| 327 | |||
| 328 | public function allowNonSSLLink() { |
||
| 331 | |||
| 332 | |||
| 333 | /** |
||
| 334 | * requestLink() |
||
| 335 | * |
||
| 336 | * |
||
| 337 | * @param Circle $circle |
||
| 338 | * @param FederatedLink $link |
||
| 339 | * |
||
| 340 | * @return boolean |
||
| 341 | * @throws Exception |
||
| 342 | */ |
||
| 343 | private function requestLink(Circle $circle, FederatedLink & $link) { |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * eventOnRequestLink(); |
||
| 382 | * |
||
| 383 | * Called by requestLink() will update status and event |
||
| 384 | * Will also manage errors returned by the remote link |
||
| 385 | * |
||
| 386 | * @param Circle $circle |
||
| 387 | * @param FederatedLink $link |
||
| 388 | * @param $status |
||
| 389 | * @param $reason |
||
| 390 | * |
||
| 391 | * @throws Exception |
||
| 392 | */ |
||
| 393 | private function eventOnRequestLink(Circle $circle, FederatedLink $link, $status, $reason) { |
||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * parseRequestLinkError(); |
||
| 413 | * |
||
| 414 | * Will parse the error reason returned by requestLink() and throw an Exception |
||
| 415 | * |
||
| 416 | * @param $reason |
||
| 417 | * |
||
| 418 | * @throws Exception |
||
| 419 | * @throws FederatedRemoteCircleDoesNotExistException |
||
| 420 | * @throws FederatedRemoteDoesNotAllowException |
||
| 421 | */ |
||
| 422 | private function parseRequestLinkError($reason) { |
||
| 456 | |||
| 457 | |||
| 458 | /** |
||
| 459 | * @param $token |
||
| 460 | * @param $uniqueId |
||
| 461 | * @param $status |
||
| 462 | * |
||
| 463 | * @return FederatedLink |
||
| 464 | * @throws Exception |
||
| 465 | */ |
||
| 466 | public function updateLinkFromRemote($token, $uniqueId, $status) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * checkUpdateLinkFromRemote(); |
||
| 488 | * |
||
| 489 | * will throw exception is the status sent by remote is not correct |
||
| 490 | * |
||
| 491 | * @param $status |
||
| 492 | * |
||
| 493 | * @throws FederatedCircleStatusUpdateException |
||
| 494 | * @internal param FederatedLink $link |
||
| 495 | */ |
||
| 496 | private function checkUpdateLinkFromRemote($status) { |
||
| 506 | |||
| 507 | |||
| 508 | /** |
||
| 509 | * checkUpdateLinkFromRemoteLinkUp() |
||
| 510 | * |
||
| 511 | * in case of a request of status update from remote for a link up, we check the current |
||
| 512 | * status of the link locally. |
||
| 513 | * |
||
| 514 | * @param Circle $circle |
||
| 515 | * @param FederatedLink $link |
||
| 516 | * @param $status |
||
| 517 | * |
||
| 518 | * @throws FederatedCircleStatusUpdateException |
||
| 519 | */ |
||
| 520 | private function checkUpdateLinkFromRemoteLinkUp(Circle $circle, FederatedLink $link, $status) { |
||
| 535 | |||
| 536 | |||
| 537 | /** |
||
| 538 | * checkUpdateLinkFromRemoteLinkRemove(); |
||
| 539 | * |
||
| 540 | * in case of a request of status update from remote for a link down, we check the current |
||
| 541 | * status of the link locally |
||
| 542 | * |
||
| 543 | * @param Circle $circle |
||
| 544 | * @param FederatedLink $link |
||
| 545 | * @param $status |
||
| 546 | * |
||
| 547 | * @throws FederatedCircleStatusUpdateException |
||
| 548 | */ |
||
| 549 | private function checkUpdateLinkFromRemoteLinkRemove( |
||
| 581 | |||
| 582 | |||
| 583 | /** |
||
| 584 | * updateLinkRemote() |
||
| 585 | * |
||
| 586 | * Send a request to the remote of the link to update its status. |
||
| 587 | * |
||
| 588 | * @param FederatedLink $link |
||
| 589 | * |
||
| 590 | * @return boolean |
||
| 591 | * @throws Exception |
||
| 592 | */ |
||
| 593 | public function updateLinkRemote(FederatedLink & $link) { |
||
| 617 | |||
| 618 | |||
| 619 | /** |
||
| 620 | * Create a new link into database and assign the correct status. |
||
| 621 | * |
||
| 622 | * @param Circle $circle |
||
| 623 | * @param FederatedLink $link |
||
| 624 | * |
||
| 625 | * @throws Exception |
||
| 626 | */ |
||
| 627 | public function initiateLink(Circle $circle, FederatedLink & $link) { |
||
| 646 | |||
| 647 | |||
| 648 | /** |
||
| 649 | * @param Circle $circle |
||
| 650 | * @param FederatedLink $link |
||
| 651 | * |
||
| 652 | * @throws LinkCreationException |
||
| 653 | */ |
||
| 654 | private function checkLinkRequestValidity($circle, $link) { |
||
| 667 | |||
| 668 | |||
| 669 | /** |
||
| 670 | * @param string $token |
||
| 671 | * @param string $uniqueId |
||
| 672 | * @param SharingFrame $frame |
||
| 673 | * |
||
| 674 | * @return bool |
||
| 675 | * @throws Exception |
||
| 676 | */ |
||
| 677 | public function receiveFrame($token, $uniqueId, SharingFrame & $frame) { |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @param integer $circleId |
||
| 707 | * @param string $uniqueId |
||
| 708 | * |
||
| 709 | * @return FederatedLink |
||
| 710 | */ |
||
| 711 | public function getLink($circleId, $uniqueId) { |
||
| 714 | |||
| 715 | |||
| 716 | /** |
||
| 717 | * @param integer $circleId |
||
| 718 | * |
||
| 719 | * @return FederatedLink[] |
||
| 720 | */ |
||
| 721 | public function getLinks($circleId) { |
||
| 724 | |||
| 725 | |||
| 726 | /** |
||
| 727 | * @param int $circleId |
||
| 728 | * @param string $uniqueId |
||
| 729 | * |
||
| 730 | * @return bool |
||
| 731 | * @throws Exception |
||
| 732 | */ |
||
| 733 | public function initiateRemoteShare($circleId, $uniqueId) { |
||
| 760 | |||
| 761 | |||
| 762 | /** |
||
| 763 | * @param SharingFrame $frame |
||
| 764 | * |
||
| 765 | * @throws Exception |
||
| 766 | */ |
||
| 767 | public function sendRemoteShare(SharingFrame $frame) { |
||
| 798 | |||
| 799 | |||
| 800 | /** |
||
| 801 | * generateHeaders() |
||
| 802 | * |
||
| 803 | * Generate new headers for the current Payload, and save them in the SharingFrame. |
||
| 804 | * |
||
| 805 | * @param SharingFrame $frame |
||
| 806 | */ |
||
| 807 | public function updateFrameWithCloudId(SharingFrame $frame) { |
||
| 811 | |||
| 812 | } |
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: