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 |
||
| 51 | class FederatedLinkService { |
||
| 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 EventsService */ |
||
| 75 | private $eventsService; |
||
| 76 | |||
| 77 | /** @var IClientService */ |
||
| 78 | private $clientService; |
||
| 79 | |||
| 80 | /** @var MiscService */ |
||
| 81 | private $miscService; |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * FederatedLinkService constructor. |
||
| 86 | * |
||
| 87 | * @param string $UserId |
||
| 88 | * @param IL10N $l10n |
||
| 89 | * @param CirclesRequest $circlesRequest |
||
| 90 | * @param ConfigService $configService |
||
| 91 | * @param CirclesService $circlesService |
||
| 92 | * @param BroadcastService $broadcastService |
||
| 93 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 94 | * @param EventsService $eventsService |
||
| 95 | * @param IClientService $clientService |
||
| 96 | * @param MiscService $miscService |
||
| 97 | */ |
||
| 98 | public function __construct( |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * linkCircle(); |
||
| 120 | * |
||
| 121 | * link to a circle. |
||
| 122 | * Function will check if settings allow Federated links between circles, and the format of |
||
| 123 | * the link ($remote). If no exception, a request to the remote circle will be initiated |
||
| 124 | * using requestLinkWithCircle() |
||
| 125 | * |
||
| 126 | * $remote format: <circle_name>@<remote_host> |
||
| 127 | * |
||
| 128 | * @param string $circleUniqueId |
||
| 129 | * @param string $remote |
||
| 130 | * |
||
| 131 | * @throws Exception |
||
| 132 | * @throws FederatedCircleLinkFormatException |
||
| 133 | * @throws CircleTypeNotValidException |
||
| 134 | * |
||
| 135 | * @return FederatedLink |
||
| 136 | */ |
||
| 137 | public function linkCircle($circleUniqueId, $remote) { |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * linkStatus() |
||
| 161 | * |
||
| 162 | * Update the status of a link. |
||
| 163 | * Function will check if user can edit the status, will update it and send the update to |
||
| 164 | * remote |
||
| 165 | * |
||
| 166 | * @param string $linkUniqueId |
||
| 167 | * @param int $status |
||
| 168 | * |
||
| 169 | * @throws Exception |
||
| 170 | * @throws FederatedCircleLinkFormatException |
||
| 171 | * @throws CircleTypeNotValidException |
||
| 172 | * @throws MemberIsNotAdminException |
||
| 173 | * |
||
| 174 | * @return FederatedLink[] |
||
| 175 | */ |
||
| 176 | public function linkStatus($linkUniqueId, $status) { |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * eventOnLinkStatus(); |
||
| 216 | * |
||
| 217 | * Called by linkStatus() to manage events when status is changing. |
||
| 218 | * If status does not need update, returns false; |
||
| 219 | * |
||
| 220 | * @param Circle $circle |
||
| 221 | * @param FederatedLink $link |
||
| 222 | * @param $status |
||
| 223 | * |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | private function eventOnLinkStatus(Circle $circle, FederatedLink $link, $status) { |
||
| 242 | |||
| 243 | |||
| 244 | /** |
||
| 245 | * requestLinkWithCircle() |
||
| 246 | * |
||
| 247 | * Using CircleId, function will get more infos from the database. |
||
| 248 | * Will check if author is at least admin and initiate a FederatedLink, save it |
||
| 249 | * in the database and send a request to the remote circle using requestLink() |
||
| 250 | * If any issue, entry is removed from the database. |
||
| 251 | * |
||
| 252 | * @param string $circleUniqueId |
||
| 253 | * @param string $remote |
||
| 254 | * |
||
| 255 | * @return FederatedLink |
||
| 256 | * @throws Exception |
||
| 257 | */ |
||
| 258 | private function requestLinkWithCircle($circleUniqueId, $remote) { |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * @param $circleUniqueId |
||
| 281 | * @param $remote |
||
| 282 | * |
||
| 283 | * @return FederatedLink |
||
| 284 | */ |
||
| 285 | private function generateNewLinkWithRemoteCircle($circleUniqueId, $remote) { |
||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $remote |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | private function generateLinkRemoteURL($remote) { |
||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * requestLink() |
||
| 315 | * |
||
| 316 | * |
||
| 317 | * @param Circle $circle |
||
| 318 | * @param FederatedLink $link |
||
| 319 | * |
||
| 320 | * @return boolean |
||
| 321 | * @throws Exception |
||
| 322 | */ |
||
| 323 | private function requestLink(Circle $circle, FederatedLink &$link) { |
||
| 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 $status |
||
| 377 | * @param $reason |
||
| 378 | * |
||
| 379 | * @throws Exception |
||
| 380 | */ |
||
| 381 | private function eventOnRequestLink(Circle $circle, FederatedLink $link, $status, $reason) { |
||
| 397 | |||
| 398 | |||
| 399 | /** |
||
| 400 | * parseRequestLinkError(); |
||
| 401 | * |
||
| 402 | * Will parse the error reason returned by requestLink() and throw an Exception |
||
| 403 | * |
||
| 404 | * @param $reason |
||
| 405 | * |
||
| 406 | * @throws Exception |
||
| 407 | * @throws FederatedRemoteCircleDoesNotExistException |
||
| 408 | * @throws FederatedRemoteDoesNotAllowException |
||
| 409 | */ |
||
| 410 | private function parseRequestLinkError($reason) { |
||
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * @param string $token |
||
| 431 | * @param string $uniqueId |
||
| 432 | * @param int $status |
||
| 433 | * |
||
| 434 | * @return FederatedLink |
||
| 435 | * @throws Exception |
||
| 436 | */ |
||
| 437 | public function updateLinkFromRemote($token, $uniqueId, $status) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * checkUpdateLinkFromRemote(); |
||
| 459 | * |
||
| 460 | * will throw exception is the status sent by remote is not correct |
||
| 461 | * |
||
| 462 | * @param int $status |
||
| 463 | * |
||
| 464 | * @throws FederatedCircleStatusUpdateException |
||
| 465 | */ |
||
| 466 | private function checkUpdateLinkFromRemote($status) { |
||
| 476 | |||
| 477 | |||
| 478 | /** |
||
| 479 | * checkUpdateLinkFromRemoteLinkUp() |
||
| 480 | * |
||
| 481 | * in case of a request of status update from remote for a link up, we check the current |
||
| 482 | * status of the link locally. |
||
| 483 | * |
||
| 484 | * @param Circle $circle |
||
| 485 | * @param FederatedLink $link |
||
| 486 | * @param int $status |
||
| 487 | * |
||
| 488 | * @throws FederatedCircleStatusUpdateException |
||
| 489 | */ |
||
| 490 | private function checkUpdateLinkFromRemoteLinkUp(Circle $circle, FederatedLink $link, $status) { |
||
| 505 | |||
| 506 | |||
| 507 | /** |
||
| 508 | * checkUpdateLinkFromRemoteLinkRemove(); |
||
| 509 | * |
||
| 510 | * in case of a request of status update from remote for a link down, we check the current |
||
| 511 | * status of the link locally |
||
| 512 | * |
||
| 513 | * @param Circle $circle |
||
| 514 | * @param FederatedLink $link |
||
| 515 | * @param int $status |
||
| 516 | * |
||
| 517 | * @throws FederatedCircleStatusUpdateException |
||
| 518 | */ |
||
| 519 | private function checkUpdateLinkFromRemoteLinkRemove(Circle $circle, FederatedLink $link, $status) { |
||
| 538 | |||
| 539 | |||
| 540 | /** |
||
| 541 | * @param Circle $circle |
||
| 542 | * @param FederatedLink $link |
||
| 543 | */ |
||
| 544 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkRequestSent(Circle $circle, FederatedLink &$link) { |
|
| 553 | |||
| 554 | |||
| 555 | /** |
||
| 556 | * @param Circle $circle |
||
| 557 | * @param FederatedLink $link |
||
| 558 | */ |
||
| 559 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkRequested(Circle $circle, FederatedLink &$link) { |
|
| 568 | |||
| 569 | |||
| 570 | /** |
||
| 571 | * @param Circle $circle |
||
| 572 | * @param FederatedLink $link |
||
| 573 | */ |
||
| 574 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkDown(Circle $circle, FederatedLink &$link) { |
|
| 583 | |||
| 584 | |||
| 585 | /** |
||
| 586 | * updateLinkRemote() |
||
| 587 | * |
||
| 588 | * Send a request to the remote of the link to update its status. |
||
| 589 | * |
||
| 590 | * @param FederatedLink $link |
||
| 591 | * |
||
| 592 | * @return bool |
||
| 593 | * @throws Exception |
||
| 594 | */ |
||
| 595 | public function updateLinkRemote(FederatedLink &$link) { |
||
| 623 | |||
| 624 | |||
| 625 | /** |
||
| 626 | * Create a new link into database and assign the correct status. |
||
| 627 | * |
||
| 628 | * @param Circle $circle |
||
| 629 | * @param FederatedLink $link |
||
| 630 | * |
||
| 631 | * @throws Exception |
||
| 632 | */ |
||
| 633 | public function initiateLink(Circle $circle, FederatedLink &$link) { |
||
| 652 | |||
| 653 | |||
| 654 | /** |
||
| 655 | * @param Circle $circle |
||
| 656 | * @param FederatedLink $link |
||
| 657 | * |
||
| 658 | * @throws FederatedLinkCreationException |
||
| 659 | */ |
||
| 660 | private function checkLinkRequestValidity($circle, $link) { |
||
| 677 | |||
| 678 | |||
| 679 | } |
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.