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 |
||
| 49 | class FederatedService { |
||
| 50 | |||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | private $userId; |
||
| 54 | |||
| 55 | /** @var IL10N */ |
||
| 56 | private $l10n; |
||
| 57 | |||
| 58 | /** @var CirclesRequest */ |
||
| 59 | private $circlesRequest; |
||
| 60 | |||
| 61 | /** @var ConfigService */ |
||
| 62 | private $configService; |
||
| 63 | |||
| 64 | /** @var CirclesService */ |
||
| 65 | private $circlesService; |
||
| 66 | |||
| 67 | /** @var BroadcastService */ |
||
| 68 | private $broadcastService; |
||
| 69 | |||
| 70 | /** @var FederatedLinksRequest */ |
||
| 71 | private $federatedLinksRequest; |
||
| 72 | |||
| 73 | /** @var string */ |
||
| 74 | private $serverHost; |
||
| 75 | |||
| 76 | /** @var ClientService */ |
||
| 77 | private $clientService; |
||
| 78 | |||
| 79 | /** @var MiscService */ |
||
| 80 | private $miscService; |
||
| 81 | |||
| 82 | /** @var bool */ |
||
| 83 | private $localTest = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * CirclesService constructor. |
||
| 87 | * |
||
| 88 | * @param $userId |
||
| 89 | * @param IL10N $l10n |
||
| 90 | * @param CirclesRequest $circlesRequest |
||
| 91 | * @param ConfigService $configService |
||
| 92 | * @param CirclesService $circlesService |
||
| 93 | * @param BroadcastService $broadcastService |
||
| 94 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 95 | * @param string $serverHost |
||
| 96 | * @param ClientService $clientService |
||
| 97 | * @param MiscService $miscService |
||
| 98 | */ |
||
| 99 | public function __construct( |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * linkCircle() |
||
| 126 | * |
||
| 127 | * link to a circle. |
||
| 128 | * Function will check if settings allow Federated links between circles, and the format of |
||
| 129 | * the link ($remote). If no exception, a request to the remote circle will be initiated |
||
| 130 | * using requestLinkWithCircle() |
||
| 131 | * |
||
| 132 | * $remote format: <circle_name>@<remote_host> |
||
| 133 | * |
||
| 134 | * @param int $circleId |
||
| 135 | * @param string $remote |
||
| 136 | * |
||
| 137 | * @throws Exception |
||
| 138 | * @throws FederatedCircleLinkFormatException |
||
| 139 | * @throws CircleTypeNotValid |
||
| 140 | * @throws MemberIsNotAdminException |
||
| 141 | * |
||
| 142 | * @return FederatedLink |
||
| 143 | */ |
||
| 144 | public function linkCircle($circleId, $remote) { |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * linkStatus() |
||
| 168 | * |
||
| 169 | * Update the status of a link. |
||
| 170 | * Function will check if user can edit the status, will update it and send the update to |
||
| 171 | * remote |
||
| 172 | * |
||
| 173 | * @param int $linkId |
||
| 174 | * @param int $status |
||
| 175 | * |
||
| 176 | * @throws Exception |
||
| 177 | * @throws FederatedCircleLinkFormatException |
||
| 178 | * @throws CircleTypeNotValid |
||
| 179 | * @throws MemberIsNotAdminException |
||
| 180 | * |
||
| 181 | * @return FederatedLink[] |
||
| 182 | */ |
||
| 183 | public function linkStatus($linkId, $status) { |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * requestLinkWithCircle() |
||
| 216 | * |
||
| 217 | * Using CircleId, function will get more infos from the database. |
||
| 218 | * Will check if author is at least admin and initiate a FederatedLink, save it |
||
| 219 | * in the database and send a request to the remote circle using requestLink() |
||
| 220 | * If any issue, entry is removed from the database. |
||
| 221 | * |
||
| 222 | * @param integer $circleId |
||
| 223 | * @param string $remote |
||
| 224 | * |
||
| 225 | * @return FederatedLink |
||
| 226 | * @throws Exception |
||
| 227 | */ |
||
| 228 | private function requestLinkWithCircle($circleId, $remote) { |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $remote |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | View Code Duplication | private function generateLinkRemoteURL($remote) { |
|
| 274 | |||
| 275 | |||
| 276 | /** |
||
| 277 | * @param string $remote |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | View Code Duplication | private function generatePayloadDeliveryURL($remote) { |
|
| 288 | |||
| 289 | |||
| 290 | public function allowNonSSLLink() { |
||
| 293 | |||
| 294 | |||
| 295 | /** |
||
| 296 | * requestLink() |
||
| 297 | * |
||
| 298 | * |
||
| 299 | * @param Circle $circle |
||
| 300 | * @param FederatedLink $link |
||
| 301 | * |
||
| 302 | * @return boolean |
||
| 303 | * @throws Exception |
||
| 304 | */ |
||
| 305 | private function requestLink(Circle $circle, FederatedLink & $link) { |
||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * @param $token |
||
| 350 | * @param $uniqueId |
||
| 351 | * @param $status |
||
| 352 | * |
||
| 353 | * @return FederatedLink |
||
| 354 | * @throws Exception |
||
| 355 | */ |
||
| 356 | public function updateLinkLocal($token, $uniqueId, $status) { |
||
| 367 | |||
| 368 | |||
| 369 | /** |
||
| 370 | * updateLinkRemote() |
||
| 371 | * |
||
| 372 | * Send a request to the remote of the link to update its status. |
||
| 373 | * |
||
| 374 | * @param FederatedLink $link |
||
| 375 | * |
||
| 376 | * @return boolean |
||
| 377 | * @throws Exception |
||
| 378 | */ |
||
| 379 | public function updateLinkRemote(FederatedLink & $link) { |
||
| 418 | |||
| 419 | |||
| 420 | private function parsingRequestLinkResult($result) { |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Create a new link into database and assign the correct status. |
||
| 458 | * |
||
| 459 | * @param Circle $circle |
||
| 460 | * @param FederatedLink $link |
||
| 461 | * |
||
| 462 | * @throws Exception |
||
| 463 | */ |
||
| 464 | public function initiateLink(Circle $circle, FederatedLink & $link) { |
||
| 481 | |||
| 482 | |||
| 483 | /** |
||
| 484 | * @param Circle $circle |
||
| 485 | * @param FederatedLink $link |
||
| 486 | * |
||
| 487 | * @throws LinkCreationException |
||
| 488 | */ |
||
| 489 | private function checkLinkRequestValidity($circle, $link) { |
||
| 502 | |||
| 503 | |||
| 504 | /** |
||
| 505 | * @param string $token |
||
| 506 | * @param string $uniqueId |
||
| 507 | * @param SharingFrame $frame |
||
| 508 | * |
||
| 509 | * @return bool |
||
| 510 | * @throws Exception |
||
| 511 | */ |
||
| 512 | public function receiveFrame($token, $uniqueId, SharingFrame & $frame) { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @param integer $circleId |
||
| 540 | * @param string $uniqueId |
||
| 541 | * |
||
| 542 | * @return FederatedLink |
||
| 543 | */ |
||
| 544 | public function getLink($circleId, $uniqueId) { |
||
| 547 | |||
| 548 | |||
| 549 | /** |
||
| 550 | * @param integer $circleId |
||
| 551 | * |
||
| 552 | * @return FederatedLink[] |
||
| 553 | */ |
||
| 554 | public function getLinks($circleId) { |
||
| 557 | |||
| 558 | |||
| 559 | /** |
||
| 560 | * @param int $circleId |
||
| 561 | * @param string $uniqueId |
||
| 562 | * |
||
| 563 | * @return bool |
||
| 564 | * @throws Exception |
||
| 565 | */ |
||
| 566 | public function initiateRemoteShare($circleId, $uniqueId) { |
||
| 593 | |||
| 594 | |||
| 595 | /** |
||
| 596 | * @param SharingFrame $frame |
||
| 597 | * |
||
| 598 | * @throws Exception |
||
| 599 | */ |
||
| 600 | public function sendRemoteShare(SharingFrame $frame) { |
||
| 631 | |||
| 632 | |||
| 633 | /** |
||
| 634 | * generateHeaders() |
||
| 635 | * |
||
| 636 | * Generate new headers for the current Payload, and save them in the SharingFrame. |
||
| 637 | * |
||
| 638 | * @param SharingFrame $frame |
||
| 639 | */ |
||
| 640 | public function updateFrameWithCloudId(SharingFrame $frame) { |
||
| 644 | |||
| 645 | } |
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: