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 |
||
| 48 | class FederatedService { |
||
| 49 | |||
| 50 | |||
| 51 | /** @var string */ |
||
| 52 | private $userId; |
||
| 53 | |||
| 54 | /** @var IL10N */ |
||
| 55 | private $l10n; |
||
| 56 | |||
| 57 | /** @var CirclesRequest */ |
||
| 58 | private $circlesRequest; |
||
| 59 | |||
| 60 | /** @var ConfigService */ |
||
| 61 | private $configService; |
||
| 62 | |||
| 63 | /** @var CirclesService */ |
||
| 64 | private $circlesService; |
||
| 65 | |||
| 66 | /** @var BroadcastService */ |
||
| 67 | private $broadcastService; |
||
| 68 | |||
| 69 | /** @var FederatedLinksRequest */ |
||
| 70 | private $federatedLinksRequest; |
||
| 71 | |||
| 72 | /** @var string */ |
||
| 73 | private $serverHost; |
||
| 74 | |||
| 75 | /** @var ClientService */ |
||
| 76 | private $clientService; |
||
| 77 | |||
| 78 | /** @var MiscService */ |
||
| 79 | private $miscService; |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * CirclesService constructor. |
||
| 84 | * |
||
| 85 | * @param $userId |
||
| 86 | * @param IL10N $l10n |
||
| 87 | * @param CirclesRequest $circlesRequest |
||
| 88 | * @param ConfigService $configService |
||
| 89 | * @param CirclesService $circlesService |
||
| 90 | * @param BroadcastService $broadcastService |
||
| 91 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 92 | * @param string $serverHost |
||
| 93 | * @param ClientService $clientService |
||
| 94 | * @param MiscService $miscService |
||
| 95 | */ |
||
| 96 | public function __construct( |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * linkCircle() |
||
| 123 | * |
||
| 124 | * link to a circle. |
||
| 125 | * Function will check if settings allow Federated links between circles, and the format of |
||
| 126 | * the link ($remote). If no exception, a request to the remote circle will be initiated |
||
| 127 | * using requestLinkWithCircle() |
||
| 128 | * |
||
| 129 | * $remote format: <circle_name>@<remote_host> |
||
| 130 | * |
||
| 131 | * @param int $circleId |
||
| 132 | * @param string $remote |
||
| 133 | * |
||
| 134 | * @throws Exception |
||
| 135 | * @throws FederatedCircleLinkFormatException |
||
| 136 | * @throws CircleTypeNotValid |
||
| 137 | * @throws MemberIsNotAdminException |
||
| 138 | * |
||
| 139 | * @return FederatedLink |
||
| 140 | */ |
||
| 141 | public function linkCircle($circleId, $remote) { |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * requestLinkWithCircle() |
||
| 165 | * |
||
| 166 | * Using CircleId, function will get more infos from the database. |
||
| 167 | * Will check if author is not admin and initiate a FederatedLink, save it |
||
| 168 | * in the database and send a request to the remote circle using requestLink() |
||
| 169 | * If any issue, entry is removed from the database. |
||
| 170 | * |
||
| 171 | * @param integer $circleId |
||
| 172 | * @param string $remote |
||
| 173 | * |
||
| 174 | * @return FederatedLink |
||
| 175 | * @throws Exception |
||
| 176 | */ |
||
| 177 | private function requestLinkWithCircle($circleId, $remote) { |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * @param string $remote |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | View Code Duplication | private function generateLinkRemoteURL($remote) { |
|
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $remote |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | View Code Duplication | private function generatePayloadDeliveryURL($remote) { |
|
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * requestLink() |
||
| 240 | * |
||
| 241 | * |
||
| 242 | * @param Circle $circle |
||
| 243 | * @param FederatedLink $link |
||
| 244 | * |
||
| 245 | * @return boolean |
||
| 246 | * @throws Exception |
||
| 247 | */ |
||
| 248 | private function requestLink(Circle $circle, FederatedLink & $link) { |
||
| 283 | |||
| 284 | |||
| 285 | private function parsingRequestLinkResult($result) { |
||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * Create a new link into database and assign the correct status. |
||
| 317 | * |
||
| 318 | * @param Circle $circle |
||
| 319 | * @param FederatedLink $link |
||
| 320 | * |
||
| 321 | * @throws Exception |
||
| 322 | */ |
||
| 323 | public function initiateLink(Circle $circle, FederatedLink & $link) { |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * @param Circle $circle |
||
| 344 | * @param FederatedLink $link |
||
| 345 | * |
||
| 346 | * @throws LinkCreationException |
||
| 347 | */ |
||
| 348 | private function checkLinkRequestValidity($circle, $link) { |
||
| 362 | |||
| 363 | |||
| 364 | /** |
||
| 365 | * @param string $token |
||
| 366 | * @param string $uniqueId |
||
| 367 | * @param SharingFrame $frame |
||
| 368 | * |
||
| 369 | * @return bool |
||
| 370 | * @throws Exception |
||
| 371 | */ |
||
| 372 | public function receiveFrame($token, $uniqueId, SharingFrame & $frame) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param integer $circleId |
||
| 400 | * @param string $uniqueId |
||
| 401 | * |
||
| 402 | * @return FederatedLink |
||
| 403 | */ |
||
| 404 | public function getLink($circleId, $uniqueId) { |
||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * @param integer $circleId |
||
| 411 | * |
||
| 412 | * @return FederatedLink[] |
||
| 413 | */ |
||
| 414 | public function getLinks($circleId) { |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * @param int $circleId |
||
| 421 | * @param string $uniqueId |
||
| 422 | * |
||
| 423 | * @return bool |
||
| 424 | * @throws Exception |
||
| 425 | */ |
||
| 426 | public function initiateRemoteShare($circleId, $uniqueId) { |
||
| 452 | |||
| 453 | |||
| 454 | /** |
||
| 455 | * @param SharingFrame $frame |
||
| 456 | * |
||
| 457 | * @throws Exception |
||
| 458 | */ |
||
| 459 | public function sendRemoteShare(SharingFrame $frame) { |
||
| 489 | |||
| 490 | |||
| 491 | /** |
||
| 492 | * generateHeaders() |
||
| 493 | * |
||
| 494 | * Generate new headers for the current Payload, and save them in the SharingFrame. |
||
| 495 | * |
||
| 496 | * @param SharingFrame $frame |
||
| 497 | */ |
||
| 498 | public function updateFrameWithCloudId(SharingFrame $frame) { |
||
| 502 | |||
| 503 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.