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 |
||
| 49 | class FederatedLinkService { |
||
| 50 | |||
| 51 | const REMOTE_URL_LINK = '/index.php/apps/circles/v1/link'; |
||
| 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 | View Code Duplication | public function __construct( |
|
| 116 | |||
| 117 | // |
||
| 118 | |||
| 119 | /** |
||
| 120 | * linkStatus() |
||
| 121 | * |
||
| 122 | * Update the status of a link. |
||
| 123 | * Function will check if user can edit the status, will update it and send the update to |
||
| 124 | * remote |
||
| 125 | * |
||
| 126 | * @param string $linkUniqueId |
||
| 127 | * @param int $status |
||
| 128 | * |
||
| 129 | * @throws Exception |
||
| 130 | * @throws FederatedCircleLinkFormatException |
||
| 131 | * @throws CircleTypeNotValidException |
||
| 132 | * @throws MemberIsNotAdminException |
||
| 133 | * |
||
| 134 | * @return FederatedLink[] |
||
| 135 | */ |
||
| 136 | public function linkStatus($linkUniqueId, $status) { |
||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * @param FederatedLink $link |
||
| 160 | * @param Circle $circle |
||
| 161 | * @param int $status |
||
| 162 | * |
||
| 163 | * @return FederatedLink[] |
||
| 164 | * @throws Exception |
||
| 165 | */ |
||
| 166 | private function updateLinkStatus(FederatedLink $link, Circle $circle, $status) { |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * eventOnLinkStatus(); |
||
| 189 | * |
||
| 190 | * Called by linkStatus() to manage events when status is changing. |
||
| 191 | * If status does not need update, returns false; |
||
| 192 | * |
||
| 193 | * @param FederatedLink $link |
||
| 194 | * @param Circle $circle |
||
| 195 | * @param int $status |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | private function eventOnUpdateLinkStatus(FederatedLink $link, Circle $circle, $status) { |
||
| 212 | |||
| 213 | |||
| 214 | /** |
||
| 215 | * @param string $remote |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function generateLinkRemoteURL($remote) { |
||
| 222 | |||
| 223 | |||
| 224 | public function parseClientRequestResult(IResponse $response) { |
||
| 234 | |||
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $token |
||
| 238 | * @param string $uniqueId |
||
| 239 | * @param int $status |
||
| 240 | * |
||
| 241 | * @return FederatedLink |
||
| 242 | * @throws Exception |
||
| 243 | */ |
||
| 244 | public function updateLinkFromRemote($token, $uniqueId, $status) { |
||
| 263 | |||
| 264 | |||
| 265 | /** |
||
| 266 | * checkUpdateLinkFromRemote(); |
||
| 267 | * |
||
| 268 | * will throw exception is the status sent by remote is not correct |
||
| 269 | * |
||
| 270 | * @param int $status |
||
| 271 | * |
||
| 272 | * @throws FederatedCircleStatusUpdateException |
||
| 273 | */ |
||
| 274 | private function checkUpdateLinkFromRemote($status) { |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * checkUpdateLinkFromRemoteLinkUp() |
||
| 288 | * |
||
| 289 | * in case of a request of status update from remote for a link up, we check the current |
||
| 290 | * status of the link locally. |
||
| 291 | * |
||
| 292 | * @param Circle $circle |
||
| 293 | * @param FederatedLink $link |
||
| 294 | * @param int $status |
||
| 295 | * |
||
| 296 | * @throws FederatedCircleStatusUpdateException |
||
| 297 | */ |
||
| 298 | private function checkUpdateLinkFromRemoteLinkUp(Circle $circle, FederatedLink $link, $status) { |
||
| 313 | |||
| 314 | |||
| 315 | /** |
||
| 316 | * updateLinkRemote() |
||
| 317 | * |
||
| 318 | * Send a request to the remote of the link to update its status. |
||
| 319 | * |
||
| 320 | * @param FederatedLink $link |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | * @throws Exception |
||
| 324 | */ |
||
| 325 | private function updateLinkRemote(FederatedLink &$link) { |
||
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * Create a new link into database and assign the correct status. |
||
| 346 | * |
||
| 347 | * @param Circle $circle |
||
| 348 | * @param FederatedLink $link |
||
| 349 | * |
||
| 350 | * @throws Exception |
||
| 351 | */ |
||
| 352 | public function initiateLink(Circle $circle, FederatedLink &$link) { |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * @param Circle $circle |
||
| 375 | * @param FederatedLink $link |
||
| 376 | * |
||
| 377 | * @throws FederatedLinkCreationException |
||
| 378 | */ |
||
| 379 | private function checkLinkRequestValidity($circle, $link) { |
||
| 396 | |||
| 397 | |||
| 398 | /** |
||
| 399 | * checkUpdateLinkFromRemoteLinkRemove(); |
||
| 400 | * |
||
| 401 | * in case of a request of status update from remote for a link down, we check the current |
||
| 402 | * status of the link locally |
||
| 403 | * |
||
| 404 | * @param Circle $circle |
||
| 405 | * @param FederatedLink $link |
||
| 406 | * @param int $status |
||
| 407 | * |
||
| 408 | * @throws FederatedCircleStatusUpdateException |
||
| 409 | */ |
||
| 410 | private function checkUpdateLinkFromRemoteLinkRemove(Circle $circle, FederatedLink $link, $status) { |
||
| 429 | |||
| 430 | |||
| 431 | /** |
||
| 432 | * @param Circle $circle |
||
| 433 | * @param FederatedLink $link |
||
| 434 | */ |
||
| 435 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkRequestSent(Circle $circle, FederatedLink &$link) { |
|
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * @param Circle $circle |
||
| 448 | * @param FederatedLink $link |
||
| 449 | */ |
||
| 450 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkRequested(Circle $circle, FederatedLink &$link) { |
|
| 459 | |||
| 460 | |||
| 461 | /** |
||
| 462 | * @param Circle $circle |
||
| 463 | * @param FederatedLink $link |
||
| 464 | */ |
||
| 465 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkDown(Circle $circle, FederatedLink &$link) { |
|
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * @param FederatedLink $link |
||
| 478 | * @param array $options |
||
| 479 | * |
||
| 480 | * @return array |
||
| 481 | */ |
||
| 482 | public static function generateClientBodyData(FederatedLink $link, $options = []) { |
||
| 500 | |||
| 501 | |||
| 502 | } |
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.