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 |
||
| 20 | View Code Duplication | class EducationsController extends BaseController |
|
|
|
|||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var EducationLevel |
||
| 24 | */ |
||
| 25 | protected $education_level; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param EducationLevel $education_level |
||
| 29 | * |
||
| 30 | * @author Bertrand Kintanar <[email protected]> |
||
| 31 | */ |
||
| 32 | 16 | public function __construct(EducationLevel $education_level) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Delete the Admin - Qualifications Education. |
||
| 39 | * |
||
| 40 | * @SWG\Delete( |
||
| 41 | * path="/admin/qualifications/educations", |
||
| 42 | * tags={"Administration"}, |
||
| 43 | * consumes={"application/json"}, |
||
| 44 | * summary="Delete the Admin - Qualifications Education.", |
||
| 45 | * @SWG\Response(response="200", description="Success", |
||
| 46 | * @SWG\Schema( |
||
| 47 | * title="data", |
||
| 48 | * type="object", |
||
| 49 | * required={"message", "status_code"}, |
||
| 50 | * @SWG\Property(property="message", type="string", default="Record successfully deleted.", description="Status message from server"), |
||
| 51 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 52 | * ) |
||
| 53 | * ), |
||
| 54 | * @SWG\Response(response="400", description="Token not provided", |
||
| 55 | * @SWG\Schema( |
||
| 56 | * title="data", |
||
| 57 | * type="object", |
||
| 58 | * required={"message", "status_code", "debug"}, |
||
| 59 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 60 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 61 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 62 | * ) |
||
| 63 | * ), |
||
| 64 | * @SWG\Response(response="422", description="Unable to delete record from the database.", |
||
| 65 | * @SWG\Schema( |
||
| 66 | * title="data", |
||
| 67 | * type="object", |
||
| 68 | * required={"message", "status_code"}, |
||
| 69 | * @SWG\Property(property="message", type="string", default="Unable to delete record from the database.", description="Status message from server"), |
||
| 70 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 71 | * ) |
||
| 72 | * ), |
||
| 73 | * @SWG\Parameter( |
||
| 74 | * name="id", |
||
| 75 | * in="formData", |
||
| 76 | * description="education level id to be deleted", |
||
| 77 | * required=true, |
||
| 78 | * type="integer", |
||
| 79 | * format="int64", |
||
| 80 | * default=1, |
||
| 81 | * ), |
||
| 82 | * @SWG\Parameter( |
||
| 83 | * name="Authorization", |
||
| 84 | * in="header", |
||
| 85 | * description="JWT Token", |
||
| 86 | * required=true, |
||
| 87 | * type="string", |
||
| 88 | * default="Bearer " |
||
| 89 | * ), |
||
| 90 | * ) |
||
| 91 | * |
||
| 92 | * @param EducationLevelRequest $request |
||
| 93 | * |
||
| 94 | * @return \Dingo\Api\Http\Response |
||
| 95 | * |
||
| 96 | * @author Bertrand Kintanar <[email protected]> |
||
| 97 | */ |
||
| 98 | 4 | public function destroy(EducationLevelRequest $request) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Retrieve the Admin - Qualifications Education. |
||
| 105 | * |
||
| 106 | * @SWG\Get( |
||
| 107 | * path="/admin/qualifications/educations", |
||
| 108 | * tags={"Administration"}, |
||
| 109 | * consumes={"application/json"}, |
||
| 110 | * summary="Save the Admin - Qualifications Education.", |
||
| 111 | * @SWG\Response(response="200", description="Success", |
||
| 112 | * @SWG\Schema( |
||
| 113 | * title="data", |
||
| 114 | * type="object", |
||
| 115 | * required={"data", "table", "message", "status_code"}, |
||
| 116 | * @SWG\Property(property="data", type="object", |
||
| 117 | * @SWG\Property(property="total", type="integer", default=22), |
||
| 118 | * @SWG\Property(property="per_page", type="integer", default=10), |
||
| 119 | * @SWG\Property(property="current_page", type="integer", default=1), |
||
| 120 | * @SWG\Property(property="last_page", type="integer", default=3), |
||
| 121 | * @SWG\Property(property="next_page_url", type="string", default="https://api.hris.dev/api/admin/qualifications/educations?page=2"), |
||
| 122 | * @SWG\Property(property="prev_page_url", type="string", default="null"), |
||
| 123 | * @SWG\Property(property="from", type="integer", default=1), |
||
| 124 | * @SWG\Property(property="to", type="integer", default=10), |
||
| 125 | * @SWG\Property(property="data", type="array", |
||
| 126 | * @SWG\Items(title="education_level", ref="#/definitions/EducationLevel"), |
||
| 127 | * ), |
||
| 128 | * ), |
||
| 129 | * @SWG\Property(property="table", type="object", |
||
| 130 | * @SWG\Property(property="title", type="string", default="Education Level"), |
||
| 131 | * @SWG\Property(property="permission", type="string", default="admin.qualifications.educations"), |
||
| 132 | * @SWG\Property(property="headers", type="array", |
||
| 133 | * @SWG\Items(title="Id", type="string", default="Id"), |
||
| 134 | * ), |
||
| 135 | * @SWG\Property(property="model", type="object", |
||
| 136 | * @SWG\Property(property="singular", type="string", default="education_level"), |
||
| 137 | * @SWG\Property(property="plural", type="string", default="education_levels"), |
||
| 138 | * @SWG\Property(property="dashed", type="string", default="education-levels"), |
||
| 139 | * ), |
||
| 140 | * @SWG\Property(property="data", type="array"), |
||
| 141 | * ), |
||
| 142 | * @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"), |
||
| 143 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 144 | * ) |
||
| 145 | * ), |
||
| 146 | * @SWG\Parameter( |
||
| 147 | * name="page", |
||
| 148 | * in="query", |
||
| 149 | * description="Page number for pagination", |
||
| 150 | * required=true, |
||
| 151 | * type="string", |
||
| 152 | * default="1" |
||
| 153 | * ), |
||
| 154 | * @SWG\Parameter( |
||
| 155 | * name="Authorization", |
||
| 156 | * in="header", |
||
| 157 | * description="JWT Token", |
||
| 158 | * required=true, |
||
| 159 | * type="string", |
||
| 160 | * default="Bearer " |
||
| 161 | * ), |
||
| 162 | * ) |
||
| 163 | * |
||
| 164 | * @param EducationLevelRequest $request |
||
| 165 | * |
||
| 166 | * @return \Dingo\Api\Http\Response |
||
| 167 | * |
||
| 168 | * @author Bertrand Kintanar <[email protected]> |
||
| 169 | */ |
||
| 170 | 2 | public function index(EducationLevelRequest $request) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Setup table for education level. |
||
| 179 | * |
||
| 180 | * @param $education_levels |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | * |
||
| 184 | * @author Bertrand Kintanar <[email protected]> |
||
| 185 | */ |
||
| 186 | 2 | protected function setupDataTable($education_levels) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Save the Admin - Qualifications Education. |
||
| 205 | * |
||
| 206 | * @SWG\Post( |
||
| 207 | * path="/admin/qualifications/educations", |
||
| 208 | * tags={"Administration"}, |
||
| 209 | * consumes={"application/json"}, |
||
| 210 | * summary="Save the Admin - Qualifications Education.", |
||
| 211 | * @SWG\Response(response="201", description="Success", |
||
| 212 | * @SWG\Schema( |
||
| 213 | * title="data", |
||
| 214 | * type="object", |
||
| 215 | * required={"education_level", "message", "status_code"}, |
||
| 216 | * @SWG\Property(property="education_level", ref="#/definitions/EducationLevel"), |
||
| 217 | * @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"), |
||
| 218 | * @SWG\Property(property="status_code", type="integer", default=201, description="Status code from server"), |
||
| 219 | * ) |
||
| 220 | * ), |
||
| 221 | * @SWG\Response(response="400", description="Token not provided", |
||
| 222 | * @SWG\Schema( |
||
| 223 | * title="data", |
||
| 224 | * type="object", |
||
| 225 | * required={"message", "status_code", "debug"}, |
||
| 226 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 227 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 228 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 229 | * ) |
||
| 230 | * ), |
||
| 231 | * @SWG\Response(response="422", description="Unable to add record to the database.", |
||
| 232 | * @SWG\Schema( |
||
| 233 | * title="data", |
||
| 234 | * type="object", |
||
| 235 | * required={"message", "status_code"}, |
||
| 236 | * @SWG\Property(property="message", type="string", default="Unable to add record to the database.", description="Status message from server"), |
||
| 237 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 238 | * ) |
||
| 239 | * ), |
||
| 240 | * @SWG\Parameter( |
||
| 241 | * name="education_level", |
||
| 242 | * in="body", |
||
| 243 | * required=true, |
||
| 244 | * @SWG\Property(ref="#/definitions/EducationLevel") |
||
| 245 | * ), |
||
| 246 | * @SWG\Parameter( |
||
| 247 | * name="Authorization", |
||
| 248 | * in="header", |
||
| 249 | * description="JWT Token", |
||
| 250 | * required=true, |
||
| 251 | * type="string", |
||
| 252 | * default="Bearer " |
||
| 253 | * ), |
||
| 254 | * ) |
||
| 255 | * |
||
| 256 | * @param EducationLevelRequest $request |
||
| 257 | * |
||
| 258 | * @return \Dingo\Api\Http\Response |
||
| 259 | * |
||
| 260 | * @author Bertrand Kintanar <[email protected]> |
||
| 261 | */ |
||
| 262 | 8 | public function store(EducationLevelRequest $request) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Get a single instance of Education Level. |
||
| 269 | * |
||
| 270 | * @SWG\Get( |
||
| 271 | * path="/admin/qualifications/educations/1", |
||
| 272 | * tags={"Administration"}, |
||
| 273 | * consumes={"application/json"}, |
||
| 274 | * summary="Get a single instance of Education Level.", |
||
| 275 | * @SWG\Response(response="200", description="Success", |
||
| 276 | * @SWG\Schema( |
||
| 277 | * title="data", |
||
| 278 | * type="object", |
||
| 279 | * required={"employment_status", "message", "status_code"}, |
||
| 280 | * @SWG\Property(property="employment_status", ref="#/definitions/EducationLevel"), |
||
| 281 | * @SWG\Property(property="message", type="string", default="Record successfully retrieved.", description="Status message from server"), |
||
| 282 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 283 | * ) |
||
| 284 | * ), |
||
| 285 | * @SWG\Response(response="400", description="Token not provided", |
||
| 286 | * @SWG\Schema( |
||
| 287 | * title="data", |
||
| 288 | * type="object", |
||
| 289 | * required={"message", "status_code", "debug"}, |
||
| 290 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 291 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 292 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 293 | * ) |
||
| 294 | * ), |
||
| 295 | * @SWG\Response(response="404", description="404 Not Found", |
||
| 296 | * @SWG\Schema( |
||
| 297 | * title="data", |
||
| 298 | * type="object", |
||
| 299 | * required={"message", "status_code"}, |
||
| 300 | * @SWG\Property(property="message", type="string", default="404 Not Found", description="Status message from server"), |
||
| 301 | * @SWG\Property(property="status_code", type="integer", default=404, description="Status code from server"), |
||
| 302 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 303 | * ) |
||
| 304 | * ), |
||
| 305 | * @SWG\Parameter( |
||
| 306 | * name="Authorization", |
||
| 307 | * in="header", |
||
| 308 | * description="JWT Token", |
||
| 309 | * required=true, |
||
| 310 | * type="string", |
||
| 311 | * default="Bearer " |
||
| 312 | * ), |
||
| 313 | * ) |
||
| 314 | * |
||
| 315 | * @param EducationLevel $education_level |
||
| 316 | * |
||
| 317 | * @return \Dingo\Api\Http\Response |
||
| 318 | * |
||
| 319 | * @author Bertrand Kintanar <[email protected]> |
||
| 320 | */ |
||
| 321 | 2 | public function show(EducationLevel $education_level) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Update the Admin - Qualifications Education. |
||
| 328 | * |
||
| 329 | * @SWG\Patch( |
||
| 330 | * path="/admin/qualifications/educations", |
||
| 331 | * tags={"Administration"}, |
||
| 332 | * consumes={"application/json"}, |
||
| 333 | * summary="Update the Admin - Qualifications Education.", |
||
| 334 | * @SWG\Response(response="200", description="Success", |
||
| 335 | * @SWG\Schema( |
||
| 336 | * title="data", |
||
| 337 | * type="object", |
||
| 338 | * required={"message", "status_code"}, |
||
| 339 | * @SWG\Property(property="message", type="string", default="Record successfully updated.", description="Status message from server"), |
||
| 340 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 341 | * ) |
||
| 342 | * ), |
||
| 343 | * @SWG\Response(response="400", description="Token not provided", |
||
| 344 | * @SWG\Schema( |
||
| 345 | * title="data", |
||
| 346 | * type="object", |
||
| 347 | * required={"message", "status_code", "debug"}, |
||
| 348 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 349 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 350 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 351 | * ) |
||
| 352 | * ), |
||
| 353 | * @SWG\Response(response="404", description="Unable to retrieve record from database.", |
||
| 354 | * @SWG\Schema( |
||
| 355 | * title="data", |
||
| 356 | * type="object", |
||
| 357 | * required={"message", "status_code", "debug"}, |
||
| 358 | * @SWG\Property(property="message", type="string", default="Unable to retrieve record from database.", description="Error message from server"), |
||
| 359 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 360 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 361 | * ) |
||
| 362 | * ), |
||
| 363 | * @SWG\Response(response="422", description="Unable to update record.", |
||
| 364 | * @SWG\Schema( |
||
| 365 | * title="data", |
||
| 366 | * type="object", |
||
| 367 | * required={"message", "status_code"}, |
||
| 368 | * @SWG\Property(property="message", type="string", default="Unable to update record.", description="Status message from server"), |
||
| 369 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 370 | * ) |
||
| 371 | * ), |
||
| 372 | * @SWG\Parameter( |
||
| 373 | * name="education_level", |
||
| 374 | * in="body", |
||
| 375 | * required=true, |
||
| 376 | * description="education level object that needs to be updated", |
||
| 377 | * @SWG\Property(ref="#/definitions/EducationLevel") |
||
| 378 | * ), |
||
| 379 | * @SWG\Parameter( |
||
| 380 | * name="Authorization", |
||
| 381 | * in="header", |
||
| 382 | * description="JWT Token", |
||
| 383 | * required=true, |
||
| 384 | * type="string", |
||
| 385 | * default="Bearer " |
||
| 386 | * ), |
||
| 387 | * ) |
||
| 388 | * |
||
| 389 | * @param EducationLevelRequest $request |
||
| 390 | * |
||
| 391 | * @return \Dingo\Api\Http\Response |
||
| 392 | * |
||
| 393 | * @author Bertrand Kintanar <[email protected]> |
||
| 394 | */ |
||
| 395 | 4 | public function update(EducationLevelRequest $request) |
|
| 399 | } |
||
| 400 |
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.