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 | 2 | public function index(Request $request, $trainingTypeID = null) |
|
52 | |||
53 | 1 | 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 | 1 | View Code Duplication | public function store(StoreTrainingRequest $request) |
72 | { |
||
73 | 1 | $data = $request->all(); |
|
74 | 1 | $training = Training::create($data); |
|
75 | |||
76 | 1 | if ($request->hasFile('files')) { |
|
77 | Attachment::upload($training, $request->file('files')); |
||
78 | } |
||
79 | |||
80 | 1 | $data['training_id'] = $training->id; |
|
81 | |||
82 | 1 | $this->createTrainingNotes($data); |
|
83 | |||
84 | 1 | Notification::container()->success('Training Created'); |
|
85 | |||
86 | 1 | return redirect()->action('TrainingController@index'); |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Show the individual training record. |
||
91 | * |
||
92 | * @param $trainingId |
||
93 | * |
||
94 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
95 | */ |
||
96 | 1 | public function show($trainingId) |
|
113 | |||
114 | 1 | View Code Duplication | public function edit(Training $training) |
124 | |||
125 | 1 | 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 | 1 | public function destroy($trainingId) |
|
153 | |||
154 | 1 | 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 | 1 | View Code Duplication | public function assign(AssignTrainingRequest $request, $trainingID) |
186 | |||
187 | /** |
||
188 | * Generate Excel file with user/training table with date of completion. |
||
189 | * |
||
190 | * Calls to \SET\app\Handlers\Excel\ |
||
191 | * |
||
192 | * @param CompletedTrainingExport $export |
||
193 | * |
||
194 | * @return mixed |
||
195 | */ |
||
196 | public function showCompleted(CompletedTrainingExport $export) |
||
202 | |||
203 | /** |
||
204 | * Send out a reminder that the training is due. |
||
205 | * |
||
206 | * @param $trainingUserId |
||
207 | * |
||
208 | * @return \Illuminate\Http\RedirectResponse |
||
209 | */ |
||
210 | 1 | public function sendReminder($trainingUserId) |
|
218 | |||
219 | /***************************/ |
||
220 | /* BEGIN PRIVATE FUNCTIONS */ |
||
221 | /***************************/ |
||
222 | |||
223 | /** |
||
224 | * Get a list of users and create training notes from that list. |
||
225 | * |
||
226 | * @param $data |
||
227 | */ |
||
228 | 3 | private function createTrainingNotes($data) |
|
258 | } |
||
259 |