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 PayGradesController extends BaseController |
|
|
|
|||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var PayGrade |
||
| 24 | */ |
||
| 25 | protected $pay_grade; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param PayGrade $pay_grade |
||
| 29 | * |
||
| 30 | * @author Bertrand Kintanar <[email protected]> |
||
| 31 | */ |
||
| 32 | 14 | public function __construct(PayGrade $pay_grade) |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Delete the Admin - Pay Grade. |
||
| 39 | * |
||
| 40 | * @SWG\Delete( |
||
| 41 | * path="/admin/job/pay-grades", |
||
| 42 | * tags={"Administration"}, |
||
| 43 | * consumes={"application/json"}, |
||
| 44 | * summary="Delete the Admin - Pay Grade.", |
||
| 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="Pay grade 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 PayGradeRequest $request |
||
| 93 | * |
||
| 94 | * @return \Dingo\Api\Http\Response |
||
| 95 | * |
||
| 96 | * @author Bertrand Kintanar <[email protected]> |
||
| 97 | */ |
||
| 98 | 4 | public function destroy(PayGradeRequest $request) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Retrieve the Admin - Pay Grade. |
||
| 105 | * |
||
| 106 | * @SWG\Get( |
||
| 107 | * path="/admin/job/pay-grades", |
||
| 108 | * tags={"Administration"}, |
||
| 109 | * consumes={"application/json"}, |
||
| 110 | * summary="Save the Admin - Pay Grade.", |
||
| 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/job/pay-grades?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="pay_grade", ref="#/definitions/PayGrade"), |
||
| 127 | * ), |
||
| 128 | * ), |
||
| 129 | * @SWG\Property(property="table", type="object", |
||
| 130 | * @SWG\Property(property="title", type="string", default="Pay Grade"), |
||
| 131 | * @SWG\Property(property="permission", type="string", default="admin.job.pay-grades"), |
||
| 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="pay_grade"), |
||
| 137 | * @SWG\Property(property="plural", type="string", default="pay_grades"), |
||
| 138 | * @SWG\Property(property="dashed", type="string", default="pay-grades"), |
||
| 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 PayGradeRequest $request |
||
| 165 | * |
||
| 166 | * @return \Dingo\Api\Http\Response |
||
| 167 | * |
||
| 168 | * @author Bertrand Kintanar <[email protected]> |
||
| 169 | */ |
||
| 170 | 2 | public function index(PayGradeRequest $request) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Setup table for pay grade. |
||
| 179 | * |
||
| 180 | * @param $pay_grades |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | * |
||
| 184 | * @author Bertrand Kintanar <[email protected]> |
||
| 185 | */ |
||
| 186 | 2 | protected function setupDataTable($pay_grades) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Save the Admin - Pay Grade. |
||
| 205 | * |
||
| 206 | * @SWG\Post( |
||
| 207 | * path="/admin/job/pay-grades", |
||
| 208 | * tags={"Administration"}, |
||
| 209 | * consumes={"application/json"}, |
||
| 210 | * summary="Save the Admin - Pay Grade.", |
||
| 211 | * @SWG\Response(response="201", description="Success", |
||
| 212 | * @SWG\Schema( |
||
| 213 | * title="data", |
||
| 214 | * type="object", |
||
| 215 | * required={"pay_grade", "message", "status_code"}, |
||
| 216 | * @SWG\Property(property="pay_grade", ref="#/definitions/PayGrade"), |
||
| 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="pay_grade", |
||
| 242 | * in="body", |
||
| 243 | * required=true, |
||
| 244 | * @SWG\Property(ref="#/definitions/PayGrade") |
||
| 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 PayGradeRequest $request |
||
| 257 | * |
||
| 258 | * @return \Dingo\Api\Http\Response |
||
| 259 | * |
||
| 260 | * @author Bertrand Kintanar <[email protected]> |
||
| 261 | */ |
||
| 262 | 6 | public function store(PayGradeRequest $request) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Update the Admin - Pay Grade. |
||
| 269 | * |
||
| 270 | * @SWG\Patch( |
||
| 271 | * path="/admin/job/pay-grades", |
||
| 272 | * tags={"Administration"}, |
||
| 273 | * consumes={"application/json"}, |
||
| 274 | * summary="Update the Admin - Pay Grade.", |
||
| 275 | * @SWG\Response(response="200", description="Success", |
||
| 276 | * @SWG\Schema( |
||
| 277 | * title="data", |
||
| 278 | * type="object", |
||
| 279 | * required={"message", "status_code"}, |
||
| 280 | * @SWG\Property(property="message", type="string", default="Record successfully updated.", description="Status message from server"), |
||
| 281 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 282 | * ) |
||
| 283 | * ), |
||
| 284 | * @SWG\Response(response="400", description="Token not provided", |
||
| 285 | * @SWG\Schema( |
||
| 286 | * title="data", |
||
| 287 | * type="object", |
||
| 288 | * required={"message", "status_code", "debug"}, |
||
| 289 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 290 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 291 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 292 | * ) |
||
| 293 | * ), |
||
| 294 | * @SWG\Response(response="404", description="Unable to retrieve record from database.", |
||
| 295 | * @SWG\Schema( |
||
| 296 | * title="data", |
||
| 297 | * type="object", |
||
| 298 | * required={"message", "status_code", "debug"}, |
||
| 299 | * @SWG\Property(property="message", type="string", default="Unable to retrieve record from database.", description="Error message from server"), |
||
| 300 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 301 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 302 | * ) |
||
| 303 | * ), |
||
| 304 | * @SWG\Response(response="422", description="Unable to update record.", |
||
| 305 | * @SWG\Schema( |
||
| 306 | * title="data", |
||
| 307 | * type="object", |
||
| 308 | * required={"message", "status_code"}, |
||
| 309 | * @SWG\Property(property="message", type="string", default="Unable to update record.", description="Status message from server"), |
||
| 310 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 311 | * ) |
||
| 312 | * ), |
||
| 313 | * @SWG\Parameter( |
||
| 314 | * name="pay_grade", |
||
| 315 | * in="body", |
||
| 316 | * required=true, |
||
| 317 | * description="pay grade object that needs to be updated", |
||
| 318 | * @SWG\Property(ref="#/definitions/PayGrade") |
||
| 319 | * ), |
||
| 320 | * @SWG\Parameter( |
||
| 321 | * name="Authorization", |
||
| 322 | * in="header", |
||
| 323 | * description="JWT Token", |
||
| 324 | * required=true, |
||
| 325 | * type="string", |
||
| 326 | * default="Bearer " |
||
| 327 | * ), |
||
| 328 | * ) |
||
| 329 | * |
||
| 330 | * @param PayGradeRequest $request |
||
| 331 | * |
||
| 332 | * @return \Dingo\Api\Http\Response |
||
| 333 | * |
||
| 334 | * @author Bertrand Kintanar <[email protected]> |
||
| 335 | */ |
||
| 336 | 4 | public function update(PayGradeRequest $request) |
|
| 340 | } |
||
| 341 |
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.