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 |
||
| 40 | class UserController extends BaseController |
||
| 41 | { |
||
| 42 | public function __construct() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * List All Users. |
||
| 48 | * |
||
| 49 | * Lists all users currently on the system. |
||
| 50 | * |
||
| 51 | * @Get("/users/{?page}") |
||
| 52 | * @Versions({"v1"}) |
||
| 53 | * @Parameters({ |
||
| 54 | * @Parameter("page", type="integer", description="The page of results to view.", default=1) |
||
| 55 | * }) |
||
| 56 | * @Response(200) |
||
| 57 | */ |
||
| 58 | public function lists(Request $request) |
||
|
|
|||
| 59 | { |
||
| 60 | return Models\User::all()->toArray(); |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * List Specific User. |
||
| 65 | * |
||
| 66 | * Lists specific fields about a user or all fields pertaining to that user. |
||
| 67 | * |
||
| 68 | * @Get("/users/{id}/{fields}") |
||
| 69 | * @Versions({"v1"}) |
||
| 70 | * @Parameters({ |
||
| 71 | * @Parameter("id", type="integer", required=true, description="The ID of the user to get information on."), |
||
| 72 | * @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.") |
||
| 73 | * }) |
||
| 74 | * @Response(200) |
||
| 75 | */ |
||
| 76 | public function view(Request $request, $id) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Create a New User. |
||
| 107 | * |
||
| 108 | * @Post("/users") |
||
| 109 | * @Versions({"v1"}) |
||
| 110 | * @Transaction({ |
||
| 111 | * @Request({ |
||
| 112 | * "email": "[email protected]", |
||
| 113 | * "password": "foopassword", |
||
| 114 | * "admin": false, |
||
| 115 | * "custom_id": 123 |
||
| 116 | * }, headers={"Authorization": "Bearer <token>"}), |
||
| 117 | * @Response(201), |
||
| 118 | * @Response(422) |
||
| 119 | * }) |
||
| 120 | */ |
||
| 121 | public function create(Request $request) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Update an Existing User. |
||
| 139 | * |
||
| 140 | * The data sent in the request will be used to update the existing user on the system. |
||
| 141 | * |
||
| 142 | * @Patch("/users/{id}") |
||
| 143 | * @Versions({"v1"}) |
||
| 144 | * @Transaction({ |
||
| 145 | * @Request({ |
||
| 146 | * "email": "[email protected]" |
||
| 147 | * }, headers={"Authorization": "Bearer <token>"}), |
||
| 148 | * @Response(200, body={"email": "[email protected]"}), |
||
| 149 | * @Response(422) |
||
| 150 | * }) |
||
| 151 | * @Parameters({ |
||
| 152 | * @Parameter("id", type="integer", required=true, description="The ID of the user to modify.") |
||
| 153 | * }) |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function update(Request $request, $id) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Delete a User. |
||
| 173 | * |
||
| 174 | * @Delete("/users/{id}") |
||
| 175 | * @Versions({"v1"}) |
||
| 176 | * @Transaction({ |
||
| 177 | * @Request(headers={"Authorization": "Bearer <token>"}), |
||
| 178 | * @Response(204), |
||
| 179 | * @Response(422) |
||
| 180 | * }) |
||
| 181 | * @Parameters({ |
||
| 182 | * @Parameter("id", type="integer", required=true, description="The ID of the user to delete.") |
||
| 183 | * }) |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function delete(Request $request, $id) |
|
| 198 | } |
||
| 199 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.