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 |
||
| 36 | class CirclesController extends BaseController { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @NoAdminRequired |
||
| 40 | * @NoSubAdminRequired |
||
| 41 | * |
||
| 42 | * @param $type |
||
| 43 | * @param string $name |
||
| 44 | * |
||
| 45 | * @return DataResponse |
||
| 46 | */ |
||
| 47 | public function create($type, $name) { |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * verifyCreationName(); |
||
| 63 | * |
||
| 64 | * Verify the name at the creation of a circle: |
||
| 65 | * Name must contain at least 3 chars. |
||
| 66 | * First char must be alpha-numeric. |
||
| 67 | * |
||
| 68 | * @param $name |
||
| 69 | * |
||
| 70 | * @throws CircleNameFirstCharException |
||
| 71 | * @throws CircleNameTooShortException |
||
| 72 | */ |
||
| 73 | private function verifyCreationName($name) { |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * @NoAdminRequired |
||
| 93 | * @NoSubAdminRequired |
||
| 94 | * |
||
| 95 | * @param $type |
||
| 96 | * @param string $name |
||
| 97 | * @param int $level |
||
| 98 | * |
||
| 99 | * @return DataResponse |
||
| 100 | */ |
||
| 101 | public function listing($type, $name = '', $level = 0) { |
||
| 112 | |||
| 113 | |||
| 114 | /** |
||
| 115 | * @NoAdminRequired |
||
| 116 | * @NoSubAdminRequired |
||
| 117 | * |
||
| 118 | * @param string $uniqueId |
||
| 119 | * |
||
| 120 | * @return DataResponse |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function details($uniqueId) { |
|
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @NoAdminRequired |
||
| 137 | * @NoSubAdminRequired |
||
| 138 | * |
||
| 139 | * @param string $uniqueId |
||
| 140 | * @param array $settings |
||
| 141 | * |
||
| 142 | * @return DataResponse |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function settings($uniqueId, $settings) { |
|
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * @NoAdminRequired |
||
| 160 | * @NoSubAdminRequired |
||
| 161 | * |
||
| 162 | * @param string $uniqueId |
||
| 163 | * |
||
| 164 | * @return DataResponse |
||
| 165 | */ |
||
| 166 | View Code Duplication | public function join($uniqueId) { |
|
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * @NoAdminRequired |
||
| 180 | * @NoSubAdminRequired |
||
| 181 | * |
||
| 182 | * @param string $uniqueId |
||
| 183 | * |
||
| 184 | * @return DataResponse |
||
| 185 | */ |
||
| 186 | View Code Duplication | public function leave($uniqueId) { |
|
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * @NoAdminRequired |
||
| 201 | * @NoSubAdminRequired |
||
| 202 | * |
||
| 203 | * @param string $uniqueId |
||
| 204 | * |
||
| 205 | * @return DataResponse |
||
| 206 | */ |
||
| 207 | public function destroy($uniqueId) { |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * link() |
||
| 220 | * |
||
| 221 | * Called from the UI to create a initiate the process of linking 2 [remote] circles. |
||
| 222 | * $remote format: <circle_name>@<remote_host> |
||
| 223 | * |
||
| 224 | * @NoAdminRequired |
||
| 225 | * @NoSubAdminRequired |
||
| 226 | * |
||
| 227 | * @param string $uniqueId |
||
| 228 | * @param string $remote |
||
| 229 | * |
||
| 230 | * @return DataResponse |
||
| 231 | */ |
||
| 232 | public function link($uniqueId, $remote) { |
||
| 247 | |||
| 248 | |||
| 249 | /** |
||
| 250 | * linkStatus(); |
||
| 251 | * |
||
| 252 | * Modify a link status. Used to confirm/dismiss a request or putting down a link. |
||
| 253 | * The function will modify local status and broadcast the status to the remote. |
||
| 254 | * |
||
| 255 | * Note: should be moved to a LinkController |
||
| 256 | * |
||
| 257 | * @NoAdminRequired |
||
| 258 | * @NoSubAdminRequired |
||
| 259 | * |
||
| 260 | * @param int $linkId |
||
| 261 | * @param int $status |
||
| 262 | * |
||
| 263 | * @return DataResponse |
||
| 264 | * @throws FederatedCircleNotAllowedException |
||
| 265 | */ |
||
| 266 | public function linkStatus($linkId, $status) { |
||
| 284 | |||
| 285 | |||
| 286 | } |
||
| 287 | |||
| 288 |
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.