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 |
||
| 26 | class TrainingController extends Controller |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 30 | */ |
||
| 31 | public function index(Request $request, $trainingTypeID = null) |
||
| 53 | |||
| 54 | View Code Duplication | public function create() |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Store a newly created resource in storage. |
||
| 67 | * |
||
| 68 | * @param StoreTrainingRequest $request |
||
| 69 | * |
||
| 70 | * @return \Illuminate\Http\RedirectResponse |
||
| 71 | */ |
||
| 72 | public function store(StoreTrainingRequest $request) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Show the individual training record. |
||
| 92 | * |
||
| 93 | * @param $trainingId |
||
| 94 | * |
||
| 95 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 96 | */ |
||
| 97 | public function show($trainingId) |
||
| 114 | |||
| 115 | View Code Duplication | public function edit(Training $training) |
|
| 125 | |||
| 126 | View Code Duplication | public function update(Request $request, Training $training) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Remove the specified resource from storage. |
||
| 146 | * |
||
| 147 | * @param int $trainingId |
||
| 148 | */ |
||
| 149 | public function destroy($trainingId) |
||
| 154 | |||
| 155 | View Code Duplication | public function assignForm($trainingID) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Assign our users to training. |
||
| 168 | * |
||
| 169 | * @param AssignTrainingRequest $request |
||
| 170 | * @param int $trainingID |
||
| 171 | * |
||
| 172 | * @return \Illuminate\Http\RedirectResponse |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function assign(AssignTrainingRequest $request, $trainingID) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Provide the form data to the bulk update a training form. |
||
| 190 | * |
||
| 191 | * @param $trainingID |
||
| 192 | * |
||
| 193 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 194 | */ |
||
| 195 | public function updateForm($trainingID) |
||
| 196 | { |
||
| 197 | $this->authorize('edit'); |
||
| 198 | $training = Training::findOrFail($trainingID); |
||
| 199 | $users = User::skipSystem()->active()->orderBy('last_name')->get()->pluck('UserFullName', 'id')->toArray(); |
||
| 200 | |||
| 201 | $incompleteUsers = $training->users()->active()->whereNull('training_user.completed_date')->get()->pluck('UserFullName', 'id')->toArray(); |
||
| 202 | |||
| 203 | return view('training.bulk_update', compact('users', 'training', 'incompleteUsers')); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Bulk update a training. Useful when FSO provides a training and needs to update |
||
| 208 | * the training for all assigned users with the same completed date and attach the sign-in sheet. |
||
| 209 | * |
||
| 210 | * @param BulkUpdateTrainingRequest $request |
||
| 211 | * @param int $trainingID |
||
| 212 | * |
||
| 213 | * @return \Illuminate\Http\RedirectResponse |
||
| 214 | */ |
||
| 215 | public function bulkupdate(BulkUpdateTrainingRequest $request, $trainingID) |
||
| 216 | { |
||
| 217 | $this->authorize('edit'); |
||
| 218 | |||
| 219 | $data = $request->all(); |
||
| 220 | $data['training_id'] = $trainingID; |
||
| 221 | $data['author_id'] = Auth::user()->id; |
||
| 222 | |||
| 223 | if (array_key_exists('users', $data)) { |
||
| 224 | $users = $data['users']; |
||
| 225 | |||
| 226 | //update records for each unique |
||
| 227 | foreach (array_unique($users) as $user) { |
||
| 228 | $data['user_id'] = $user; |
||
| 229 | |||
| 230 | // Getting only one record as a collection, so have to use first() |
||
| 231 | // to extract out of the array. |
||
| 232 | $trainingUser = TrainingUser::whereTraining_id($trainingID) |
||
|
|
|||
| 233 | ->where('user_id', $user)->whereNull('completed_date')->get()->first(); |
||
| 234 | $trainingUser->update($data); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | $training = Training::findOrFail($trainingID); |
||
| 239 | if ($request->hasFile('files')) { |
||
| 240 | $encrypt = false; |
||
| 241 | if (array_key_exists('encrypt', $data)) { |
||
| 242 | $encrypt = $data['encrypt']; |
||
| 243 | } |
||
| 244 | |||
| 245 | $admin_only = false; |
||
| 246 | if (array_key_exists('admin_only', $data)) { |
||
| 247 | $admin_only = $data['admin_only']; |
||
| 248 | } |
||
| 249 | Attachment::upload($training, $request->file('files'), $encrypt, $admin_only); |
||
| 250 | } |
||
| 251 | |||
| 252 | Notification::container()->success('Training was updated for the users.'); |
||
| 253 | |||
| 254 | return redirect()->action('TrainingController@show', $trainingID); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Generate Excel file with user/training table with date of completion. |
||
| 259 | * |
||
| 260 | * Calls to \SET\app\Handlers\Excel\ |
||
| 261 | * |
||
| 262 | * @param CompletedTrainingExport $export |
||
| 263 | * |
||
| 264 | * @return mixed |
||
| 265 | */ |
||
| 266 | public function showCompleted(CompletedTrainingExport $export) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Send out a reminder that the training is due. |
||
| 275 | * |
||
| 276 | * @param $trainingUserId |
||
| 277 | * |
||
| 278 | * @return \Illuminate\Http\RedirectResponse |
||
| 279 | */ |
||
| 280 | public function sendReminder($trainingUserId) |
||
| 288 | |||
| 289 | /***************************/ |
||
| 290 | /* BEGIN PRIVATE FUNCTIONS */ |
||
| 291 | /***************************/ |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Get a list of users and create training notes from that list. |
||
| 295 | * |
||
| 296 | * @param $data |
||
| 297 | */ |
||
| 298 | private function createTrainingNotes($data) |
||
| 328 | } |
||
| 329 |
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.