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 string */ |
||
| 75 | private $serverHost; |
||
| 76 | |||
| 77 | /** @var ClientService */ |
||
| 78 | private $clientService; |
||
| 79 | |||
| 80 | /** @var MiscService */ |
||
| 81 | private $miscService; |
||
| 82 | |||
| 83 | /** @var bool */ |
||
| 84 | private $localTest = false; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * CirclesService constructor. |
||
| 88 | * |
||
| 89 | * @param $userId |
||
| 90 | * @param IL10N $l10n |
||
| 91 | * @param CirclesRequest $circlesRequest |
||
| 92 | * @param ConfigService $configService |
||
| 93 | * @param CirclesService $circlesService |
||
| 94 | * @param BroadcastService $broadcastService |
||
| 95 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 96 | * @param string $serverHost |
||
| 97 | * @param ClientService $clientService |
||
| 98 | * @param MiscService $miscService |
||
| 99 | */ |
||
| 100 | public function __construct( |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * linkCircle() |
||
| 127 | * |
||
| 128 | * link to a circle. |
||
| 129 | * Function will check if settings allow Federated links between circles, and the format of |
||
| 130 | * the link ($remote). If no exception, a request to the remote circle will be initiated |
||
| 131 | * using requestLinkWithCircle() |
||
| 132 | * |
||
| 133 | * $remote format: <circle_name>@<remote_host> |
||
| 134 | * |
||
| 135 | * @param int $circleId |
||
| 136 | * @param string $remote |
||
| 137 | * |
||
| 138 | * @throws Exception |
||
| 139 | * @throws FederatedCircleLinkFormatException |
||
| 140 | * @throws CircleTypeNotValid |
||
| 141 | * @throws MemberIsNotAdminException |
||
| 142 | * |
||
| 143 | * @return FederatedLink |
||
| 144 | */ |
||
| 145 | public function linkCircle($circleId, $remote) { |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * linkStatus() |
||
| 169 | * |
||
| 170 | * Update the status of a link. |
||
| 171 | * Function will check if user can edit the status, will update it and send the update to |
||
| 172 | * remote |
||
| 173 | * |
||
| 174 | * @param int $linkId |
||
| 175 | * @param int $status |
||
| 176 | * |
||
| 177 | * @throws Exception |
||
| 178 | * @throws FederatedCircleLinkFormatException |
||
| 179 | * @throws CircleTypeNotValid |
||
| 180 | * @throws MemberIsNotAdminException |
||
| 181 | * |
||
| 182 | * @return FederatedLink[] |
||
| 183 | */ |
||
| 184 | public function linkStatus($linkId, $status) { |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * requestLinkWithCircle() |
||
| 215 | * |
||
| 216 | * Using CircleId, function will get more infos from the database. |
||
| 217 | * Will check if author is at least admin and initiate a FederatedLink, save it |
||
| 218 | * in the database and send a request to the remote circle using requestLink() |
||
| 219 | * If any issue, entry is removed from the database. |
||
| 220 | * |
||
| 221 | * @param integer $circleId |
||
| 222 | * @param string $remote |
||
| 223 | * |
||
| 224 | * @return FederatedLink |
||
| 225 | * @throws Exception |
||
| 226 | */ |
||
| 227 | private function requestLinkWithCircle($circleId, $remote) { |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $remote |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | View Code Duplication | private function generateLinkRemoteURL($remote) { |
|
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $remote |
||
| 277 | * |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | View Code Duplication | private function generatePayloadDeliveryURL($remote) { |
|
| 287 | |||
| 288 | |||
| 289 | public function allowNonSSLLink() { |
||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * requestLink() |
||
| 296 | * |
||
| 297 | * |
||
| 298 | * @param Circle $circle |
||
| 299 | * @param FederatedLink $link |
||
| 300 | * |
||
| 301 | * @return boolean |
||
| 302 | * @throws Exception |
||
| 303 | */ |
||
| 304 | private function requestLink(Circle $circle, FederatedLink & $link) { |
||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * @param $token |
||
| 348 | * @param $uniqueId |
||
| 349 | * @param $status |
||
| 350 | * |
||
| 351 | * @return FederatedLink |
||
| 352 | * @throws Exception |
||
| 353 | */ |
||
| 354 | public function updateLinkFromRemote($token, $uniqueId, $status) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param FederatedLink $link |
||
| 377 | * @param $status |
||
| 378 | * |
||
| 379 | * @throws FederatedCircleStatusUpdateException |
||
| 380 | */ |
||
| 381 | private function checkUpdateLinkFromRemote(FederatedLink &$link, $status) { |
||
| 393 | |||
| 394 | |||
| 395 | /** |
||
| 396 | * @param FederatedLink $link |
||
| 397 | * @param $status |
||
| 398 | * |
||
| 399 | * @throws FederatedCircleStatusUpdateException |
||
| 400 | */ |
||
| 401 | private function checkUpdateLinkFromRemoteLinkUp(FederatedLink &$link, $status) { |
||
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * @param FederatedLink $link |
||
| 418 | * @param $status |
||
| 419 | * |
||
| 420 | * @throws FederatedCircleStatusUpdateException |
||
| 421 | */ |
||
| 422 | private function checkUpdateLinkFromRemoteLinkRemove(FederatedLink &$link, $status) { |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * updateLinkRemote() |
||
| 449 | * |
||
| 450 | * Send a request to the remote of the link to update its status. |
||
| 451 | * |
||
| 452 | * @param FederatedLink $link |
||
| 453 | * |
||
| 454 | * @return boolean |
||
| 455 | * @throws Exception |
||
| 456 | */ |
||
| 457 | public function updateLinkRemote(FederatedLink & $link) { |
||
| 481 | |||
| 482 | |||
| 483 | private function parsingRequestLinkResult($result) { |
||
| 517 | |||
| 518 | |||
| 519 | /** |
||
| 520 | * Create a new link into database and assign the correct status. |
||
| 521 | * |
||
| 522 | * @param Circle $circle |
||
| 523 | * @param FederatedLink $link |
||
| 524 | * |
||
| 525 | * @throws Exception |
||
| 526 | */ |
||
| 527 | public function initiateLink(Circle $circle, FederatedLink & $link) { |
||
| 544 | |||
| 545 | |||
| 546 | /** |
||
| 547 | * @param Circle $circle |
||
| 548 | * @param FederatedLink $link |
||
| 549 | * |
||
| 550 | * @throws LinkCreationException |
||
| 551 | */ |
||
| 552 | private function checkLinkRequestValidity($circle, $link) { |
||
| 565 | |||
| 566 | |||
| 567 | /** |
||
| 568 | * @param string $token |
||
| 569 | * @param string $uniqueId |
||
| 570 | * @param SharingFrame $frame |
||
| 571 | * |
||
| 572 | * @return bool |
||
| 573 | * @throws Exception |
||
| 574 | */ |
||
| 575 | public function receiveFrame($token, $uniqueId, SharingFrame & $frame) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @param integer $circleId |
||
| 605 | * @param string $uniqueId |
||
| 606 | * |
||
| 607 | * @return FederatedLink |
||
| 608 | */ |
||
| 609 | public function getLink($circleId, $uniqueId) { |
||
| 612 | |||
| 613 | |||
| 614 | /** |
||
| 615 | * @param integer $circleId |
||
| 616 | * |
||
| 617 | * @return FederatedLink[] |
||
| 618 | */ |
||
| 619 | public function getLinks($circleId) { |
||
| 622 | |||
| 623 | |||
| 624 | /** |
||
| 625 | * @param int $circleId |
||
| 626 | * @param string $uniqueId |
||
| 627 | * |
||
| 628 | * @return bool |
||
| 629 | * @throws Exception |
||
| 630 | */ |
||
| 631 | public function initiateRemoteShare($circleId, $uniqueId) { |
||
| 658 | |||
| 659 | |||
| 660 | /** |
||
| 661 | * @param SharingFrame $frame |
||
| 662 | * |
||
| 663 | * @throws Exception |
||
| 664 | */ |
||
| 665 | public function sendRemoteShare(SharingFrame $frame) { |
||
| 696 | |||
| 697 | |||
| 698 | /** |
||
| 699 | * generateHeaders() |
||
| 700 | * |
||
| 701 | * Generate new headers for the current Payload, and save them in the SharingFrame. |
||
| 702 | * |
||
| 703 | * @param SharingFrame $frame |
||
| 704 | */ |
||
| 705 | public function updateFrameWithCloudId(SharingFrame $frame) { |
||
| 709 | |||
| 710 | } |
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: