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 |
||
| 41 | class ServerController extends BaseController |
||
| 42 | { |
||
| 43 | public function __construct() |
||
| 47 | |||
| 48 | /** |
||
| 49 | * List All Servers. |
||
| 50 | * |
||
| 51 | * Lists all servers currently on the system. |
||
| 52 | * |
||
| 53 | * @Get("/servers/{?page}") |
||
| 54 | * @Versions({"v1"}) |
||
| 55 | * @Parameters({ |
||
| 56 | * @Parameter("page", type="integer", description="The page of results to view.", default=1) |
||
| 57 | * }) |
||
| 58 | * @Response(200) |
||
| 59 | */ |
||
| 60 | public function lists(Request $request) |
||
|
|
|||
| 61 | { |
||
| 62 | return Models\Server::all()->toArray(); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Create Server. |
||
| 67 | * |
||
| 68 | * @Post("/servers") |
||
| 69 | * @Versions({"v1"}) |
||
| 70 | * @Response(201) |
||
| 71 | */ |
||
| 72 | View Code Duplication | public function create(Request $request) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * List Specific Server. |
||
| 91 | * |
||
| 92 | * Lists specific fields about a server or all fields pertaining to that server. |
||
| 93 | * |
||
| 94 | * @Get("/servers/{id}{?fields}") |
||
| 95 | * @Versions({"v1"}) |
||
| 96 | * @Parameters({ |
||
| 97 | * @Parameter("id", type="integer", required=true, description="The ID of the server to get information on."), |
||
| 98 | * @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.") |
||
| 99 | * }) |
||
| 100 | * @Response(200) |
||
| 101 | */ |
||
| 102 | public function view(Request $request, $id) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Update Server configuration. |
||
| 157 | * |
||
| 158 | * Updates display information on panel. |
||
| 159 | * |
||
| 160 | * @Patch("/servers/{id}/config") |
||
| 161 | * @Versions({"v1"}) |
||
| 162 | * @Transaction({ |
||
| 163 | * @Request({ |
||
| 164 | * "owner": "[email protected]", |
||
| 165 | * "name": "New Name", |
||
| 166 | * "reset_token": true |
||
| 167 | * }, headers={"Authorization": "Bearer <token>"}), |
||
| 168 | * @Response(200, body={"name": "New Name"}), |
||
| 169 | * @Response(422) |
||
| 170 | * }) |
||
| 171 | * @Parameters({ |
||
| 172 | * @Parameter("id", type="integer", required=true, description="The ID of the server to modify.") |
||
| 173 | * }) |
||
| 174 | */ |
||
| 175 | View Code Duplication | public function config(Request $request, $id) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Update Server Build Configuration. |
||
| 193 | * |
||
| 194 | * Updates server build information on panel and on node. |
||
| 195 | * |
||
| 196 | * @Patch("/servers/{id}/build") |
||
| 197 | * @Versions({"v1"}) |
||
| 198 | * @Transaction({ |
||
| 199 | * @Request({ |
||
| 200 | * "default": "192.168.0.1:25565", |
||
| 201 | * "add_additional": [ |
||
| 202 | * "192.168.0.1:25566", |
||
| 203 | * "192.168.0.1:25567", |
||
| 204 | * "192.168.0.1:25568" |
||
| 205 | * ], |
||
| 206 | * "remove_additional": [], |
||
| 207 | * "memory": 1024, |
||
| 208 | * "swap": 0, |
||
| 209 | * "io": 500, |
||
| 210 | * "cpu": 0, |
||
| 211 | * "disk": 1024 |
||
| 212 | * }, headers={"Authorization": "Bearer <token>"}), |
||
| 213 | * @Response(200, body={"name": "New Name"}), |
||
| 214 | * @Response(422) |
||
| 215 | * }) |
||
| 216 | * @Parameters({ |
||
| 217 | * @Parameter("id", type="integer", required=true, description="The ID of the server to modify.") |
||
| 218 | * }) |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function build(Request $request, $id) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Suspend Server. |
||
| 238 | * |
||
| 239 | * @Post("/servers/{id}/suspend") |
||
| 240 | * @Versions({"v1"}) |
||
| 241 | * @Parameters({ |
||
| 242 | * @Parameter("id", type="integer", required=true, description="The ID of the server."), |
||
| 243 | * }) |
||
| 244 | * @Response(204) |
||
| 245 | */ |
||
| 246 | View Code Duplication | public function suspend(Request $request, $id) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Unsuspend Server. |
||
| 262 | * |
||
| 263 | * @Post("/servers/{id}/unsuspend") |
||
| 264 | * @Versions({"v1"}) |
||
| 265 | * @Parameters({ |
||
| 266 | * @Parameter("id", type="integer", required=true, description="The ID of the server."), |
||
| 267 | * }) |
||
| 268 | * @Response(204) |
||
| 269 | */ |
||
| 270 | View Code Duplication | public function unsuspend(Request $request, $id) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Delete Server. |
||
| 286 | * |
||
| 287 | * @Delete("/servers/{id}/{force}") |
||
| 288 | * @Versions({"v1"}) |
||
| 289 | * @Parameters({ |
||
| 290 | * @Parameter("id", type="integer", required=true, description="The ID of the server."), |
||
| 291 | * @Parameter("force", type="string", required=false, description="Use 'force' if the server should be removed regardless of daemon response."), |
||
| 292 | * }) |
||
| 293 | * @Response(204) |
||
| 294 | */ |
||
| 295 | View Code Duplication | public function delete(Request $request, $id, $force = null) |
|
| 308 | } |
||
| 309 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.