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 FederatedController extends Controller { |
||
| 49 | |||
| 50 | /** @var ConfigService */ |
||
| 51 | protected $configService; |
||
| 52 | |||
| 53 | /** @var CirclesService */ |
||
| 54 | protected $circlesService; |
||
| 55 | |||
| 56 | /** @var MembersService */ |
||
| 57 | protected $membersService; |
||
| 58 | |||
| 59 | /** @var SharingFrameService */ |
||
| 60 | protected $sharingFrameService; |
||
| 61 | |||
| 62 | /** @var BroadcastService */ |
||
| 63 | protected $broadcastService; |
||
| 64 | |||
| 65 | /** @var FederatedLinkService */ |
||
| 66 | protected $federatedLinkService; |
||
| 67 | |||
| 68 | /** @var FederatedLinkCreationService */ |
||
| 69 | protected $federatedLinkCreationService; |
||
| 70 | |||
| 71 | /** @var MiscService */ |
||
| 72 | protected $miscService; |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * BaseController constructor. |
||
| 77 | * |
||
| 78 | * @param string $appName |
||
| 79 | * @param IRequest $request |
||
| 80 | * @param ConfigService $configService |
||
| 81 | * @param CirclesService $circlesService |
||
| 82 | * @param SharingFrameService $sharingFrameService |
||
| 83 | * @param BroadcastService $broadcastService |
||
| 84 | * @param FederatedLinkService $federatedLinkService |
||
| 85 | * @param FederatedLinkCreationService $federatedLinkCreationService |
||
| 86 | * @param MiscService $miscService |
||
| 87 | */ |
||
| 88 | View Code Duplication | public function __construct( |
|
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * requestedLink() |
||
| 107 | * |
||
| 108 | * Called when a remote circle want to create a link. |
||
| 109 | * The function check if it is possible first; then create a link-object |
||
| 110 | * and sent it to be saved in the database. |
||
| 111 | * |
||
| 112 | * @PublicPage |
||
| 113 | * @NoCSRFRequired |
||
| 114 | * |
||
| 115 | * @param $data |
||
| 116 | * |
||
| 117 | * @return DataResponse |
||
| 118 | */ |
||
| 119 | public function requestedLink($data) { |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * @param $data |
||
| 145 | * |
||
| 146 | * @return FederatedLink |
||
| 147 | */ |
||
| 148 | private function generateNewLink($data) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * receiveFederatedDelivery() |
||
| 162 | * |
||
| 163 | * Note: this function will close the request mid-run from the client but will still |
||
| 164 | * running its process. |
||
| 165 | * |
||
| 166 | * Called by a remote circle to broadcast a Share item, the function will save the item |
||
| 167 | * in the database and broadcast it locally. A status response is sent to the remote to free |
||
| 168 | * the remote process before starting to broadcast the item to other federated links. |
||
| 169 | * |
||
| 170 | * @PublicPage |
||
| 171 | * @NoCSRFRequired |
||
| 172 | * |
||
| 173 | * @param array $apiVersion |
||
| 174 | * @param string $token |
||
| 175 | * @param string $uniqueId |
||
| 176 | * @param string $item |
||
| 177 | * |
||
| 178 | * @return DataResponse |
||
| 179 | */ |
||
| 180 | public function receiveFederatedDelivery($apiVersion, $token, $uniqueId, $item) { |
||
| 201 | |||
| 202 | |||
| 203 | /** |
||
| 204 | * updateLink(); |
||
| 205 | * |
||
| 206 | * Update the current status of a link, based on UniqueId and Token. |
||
| 207 | * |
||
| 208 | * @PublicPage |
||
| 209 | * @NoCSRFRequired |
||
| 210 | * |
||
| 211 | * @param $data |
||
| 212 | * |
||
| 213 | * @return DataResponse |
||
| 214 | */ |
||
| 215 | public function updateLink($data) { |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * send a positive response to a request with an array of data, and confirm |
||
| 239 | * the identity of the link with a token |
||
| 240 | * |
||
| 241 | * @param array $data |
||
| 242 | * @param FederatedLink $link |
||
| 243 | * |
||
| 244 | * @return DataResponse |
||
| 245 | */ |
||
| 246 | private function federatedSuccess(array $data = array(), FederatedLink $link = null) { |
||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * send a negative response to a request, with a reason of the failure. |
||
| 262 | * |
||
| 263 | * @param string $reason |
||
| 264 | * |
||
| 265 | * @return DataResponse |
||
| 266 | */ |
||
| 267 | private function federatedFail($reason) { |
||
| 278 | } |
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.