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 |
||
| 33 | View Code Duplication | class JobTitlesController extends BaseController |
|
|
|
|||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var JobTitle |
||
| 37 | */ |
||
| 38 | protected $job_title; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param JobTitle $job_title |
||
| 42 | * |
||
| 43 | * @author Bertrand Kintanar <[email protected]> |
||
| 44 | */ |
||
| 45 | 14 | public function __construct(JobTitle $job_title) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Deletes a single instance of Job Title. |
||
| 52 | * |
||
| 53 | * @SWG\Delete( |
||
| 54 | * path="/admin/job/titles/{job_title}", |
||
| 55 | * description="This route provides the ability to delete a Job Title.", |
||
| 56 | * tags={"Administration"}, |
||
| 57 | * consumes={"application/json"}, |
||
| 58 | * summary="Deletes a single instance of Employment Status.", |
||
| 59 | * @SWG\Response(response="200", description="Success", |
||
| 60 | * @SWG\Schema( |
||
| 61 | * title="data", |
||
| 62 | * type="object", |
||
| 63 | * required={"message", "status_code"}, |
||
| 64 | * @SWG\Property(property="message", type="string", default="Record successfully deleted.", description="Status message from server"), |
||
| 65 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 66 | * ) |
||
| 67 | * ), |
||
| 68 | * @SWG\Response(response="400", description="Token not provided", |
||
| 69 | * @SWG\Schema( |
||
| 70 | * title="data", |
||
| 71 | * type="object", |
||
| 72 | * required={"message", "status_code", "debug"}, |
||
| 73 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 74 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 75 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 76 | * ) |
||
| 77 | * ), |
||
| 78 | * @SWG\Response(response="422", description="422 Unprocessable Entity", |
||
| 79 | * @SWG\Schema( |
||
| 80 | * title="data", |
||
| 81 | * type="object", |
||
| 82 | * required={"message", "status_code"}, |
||
| 83 | * @SWG\Property(property="message", type="string", default="422 Unprocessable Entity", description="Status message from server"), |
||
| 84 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 85 | * ) |
||
| 86 | * ), |
||
| 87 | * @SWG\Parameter( |
||
| 88 | * name="job_title", |
||
| 89 | * in="path", |
||
| 90 | * description="Job title id to be deleted", |
||
| 91 | * required=true, |
||
| 92 | * type="integer", |
||
| 93 | * format="int64", |
||
| 94 | * default=1, |
||
| 95 | * ), |
||
| 96 | * @SWG\Parameter( |
||
| 97 | * name="Authorization", |
||
| 98 | * in="header", |
||
| 99 | * description="JWT Token", |
||
| 100 | * required=true, |
||
| 101 | * type="string", |
||
| 102 | * default="Bearer " |
||
| 103 | * ), |
||
| 104 | * ) |
||
| 105 | * |
||
| 106 | * @param JobTitle $job_title |
||
| 107 | * @param JobTitleRequest $request |
||
| 108 | * |
||
| 109 | * @return \Dingo\Api\Http\Response |
||
| 110 | * |
||
| 111 | * @author Bertrand Kintanar <[email protected]> |
||
| 112 | */ |
||
| 113 | 2 | public function destroy(JobTitle $job_title, JobTitleRequest $request) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Retrieves a paginate aware collection of Job Title. |
||
| 120 | * |
||
| 121 | * @SWG\Get( |
||
| 122 | * path="/admin/job/titles", |
||
| 123 | * description="This route provides the ability to retrieve a paginate aware collection of Job Titles.", |
||
| 124 | * tags={"Administration"}, |
||
| 125 | * consumes={"application/json"}, |
||
| 126 | * summary="Retrieves a paginate aware collection of Job Title.", |
||
| 127 | * @SWG\Response(response="200", description="Success", |
||
| 128 | * @SWG\Schema( |
||
| 129 | * title="data", |
||
| 130 | * type="object", |
||
| 131 | * required={"data", "table", "message", "status_code"}, |
||
| 132 | * @SWG\Property(property="data", type="object", |
||
| 133 | * @SWG\Property(property="total", type="integer", default=22), |
||
| 134 | * @SWG\Property(property="per_page", type="integer", default=10), |
||
| 135 | * @SWG\Property(property="current_page", type="integer", default=1), |
||
| 136 | * @SWG\Property(property="last_page", type="integer", default=3), |
||
| 137 | * @SWG\Property(property="next_page_url", type="string", default="https://api.hris.dev/api/admin/job/titles?page=2"), |
||
| 138 | * @SWG\Property(property="prev_page_url", type="string", default="null"), |
||
| 139 | * @SWG\Property(property="from", type="integer", default=1), |
||
| 140 | * @SWG\Property(property="to", type="integer", default=10), |
||
| 141 | * @SWG\Property(property="data", type="array", |
||
| 142 | * @SWG\Items(ref="#/definitions/JobTitle"), |
||
| 143 | * ), |
||
| 144 | * ), |
||
| 145 | * @SWG\Property(property="table", type="object", |
||
| 146 | * @SWG\Property(property="title", type="string", default="Job Title"), |
||
| 147 | * @SWG\Property(property="permission", type="string", default="admin.job.titles"), |
||
| 148 | * @SWG\Property(property="headers", type="array", |
||
| 149 | * @SWG\Items(title="Id", type="string", default="Id"), |
||
| 150 | * ), |
||
| 151 | * @SWG\Property(property="model", type="object", |
||
| 152 | * @SWG\Property(property="singular", type="string", default="job_title"), |
||
| 153 | * @SWG\Property(property="plural", type="string", default="job_titles"), |
||
| 154 | * @SWG\Property(property="dashed", type="string", default="job-titles"), |
||
| 155 | * ), |
||
| 156 | * @SWG\Property(property="data", type="array", |
||
| 157 | * @SWG\Items(ref="#/definitions/JobTitle"), |
||
| 158 | * ), |
||
| 159 | * ), |
||
| 160 | * @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"), |
||
| 161 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 162 | * ) |
||
| 163 | * ), |
||
| 164 | * @SWG\Parameter( |
||
| 165 | * name="page", |
||
| 166 | * in="query", |
||
| 167 | * description="Page number for pagination", |
||
| 168 | * required=true, |
||
| 169 | * type="string", |
||
| 170 | * default="1" |
||
| 171 | * ), |
||
| 172 | * @SWG\Parameter( |
||
| 173 | * name="Authorization", |
||
| 174 | * in="header", |
||
| 175 | * description="JWT Token", |
||
| 176 | * required=true, |
||
| 177 | * type="string", |
||
| 178 | * default="Bearer " |
||
| 179 | * ), |
||
| 180 | * ) |
||
| 181 | * |
||
| 182 | * @param JobTitleRequest $request |
||
| 183 | * |
||
| 184 | * @return \Dingo\Api\Http\Response |
||
| 185 | * |
||
| 186 | * @author Bertrand Kintanar <[email protected]> |
||
| 187 | */ |
||
| 188 | 2 | public function index(JobTitleRequest $request) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Setup table for job title. |
||
| 199 | * |
||
| 200 | * @param $job_titles |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | * |
||
| 204 | * @author Bertrand Kintanar <[email protected]> |
||
| 205 | */ |
||
| 206 | 2 | protected function setupDataTable($job_titles) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Stores a single instance of Job Title. |
||
| 225 | * |
||
| 226 | * @SWG\Post( |
||
| 227 | * path="/admin/job/titles", |
||
| 228 | * description="This route provides the ability to store a single instance of Job Title.", |
||
| 229 | * tags={"Administration"}, |
||
| 230 | * consumes={"application/json"}, |
||
| 231 | * summary="Stores a single instance of Employment Status.", |
||
| 232 | * @SWG\Response(response="201", description="Success", |
||
| 233 | * @SWG\Schema( |
||
| 234 | * title="data", |
||
| 235 | * type="object", |
||
| 236 | * required={"job_title", "message", "status_code"}, |
||
| 237 | * @SWG\Property(property="job_title", ref="#/definitions/JobTitle"), |
||
| 238 | * @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"), |
||
| 239 | * @SWG\Property(property="status_code", type="integer", default=201, description="Status code from server"), |
||
| 240 | * ) |
||
| 241 | * ), |
||
| 242 | * @SWG\Response(response="400", description="Token not provided", |
||
| 243 | * @SWG\Schema( |
||
| 244 | * title="data", |
||
| 245 | * type="object", |
||
| 246 | * required={"message", "status_code", "debug"}, |
||
| 247 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 248 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 249 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 250 | * ) |
||
| 251 | * ), |
||
| 252 | * @SWG\Response(response="422", description="Unable to add record to the database.", |
||
| 253 | * @SWG\Schema( |
||
| 254 | * title="data", |
||
| 255 | * type="object", |
||
| 256 | * required={"message", "status_code"}, |
||
| 257 | * @SWG\Property(property="message", type="string", default="Unable to add record to the database.", description="Status message from server"), |
||
| 258 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 259 | * ) |
||
| 260 | * ), |
||
| 261 | * @SWG\Parameter( |
||
| 262 | * name="job_title", |
||
| 263 | * in="body", |
||
| 264 | * required=true, |
||
| 265 | * @SWG\Property(ref="#/definitions/JobTitle") |
||
| 266 | * ), |
||
| 267 | * @SWG\Parameter( |
||
| 268 | * name="Authorization", |
||
| 269 | * in="header", |
||
| 270 | * description="JWT Token", |
||
| 271 | * required=true, |
||
| 272 | * type="string", |
||
| 273 | * default="Bearer " |
||
| 274 | * ), |
||
| 275 | * ) |
||
| 276 | * |
||
| 277 | * @param JobTitleRequest $request |
||
| 278 | * |
||
| 279 | * @return \Dingo\Api\Http\Response |
||
| 280 | * |
||
| 281 | * @author Bertrand Kintanar <[email protected]> |
||
| 282 | */ |
||
| 283 | 6 | public function store(JobTitleRequest $request) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Retrieves a single instance of Job Title. |
||
| 290 | * |
||
| 291 | * @SWG\Get( |
||
| 292 | * path="/admin/job/titles/{job_title}", |
||
| 293 | * description="This route provides the ability to retrieve a single instance of Job Title.", |
||
| 294 | * tags={"Administration"}, |
||
| 295 | * consumes={"application/json"}, |
||
| 296 | * summary="Retrieves a single instance of Job Title.", |
||
| 297 | * @SWG\Response(response="200", description="Success", |
||
| 298 | * @SWG\Schema( |
||
| 299 | * title="data", |
||
| 300 | * type="object", |
||
| 301 | * required={"employment_status", "message", "status_code"}, |
||
| 302 | * @SWG\Property(property="employment_status", ref="#/definitions/JobTitle"), |
||
| 303 | * @SWG\Property(property="message", type="string", default="Record successfully retrieved.", description="Status message from server"), |
||
| 304 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 305 | * ) |
||
| 306 | * ), |
||
| 307 | * @SWG\Response(response="400", description="Token not provided", |
||
| 308 | * @SWG\Schema( |
||
| 309 | * title="data", |
||
| 310 | * type="object", |
||
| 311 | * required={"message", "status_code", "debug"}, |
||
| 312 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 313 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 314 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 315 | * ) |
||
| 316 | * ), |
||
| 317 | * @SWG\Response(response="422", description="422 Unprocessable Entity", |
||
| 318 | * @SWG\Schema( |
||
| 319 | * title="data", |
||
| 320 | * type="object", |
||
| 321 | * required={"message", "status_code"}, |
||
| 322 | * @SWG\Property(property="message", type="string", default="422 Unprocessable Entity", description="Status message from server"), |
||
| 323 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 324 | * ) |
||
| 325 | * ), |
||
| 326 | * @SWG\Parameter( |
||
| 327 | * name="job_title", |
||
| 328 | * in="path", |
||
| 329 | * description="Job title id to be retrieved.", |
||
| 330 | * required=true, |
||
| 331 | * type="integer", |
||
| 332 | * format="int64", |
||
| 333 | * default=1, |
||
| 334 | * ), |
||
| 335 | * @SWG\Parameter( |
||
| 336 | * name="Authorization", |
||
| 337 | * in="header", |
||
| 338 | * description="JWT Token", |
||
| 339 | * required=true, |
||
| 340 | * type="string", |
||
| 341 | * default="Bearer " |
||
| 342 | * ), |
||
| 343 | * ) |
||
| 344 | * |
||
| 345 | * @param JobTitle $job_title |
||
| 346 | * |
||
| 347 | * @return \Dingo\Api\Http\Response |
||
| 348 | * |
||
| 349 | * @author Bertrand Kintanar <[email protected]> |
||
| 350 | */ |
||
| 351 | 2 | public function show(JobTitle $job_title) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Updates a single instance of Job Title. |
||
| 358 | * |
||
| 359 | * @SWG\Patch( |
||
| 360 | * path="/admin/job/titles", |
||
| 361 | * description="This route provides the ability to update a single instance of Job Title.", |
||
| 362 | * tags={"Administration"}, |
||
| 363 | * consumes={"application/json"}, |
||
| 364 | * summary="Updates a single instance of Employment Status.", |
||
| 365 | * @SWG\Response(response="200", description="Success", |
||
| 366 | * @SWG\Schema( |
||
| 367 | * title="data", |
||
| 368 | * type="object", |
||
| 369 | * required={"message", "status_code"}, |
||
| 370 | * @SWG\Property(property="message", type="string", default="Record successfully updated.", description="Status message from server"), |
||
| 371 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 372 | * ) |
||
| 373 | * ), |
||
| 374 | * @SWG\Response(response="400", description="Token not provided", |
||
| 375 | * @SWG\Schema( |
||
| 376 | * title="data", |
||
| 377 | * type="object", |
||
| 378 | * required={"message", "status_code", "debug"}, |
||
| 379 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 380 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 381 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 382 | * ) |
||
| 383 | * ), |
||
| 384 | * @SWG\Response(response="404", description="Unable to retrieve record from database.", |
||
| 385 | * @SWG\Schema( |
||
| 386 | * title="data", |
||
| 387 | * type="object", |
||
| 388 | * required={"message", "status_code", "debug"}, |
||
| 389 | * @SWG\Property(property="message", type="string", default="Unable to retrieve record from database.", description="Error message from server"), |
||
| 390 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 391 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 392 | * ) |
||
| 393 | * ), |
||
| 394 | * @SWG\Response(response="422", description="Unable to update record.", |
||
| 395 | * @SWG\Schema( |
||
| 396 | * title="data", |
||
| 397 | * type="object", |
||
| 398 | * required={"message", "status_code"}, |
||
| 399 | * @SWG\Property(property="message", type="string", default="Unable to update record.", description="Status message from server"), |
||
| 400 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 401 | * ) |
||
| 402 | * ), |
||
| 403 | * @SWG\Parameter( |
||
| 404 | * name="job_title", |
||
| 405 | * in="body", |
||
| 406 | * required=true, |
||
| 407 | * description="job title object that needs to be updated", |
||
| 408 | * @SWG\Property(ref="#/definitions/JobTitle") |
||
| 409 | * ), |
||
| 410 | * @SWG\Parameter( |
||
| 411 | * name="Authorization", |
||
| 412 | * in="header", |
||
| 413 | * description="JWT Token", |
||
| 414 | * required=true, |
||
| 415 | * type="string", |
||
| 416 | * default="Bearer " |
||
| 417 | * ), |
||
| 418 | * ) |
||
| 419 | * |
||
| 420 | * @param JobTitleRequest $request |
||
| 421 | * |
||
| 422 | * @return \Dingo\Api\Http\Response |
||
| 423 | * |
||
| 424 | * @author Bertrand Kintanar <[email protected]> |
||
| 425 | */ |
||
| 426 | 4 | public function update(JobTitleRequest $request) |
|
| 430 | } |
||
| 431 |
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.