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 |
||
| 21 | View Code Duplication | class JobTitlesController extends BaseController |
|
|
|
|||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var JobTitle |
||
| 25 | */ |
||
| 26 | protected $job_title; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param JobTitle $job_title |
||
| 30 | * |
||
| 31 | * @author Bertrand Kintanar <[email protected]> |
||
| 32 | */ |
||
| 33 | 14 | public function __construct(JobTitle $job_title) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Delete the Admin - Job Title. |
||
| 40 | * |
||
| 41 | * @SWG\Delete( |
||
| 42 | * path="/admin/job/titles", |
||
| 43 | * tags={"Administration"}, |
||
| 44 | * consumes={"application/json"}, |
||
| 45 | * summary="Delete the Admin - Job Title.", |
||
| 46 | * @SWG\Response(response="200", description="Success", |
||
| 47 | * @SWG\Schema( |
||
| 48 | * title="data", |
||
| 49 | * type="object", |
||
| 50 | * required={"message", "status_code"}, |
||
| 51 | * @SWG\Property(property="message", type="string", default="Record successfully deleted.", description="Status message from server"), |
||
| 52 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 53 | * ) |
||
| 54 | * ), |
||
| 55 | * @SWG\Response(response="400", description="Token not provided", |
||
| 56 | * @SWG\Schema( |
||
| 57 | * title="data", |
||
| 58 | * type="object", |
||
| 59 | * required={"message", "status_code", "debug"}, |
||
| 60 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 61 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 62 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 63 | * ) |
||
| 64 | * ), |
||
| 65 | * @SWG\Response(response="422", description="Unable to delete record from the database.", |
||
| 66 | * @SWG\Schema( |
||
| 67 | * title="data", |
||
| 68 | * type="object", |
||
| 69 | * required={"message", "status_code"}, |
||
| 70 | * @SWG\Property(property="message", type="string", default="Unable to delete record from the database.", description="Status message from server"), |
||
| 71 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 72 | * ) |
||
| 73 | * ), |
||
| 74 | * @SWG\Parameter( |
||
| 75 | * name="id", |
||
| 76 | * in="formData", |
||
| 77 | * description="Job title id to be deleted", |
||
| 78 | * required=true, |
||
| 79 | * type="integer", |
||
| 80 | * format="int64", |
||
| 81 | * default=1, |
||
| 82 | * ), |
||
| 83 | * @SWG\Parameter( |
||
| 84 | * name="Authorization", |
||
| 85 | * in="header", |
||
| 86 | * description="JWT Token", |
||
| 87 | * required=true, |
||
| 88 | * type="string", |
||
| 89 | * default="Bearer " |
||
| 90 | * ), |
||
| 91 | * ) |
||
| 92 | * |
||
| 93 | * @param JobTitleRequest $request |
||
| 94 | * |
||
| 95 | * @return \Dingo\Api\Http\Response |
||
| 96 | * |
||
| 97 | * @author Bertrand Kintanar <[email protected]> |
||
| 98 | */ |
||
| 99 | 4 | public function destroy(JobTitleRequest $request) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Retrieve the Admin - Job Title. |
||
| 106 | * |
||
| 107 | * @SWG\Get( |
||
| 108 | * path="/admin/job/titles", |
||
| 109 | * tags={"Administration"}, |
||
| 110 | * consumes={"application/json"}, |
||
| 111 | * summary="Save the Admin - Job Title.", |
||
| 112 | * @SWG\Response(response="200", description="Success", |
||
| 113 | * @SWG\Schema( |
||
| 114 | * title="data", |
||
| 115 | * type="object", |
||
| 116 | * required={"data", "table", "message", "status_code"}, |
||
| 117 | * @SWG\Property(property="data", type="object", |
||
| 118 | * @SWG\Property(property="total", type="integer", default=22), |
||
| 119 | * @SWG\Property(property="per_page", type="integer", default=10), |
||
| 120 | * @SWG\Property(property="current_page", type="integer", default=1), |
||
| 121 | * @SWG\Property(property="last_page", type="integer", default=3), |
||
| 122 | * @SWG\Property(property="next_page_url", type="string", default="https://api.hris.dev/api/admin/job/titles?page=2"), |
||
| 123 | * @SWG\Property(property="prev_page_url", type="string", default="null"), |
||
| 124 | * @SWG\Property(property="from", type="integer", default=1), |
||
| 125 | * @SWG\Property(property="to", type="integer", default=10), |
||
| 126 | * @SWG\Property(property="data", type="array", |
||
| 127 | * @SWG\Items(title="job_title", ref="#/definitions/JobTitle"), |
||
| 128 | * ), |
||
| 129 | * ), |
||
| 130 | * @SWG\Property(property="table", type="object", |
||
| 131 | * @SWG\Property(property="title", type="string", default="Job Title"), |
||
| 132 | * @SWG\Property(property="permission", type="string", default="admin.job.titles"), |
||
| 133 | * @SWG\Property(property="headers", type="array", |
||
| 134 | * @SWG\Items(title="Id", type="string", default="Id"), |
||
| 135 | * ), |
||
| 136 | * @SWG\Property(property="model", type="object", |
||
| 137 | * @SWG\Property(property="singular", type="string", default="job_title"), |
||
| 138 | * @SWG\Property(property="plural", type="string", default="job_titles"), |
||
| 139 | * @SWG\Property(property="dashed", type="string", default="job-titles"), |
||
| 140 | * ), |
||
| 141 | * @SWG\Property(property="data", type="array"), |
||
| 142 | * ), |
||
| 143 | * @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"), |
||
| 144 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 145 | * ) |
||
| 146 | * ), |
||
| 147 | * @SWG\Parameter( |
||
| 148 | * name="page", |
||
| 149 | * in="query", |
||
| 150 | * description="Page number for pagination", |
||
| 151 | * required=true, |
||
| 152 | * type="string", |
||
| 153 | * default="1" |
||
| 154 | * ), |
||
| 155 | * @SWG\Parameter( |
||
| 156 | * name="Authorization", |
||
| 157 | * in="header", |
||
| 158 | * description="JWT Token", |
||
| 159 | * required=true, |
||
| 160 | * type="string", |
||
| 161 | * default="Bearer " |
||
| 162 | * ), |
||
| 163 | * ) |
||
| 164 | * |
||
| 165 | * @param JobTitleRequest $request |
||
| 166 | * |
||
| 167 | * @return \Dingo\Api\Http\Response |
||
| 168 | * |
||
| 169 | * @author Bertrand Kintanar <[email protected]> |
||
| 170 | */ |
||
| 171 | 2 | public function index(JobTitleRequest $request) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Setup table for job title. |
||
| 180 | * |
||
| 181 | * @param $job_titles |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | * |
||
| 185 | * @author Bertrand Kintanar <[email protected]> |
||
| 186 | */ |
||
| 187 | 2 | protected function setupDataTable($job_titles) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Save the Admin - Job Title. |
||
| 206 | * |
||
| 207 | * @SWG\Post( |
||
| 208 | * path="/admin/job/titles", |
||
| 209 | * tags={"Administration"}, |
||
| 210 | * consumes={"application/json"}, |
||
| 211 | * summary="Save the Admin - Job Title.", |
||
| 212 | * @SWG\Response(response="201", description="Success", |
||
| 213 | * @SWG\Schema( |
||
| 214 | * title="data", |
||
| 215 | * type="object", |
||
| 216 | * required={"job_title", "message", "status_code"}, |
||
| 217 | * @SWG\Property(property="job_title", ref="#/definitions/JobTitle"), |
||
| 218 | * @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"), |
||
| 219 | * @SWG\Property(property="status_code", type="integer", default=201, description="Status code from server"), |
||
| 220 | * ) |
||
| 221 | * ), |
||
| 222 | * @SWG\Response(response="400", description="Token not provided", |
||
| 223 | * @SWG\Schema( |
||
| 224 | * title="data", |
||
| 225 | * type="object", |
||
| 226 | * required={"message", "status_code", "debug"}, |
||
| 227 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 228 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 229 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 230 | * ) |
||
| 231 | * ), |
||
| 232 | * @SWG\Response(response="422", description="Unable to add record to the database.", |
||
| 233 | * @SWG\Schema( |
||
| 234 | * title="data", |
||
| 235 | * type="object", |
||
| 236 | * required={"message", "status_code"}, |
||
| 237 | * @SWG\Property(property="message", type="string", default="Unable to add record to the database.", description="Status message from server"), |
||
| 238 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 239 | * ) |
||
| 240 | * ), |
||
| 241 | * @SWG\Parameter( |
||
| 242 | * name="job_title", |
||
| 243 | * in="body", |
||
| 244 | * required=true, |
||
| 245 | * @SWG\Property(ref="#/definitions/JobTitle") |
||
| 246 | * ), |
||
| 247 | * @SWG\Parameter( |
||
| 248 | * name="Authorization", |
||
| 249 | * in="header", |
||
| 250 | * description="JWT Token", |
||
| 251 | * required=true, |
||
| 252 | * type="string", |
||
| 253 | * default="Bearer " |
||
| 254 | * ), |
||
| 255 | * ) |
||
| 256 | * |
||
| 257 | * @param JobTitleRequest $request |
||
| 258 | * |
||
| 259 | * @return \Dingo\Api\Http\Response |
||
| 260 | * |
||
| 261 | * @author Bertrand Kintanar <[email protected]> |
||
| 262 | */ |
||
| 263 | 6 | public function store(JobTitleRequest $request) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Update the Admin - Job Title. |
||
| 270 | * |
||
| 271 | * @SWG\Patch( |
||
| 272 | * path="/admin/job/titles", |
||
| 273 | * tags={"Administration"}, |
||
| 274 | * consumes={"application/json"}, |
||
| 275 | * summary="Update the Admin - Job Title.", |
||
| 276 | * @SWG\Response(response="200", description="Success", |
||
| 277 | * @SWG\Schema( |
||
| 278 | * title="data", |
||
| 279 | * type="object", |
||
| 280 | * required={"message", "status_code"}, |
||
| 281 | * @SWG\Property(property="message", type="string", default="Record successfully updated.", 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="Unable to retrieve record from database.", |
||
| 296 | * @SWG\Schema( |
||
| 297 | * title="data", |
||
| 298 | * type="object", |
||
| 299 | * required={"message", "status_code", "debug"}, |
||
| 300 | * @SWG\Property(property="message", type="string", default="Unable to retrieve record from database.", description="Error message from server"), |
||
| 301 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 302 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 303 | * ) |
||
| 304 | * ), |
||
| 305 | * @SWG\Response(response="422", description="Unable to update record.", |
||
| 306 | * @SWG\Schema( |
||
| 307 | * title="data", |
||
| 308 | * type="object", |
||
| 309 | * required={"message", "status_code"}, |
||
| 310 | * @SWG\Property(property="message", type="string", default="Unable to update record.", description="Status message from server"), |
||
| 311 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 312 | * ) |
||
| 313 | * ), |
||
| 314 | * @SWG\Parameter( |
||
| 315 | * name="job_title", |
||
| 316 | * in="body", |
||
| 317 | * required=true, |
||
| 318 | * description="job title object that needs to be updated", |
||
| 319 | * @SWG\Property(ref="#/definitions/JobTitle") |
||
| 320 | * ), |
||
| 321 | * @SWG\Parameter( |
||
| 322 | * name="Authorization", |
||
| 323 | * in="header", |
||
| 324 | * description="JWT Token", |
||
| 325 | * required=true, |
||
| 326 | * type="string", |
||
| 327 | * default="Bearer " |
||
| 328 | * ), |
||
| 329 | * ) |
||
| 330 | * |
||
| 331 | * @param JobTitleRequest $request |
||
| 332 | * |
||
| 333 | * @return \Dingo\Api\Http\Response |
||
| 334 | * |
||
| 335 | * @author Bertrand Kintanar <[email protected]> |
||
| 336 | */ |
||
| 337 | 4 | public function update(JobTitleRequest $request) |
|
| 341 | } |
||
| 342 |
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.