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) { |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * eventOnLinkStatus(); |
||
| 217 | * |
||
| 218 | * Called by linkStatus() to manage events when status is changing. |
||
| 219 | * If status does not need update, returns false; |
||
| 220 | * |
||
| 221 | * @param Circle $circle |
||
| 222 | * @param FederatedLink $link |
||
| 223 | * @param $status |
||
| 224 | * |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | private function eventOnLinkStatus(Circle $circle, FederatedLink $link, $status) { |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * requestLinkWithCircle() |
||
| 247 | * |
||
| 248 | * Using CircleId, function will get more infos from the database. |
||
| 249 | * Will check if author is at least admin and initiate a FederatedLink, save it |
||
| 250 | * in the database and send a request to the remote circle using requestLink() |
||
| 251 | * If any issue, entry is removed from the database. |
||
| 252 | * |
||
| 253 | * @param string $circleUniqueId |
||
| 254 | * @param string $remote |
||
| 255 | * |
||
| 256 | * @return FederatedLink |
||
| 257 | * @throws Exception |
||
| 258 | */ |
||
| 259 | private function requestLinkWithCircle($circleUniqueId, $remote) { |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * @param $circleUniqueId |
||
| 282 | * @param $remote |
||
| 283 | * |
||
| 284 | * @return FederatedLink |
||
| 285 | */ |
||
| 286 | private function generateNewLinkWithRemoteCircle($circleUniqueId, $remote) { |
||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $remote |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | private function generateLinkRemoteURL($remote) { |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * requestLink() |
||
| 316 | * |
||
| 317 | * |
||
| 318 | * @param Circle $circle |
||
| 319 | * @param FederatedLink $link |
||
| 320 | * |
||
| 321 | * @return boolean |
||
| 322 | * @throws Exception |
||
| 323 | */ |
||
| 324 | private function requestLink(Circle $circle, FederatedLink &$link) { |
||
| 344 | |||
| 345 | |||
| 346 | private function parseRequestLinkResult(IResponse $response) { |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * eventOnRequestLink(); |
||
| 360 | * |
||
| 361 | * Called by requestLink() will update status and event |
||
| 362 | * Will also manage errors returned by the remote link |
||
| 363 | * |
||
| 364 | * @param Circle $circle |
||
| 365 | * @param FederatedLink $link |
||
| 366 | * @param int $status |
||
| 367 | * @param string $reason |
||
| 368 | * |
||
| 369 | * @throws Exception |
||
| 370 | */ |
||
| 371 | private function eventOnRequestLink(Circle $circle, FederatedLink &$link, $status, $reason) { |
||
| 388 | |||
| 389 | |||
| 390 | /** |
||
| 391 | * parseRequestLinkError(); |
||
| 392 | * |
||
| 393 | * Will parse the error reason returned by requestLink() and throw an Exception |
||
| 394 | * |
||
| 395 | * @param $reason |
||
| 396 | * |
||
| 397 | * @throws Exception |
||
| 398 | * @throws FederatedRemoteCircleDoesNotExistException |
||
| 399 | * @throws FederatedRemoteDoesNotAllowException |
||
| 400 | */ |
||
| 401 | private function parseRequestLinkError($reason) { |
||
| 418 | |||
| 419 | |||
| 420 | /** |
||
| 421 | * @param string $token |
||
| 422 | * @param string $uniqueId |
||
| 423 | * @param int $status |
||
| 424 | * |
||
| 425 | * @return FederatedLink |
||
| 426 | * @throws Exception |
||
| 427 | */ |
||
| 428 | public function updateLinkFromRemote($token, $uniqueId, $status) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * checkUpdateLinkFromRemote(); |
||
| 450 | * |
||
| 451 | * will throw exception is the status sent by remote is not correct |
||
| 452 | * |
||
| 453 | * @param int $status |
||
| 454 | * |
||
| 455 | * @throws FederatedCircleStatusUpdateException |
||
| 456 | */ |
||
| 457 | private function checkUpdateLinkFromRemote($status) { |
||
| 467 | |||
| 468 | |||
| 469 | /** |
||
| 470 | * checkUpdateLinkFromRemoteLinkUp() |
||
| 471 | * |
||
| 472 | * in case of a request of status update from remote for a link up, we check the current |
||
| 473 | * status of the link locally. |
||
| 474 | * |
||
| 475 | * @param Circle $circle |
||
| 476 | * @param FederatedLink $link |
||
| 477 | * @param int $status |
||
| 478 | * |
||
| 479 | * @throws FederatedCircleStatusUpdateException |
||
| 480 | */ |
||
| 481 | private function checkUpdateLinkFromRemoteLinkUp(Circle $circle, FederatedLink $link, $status) { |
||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * updateLinkRemote() |
||
| 500 | * |
||
| 501 | * Send a request to the remote of the link to update its status. |
||
| 502 | * |
||
| 503 | * @param FederatedLink $link |
||
| 504 | * |
||
| 505 | * @return bool |
||
| 506 | * @throws Exception |
||
| 507 | */ |
||
| 508 | public function updateLinkRemote(FederatedLink &$link) { |
||
| 525 | |||
| 526 | |||
| 527 | /** |
||
| 528 | * Create a new link into database and assign the correct status. |
||
| 529 | * |
||
| 530 | * @param Circle $circle |
||
| 531 | * @param FederatedLink $link |
||
| 532 | * |
||
| 533 | * @throws Exception |
||
| 534 | */ |
||
| 535 | public function initiateLink(Circle $circle, FederatedLink &$link) { |
||
| 554 | |||
| 555 | |||
| 556 | /** |
||
| 557 | * @param Circle $circle |
||
| 558 | * @param FederatedLink $link |
||
| 559 | * |
||
| 560 | * @throws FederatedLinkCreationException |
||
| 561 | */ |
||
| 562 | private function checkLinkRequestValidity($circle, $link) { |
||
| 579 | |||
| 580 | |||
| 581 | /** |
||
| 582 | * checkUpdateLinkFromRemoteLinkRemove(); |
||
| 583 | * |
||
| 584 | * in case of a request of status update from remote for a link down, we check the current |
||
| 585 | * status of the link locally |
||
| 586 | * |
||
| 587 | * @param Circle $circle |
||
| 588 | * @param FederatedLink $link |
||
| 589 | * @param int $status |
||
| 590 | * |
||
| 591 | * @throws FederatedCircleStatusUpdateException |
||
| 592 | */ |
||
| 593 | private function checkUpdateLinkFromRemoteLinkRemove(Circle $circle, FederatedLink $link, $status) { |
||
| 612 | |||
| 613 | |||
| 614 | /** |
||
| 615 | * @param Circle $circle |
||
| 616 | * @param FederatedLink $link |
||
| 617 | */ |
||
| 618 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkRequestSent(Circle $circle, FederatedLink &$link) { |
|
| 627 | |||
| 628 | |||
| 629 | /** |
||
| 630 | * @param Circle $circle |
||
| 631 | * @param FederatedLink $link |
||
| 632 | */ |
||
| 633 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkRequested(Circle $circle, FederatedLink &$link) { |
|
| 642 | |||
| 643 | |||
| 644 | /** |
||
| 645 | * @param Circle $circle |
||
| 646 | * @param FederatedLink $link |
||
| 647 | */ |
||
| 648 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkDown(Circle $circle, FederatedLink &$link) { |
|
| 657 | |||
| 658 | |||
| 659 | /** |
||
| 660 | * @param FederatedLink $link |
||
| 661 | * |
||
| 662 | * @return array |
||
| 663 | */ |
||
| 664 | private static function generateLinkData(FederatedLink $link) { |
||
| 673 | |||
| 674 | |||
| 675 | /** |
||
| 676 | * @param array $args |
||
| 677 | * |
||
| 678 | * @return array |
||
| 679 | */ |
||
| 680 | private static function generateClientBodyData($args) { |
||
| 687 | |||
| 688 | |||
| 689 | } |
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.