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 |
||
25 | class TrainingController extends Controller |
||
26 | { |
||
27 | /** |
||
28 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
29 | */ |
||
30 | public function index(Request $request, $trainingTypeID = null) |
||
31 | { |
||
32 | $this->authorize('view'); |
||
33 | |||
34 | $hasTrainingType = TrainingType::count() > 0; |
||
35 | $isTrainingType = (strpos($request->path(), '/trainingtype/')); |
||
36 | |||
37 | if ($trainingTypeID) { |
||
38 | $trainings = Training::with(['users' => function ($q) { |
||
39 | $q->active(); |
||
40 | }]) |
||
41 | ->where('training_type_id', $trainingTypeID) |
||
42 | ->get() |
||
43 | ->sortBy('name'); |
||
44 | } else { |
||
45 | $trainings = Training::with(['users' => function ($q) { |
||
46 | $q->active(); |
||
47 | }])->get()->sortBy('name'); |
||
48 | } |
||
49 | |||
50 | return view('training.index', compact('trainings', 'isTrainingType', 'hasTrainingType')); |
||
51 | } |
||
52 | |||
53 | View Code Duplication | public function create() |
|
63 | |||
64 | /** |
||
65 | * Store a newly created resource in storage. |
||
66 | * |
||
67 | * @param StoreTrainingRequest $request |
||
68 | * |
||
69 | * @return \Illuminate\Http\RedirectResponse |
||
70 | */ |
||
71 | View Code Duplication | public function store(StoreTrainingRequest $request) |
|
88 | |||
89 | /** |
||
90 | * Show the individual training record. |
||
91 | * |
||
92 | * @param $trainingId |
||
93 | * |
||
94 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
95 | */ |
||
96 | public function show($trainingId) |
||
113 | |||
114 | View Code Duplication | public function edit(Training $training) |
|
124 | |||
125 | View Code Duplication | public function update(Request $request, Training $training) |
|
142 | |||
143 | /** |
||
144 | * Remove the specified resource from storage. |
||
145 | * |
||
146 | * @param int $trainingId |
||
147 | */ |
||
148 | public function destroy($trainingId) |
||
153 | |||
154 | View Code Duplication | public function assignForm($trainingID) |
|
164 | |||
165 | /** |
||
166 | * Assign our users to training. |
||
167 | * |
||
168 | * @param AssignTrainingRequest $request |
||
169 | * @param int $trainingID |
||
170 | * |
||
171 | * @return \Illuminate\Http\RedirectResponse |
||
172 | */ |
||
173 | View Code Duplication | public function assign(AssignTrainingRequest $request, $trainingID) |
|
186 | |||
187 | /** |
||
188 | * Provide the form data to the bulk update a training form. |
||
189 | * |
||
190 | * @param $trainingID |
||
191 | * |
||
192 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
193 | */ |
||
194 | View Code Duplication | public function updateForm($trainingID) |
|
203 | |||
204 | /** |
||
205 | * Bulk update a training. Useful when FSO provides a training and needs to update |
||
206 | * the training for all assigned users with the same completed date and attach the sign-in sheet. |
||
207 | * |
||
208 | * @param AssignTrainingRequest $request |
||
209 | * @param int $trainingID |
||
210 | * |
||
211 | * @return \Illuminate\Http\RedirectResponse |
||
212 | */ |
||
213 | public function bulkupdate(Request $request, $trainingID) |
||
255 | |||
256 | /** |
||
257 | * Generate Excel file with user/training table with date of completion. |
||
258 | * |
||
259 | * Calls to \SET\app\Handlers\Excel\ |
||
260 | * |
||
261 | * @param CompletedTrainingExport $export |
||
262 | * |
||
263 | * @return mixed |
||
264 | */ |
||
265 | public function showCompleted(CompletedTrainingExport $export) |
||
271 | |||
272 | /** |
||
273 | * Send out a reminder that the training is due. |
||
274 | * |
||
275 | * @param $trainingUserId |
||
276 | * |
||
277 | * @return \Illuminate\Http\RedirectResponse |
||
278 | */ |
||
279 | public function sendReminder($trainingUserId) |
||
287 | |||
288 | /***************************/ |
||
289 | /* BEGIN PRIVATE FUNCTIONS */ |
||
290 | /***************************/ |
||
291 | |||
292 | /** |
||
293 | * Get a list of users and create training notes from that list. |
||
294 | * |
||
295 | * @param $data |
||
296 | */ |
||
297 | private function createTrainingNotes($data) |
||
327 | } |
||
328 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.