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 |
||
16 | class EventController extends Controller |
||
17 | { |
||
18 | //------------------------------------------------- |
||
19 | // Events |
||
20 | public function index() |
||
21 | { |
||
22 | $events = Event::orderBy('start_date', 'desc')->get(); |
||
23 | |||
24 | return view('events.index', [ |
||
25 | 'events' => $events, |
||
26 | ]); |
||
27 | } |
||
28 | |||
29 | View Code Duplication | public function show($id) |
|
|
|||
30 | { |
||
31 | $event = Event::whereId($id)->first(); |
||
32 | $reg = EventUserRegistered::whereEventId($id)->where('user_id', '=', \Auth::id())->get(); |
||
33 | |||
34 | return view('events.show', [ |
||
35 | 'event' => $event, |
||
36 | 'reg_user' => $reg, |
||
37 | ]); |
||
38 | } |
||
39 | |||
40 | public function create() |
||
41 | { |
||
42 | return view('events.create'); |
||
43 | } |
||
44 | |||
45 | public function store(Request $request) |
||
46 | { |
||
47 | $this->validate($request, [ |
||
48 | 'title' => 'required', |
||
49 | 'desc' => 'required', |
||
50 | 'start' => 'required|date', |
||
51 | 'end' => 'required|date', |
||
52 | 'slots' => 'required|numeric', |
||
53 | 'reg_start_date' => 'date', |
||
54 | 'reg_end_date' => 'date', |
||
55 | 'price' => 'numeric', |
||
56 | ]); |
||
57 | |||
58 | $e = new Event(); |
||
59 | $e->title = $request->get('title'); |
||
60 | $e->description = $request->get('desc'); |
||
61 | $e->start_date = $request->get('start'); |
||
62 | $e->end_date = $request->get('end'); |
||
63 | $e->user_id = \Auth::id(); |
||
64 | $e->save(); |
||
65 | |||
66 | $es = new EventSetting(); |
||
67 | $es->event_id = $e->id; |
||
68 | $es->slots = $request->get('slots'); |
||
69 | $es->reg_price = $request->get('price'); |
||
70 | $es->reg_start_date = $request->get('reg_start'); |
||
71 | $es->reg_end_date = $request->get('reg_end'); |
||
72 | if ($request->get('reg_allowed') == 'on') { |
||
73 | $es->reg_allowed = 1; |
||
74 | } else { |
||
75 | $es->reg_allowed = 0; |
||
76 | } |
||
77 | $es->save(); |
||
78 | |||
79 | return redirect()->action('EventController@show', [$e->id]); |
||
80 | } |
||
81 | |||
82 | public function edit($id) |
||
90 | |||
91 | public function update($id) |
||
114 | |||
115 | View Code Duplication | public function register($eventid) |
|
125 | |||
126 | public function register_store($eventid) |
||
127 | { |
||
128 | $event = Event::whereId($eventid)->first(); |
||
129 | |||
130 | if ($event->settings->reg_allowed == 1 && $event->settings->slots > $event->users_registered->count()) { |
||
131 | if (EventUserRegistered::whereEventId($eventid)->where('user_id', '=', \Auth::id())->count() == 0) { |
||
132 | EventUserRegistered::firstOrNew([ |
||
133 | 'event_id' => $eventid, |
||
134 | 'user_id' => \Auth::id(), |
||
135 | 'reg_price_payed' => 0, |
||
136 | 'reg_state' => 0, |
||
137 | ])->save(); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | return redirect()->action('EventController@show', $eventid); |
||
142 | } |
||
143 | |||
144 | //------------------------------------------------- |
||
145 | // Meetings |
||
146 | public function meeting_index($eventid) |
||
149 | |||
150 | public function meeting_show($id) |
||
153 | |||
154 | public function meeting_create() |
||
157 | |||
158 | public function meeting_store() |
||
161 | |||
162 | public function meeting_edit($id) |
||
165 | |||
166 | public function meeting_update($id) |
||
169 | |||
170 | //------------------------------------------------- |
||
171 | // Pictures |
||
172 | public function picture_index() |
||
175 | |||
176 | public function picture_show($id) |
||
179 | |||
180 | public function picture_create() |
||
183 | |||
184 | public function picture_store() |
||
187 | |||
188 | public function picture_edit($id) |
||
191 | |||
192 | public function picture_update($id) |
||
195 | } |
||
196 |
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.