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 |
||
| 48 | class SharingFrameService { |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $userId; |
||
| 52 | |||
| 53 | /** @var ConfigService */ |
||
| 54 | private $configService; |
||
| 55 | |||
| 56 | /** @var CirclesRequest */ |
||
| 57 | private $circlesRequest; |
||
| 58 | |||
| 59 | /** @var FederatedLinksRequest */ |
||
| 60 | private $federatedLinksRequest; |
||
| 61 | |||
| 62 | /** @var BroadcastService */ |
||
| 63 | private $broadcastService; |
||
| 64 | |||
| 65 | /** @var FederatedLinkService */ |
||
| 66 | private $federatedLinkService; |
||
| 67 | |||
| 68 | /** @var ClientService */ |
||
| 69 | private $clientService; |
||
| 70 | |||
| 71 | /** @var MiscService */ |
||
| 72 | private $miscService; |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * SharingFrameService constructor. |
||
| 77 | * |
||
| 78 | * @param string $UserId |
||
| 79 | * @param ConfigService $configService |
||
| 80 | * @param CirclesRequest $circlesRequest |
||
| 81 | * @param FederatedLinksRequest $federatedLinksRequest |
||
| 82 | * @param BroadcastService $broadcastService |
||
| 83 | * @param FederatedLinkService $federatedLinkService |
||
| 84 | * @param IClientService $clientService |
||
| 85 | * @param MiscService $miscService |
||
| 86 | */ |
||
| 87 | View Code Duplication | public function __construct( |
|
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * createFrame() |
||
| 110 | * |
||
| 111 | * Save the Frame containing the Payload. |
||
| 112 | * The Payload will be shared locally, and spread it live if a Broadcaster is set. |
||
| 113 | * Function will also initiate the federated broadcast to linked circles. |
||
| 114 | * |
||
| 115 | * @param string $circleUniqueId |
||
| 116 | * @param SharingFrame $frame |
||
| 117 | * @param string|null $broadcast |
||
| 118 | * |
||
| 119 | * @throws Exception |
||
| 120 | * @throws MemberDoesNotExistException |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function createFrame($circleUniqueId, SharingFrame $frame, $broadcast = null) { |
|
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * Generate Headers and few more thing like UniqueId and Author. |
||
| 143 | * Check if the source is NOT Circles. |
||
| 144 | * |
||
| 145 | * @param SharingFrame $frame |
||
| 146 | * @param Circle $circle |
||
| 147 | * @param $broadcast |
||
| 148 | */ |
||
| 149 | private function generateHeaders(SharingFrame $frame, Circle $circle, $broadcast) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $circleUniqueId |
||
| 168 | * @param string $frameUniqueId |
||
| 169 | * |
||
| 170 | * @return null|SharingFrame |
||
| 171 | * @throws SharingFrameAlreadyDeliveredException |
||
| 172 | * @throws SharingFrameDoesNotExistException |
||
| 173 | */ |
||
| 174 | public function getFrameFromUniqueId($circleUniqueId, $frameUniqueId) { |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $token |
||
| 194 | * @param string $uniqueId |
||
| 195 | * @param SharingFrame $frame |
||
| 196 | * |
||
| 197 | * @return bool |
||
| 198 | * @throws Exception |
||
| 199 | */ |
||
| 200 | public function receiveFrame($token, $uniqueId, SharingFrame &$frame) { |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $circleUniqueId |
||
| 228 | * @param string $frameUniqueId |
||
| 229 | * |
||
| 230 | * @return bool |
||
| 231 | * @throws Exception |
||
| 232 | */ |
||
| 233 | public function initiateShare($circleUniqueId, $frameUniqueId) { |
||
| 254 | |||
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $remote |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | private function generatePayloadDeliveryURL($remote) { |
||
| 264 | |||
| 265 | |||
| 266 | /** |
||
| 267 | * @param SharingFrame $frame |
||
| 268 | * |
||
| 269 | * @throws Exception |
||
| 270 | */ |
||
| 271 | public function forwardSharingFrame(SharingFrame $frame) { |
||
| 289 | |||
| 290 | |||
| 291 | /** |
||
| 292 | * @param Circle $circle |
||
| 293 | * @param SharingFrame $frame |
||
| 294 | * @param FederatedLink[] $links |
||
| 295 | */ |
||
| 296 | private function forwardSharingFrameToFederatedLinks(Circle $circle, SharingFrame $frame, $links) { |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * sendRemoteShareToLinks(); |
||
| 313 | * |
||
| 314 | * @param FederatedLink $link |
||
| 315 | * @param array $args |
||
| 316 | */ |
||
| 317 | private function deliverSharingFrameToLink($link, $args) { |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * @param SharingFrame $frame |
||
| 344 | */ |
||
| 345 | public function updateFrameWithCloudId(SharingFrame $frame) { |
||
| 349 | |||
| 350 | |||
| 351 | } |
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.