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 |
||
15 | class EventVenueController extends Controller |
||
16 | { |
||
17 | /* Restrict the access to this resource just to logged in users except show view */ |
||
18 | public function __construct() |
||
22 | |||
23 | /***************************************************************************/ |
||
24 | |||
25 | /** |
||
26 | * Display a listing of the resource. |
||
27 | * |
||
28 | * @return \Illuminate\Http\Response |
||
29 | */ |
||
30 | View Code Duplication | public function index(Request $request) |
|
72 | |||
73 | /***************************************************************************/ |
||
74 | |||
75 | /** |
||
76 | * Show the form for creating a new resource. |
||
77 | * |
||
78 | * @return \Illuminate\Http\Response |
||
79 | */ |
||
80 | View Code Duplication | public function create() |
|
91 | |||
92 | /***************************************************************************/ |
||
93 | |||
94 | /** |
||
95 | * Store a newly created resource in storage. |
||
96 | * |
||
97 | * @param \Illuminate\Http\Request $request |
||
98 | * @return \Illuminate\Http\Response |
||
99 | */ |
||
100 | View Code Duplication | public function store(Request $request) |
|
116 | |||
117 | /***************************************************************************/ |
||
118 | |||
119 | /** |
||
120 | * Display the specified resource. |
||
121 | * |
||
122 | * @param \App\EventVenue $eventVenue |
||
123 | * @return \Illuminate\Http\Response |
||
124 | */ |
||
125 | public function show(EventVenue $eventVenue) |
||
134 | |||
135 | /***************************************************************************/ |
||
136 | |||
137 | /** |
||
138 | * Show the form for editing the specified resource. |
||
139 | * |
||
140 | * @param \App\EventVenue $eventVenue |
||
141 | * @return \Illuminate\Http\Response |
||
142 | */ |
||
143 | View Code Duplication | public function edit(EventVenue $eventVenue) |
|
158 | |||
159 | /***************************************************************************/ |
||
160 | |||
161 | /** |
||
162 | * Update the specified resource in storage. |
||
163 | * |
||
164 | * @param \Illuminate\Http\Request $request |
||
165 | * @param \App\EventVenue $eventVenue |
||
166 | * @return \Illuminate\Http\Response |
||
167 | */ |
||
168 | public function update(Request $request, EventVenue $eventVenue) |
||
182 | |||
183 | /***************************************************************************/ |
||
184 | |||
185 | /** |
||
186 | * Remove the specified resource from storage. |
||
187 | * |
||
188 | * @param \App\EventVenue $eventVenue |
||
189 | * @return \Illuminate\Http\Response |
||
190 | */ |
||
191 | public function destroy(EventVenue $eventVenue) |
||
198 | |||
199 | /***************************************************************************/ |
||
200 | |||
201 | /** |
||
202 | * Save the record on DB. |
||
203 | * |
||
204 | * @param \App\Teacher $teacher |
||
205 | * @return \Illuminate\Http\Response |
||
206 | */ |
||
207 | public function saveOnDb($request, $eventVenue) |
||
226 | |||
227 | /***************************************************************************/ |
||
228 | |||
229 | /** |
||
230 | * Open a modal in the event view when create teachers is clicked. |
||
231 | * |
||
232 | * @return view |
||
233 | */ |
||
234 | public function modal() |
||
240 | |||
241 | /***************************************************************************/ |
||
242 | |||
243 | /** |
||
244 | * Store a newly created teacher from the create event view modal in storage. |
||
245 | * |
||
246 | * @param \Illuminate\Http\Request $request |
||
247 | * @return \Illuminate\Http\Response |
||
248 | */ |
||
249 | View Code Duplication | public function storeFromModal(Request $request) |
|
261 | |||
262 | /***************************************************************************/ |
||
263 | |||
264 | /** |
||
265 | * Return the Venue validator with all the defined constraint. |
||
266 | * |
||
267 | * @return \Illuminate\Validation\Validator |
||
268 | */ |
||
269 | View Code Duplication | public function eventsVenueValidator($request) |
|
283 | } |
||
284 |
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.