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) { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * requestLinkWithCircle() |
||
| 212 | * |
||
| 213 | * Using CircleId, function will get more infos from the database. |
||
| 214 | * Will check if author is at least admin and initiate a FederatedLink, save it |
||
| 215 | * in the database and send a request to the remote circle using requestLink() |
||
| 216 | * If any issue, entry is removed from the database. |
||
| 217 | * |
||
| 218 | * @param integer $circleId |
||
| 219 | * @param string $remote |
||
| 220 | * |
||
| 221 | * @return FederatedLink |
||
| 222 | * @throws Exception |
||
| 223 | */ |
||
| 224 | private function requestLinkWithCircle($circleId, $remote) { |
||
| 256 | |||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $remote |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | View Code Duplication | private function generateLinkRemoteURL($remote) { |
|
| 270 | |||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $remote |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | View Code Duplication | private function generatePayloadDeliveryURL($remote) { |
|
| 284 | |||
| 285 | |||
| 286 | public function allowNonSSLLink() { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * requestLink() |
||
| 293 | * |
||
| 294 | * |
||
| 295 | * @param Circle $circle |
||
| 296 | * @param FederatedLink $link |
||
| 297 | * |
||
| 298 | * @return boolean |
||
| 299 | * @throws Exception |
||
| 300 | */ |
||
| 301 | private function requestLink(Circle $circle, FederatedLink & $link) { |
||
| 341 | |||
| 342 | |||
| 343 | private function parsingRequestLinkResult($result) { |
||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * Create a new link into database and assign the correct status. |
||
| 381 | * |
||
| 382 | * @param Circle $circle |
||
| 383 | * @param FederatedLink $link |
||
| 384 | * |
||
| 385 | * @throws Exception |
||
| 386 | */ |
||
| 387 | public function initiateLink(Circle $circle, FederatedLink & $link) { |
||
| 404 | |||
| 405 | |||
| 406 | /** |
||
| 407 | * @param Circle $circle |
||
| 408 | * @param FederatedLink $link |
||
| 409 | * |
||
| 410 | * @throws LinkCreationException |
||
| 411 | */ |
||
| 412 | private function checkLinkRequestValidity($circle, $link) { |
||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * @param string $token |
||
| 429 | * @param string $uniqueId |
||
| 430 | * @param SharingFrame $frame |
||
| 431 | * |
||
| 432 | * @return bool |
||
| 433 | * @throws Exception |
||
| 434 | */ |
||
| 435 | public function receiveFrame($token, $uniqueId, SharingFrame & $frame) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param integer $circleId |
||
| 463 | * @param string $uniqueId |
||
| 464 | * |
||
| 465 | * @return FederatedLink |
||
| 466 | */ |
||
| 467 | public function getLink($circleId, $uniqueId) { |
||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * @param integer $circleId |
||
| 474 | * |
||
| 475 | * @return FederatedLink[] |
||
| 476 | */ |
||
| 477 | public function getLinks($circleId) { |
||
| 480 | |||
| 481 | |||
| 482 | /** |
||
| 483 | * @param int $circleId |
||
| 484 | * @param string $uniqueId |
||
| 485 | * |
||
| 486 | * @return bool |
||
| 487 | * @throws Exception |
||
| 488 | */ |
||
| 489 | public function initiateRemoteShare($circleId, $uniqueId) { |
||
| 516 | |||
| 517 | |||
| 518 | /** |
||
| 519 | * @param SharingFrame $frame |
||
| 520 | * |
||
| 521 | * @throws Exception |
||
| 522 | */ |
||
| 523 | public function sendRemoteShare(SharingFrame $frame) { |
||
| 554 | |||
| 555 | |||
| 556 | /** |
||
| 557 | * generateHeaders() |
||
| 558 | * |
||
| 559 | * Generate new headers for the current Payload, and save them in the SharingFrame. |
||
| 560 | * |
||
| 561 | * @param SharingFrame $frame |
||
| 562 | */ |
||
| 563 | public function updateFrameWithCloudId(SharingFrame $frame) { |
||
| 567 | |||
| 568 | } |
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: