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 |
||
20 | class UserController extends Controller |
||
21 | { |
||
22 | /** |
||
23 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
24 | */ |
||
25 | public function index() |
||
26 | { |
||
27 | $this->authorize('view'); |
||
28 | |||
29 | $users = User::with([ |
||
30 | 'assignedTrainings' => function ($q) { |
||
31 | $q->whereNull('completed_date') |
||
32 | ->whereBetween('due_date', [Carbon::now()->subYear(), Carbon::now()->addWeeks(4)]); |
||
33 | }, |
||
34 | 'trainings', |
||
35 | ]) |
||
36 | ->skipSystem() |
||
37 | ->orderBy('last_name')->get(); |
||
38 | |||
39 | return view('user.index', compact('users')); |
||
40 | } |
||
41 | |||
42 | View Code Duplication | public function create() |
|
43 | { |
||
44 | $this->authorize('edit'); |
||
45 | |||
46 | $supervisors = User::skipSystem()->active()->orderBy('last_name')->get()->pluck('userFullName', 'id')->toArray(); |
||
47 | $groups = Group::all(); |
||
48 | |||
49 | return view('user.create', compact('supervisors', 'groups')); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param StoreUserRequest $request |
||
54 | * |
||
55 | * @return \Illuminate\Http\RedirectResponse |
||
56 | */ |
||
57 | public function store(StoreUserRequest $request) |
||
70 | |||
71 | /** |
||
72 | * @param $userId |
||
73 | * |
||
74 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
75 | */ |
||
76 | public function show($userId) |
||
77 | { |
||
78 | $user = User::with(['subordinates' => function ($query) { |
||
79 | $query->active(); |
||
80 | }, |
||
81 | 'groups', 'duties', 'attachments', |
||
82 | 'visits', 'notes.author', 'notes.attachments', |
||
83 | 'travels.author', 'travels.attachments', ]) |
||
84 | ->findOrFail($userId); |
||
85 | |||
86 | //Make sure the user can't access other people's pages. |
||
87 | $this->authorize('show_user', $user); |
||
88 | |||
89 | $user['clearance'] = $this->spellOutClearance($user['clearance']); |
||
90 | $user['access_level'] = $this->spellOutClearance($user['access_level']); |
||
91 | |||
92 | $trainings = $user->assignedTrainings()->with('author', 'training.attachments', 'attachments')->orderBy('completed_date', 'DESC')->get(); |
||
93 | |||
94 | $user_training_types = $this->getUserTrainingTypes($trainings); |
||
95 | $training_user_types = $user_training_types[0]; // List of the user's training types |
||
96 | $training_blocks = $user_training_types[1]; // List of training block titles for user |
||
97 | |||
98 | $activityLog = []; |
||
99 | if (Gate::allows('view')) { |
||
100 | $activityLog = $user->getUserLog($user); |
||
101 | } |
||
102 | |||
103 | $this->previousAndNextUsers($user, $previous, $next); |
||
104 | |||
105 | //This mess is just so that we can output the Security Check list or show none. Mainly just to show none. |
||
106 | $duties = Duty::whereHas('users', function ($q) use ($userId) { |
||
107 | $q->where('id', $userId); |
||
108 | })->orWhereHas('groups.users', function ($q) use ($userId) { |
||
109 | $q->where('id', $userId); |
||
110 | })->get(); |
||
111 | |||
112 | return view('user.show', compact('user', 'duties', 'previous', 'next', |
||
113 | 'trainings', 'activityLog', 'training_blocks', 'training_user_types')); |
||
114 | } |
||
115 | |||
116 | View Code Duplication | public function edit(User $user) |
|
117 | { |
||
118 | $this->authorize('edit'); |
||
119 | |||
120 | $supervisors = User::skipSystem()->active()->orderBy('last_name')->get()->pluck('userFullName', 'id')->toArray(); |
||
121 | $groups = Group::all(); |
||
122 | |||
123 | return view('user.edit', compact('user', 'supervisors', 'groups')); |
||
124 | } |
||
125 | |||
126 | public function update(User $user) |
||
127 | { |
||
128 | $this->authorize('edit'); |
||
129 | |||
130 | $data = Input::all(); |
||
131 | |||
132 | $data['destroyed_date'] = $user->getDestroyDate($data['status']); |
||
133 | |||
134 | $user->update($data); |
||
135 | |||
136 | //Handle user groups |
||
137 | if (!array_key_exists('groups', $data)) { |
||
138 | $data['groups'] = []; |
||
139 | } |
||
140 | $user->groups()->sync($data['groups']); |
||
141 | |||
142 | //Handled closed area access (MUST come AFTER syncing groups). |
||
143 | if (array_key_exists('access', $data)) { |
||
144 | foreach ($data['access'] as $group_id => $accessLevel) { |
||
145 | $user->groups()->updateExistingPivot($group_id, ['access' => $accessLevel]); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | return redirect()->action('UserController@show', $user->id); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param $userId |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function destroy($userId) |
||
166 | |||
167 | |||
168 | /** |
||
169 | * @param $trainings[] |
||
170 | * From the User's trainings, a list of the training types is determined and |
||
171 | * a list of the training block titles is determined. |
||
172 | * @return user_training_types[], training_block_titles[] |
||
|
|||
173 | */ |
||
174 | public function getUserTrainingTypes($trainings=[]) |
||
193 | |||
194 | /** |
||
195 | * Process our JPAS import. Once that has been handled, we pass the file, changes, |
||
196 | * unique/unmapped users & a user list to the user.import view. |
||
197 | * That way we keep all this data for the resolve phase. |
||
198 | * |
||
199 | * @param JpasImport $import |
||
200 | * |
||
201 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
202 | */ |
||
203 | public function import(JpasImport $import) |
||
215 | |||
216 | /** |
||
217 | * @param JpasImport $import |
||
218 | * |
||
219 | * @return \Illuminate\Http\RedirectResponse |
||
220 | */ |
||
221 | public function resolveImport(JpasImport $import) |
||
231 | |||
232 | /** |
||
233 | * Generate the grab the previous and next user if our users are sorted alphabetically. |
||
234 | * |
||
235 | * @param $user |
||
236 | * @param $previous |
||
237 | * @param $next |
||
238 | */ |
||
239 | private function previousAndNextUsers($user, &$previous, &$next) |
||
255 | |||
256 | /** |
||
257 | * @param $clearance |
||
258 | * |
||
259 | * @return mixed |
||
260 | */ |
||
261 | private function spellOutClearance($clearance) |
||
278 | } |
||
279 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.