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 |
||
| 42 | class FederatedLinkCreationService { |
||
| 43 | |||
| 44 | /** @var string */ |
||
| 45 | private $userId; |
||
| 46 | |||
| 47 | /** @var IL10N */ |
||
| 48 | private $l10n; |
||
| 49 | |||
| 50 | /** @var CirclesRequest */ |
||
| 51 | private $circlesRequest; |
||
| 52 | |||
| 53 | /** @var ConfigService */ |
||
| 54 | private $configService; |
||
| 55 | |||
| 56 | /** @var CirclesService */ |
||
| 57 | private $circlesService; |
||
| 58 | |||
| 59 | /** @var BroadcastService */ |
||
| 60 | private $broadcastService; |
||
| 61 | |||
| 62 | /** @var BroadcastService */ |
||
| 63 | private $federatedLinkService; |
||
| 64 | |||
| 65 | /** @var FederatedLinksRequest */ |
||
| 66 | private $federatedLinksRequest; |
||
| 67 | |||
| 68 | /** @var EventsService */ |
||
| 69 | private $eventsService; |
||
| 70 | |||
| 71 | /** @var IClientService */ |
||
| 72 | private $clientService; |
||
| 73 | |||
| 74 | /** @var MiscService */ |
||
| 75 | private $miscService; |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * FederatedLinkCreationService constructor. |
||
| 80 | * |
||
| 81 | * @param string $UserId |
||
| 82 | * @param IL10N $l10n |
||
| 83 | * @param CirclesRequest $circlesRequest |
||
| 84 | * @param ConfigService $configService |
||
| 85 | * @param CirclesService $circlesService |
||
| 86 | * @param BroadcastService $broadcastService |
||
| 87 | * @param FederatedLinkService $federatedService |
||
| 88 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 89 | * @param EventsService $eventsService |
||
| 90 | * @param IClientService $clientService |
||
| 91 | * @param MiscService $miscService |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function __construct( |
|
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * createLinkWithRemoteCircle(); |
||
| 117 | * |
||
| 118 | * link to a circle. |
||
| 119 | * Function will check if settings allow Federated links between circles, and the format of |
||
| 120 | * the link ($remote). If no exception, a request to the remote circle will be initiated |
||
| 121 | * using requestLinkWithRemoteCircle() |
||
| 122 | * |
||
| 123 | * $remote format: <circle_name>@<remote_host> |
||
| 124 | * |
||
| 125 | * @param string $circleUniqueId |
||
| 126 | * @param string $remote |
||
| 127 | * |
||
| 128 | * @throws Exception |
||
| 129 | * @throws FederatedCircleLinkFormatException |
||
| 130 | * @throws CircleTypeNotValidException |
||
| 131 | * |
||
| 132 | * @return FederatedLink |
||
| 133 | */ |
||
| 134 | public function createLinkWithRemoteCircle($circleUniqueId, $remote) { |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * requestLinkWithRemoteCircle() |
||
| 158 | * |
||
| 159 | * Using CircleId, function will get more infos from the database. |
||
| 160 | * Will check if author is at least admin and initiate a FederatedLink, save it |
||
| 161 | * in the database and send a request to the remote circle using requestLink() |
||
| 162 | * If any issue, entry is removed from the database. |
||
| 163 | * |
||
| 164 | * @param string $circleUniqueId |
||
| 165 | * @param string $remote |
||
| 166 | * |
||
| 167 | * @return FederatedLink |
||
| 168 | * @throws Exception |
||
| 169 | */ |
||
| 170 | private function requestLinkWithRemoteCircle($circleUniqueId, $remote) { |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * @param $circleUniqueId |
||
| 193 | * @param $remote |
||
| 194 | * |
||
| 195 | * @return FederatedLink |
||
| 196 | */ |
||
| 197 | private function generateNewLink($circleUniqueId, $remote) { |
||
| 213 | |||
| 214 | |||
| 215 | /** |
||
| 216 | * requestLink() |
||
| 217 | * |
||
| 218 | * |
||
| 219 | * @param Circle $circle |
||
| 220 | * @param FederatedLink $link |
||
| 221 | * |
||
| 222 | * @return boolean |
||
| 223 | * @throws Exception |
||
| 224 | */ |
||
| 225 | private function forceRequestNewLink(Circle $circle, FederatedLink &$link) { |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * eventOnRequestLink(); |
||
| 249 | * |
||
| 250 | * Called by requestLink() will update status and event |
||
| 251 | * Will also manage errors returned by the remote link |
||
| 252 | * |
||
| 253 | * @param Circle $circle |
||
| 254 | * @param FederatedLink $link |
||
| 255 | * @param int $status |
||
| 256 | * @param string $reason |
||
| 257 | * |
||
| 258 | * @throws Exception |
||
| 259 | */ |
||
| 260 | private function eventOnRequestLink(Circle $circle, FederatedLink &$link, $status, $reason) { |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * parseRequestLinkError(); |
||
| 281 | * |
||
| 282 | * Will parse the error reason returned by requestLink() and throw an Exception |
||
| 283 | * |
||
| 284 | * @param $reason |
||
| 285 | * |
||
| 286 | * @throws Exception |
||
| 287 | * @throws FederatedRemoteDoesNotAllowException |
||
| 288 | */ |
||
| 289 | private function parseRequestLinkError($reason) { |
||
| 306 | |||
| 307 | |||
| 308 | } |
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.