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 |
||
| 32 | class CirclesController extends BaseController { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @NoAdminRequired |
||
| 36 | * @NoSubAdminRequired |
||
| 37 | * |
||
| 38 | * @param $type |
||
| 39 | * @param string $name |
||
| 40 | * |
||
| 41 | * @return DataResponse |
||
| 42 | */ |
||
| 43 | public function create($type, $name) { |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * @NoAdminRequired |
||
| 66 | * @NoSubAdminRequired |
||
| 67 | * |
||
| 68 | * @param $type |
||
| 69 | * @param string $name |
||
| 70 | * @param int $level |
||
| 71 | * |
||
| 72 | * @return DataResponse |
||
| 73 | */ |
||
| 74 | public function listing($type, $name = '', $level = 0) { |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * @NoAdminRequired |
||
| 89 | * @NoSubAdminRequired |
||
| 90 | * |
||
| 91 | * @param $id |
||
| 92 | * |
||
| 93 | * @return DataResponse |
||
| 94 | * @internal param string $name |
||
| 95 | * |
||
| 96 | */ |
||
| 97 | public function details($id) { |
||
| 109 | |||
| 110 | |||
| 111 | /** |
||
| 112 | * @NoAdminRequired |
||
| 113 | * @NoSubAdminRequired |
||
| 114 | * |
||
| 115 | * @param $id |
||
| 116 | * |
||
| 117 | * @return DataResponse |
||
| 118 | * @internal param string $name |
||
| 119 | * |
||
| 120 | */ |
||
| 121 | View Code Duplication | public function settings($id, $settings) { |
|
| 132 | |||
| 133 | |||
| 134 | /** |
||
| 135 | * @NoAdminRequired |
||
| 136 | * @NoSubAdminRequired |
||
| 137 | * |
||
| 138 | * @param $id |
||
| 139 | * |
||
| 140 | * @return DataResponse |
||
| 141 | * @internal param string $name |
||
| 142 | * |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function join($id) { |
|
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * @NoAdminRequired |
||
| 158 | * @NoSubAdminRequired |
||
| 159 | * |
||
| 160 | * @param $id |
||
| 161 | * |
||
| 162 | * @return DataResponse |
||
| 163 | * @internal param string $name |
||
| 164 | * |
||
| 165 | */ |
||
| 166 | View Code Duplication | public function leave($id) { |
|
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * @NoAdminRequired |
||
| 181 | * @NoSubAdminRequired |
||
| 182 | * |
||
| 183 | * @param $id |
||
| 184 | * |
||
| 185 | * @return DataResponse |
||
| 186 | * @internal param string $name |
||
| 187 | * |
||
| 188 | */ |
||
| 189 | View Code Duplication | public function destroy($id) { |
|
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * link() |
||
| 202 | * |
||
| 203 | * Called from the UI to create a initiate the process of linking 2 [remote] circles. |
||
| 204 | * $remote format: <circle_name>@<remote_host> |
||
| 205 | * |
||
| 206 | * @NoAdminRequired |
||
| 207 | * @NoSubAdminRequired |
||
| 208 | * |
||
| 209 | * @param int $circleId |
||
| 210 | * @param string $remote |
||
| 211 | * |
||
| 212 | * @return DataResponse |
||
| 213 | */ |
||
| 214 | public function link($circleId, $remote) { |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * linkStatus(); |
||
| 233 | * |
||
| 234 | * Modify a link status. Used to confirm/dismiss a request or putting down a link. |
||
| 235 | * The function will modify local status and broadcast the status to the remote. |
||
| 236 | * |
||
| 237 | * Note: should be moved to a LinkController |
||
| 238 | * |
||
| 239 | * @NoAdminRequired |
||
| 240 | * @NoSubAdminRequired |
||
| 241 | * |
||
| 242 | * @param int $linkId |
||
| 243 | * @param int $status |
||
| 244 | * |
||
| 245 | * @return DataResponse |
||
| 246 | */ |
||
| 247 | public function linkStatus($linkId, $status) { |
||
| 258 | |||
| 259 | |||
| 260 | } |
||
| 261 | |||
| 262 |
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.