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 FederatedLinkService 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 FederatedLinkService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class FederatedLinkService { |
||
| 53 | |||
| 54 | /** @var string */ |
||
| 55 | private $userId; |
||
| 56 | |||
| 57 | /** @var IL10N */ |
||
| 58 | private $l10n; |
||
| 59 | |||
| 60 | /** @var CirclesRequest */ |
||
| 61 | private $circlesRequest; |
||
| 62 | |||
| 63 | /** @var ConfigService */ |
||
| 64 | private $configService; |
||
| 65 | |||
| 66 | /** @var CirclesService */ |
||
| 67 | private $circlesService; |
||
| 68 | |||
| 69 | /** @var BroadcastService */ |
||
| 70 | private $broadcastService; |
||
| 71 | |||
| 72 | /** @var FederatedLinksRequest */ |
||
| 73 | private $federatedLinksRequest; |
||
| 74 | |||
| 75 | /** @var EventsService */ |
||
| 76 | private $eventsService; |
||
| 77 | |||
| 78 | /** @var IClientService */ |
||
| 79 | private $clientService; |
||
| 80 | |||
| 81 | /** @var MiscService */ |
||
| 82 | private $miscService; |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * FederatedLinkService constructor. |
||
| 87 | * |
||
| 88 | * @param string $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 EventsService $eventsService |
||
| 96 | * @param IClientService $clientService |
||
| 97 | * @param MiscService $miscService |
||
| 98 | */ |
||
| 99 | public function __construct( |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * linkCircle(); |
||
| 121 | * |
||
| 122 | * link to a circle. |
||
| 123 | * Function will check if settings allow Federated links between circles, and the format of |
||
| 124 | * the link ($remote). If no exception, a request to the remote circle will be initiated |
||
| 125 | * using requestLinkWithCircle() |
||
| 126 | * |
||
| 127 | * $remote format: <circle_name>@<remote_host> |
||
| 128 | * |
||
| 129 | * @param string $circleUniqueId |
||
| 130 | * @param string $remote |
||
| 131 | * |
||
| 132 | * @throws Exception |
||
| 133 | * @throws FederatedCircleLinkFormatException |
||
| 134 | * @throws CircleTypeNotValidException |
||
| 135 | * |
||
| 136 | * @return FederatedLink |
||
| 137 | */ |
||
| 138 | public function linkCircle($circleUniqueId, $remote) { |
||
| 158 | |||
| 159 | |||
| 160 | /** |
||
| 161 | * linkStatus() |
||
| 162 | * |
||
| 163 | * Update the status of a link. |
||
| 164 | * Function will check if user can edit the status, will update it and send the update to |
||
| 165 | * remote |
||
| 166 | * |
||
| 167 | * @param string $linkUniqueId |
||
| 168 | * @param int $status |
||
| 169 | * |
||
| 170 | * @throws Exception |
||
| 171 | * @throws FederatedCircleLinkFormatException |
||
| 172 | * @throws CircleTypeNotValidException |
||
| 173 | * @throws MemberIsNotAdminException |
||
| 174 | * |
||
| 175 | * @return FederatedLink[] |
||
| 176 | */ |
||
| 177 | public function linkStatus($linkUniqueId, $status) { |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * @param FederatedLink $link |
||
| 201 | * @param Circle $circle |
||
| 202 | * @param int $status |
||
| 203 | * |
||
| 204 | * @return FederatedLink[] |
||
| 205 | * @throws Exception |
||
| 206 | */ |
||
| 207 | private function updateLinkStatus(FederatedLink $link, Circle $circle, $status) { |
||
| 226 | |||
| 227 | |||
| 228 | /** |
||
| 229 | * eventOnLinkStatus(); |
||
| 230 | * |
||
| 231 | * Called by linkStatus() to manage events when status is changing. |
||
| 232 | * If status does not need update, returns false; |
||
| 233 | * |
||
| 234 | * @param FederatedLink $link |
||
| 235 | * @param Circle $circle |
||
| 236 | * @param int $status |
||
| 237 | * |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | private function eventOnUpdateLinkStatus(FederatedLink $link, Circle $circle, $status) { |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * requestLinkWithCircle() |
||
| 257 | * |
||
| 258 | * Using CircleId, function will get more infos from the database. |
||
| 259 | * Will check if author is at least admin and initiate a FederatedLink, save it |
||
| 260 | * in the database and send a request to the remote circle using requestLink() |
||
| 261 | * If any issue, entry is removed from the database. |
||
| 262 | * |
||
| 263 | * @param string $circleUniqueId |
||
| 264 | * @param string $remote |
||
| 265 | * |
||
| 266 | * @return FederatedLink |
||
| 267 | * @throws Exception |
||
| 268 | */ |
||
| 269 | private function requestLinkWithCircle($circleUniqueId, $remote) { |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * @param $circleUniqueId |
||
| 292 | * @param $remote |
||
| 293 | * |
||
| 294 | * @return FederatedLink |
||
| 295 | */ |
||
| 296 | private function generateNewLinkWithRemoteCircle($circleUniqueId, $remote) { |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $remote |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | private function generateLinkRemoteURL($remote) { |
||
| 322 | |||
| 323 | |||
| 324 | /** |
||
| 325 | * requestLink() |
||
| 326 | * |
||
| 327 | * |
||
| 328 | * @param Circle $circle |
||
| 329 | * @param FederatedLink $link |
||
| 330 | * |
||
| 331 | * @return boolean |
||
| 332 | * @throws Exception |
||
| 333 | */ |
||
| 334 | private function requestLink(Circle $circle, FederatedLink &$link) { |
||
| 354 | |||
| 355 | |||
| 356 | private function parseRequestLinkResult(IResponse $response) { |
||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * eventOnRequestLink(); |
||
| 370 | * |
||
| 371 | * Called by requestLink() will update status and event |
||
| 372 | * Will also manage errors returned by the remote link |
||
| 373 | * |
||
| 374 | * @param Circle $circle |
||
| 375 | * @param FederatedLink $link |
||
| 376 | * @param int $status |
||
| 377 | * @param string $reason |
||
| 378 | * |
||
| 379 | * @throws Exception |
||
| 380 | */ |
||
| 381 | private function eventOnRequestLink(Circle $circle, FederatedLink &$link, $status, $reason) { |
||
| 398 | |||
| 399 | |||
| 400 | /** |
||
| 401 | * parseRequestLinkError(); |
||
| 402 | * |
||
| 403 | * Will parse the error reason returned by requestLink() and throw an Exception |
||
| 404 | * |
||
| 405 | * @param $reason |
||
| 406 | * |
||
| 407 | * @throws Exception |
||
| 408 | * @throws FederatedRemoteCircleDoesNotExistException |
||
| 409 | * @throws FederatedRemoteDoesNotAllowException |
||
| 410 | */ |
||
| 411 | private function parseRequestLinkError($reason) { |
||
| 428 | |||
| 429 | |||
| 430 | /** |
||
| 431 | * @param string $token |
||
| 432 | * @param string $uniqueId |
||
| 433 | * @param int $status |
||
| 434 | * |
||
| 435 | * @return FederatedLink |
||
| 436 | * @throws Exception |
||
| 437 | */ |
||
| 438 | public function updateLinkFromRemote($token, $uniqueId, $status) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * checkUpdateLinkFromRemote(); |
||
| 460 | * |
||
| 461 | * will throw exception is the status sent by remote is not correct |
||
| 462 | * |
||
| 463 | * @param int $status |
||
| 464 | * |
||
| 465 | * @throws FederatedCircleStatusUpdateException |
||
| 466 | */ |
||
| 467 | private function checkUpdateLinkFromRemote($status) { |
||
| 477 | |||
| 478 | |||
| 479 | /** |
||
| 480 | * checkUpdateLinkFromRemoteLinkUp() |
||
| 481 | * |
||
| 482 | * in case of a request of status update from remote for a link up, we check the current |
||
| 483 | * status of the link locally. |
||
| 484 | * |
||
| 485 | * @param Circle $circle |
||
| 486 | * @param FederatedLink $link |
||
| 487 | * @param int $status |
||
| 488 | * |
||
| 489 | * @throws FederatedCircleStatusUpdateException |
||
| 490 | */ |
||
| 491 | private function checkUpdateLinkFromRemoteLinkUp(Circle $circle, FederatedLink $link, $status) { |
||
| 506 | |||
| 507 | |||
| 508 | /** |
||
| 509 | * updateLinkRemote() |
||
| 510 | * |
||
| 511 | * Send a request to the remote of the link to update its status. |
||
| 512 | * |
||
| 513 | * @param FederatedLink $link |
||
| 514 | * |
||
| 515 | * @return bool |
||
| 516 | * @throws Exception |
||
| 517 | */ |
||
| 518 | private function updateLinkRemote(FederatedLink &$link) { |
||
| 535 | |||
| 536 | |||
| 537 | /** |
||
| 538 | * Create a new link into database and assign the correct status. |
||
| 539 | * |
||
| 540 | * @param Circle $circle |
||
| 541 | * @param FederatedLink $link |
||
| 542 | * |
||
| 543 | * @throws Exception |
||
| 544 | */ |
||
| 545 | public function initiateLink(Circle $circle, FederatedLink &$link) { |
||
| 564 | |||
| 565 | |||
| 566 | /** |
||
| 567 | * @param Circle $circle |
||
| 568 | * @param FederatedLink $link |
||
| 569 | * |
||
| 570 | * @throws FederatedLinkCreationException |
||
| 571 | */ |
||
| 572 | private function checkLinkRequestValidity($circle, $link) { |
||
| 589 | |||
| 590 | |||
| 591 | /** |
||
| 592 | * checkUpdateLinkFromRemoteLinkRemove(); |
||
| 593 | * |
||
| 594 | * in case of a request of status update from remote for a link down, we check the current |
||
| 595 | * status of the link locally |
||
| 596 | * |
||
| 597 | * @param Circle $circle |
||
| 598 | * @param FederatedLink $link |
||
| 599 | * @param int $status |
||
| 600 | * |
||
| 601 | * @throws FederatedCircleStatusUpdateException |
||
| 602 | */ |
||
| 603 | private function checkUpdateLinkFromRemoteLinkRemove(Circle $circle, FederatedLink $link, $status) { |
||
| 622 | |||
| 623 | |||
| 624 | /** |
||
| 625 | * @param Circle $circle |
||
| 626 | * @param FederatedLink $link |
||
| 627 | */ |
||
| 628 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkRequestSent(Circle $circle, FederatedLink &$link) { |
|
| 637 | |||
| 638 | |||
| 639 | /** |
||
| 640 | * @param Circle $circle |
||
| 641 | * @param FederatedLink $link |
||
| 642 | */ |
||
| 643 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkRequested(Circle $circle, FederatedLink &$link) { |
|
| 652 | |||
| 653 | |||
| 654 | /** |
||
| 655 | * @param Circle $circle |
||
| 656 | * @param FederatedLink $link |
||
| 657 | */ |
||
| 658 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkDown(Circle $circle, FederatedLink &$link) { |
|
| 667 | |||
| 668 | |||
| 669 | /** |
||
| 670 | * @param FederatedLink $link |
||
| 671 | * |
||
| 672 | * @return array |
||
| 673 | */ |
||
| 674 | private static function generateLinkData(FederatedLink $link) { |
||
| 683 | |||
| 684 | |||
| 685 | /** |
||
| 686 | * @param array $args |
||
| 687 | * |
||
| 688 | * @return array |
||
| 689 | */ |
||
| 690 | private static function generateClientBodyData($args) { |
||
| 697 | |||
| 698 | |||
| 699 | } |
This check marks parameter names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.