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 |
||
| 30 | View Code Duplication | class ReportsToController extends BaseController |
|
|
|
|||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var Employee |
||
| 34 | */ |
||
| 35 | protected $employee; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var EmployeeSupervisor |
||
| 39 | */ |
||
| 40 | protected $employee_supervisor; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param Employee $employee |
||
| 44 | * @param EmployeeSupervisor $employee_supervisor |
||
| 45 | * |
||
| 46 | * @author Bertrand Kintanar <[email protected]> |
||
| 47 | */ |
||
| 48 | 10 | public function __construct(Employee $employee, EmployeeSupervisor $employee_supervisor) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Stores a single instance of Employee Supervisor. |
||
| 56 | * |
||
| 57 | * @SWG\Post( |
||
| 58 | * path="/profile/reports-to", |
||
| 59 | * description="This route provides the ability to store a single instance of Employee Supervisor.", |
||
| 60 | * tags={"Employee Profiles"}, |
||
| 61 | * consumes={"application/json"}, |
||
| 62 | * summary="Stores a single instance of Employee Supervisor.", |
||
| 63 | * @SWG\Response(response="201", description="Success", |
||
| 64 | * @SWG\Schema( |
||
| 65 | * title="data", |
||
| 66 | * type="object", |
||
| 67 | * required={"supervisor", "message", "status_code"}, |
||
| 68 | * @SWG\Property(property="supervisor", ref="#/definitions/Employee"), |
||
| 69 | * @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"), |
||
| 70 | * @SWG\Property(property="status_code", type="integer", default=201, description="Status code from server"), |
||
| 71 | * ) |
||
| 72 | * ), |
||
| 73 | * @SWG\Response(response="400", description="Token not provided", |
||
| 74 | * @SWG\Schema( |
||
| 75 | * title="data", |
||
| 76 | * type="object", |
||
| 77 | * required={"message", "status_code", "debug"}, |
||
| 78 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 79 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 80 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 81 | * ) |
||
| 82 | * ), |
||
| 83 | * @SWG\Response(response="422", description="Unable to add record to the database.", |
||
| 84 | * @SWG\Schema( |
||
| 85 | * title="data", |
||
| 86 | * type="object", |
||
| 87 | * required={"message", "status_code"}, |
||
| 88 | * @SWG\Property(property="message", type="string", default="Unable to add record to the database.", description="Status message from server"), |
||
| 89 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 90 | * ) |
||
| 91 | * ), |
||
| 92 | * @SWG\Parameter( |
||
| 93 | * name="supervisor", |
||
| 94 | * in="body", |
||
| 95 | * required=true, |
||
| 96 | * @SWG\Property(ref="#/definitions/Employee") |
||
| 97 | * ), |
||
| 98 | * @SWG\Parameter( |
||
| 99 | * name="Authorization", |
||
| 100 | * in="header", |
||
| 101 | * description="JWT Token", |
||
| 102 | * required=true, |
||
| 103 | * type="string", |
||
| 104 | * default="Bearer " |
||
| 105 | * ), |
||
| 106 | * ) |
||
| 107 | * |
||
| 108 | * @param ReportsToRequest $request |
||
| 109 | * |
||
| 110 | * @return \Dingo\Api\Http\Response |
||
| 111 | * |
||
| 112 | * @author Bertrand Kintanar <[email protected]> |
||
| 113 | */ |
||
| 114 | 6 | public function store(ReportsToRequest $request) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Updates a single instance of Employee Supervisor. |
||
| 121 | * |
||
| 122 | * @SWG\Patch( |
||
| 123 | * path="/profile/reports-to", |
||
| 124 | * description="This route provides the ability to update a single instance of Employee Supervisor.", |
||
| 125 | * tags={"Employee Profiles"}, |
||
| 126 | * consumes={"application/json"}, |
||
| 127 | * summary="Updates a single instance of Employee Supervisor.", |
||
| 128 | * @SWG\Response(response="200", description="Success", |
||
| 129 | * @SWG\Schema( |
||
| 130 | * title="data", |
||
| 131 | * type="object", |
||
| 132 | * required={"message", "status_code"}, |
||
| 133 | * @SWG\Property(property="message", type="string", default="Record successfully updated.", description="Status message from server"), |
||
| 134 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 135 | * ) |
||
| 136 | * ), |
||
| 137 | * @SWG\Response(response="400", description="Token not provided", |
||
| 138 | * @SWG\Schema( |
||
| 139 | * title="data", |
||
| 140 | * type="object", |
||
| 141 | * required={"message", "status_code", "debug"}, |
||
| 142 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 143 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 144 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 145 | * ) |
||
| 146 | * ), |
||
| 147 | * @SWG\Response(response="404", description="Unable to retrieve record from database.", |
||
| 148 | * @SWG\Schema( |
||
| 149 | * title="data", |
||
| 150 | * type="object", |
||
| 151 | * required={"message", "status_code", "debug"}, |
||
| 152 | * @SWG\Property(property="message", type="string", default="Unable to retrieve record from database.", description="Error message from server"), |
||
| 153 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 154 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 155 | * ) |
||
| 156 | * ), |
||
| 157 | * @SWG\Response(response="422", description="Unable to update record.", |
||
| 158 | * @SWG\Schema( |
||
| 159 | * title="data", |
||
| 160 | * type="object", |
||
| 161 | * required={"message", "status_code"}, |
||
| 162 | * @SWG\Property(property="message", type="string", default="Unable to update record.", description="Status message from server"), |
||
| 163 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 164 | * ) |
||
| 165 | * ), |
||
| 166 | * @SWG\Parameter( |
||
| 167 | * name="supervisor", |
||
| 168 | * in="body", |
||
| 169 | * required=true, |
||
| 170 | * description="employee's supervisor object that needs to be updated", |
||
| 171 | * @SWG\Property(ref="#/definitions/Employee") |
||
| 172 | * ), |
||
| 173 | * @SWG\Parameter( |
||
| 174 | * name="Authorization", |
||
| 175 | * in="header", |
||
| 176 | * description="JWT Token", |
||
| 177 | * required=true, |
||
| 178 | * type="string", |
||
| 179 | * default="Bearer " |
||
| 180 | * ), |
||
| 181 | * ) |
||
| 182 | * |
||
| 183 | * @param ReportsToRequest $request |
||
| 184 | * |
||
| 185 | * @return \Dingo\Api\Http\Response |
||
| 186 | * |
||
| 187 | * @author Bertrand Kintanar <[email protected]> |
||
| 188 | */ |
||
| 189 | 4 | public function update(ReportsToRequest $request) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Deletes a single instance of EmployeeSupervisor. |
||
| 196 | * |
||
| 197 | * @SWG\Delete( |
||
| 198 | * path="/profile/reports-to/{employee_supervisor}", |
||
| 199 | * description="This route provides the ability to delete a EmployeeSupervisor.", |
||
| 200 | * tags={"Employee Profiles"}, |
||
| 201 | * consumes={"application/json"}, |
||
| 202 | * summary="Deletes a single instance of EmployeeSupervisor.", |
||
| 203 | * @SWG\Response(response="200", description="Success", |
||
| 204 | * @SWG\Schema( |
||
| 205 | * title="data", |
||
| 206 | * type="object", |
||
| 207 | * required={"message", "status_code"}, |
||
| 208 | * @SWG\Property(property="message", type="string", default="Record successfully deleted.", description="Status message from server"), |
||
| 209 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 210 | * ) |
||
| 211 | * ), |
||
| 212 | * @SWG\Response(response="400", description="Token not provided", |
||
| 213 | * @SWG\Schema( |
||
| 214 | * title="data", |
||
| 215 | * type="object", |
||
| 216 | * required={"message", "status_code", "debug"}, |
||
| 217 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 218 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 219 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 220 | * ) |
||
| 221 | * ), |
||
| 222 | * @SWG\Response(response="422", description="422 Unprocessable Entity", |
||
| 223 | * @SWG\Schema( |
||
| 224 | * title="data", |
||
| 225 | * type="object", |
||
| 226 | * required={"message", "status_code"}, |
||
| 227 | * @SWG\Property(property="message", type="string", default="422 Unprocessable Entity", description="Status message from server"), |
||
| 228 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 229 | * ) |
||
| 230 | * ), |
||
| 231 | * @SWG\Parameter( |
||
| 232 | * name="employee_supervisor", |
||
| 233 | * in="path", |
||
| 234 | * description="Employee supervisor id to be deleted", |
||
| 235 | * required=true, |
||
| 236 | * type="integer", |
||
| 237 | * format="int64", |
||
| 238 | * default=1, |
||
| 239 | * ), |
||
| 240 | * @SWG\Parameter( |
||
| 241 | * name="Authorization", |
||
| 242 | * in="header", |
||
| 243 | * description="JWT Token", |
||
| 244 | * required=true, |
||
| 245 | * type="string", |
||
| 246 | * default="Bearer " |
||
| 247 | * ), |
||
| 248 | * ) |
||
| 249 | * |
||
| 250 | * @param EmployeeSupervisor $employee_supervisor |
||
| 251 | * @param ReportsToRequest $request |
||
| 252 | * |
||
| 253 | * @return \Dingo\Api\Http\Response |
||
| 254 | * |
||
| 255 | * @author Bertrand Kintanar <[email protected]> |
||
| 256 | */ |
||
| 257 | 2 | public function destroy(EmployeeSupervisor $employee_supervisor, ReportsToRequest $request) |
|
| 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.