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:
| 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 | * checkUpdateLinkFromRemoteLinkRemove(); |
||
| 346 | * |
||
| 347 | * in case of a request of status update from remote for a link down, we check the current |
||
| 348 | * status of the link locally |
||
| 349 | * |
||
| 350 | * @param Circle $circle |
||
| 351 | * @param FederatedLink $link |
||
| 352 | * @param int $status |
||
| 353 | * |
||
| 354 | * @throws FederatedCircleStatusUpdateException |
||
| 355 | */ |
||
| 356 | private function checkUpdateLinkFromRemoteLinkRemove(Circle $circle, FederatedLink $link, $status) { |
||
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * @param Circle $circle |
||
| 379 | * @param FederatedLink $link |
||
| 380 | */ |
||
| 381 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkRequestSent(Circle $circle, FederatedLink &$link) { |
|
| 390 | |||
| 391 | |||
| 392 | /** |
||
| 393 | * @param Circle $circle |
||
| 394 | * @param FederatedLink $link |
||
| 395 | */ |
||
| 396 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkRequested(Circle $circle, FederatedLink &$link) { |
|
| 405 | |||
| 406 | |||
| 407 | /** |
||
| 408 | * @param Circle $circle |
||
| 409 | * @param FederatedLink $link |
||
| 410 | */ |
||
| 411 | View Code Duplication | private function checkUpdateLinkFromRemoteLinkDown(Circle $circle, FederatedLink &$link) { |
|
| 420 | |||
| 421 | |||
| 422 | /** |
||
| 423 | * @param FederatedLink $link |
||
| 424 | * @param array $options |
||
| 425 | * |
||
| 426 | * @return array |
||
| 427 | */ |
||
| 428 | public static function generateClientBodyData(FederatedLink $link, $options = []) { |
||
| 446 | |||
| 447 | |||
| 448 | } |
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.