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 |
||
| 9 | class GuestController extends Controller |
||
| 10 | { |
||
| 11 | 5 | private function getRouteName() |
|
| 12 | { |
||
| 13 | 5 | return 'guest'; |
|
| 14 | } |
||
| 15 | |||
| 16 | // TODO |
||
| 17 | 4 | View Code Duplication | public function index() |
|
|
|||
| 18 | { |
||
| 19 | 4 | $title = trans('general.guests'); |
|
| 20 | |||
| 21 | 4 | $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact') |
|
| 22 | 4 | ->paginate($this->getItemsPerPage()); |
|
| 23 | |||
| 24 | $viewData = [ |
||
| 25 | 4 | 'columns' => $this->getColumns(), |
|
| 26 | 4 | 'dataset' => $dataset, |
|
| 27 | 4 | 'routeName' => $this->getRouteName(), |
|
| 28 | 4 | 'title' => $title, |
|
| 29 | 4 | 'deleteMessage' => mb_strtolower(trans('general.guest')).' '.mb_strtolower(trans('general.number')), |
|
| 30 | ]; |
||
| 31 | |||
| 32 | 4 | return view('list', $viewData); |
|
| 33 | } |
||
| 34 | |||
| 35 | // TODO |
||
| 36 | View Code Duplication | public function store(GuestRequest $request, $id = null) |
|
| 60 | |||
| 61 | 1 | View Code Duplication | public function delete($id) |
| 62 | { |
||
| 63 | 1 | Guest::destroy($id); |
|
| 64 | 1 | $data = ['class' => 'alert-success', 'message' => trans('general.deleted')]; |
|
| 65 | |||
| 66 | 1 | return response()->json($data); |
|
| 67 | } |
||
| 68 | |||
| 69 | // TODO |
||
| 70 | 3 | View Code Duplication | public function showAddEditForm($id = null) |
| 71 | { |
||
| 72 | 3 | if ($id === null) { |
|
| 73 | 1 | $dataset = new Guest(); |
|
| 74 | 1 | $title = trans('general.add'); |
|
| 75 | 1 | $submitRoute = route($this->getRouteName().'.postadd'); |
|
| 76 | } else { |
||
| 77 | try { |
||
| 78 | 2 | $dataset = Guest::select('id', 'first_name', 'last_name', 'address', 'zip_code', 'place', 'PESEL', 'contact')->findOrFail($id); |
|
| 79 | 1 | } catch (ModelNotFoundException $e) { |
|
| 80 | 1 | return Controller::returnBack([ |
|
| 81 | 1 | 'message' => trans('general.object_not_found'), |
|
| 82 | 1 | 'alert-class' => 'alert-danger', |
|
| 83 | ]); |
||
| 84 | } |
||
| 85 | |||
| 86 | 1 | $title = trans('general.edit'); |
|
| 87 | 1 | $submitRoute = route($this->getRouteName().'.postedit', $id); |
|
| 88 | } |
||
| 89 | |||
| 90 | 2 | $title .= ' '.mb_strtolower(trans('general.guest')); |
|
| 91 | |||
| 92 | $viewData = [ |
||
| 93 | 2 | 'dataset' => $dataset, |
|
| 94 | 2 | 'fields' => $this->getFields(), |
|
| 95 | 2 | 'title' => $title, |
|
| 96 | 2 | 'submitRoute' => $submitRoute, |
|
| 97 | 2 | 'routeName' => $this->getRouteName(), |
|
| 98 | ]; |
||
| 99 | |||
| 100 | 2 | return view('addedit', $viewData); |
|
| 101 | } |
||
| 102 | |||
| 103 | // TODO |
||
| 104 | 2 | private function getFields() |
|
| 181 | |||
| 182 | // TODO |
||
| 183 | 4 | private function getColumns() |
|
| 232 | } |
||
| 233 |
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.