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 | class EmploymentStatusController extends BaseController |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var EmploymentStatus |
||
| 25 | */ |
||
| 26 | protected $employment_status; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param EmploymentStatus $employment_status |
||
| 30 | * |
||
| 31 | * @author Bertrand Kintanar <[email protected]> |
||
| 32 | */ |
||
| 33 | 14 | public function __construct(EmploymentStatus $employment_status) |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Delete the Admin - Employment Status. |
||
| 40 | * |
||
| 41 | * @SWG\Delete( |
||
| 42 | * path="/admin/job/employment-status", |
||
| 43 | * tags={"Administration"}, |
||
| 44 | * consumes={"application/json"}, |
||
| 45 | * summary="Delete the Admin - Employment Status.", |
||
| 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="Employment status 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 EmploymentStatusRequest $request |
||
| 94 | * |
||
| 95 | * @return \Illuminate\Http\RedirectResponse |
||
| 96 | * |
||
| 97 | * @author Bertrand Kintanar <[email protected]> |
||
| 98 | */ |
||
| 99 | 4 | public function destroy(EmploymentStatusRequest $request) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Retrieve the Admin - Employment Status. |
||
| 114 | * |
||
| 115 | * @SWG\Get( |
||
| 116 | * path="/admin/job/employment-status", |
||
| 117 | * tags={"Administration"}, |
||
| 118 | * consumes={"application/json"}, |
||
| 119 | * summary="Save the Admin - Employment Status.", |
||
| 120 | * @SWG\Response(response="200", description="Success", |
||
| 121 | * @SWG\Schema( |
||
| 122 | * title="data", |
||
| 123 | * type="object", |
||
| 124 | * required={"data", "table", "message", "status_code"}, |
||
| 125 | * @SWG\Property(property="data", type="object", |
||
| 126 | * @SWG\Property(property="total", type="integer", default=22), |
||
| 127 | * @SWG\Property(property="per_page", type="integer", default=10), |
||
| 128 | * @SWG\Property(property="current_page", type="integer", default=1), |
||
| 129 | * @SWG\Property(property="last_page", type="integer", default=3), |
||
| 130 | * @SWG\Property(property="next_page_url", type="string", default="https://api.hris.dev/api/admin/job/employment-status?page=2"), |
||
| 131 | * @SWG\Property(property="prev_page_url", type="string", default="null"), |
||
| 132 | * @SWG\Property(property="from", type="integer", default=1), |
||
| 133 | * @SWG\Property(property="to", type="integer", default=10), |
||
| 134 | * @SWG\Property(property="data", type="array", |
||
| 135 | * @SWG\Items(title="employment_status", ref="#/definitions/EmploymentStatus"), |
||
| 136 | * ), |
||
| 137 | * ), |
||
| 138 | * @SWG\Property(property="table", type="object", |
||
| 139 | * @SWG\Property(property="title", type="string", default="Employment Status"), |
||
| 140 | * @SWG\Property(property="permission", type="string", default="admin.job.employment-status"), |
||
| 141 | * @SWG\Property(property="headers", type="array", |
||
| 142 | * @SWG\Items(title="Id", type="string", default="Id"), |
||
| 143 | * ), |
||
| 144 | * @SWG\Property(property="model", type="object", |
||
| 145 | * @SWG\Property(property="singular", type="string", default="employment_status"), |
||
| 146 | * @SWG\Property(property="plural", type="string", default="employment_statuses"), |
||
| 147 | * @SWG\Property(property="dashed", type="string", default="employment-statuses"), |
||
| 148 | * ), |
||
| 149 | * @SWG\Property(property="data", type="array"), |
||
| 150 | * ), |
||
| 151 | * @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"), |
||
| 152 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 153 | * ) |
||
| 154 | * ), |
||
| 155 | * @SWG\Parameter( |
||
| 156 | * name="page", |
||
| 157 | * in="query", |
||
| 158 | * description="Page number for pagination", |
||
| 159 | * required=true, |
||
| 160 | * type="string", |
||
| 161 | * default="1" |
||
| 162 | * ), |
||
| 163 | * @SWG\Parameter( |
||
| 164 | * name="Authorization", |
||
| 165 | * in="header", |
||
| 166 | * description="JWT Token", |
||
| 167 | * required=true, |
||
| 168 | * type="string", |
||
| 169 | * default="Bearer " |
||
| 170 | * ), |
||
| 171 | * ) |
||
| 172 | * |
||
| 173 | * @param EmploymentStatusRequest $request |
||
| 174 | * |
||
| 175 | * @return \Illuminate\View\View |
||
| 176 | * |
||
| 177 | * @author Bertrand Kintanar <[email protected]> |
||
| 178 | */ |
||
| 179 | 2 | public function index(EmploymentStatusRequest $request) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Setup table for employment status. |
||
| 188 | * |
||
| 189 | * @param $employment_statuses |
||
| 190 | * |
||
| 191 | * @return array |
||
| 192 | * |
||
| 193 | * @author Bertrand Kintanar <[email protected]> |
||
| 194 | */ |
||
| 195 | 2 | protected function setupDataTable($employment_statuses) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Save the Admin - Employment Status. |
||
| 214 | * |
||
| 215 | * @SWG\Post( |
||
| 216 | * path="/admin/job/employment-status", |
||
| 217 | * tags={"Administration"}, |
||
| 218 | * consumes={"application/json"}, |
||
| 219 | * summary="Save the Admin - Employment Status.", |
||
| 220 | * @SWG\Response(response="201", description="Success", |
||
| 221 | * @SWG\Schema( |
||
| 222 | * title="data", |
||
| 223 | * type="object", |
||
| 224 | * required={"employment_status", "message", "status_code"}, |
||
| 225 | * @SWG\Property(property="employment_status", ref="#/definitions/EmploymentStatus"), |
||
| 226 | * @SWG\Property(property="message", type="string", default="Record successfully added.", description="Status message from server"), |
||
| 227 | * @SWG\Property(property="status_code", type="integer", default=201, description="Status code from server"), |
||
| 228 | * ) |
||
| 229 | * ), |
||
| 230 | * @SWG\Response(response="400", description="Token not provided", |
||
| 231 | * @SWG\Schema( |
||
| 232 | * title="data", |
||
| 233 | * type="object", |
||
| 234 | * required={"message", "status_code", "debug"}, |
||
| 235 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 236 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 237 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 238 | * ) |
||
| 239 | * ), |
||
| 240 | * @SWG\Response(response="422", description="Unable to add record to the database.", |
||
| 241 | * @SWG\Schema( |
||
| 242 | * title="data", |
||
| 243 | * type="object", |
||
| 244 | * required={"message", "status_code"}, |
||
| 245 | * @SWG\Property(property="message", type="string", default="Unable to add record to the database.", description="Status message from server"), |
||
| 246 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 247 | * ) |
||
| 248 | * ), |
||
| 249 | * @SWG\Parameter( |
||
| 250 | * name="employment_status", |
||
| 251 | * in="body", |
||
| 252 | * required=true, |
||
| 253 | * @SWG\Property(ref="#/definitions/EmploymentStatus") |
||
| 254 | * ), |
||
| 255 | * @SWG\Parameter( |
||
| 256 | * name="Authorization", |
||
| 257 | * in="header", |
||
| 258 | * description="JWT Token", |
||
| 259 | * required=true, |
||
| 260 | * type="string", |
||
| 261 | * default="Bearer " |
||
| 262 | * ), |
||
| 263 | * ) |
||
| 264 | * |
||
| 265 | * @param EmploymentStatusRequest $request |
||
| 266 | * |
||
| 267 | * @return \Illuminate\Http\RedirectResponse |
||
| 268 | * |
||
| 269 | * @author Bertrand Kintanar <[email protected]> |
||
| 270 | */ |
||
| 271 | 6 | View Code Duplication | public function store(EmploymentStatusRequest $request) |
| 281 | |||
| 282 | /** |
||
| 283 | * Update the Admin - Employment Status. |
||
| 284 | * |
||
| 285 | * @SWG\Patch( |
||
| 286 | * path="/admin/job/employment-status", |
||
| 287 | * tags={"Administration"}, |
||
| 288 | * consumes={"application/json"}, |
||
| 289 | * summary="Update the Admin - Employment Status.", |
||
| 290 | * @SWG\Response(response="200", description="Success", |
||
| 291 | * @SWG\Schema( |
||
| 292 | * title="data", |
||
| 293 | * type="object", |
||
| 294 | * required={"message", "status_code"}, |
||
| 295 | * @SWG\Property(property="message", type="string", default="Record successfully updated.", description="Status message from server"), |
||
| 296 | * @SWG\Property(property="status_code", type="integer", default=200, description="Status code from server"), |
||
| 297 | * ) |
||
| 298 | * ), |
||
| 299 | * @SWG\Response(response="400", description="Token not provided", |
||
| 300 | * @SWG\Schema( |
||
| 301 | * title="data", |
||
| 302 | * type="object", |
||
| 303 | * required={"message", "status_code", "debug"}, |
||
| 304 | * @SWG\Property(property="message", type="string", default="Token not provided", description="Error message from server"), |
||
| 305 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 306 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 307 | * ) |
||
| 308 | * ), |
||
| 309 | * @SWG\Response(response="404", description="Unable to retrieve record from database.", |
||
| 310 | * @SWG\Schema( |
||
| 311 | * title="data", |
||
| 312 | * type="object", |
||
| 313 | * required={"message", "status_code", "debug"}, |
||
| 314 | * @SWG\Property(property="message", type="string", default="Unable to retrieve record from database.", description="Error message from server"), |
||
| 315 | * @SWG\Property(property="status_code", type="integer", default=400, description="Status code from server"), |
||
| 316 | * @SWG\Property(property="debug", type="object", description="Debug back trace"), |
||
| 317 | * ) |
||
| 318 | * ), |
||
| 319 | * @SWG\Response(response="422", description="Unable to update record.", |
||
| 320 | * @SWG\Schema( |
||
| 321 | * title="data", |
||
| 322 | * type="object", |
||
| 323 | * required={"message", "status_code"}, |
||
| 324 | * @SWG\Property(property="message", type="string", default="Unable to update record.", description="Status message from server"), |
||
| 325 | * @SWG\Property(property="status_code", type="integer", default=422, description="Status code from server"), |
||
| 326 | * ) |
||
| 327 | * ), |
||
| 328 | * @SWG\Parameter( |
||
| 329 | * name="employment_status", |
||
| 330 | * in="body", |
||
| 331 | * required=true, |
||
| 332 | * description="employment status object that needs to be updated", |
||
| 333 | * @SWG\Property(ref="#/definitions/EmploymentStatus") |
||
| 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 EmploymentStatusRequest $request |
||
| 346 | * |
||
| 347 | * @return \Illuminate\Http\RedirectResponse |
||
| 348 | * |
||
| 349 | * @author Bertrand Kintanar <[email protected]> |
||
| 350 | */ |
||
| 351 | 4 | public function update(EmploymentStatusRequest $request) |
|
| 366 | } |
||
| 367 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: