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 |
||
| 47 | class FederatedController extends BaseController { |
||
| 48 | |||
| 49 | /** @var string */ |
||
| 50 | protected $userId; |
||
| 51 | |||
| 52 | /** @var IL10N */ |
||
| 53 | protected $l10n; |
||
| 54 | |||
| 55 | /** @var ConfigService */ |
||
| 56 | protected $configService; |
||
| 57 | |||
| 58 | /** @var CirclesService */ |
||
| 59 | protected $circlesService; |
||
| 60 | |||
| 61 | /** @var MembersService */ |
||
| 62 | protected $membersService; |
||
| 63 | |||
| 64 | /** @var SharingFrameService */ |
||
| 65 | protected $sharingFrameService; |
||
| 66 | |||
| 67 | /** @var FederatedLinkService */ |
||
| 68 | protected $federatedLinkService; |
||
| 69 | |||
| 70 | /** @var MiscService */ |
||
| 71 | protected $miscService; |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * requestedLink() |
||
| 76 | * |
||
| 77 | * Called when a remote circle want to create a link. |
||
| 78 | * The function check if it is possible first; then create a link-object |
||
| 79 | * and sent it to be saved in the database. |
||
| 80 | * |
||
| 81 | * @PublicPage |
||
| 82 | * @NoCSRFRequired |
||
| 83 | * |
||
| 84 | * @param $data |
||
| 85 | * |
||
| 86 | * @return DataResponse |
||
| 87 | */ |
||
| 88 | public function requestedLink($data) { |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * @param $data |
||
| 114 | * |
||
| 115 | * @return FederatedLink |
||
| 116 | */ |
||
| 117 | private function generateNewLink($data) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * receiveFederatedDelivery() |
||
| 131 | * |
||
| 132 | * Note: this function will close the request mid-run from the client but will still |
||
| 133 | * running its process. |
||
| 134 | * |
||
| 135 | * Called by a remote circle to broadcast a Share item, the function will save the item |
||
| 136 | * in the database and broadcast it locally. A status response is sent to the remote to free |
||
| 137 | * the remote process before starting to broadcast the item to other federated links. |
||
| 138 | * |
||
| 139 | * @PublicPage |
||
| 140 | * @NoCSRFRequired |
||
| 141 | * |
||
| 142 | * @param array $apiVersion |
||
| 143 | * @param string $token |
||
| 144 | * @param string $uniqueId |
||
| 145 | * @param string $item |
||
| 146 | * |
||
| 147 | * @return DataResponse |
||
| 148 | */ |
||
| 149 | public function receiveFederatedDelivery($apiVersion, $token, $uniqueId, $item) { |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * updateLink(); |
||
| 174 | * |
||
| 175 | * Update the current status of a link, based on UniqueId and Token. |
||
| 176 | * |
||
| 177 | * @PublicPage |
||
| 178 | * @NoCSRFRequired |
||
| 179 | * |
||
| 180 | * @param $data |
||
| 181 | * |
||
| 182 | * @return DataResponse |
||
| 183 | */ |
||
| 184 | public function updateLink($data) { |
||
| 204 | |||
| 205 | |||
| 206 | /** |
||
| 207 | * send a positive response to a request with an array of data, and confirm |
||
| 208 | * the identity of the link with a token |
||
| 209 | * |
||
| 210 | * @param array $data |
||
| 211 | * @param FederatedLink $link |
||
| 212 | * |
||
| 213 | * @return DataResponse |
||
| 214 | */ |
||
| 215 | private function federatedSuccess(array $data = array(), FederatedLink $link = null) { |
||
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * send a negative response to a request, with a reason of the failure. |
||
| 231 | * |
||
| 232 | * @param string $reason |
||
| 233 | * |
||
| 234 | * @return DataResponse |
||
| 235 | */ |
||
| 236 | private function federatedFail($reason) { |
||
| 247 | } |
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.