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 |
||
| 54 | class SharesController extends Controller { |
||
| 55 | |||
| 56 | |||
| 57 | use TStringTools; |
||
| 58 | |||
| 59 | |||
| 60 | /** @var TokensRequest */ |
||
| 61 | private $tokenRequest; |
||
| 62 | |||
| 63 | /** @var SharesRequest */ |
||
| 64 | private $sharesRequest; |
||
| 65 | |||
| 66 | /** @var IURLGenerator */ |
||
| 67 | private $urlGenerator; |
||
| 68 | |||
| 69 | /** @var MembersService */ |
||
| 70 | private $membersService; |
||
| 71 | |||
| 72 | /** @var BroadcastService */ |
||
| 73 | private $broadcastService; |
||
| 74 | |||
| 75 | /** @var SharingFrameService */ |
||
| 76 | private $sharingFrameService; |
||
| 77 | |||
| 78 | /** @var ConfigService */ |
||
| 79 | private $configService; |
||
| 80 | |||
| 81 | /** @var MiscService */ |
||
| 82 | private $miscService; |
||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * SharesController constructor. |
||
| 87 | * |
||
| 88 | * @param $appName |
||
| 89 | * @param IRequest $request |
||
| 90 | * @param TokensRequest $tokenRequest |
||
| 91 | * @param SharesRequest $sharesRequest |
||
| 92 | * @param IURLGenerator $urlGenerator |
||
| 93 | * @param MembersService $membersService |
||
| 94 | * @param BroadcastService $broadcastService |
||
| 95 | * @param SharingFrameService $sharingFrameService |
||
| 96 | * @param ConfigService $configService |
||
| 97 | * @param MiscService $miscService |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function __construct( |
|
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * Called by the JavaScript API when creating a new Share item that will be |
||
| 121 | * broadcasted to the circle itself, and any other circle linked to it. |
||
| 122 | * |
||
| 123 | * @NoAdminRequired |
||
| 124 | * @NoSubAdminRequired |
||
| 125 | * |
||
| 126 | * @param string $circleUniqueId |
||
| 127 | * @param string $source |
||
| 128 | * @param string $type |
||
| 129 | * @param array $payload |
||
| 130 | * |
||
| 131 | * @return DataResponse |
||
| 132 | */ |
||
| 133 | public function create($circleUniqueId, $source, $type, $payload) { |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * initShareDelivery() |
||
| 165 | * |
||
| 166 | * Note: this function will close the request mid-run from the client but will still |
||
| 167 | * running its process. |
||
| 168 | * |
||
| 169 | * Called by locally, the function will get the SharingFrame by its uniqueId from the database. |
||
| 170 | * After closing the socket, will broadcast the Frame locally and - if Federated Shares are |
||
| 171 | * enable - will deliver it to each remote circles linked to the circle the Payload belongs to. |
||
| 172 | * |
||
| 173 | * A status response is sent to free the client process before starting to broadcast the item |
||
| 174 | * to other federated links. |
||
| 175 | * |
||
| 176 | * @PublicPage |
||
| 177 | * @NoCSRFRequired |
||
| 178 | * |
||
| 179 | * @param string $circleId |
||
| 180 | * @param string $frameId |
||
| 181 | * |
||
| 182 | * @return DataResponse |
||
| 183 | */ |
||
| 184 | public function initShareDelivery($circleId, $frameId) { |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * @PublicPage |
||
| 209 | * @NoCSRFRequired |
||
| 210 | * |
||
| 211 | * @param string $token |
||
| 212 | * |
||
| 213 | * @return RedirectResponse |
||
| 214 | */ |
||
| 215 | public function public(string $token) { |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * @param SharesToken $shareToken |
||
| 234 | * |
||
| 235 | * @throws MemberDoesNotExistException |
||
| 236 | */ |
||
| 237 | private function checkContactMail(SharesToken $shareToken) { |
||
| 258 | } |
||
| 259 | |||
| 260 |
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.