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 |
||
29 | class ProjectController extends Controller |
||
30 | { |
||
31 | /** |
||
32 | * Display activity for a project. |
||
33 | * |
||
34 | * @param Project $project |
||
35 | * |
||
36 | * @return \Illuminate\View\View |
||
37 | */ |
||
38 | 11 | public function getIndex(Project $project) |
|
39 | { |
||
40 | 11 | $activities = $project->activities() |
|
41 | 11 | ->with('activity', 'issue', 'user', 'assignTo', 'comment', 'note') |
|
42 | 11 | ->orderBy('created_at', 'DESC') |
|
43 | 11 | ->take(10) |
|
44 | 11 | ->get(); |
|
45 | |||
46 | 11 | return view('project.index', [ |
|
|
|||
47 | 11 | 'tabs' => $this->projectMainViewTabs($project, 'index'), |
|
48 | 11 | 'project' => $project, |
|
49 | 11 | 'active' => 'activity', |
|
50 | 11 | 'activities' => $activities, |
|
51 | 11 | 'sidebar' => 'project', |
|
52 | ]); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Display issues for a project. |
||
57 | * |
||
58 | * @param FilterForm $filterForm |
||
59 | * @param Request $request |
||
60 | * @param Project $project |
||
61 | * @param int $status |
||
62 | * |
||
63 | * @return \Illuminate\View\View |
||
64 | */ |
||
65 | 2 | public function getIssues(FilterForm $filterForm, Request $request, Project $project, $status = Issue::STATUS_OPEN) |
|
66 | { |
||
67 | 2 | $active = $status == Issue::STATUS_OPEN ? 'open_issue' : 'closed_issue'; |
|
68 | 2 | $issues = $project->listIssues($status, $request->all()); |
|
69 | |||
70 | 2 | return view('project.index', [ |
|
71 | 2 | 'tabs' => $this->projectMainViewTabs($project, 'issues', $issues, $status), |
|
72 | 2 | 'project' => $project, |
|
73 | 2 | 'active' => $active, |
|
74 | 2 | 'issues' => $issues, |
|
75 | 2 | 'sidebar' => 'project', |
|
76 | 2 | 'filterForm' => $filterForm, |
|
77 | ]); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Display issues assigned to current user for a project. |
||
82 | * |
||
83 | * @param Project $project |
||
84 | * |
||
85 | * @return \Illuminate\View\View |
||
86 | */ |
||
87 | 1 | public function getAssigned(Project $project) |
|
88 | { |
||
89 | 1 | $issues = $project->listAssignedIssues($this->auth->user()->id); |
|
90 | |||
91 | 1 | return view('project.index', [ |
|
92 | 1 | 'tabs' => $this->projectMainViewTabs($project, 'assigned', $issues), |
|
93 | 1 | 'project' => $project, |
|
94 | 1 | 'active' => 'issue_assigned_to_you', |
|
95 | 1 | 'issues' => $issues, |
|
96 | 1 | 'sidebar' => 'project', |
|
97 | ]); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Display notes for a project. |
||
102 | * |
||
103 | * @param Project $project |
||
104 | * @param NoteForm $form |
||
105 | * |
||
106 | * @return \Illuminate\View\View |
||
107 | */ |
||
108 | 7 | public function getNotes(Project $project, NoteForm $form) |
|
109 | { |
||
110 | 7 | $notes = $project->notes()->with('createdBy')->get(); |
|
111 | |||
112 | 7 | return view('project.index', [ |
|
113 | 7 | 'tabs' => $this->projectMainViewTabs($project, 'notes', $notes), |
|
114 | 7 | 'project' => $project, |
|
115 | 7 | 'active' => 'notes', |
|
116 | 7 | 'notes' => $notes, |
|
117 | 7 | 'sidebar' => 'project', |
|
118 | 7 | 'noteForm' => $form, |
|
119 | ]); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param Project $project |
||
124 | * @param $view |
||
125 | * @param null $data |
||
126 | * @param bool $status |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 21 | protected function projectMainViewTabs(Project $project, $view, $data = null, $status = false) |
|
131 | { |
||
132 | 21 | $notesCount = $view === 'note' ? $data->count() : $project->notes()->count(); |
|
133 | |||
134 | 21 | $assignedIssuesCount = 0; |
|
135 | 21 | if ($view !== 'assigned' && !$this->auth->guest()) { |
|
136 | 19 | $assignedIssuesCount = $this->auth->user()->assignedIssuesCount($project->id); |
|
137 | 2 | } elseif ($view === 'assigned') { |
|
138 | 1 | $assignedIssuesCount = $data->count(); |
|
139 | } |
||
140 | |||
141 | 21 | if ($view === 'issues') { |
|
142 | 2 | if ($status == Issue::STATUS_OPEN) { |
|
143 | 2 | $closedIssuesCount = $project->closedIssuesCount()->count(); |
|
144 | 2 | $openIssuesCount = $data->count(); |
|
145 | } else { |
||
146 | 1 | $closedIssuesCount = $data->count(); |
|
147 | 2 | $openIssuesCount = $project->openIssuesCount()->count(); |
|
148 | } |
||
149 | } else { |
||
150 | 19 | $openIssuesCount = $project->openIssuesCount()->count(); |
|
151 | 19 | $closedIssuesCount = $project->closedIssuesCount()->count(); |
|
152 | } |
||
153 | |||
154 | 21 | $tabs = []; |
|
155 | 21 | $tabs[] = [ |
|
156 | 21 | 'url' => $project->to(), |
|
157 | 21 | 'page' => 'activity', |
|
158 | ]; |
||
159 | 21 | $tabs[] = [ |
|
160 | 21 | 'url' => $project->to('issues'), |
|
161 | 21 | 'page' => 'open_issue', |
|
162 | 21 | 'prefix' => $openIssuesCount, |
|
163 | ]; |
||
164 | 21 | $tabs[] = [ |
|
165 | 21 | 'url' => $project->to('issues') . '/0', |
|
166 | 21 | 'page' => 'closed_issue', |
|
167 | 21 | 'prefix' => $closedIssuesCount, |
|
168 | ]; |
||
169 | 21 | if (!$this->auth->guest()) { |
|
170 | 20 | $tabs[] = [ |
|
171 | 20 | 'url' => $project->to('assigned'), |
|
172 | 20 | 'page' => 'issue_assigned_to_you', |
|
173 | 20 | 'prefix' => $assignedIssuesCount, |
|
174 | ]; |
||
175 | } |
||
176 | 21 | $tabs[] = [ |
|
177 | 21 | 'url' => $project->to('notes'), |
|
178 | 21 | 'page' => 'notes', |
|
179 | 21 | 'prefix' => $notesCount, |
|
180 | ]; |
||
181 | |||
182 | 21 | return $tabs; |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * Edit the project. |
||
187 | * |
||
188 | * @param Project $project |
||
189 | * @param Form $form |
||
190 | * |
||
191 | * @return \Illuminate\View\View |
||
192 | */ |
||
193 | 2 | public function getEdit(Project $project, Form $form) |
|
194 | { |
||
195 | 2 | return view('project.edit', [ |
|
196 | 2 | 'form' => $form, |
|
197 | 2 | 'project' => $project, |
|
198 | 2 | 'sidebar' => 'project', |
|
199 | ]); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * To update project details. |
||
204 | * |
||
205 | * @param Project $project |
||
206 | * @param FormRequest\Project $request |
||
207 | * |
||
208 | * @return \Illuminate\Http\RedirectResponse |
||
209 | */ |
||
210 | 2 | public function postEdit(Project $project, FormRequest\Project $request) |
|
211 | { |
||
212 | // Delete the project |
||
213 | 2 | if ($request->has('delete-project')) { |
|
214 | 1 | $project->delete(); |
|
215 | |||
216 | 1 | return redirect('projects') |
|
217 | 1 | ->with('notice', trans('tinyissue.project_has_been_deleted')); |
|
218 | } |
||
219 | |||
220 | 1 | $project->update($request->all()); |
|
221 | |||
222 | 1 | return redirect($project->to()) |
|
223 | 1 | ->with('notice', trans('tinyissue.project_has_been_updated')); |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Ajax: returns list of users that are not in the project. |
||
228 | * |
||
229 | * @param Project $project |
||
230 | * |
||
231 | * @return \Symfony\Component\HttpFoundation\Response |
||
232 | */ |
||
233 | 1 | public function getInactiveUsers(Project $project = null) |
|
234 | { |
||
235 | 1 | $users = $project->usersNotIn(); |
|
236 | |||
237 | 1 | return response()->json($users); |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * Ajax: add user to the project. |
||
242 | * |
||
243 | * @param Project $project |
||
244 | * @param Request $request |
||
245 | * |
||
246 | * @return \Symfony\Component\HttpFoundation\Response |
||
247 | */ |
||
248 | 1 | View Code Duplication | public function postAssign(Project $project, Request $request) |
249 | { |
||
250 | 1 | $status = false; |
|
251 | 1 | if ($request->has('user_id')) { |
|
252 | 1 | $project->assignUser((int) $request->input('user_id')); |
|
253 | 1 | $status = true; |
|
254 | } |
||
255 | |||
256 | 1 | return response()->json(['status' => $status]); |
|
257 | } |
||
258 | |||
259 | /** |
||
260 | * Ajax: remove user from the project. |
||
261 | * |
||
262 | * @param Project $project |
||
263 | * @param Request $request |
||
264 | * |
||
265 | * @return \Symfony\Component\HttpFoundation\Response |
||
266 | */ |
||
267 | 1 | View Code Duplication | public function postUnassign(Project $project, Request $request) |
268 | { |
||
269 | 1 | $status = false; |
|
270 | 1 | if ($request->has('user_id')) { |
|
271 | 1 | $project->unassignUser((int) $request->input('user_id')); |
|
272 | 1 | $status = true; |
|
273 | } |
||
274 | |||
275 | 1 | return response()->json(['status' => $status]); |
|
276 | } |
||
277 | |||
278 | /** |
||
279 | * To add a new note to the project. |
||
280 | * |
||
281 | * @param Project $project |
||
282 | * @param Note $note |
||
283 | * @param FormRequest\Note $request |
||
284 | * |
||
285 | * @return \Illuminate\Http\RedirectResponse |
||
286 | */ |
||
287 | 2 | public function postAddNote(Project $project, Note $note, FormRequest\Note $request) |
|
288 | { |
||
289 | 2 | $note->setRelation('project', $project); |
|
290 | 2 | $note->setRelation('createdBy', $this->auth->user()); |
|
291 | 2 | $note->createNote($request->all()); |
|
292 | |||
293 | 2 | return redirect($note->to())->with('notice', trans('tinyissue.your_note_added')); |
|
294 | } |
||
295 | |||
296 | /** |
||
297 | * Ajax: To update project note. |
||
298 | * |
||
299 | * @param Project $project |
||
300 | * @param Note $note |
||
301 | * @param Request $request |
||
302 | * |
||
303 | * @return \Symfony\Component\HttpFoundation\Response |
||
304 | */ |
||
305 | 1 | public function postEditNote(Project $project, Project\Note $note, Request $request) |
|
306 | { |
||
307 | 1 | $body = ''; |
|
308 | 1 | if ($request->has('body')) { |
|
309 | 1 | $note->setRelation('project', $project); |
|
310 | 1 | $note->body = $request->input('body'); |
|
311 | 1 | $note->save(); |
|
312 | 1 | $body = \Html::format($note->body); |
|
313 | } |
||
314 | |||
315 | 1 | return response()->json(['status' => true, 'text' => $body]); |
|
316 | } |
||
317 | |||
318 | /** |
||
319 | * Ajax: to delete a project note. |
||
320 | * |
||
321 | * @param Project $project |
||
322 | * @param Note $note |
||
323 | * |
||
324 | * @return \Symfony\Component\HttpFoundation\Response |
||
325 | */ |
||
326 | 1 | public function getDeleteNote(Project $project, Project\Note $note) |
|
327 | { |
||
328 | 1 | $note->delete(); |
|
329 | |||
330 | 1 | return response()->json(['status' => true]); |
|
331 | } |
||
332 | |||
333 | /** |
||
334 | * Ajax: generate the issues export file. |
||
335 | * |
||
336 | * @param Project $project |
||
337 | * @param Exporter $exporter |
||
338 | * @param Request $request |
||
339 | * |
||
340 | * @return \Symfony\Component\HttpFoundation\Response |
||
341 | */ |
||
342 | 4 | public function postExportIssues(Project $project, Exporter $exporter, Request $request) |
|
343 | { |
||
344 | // Generate export file |
||
345 | 4 | $info = $exporter->exportFile( |
|
346 | 4 | 'Project\Issue', |
|
347 | 4 | $request->input('format', Exporter::TYPE_CSV), |
|
348 | 4 | $request->all() |
|
349 | ); |
||
350 | |||
351 | // Download link |
||
352 | 4 | $link = link_to( |
|
353 | 4 | $project->to('download_export/' . $info['file']), |
|
354 | 4 | trans('tinyissue.download_export'), |
|
355 | 4 | ['class' => 'btn btn-link'] |
|
356 | ); |
||
357 | |||
358 | 4 | return response()->json([ |
|
359 | 4 | 'link' => $link, |
|
360 | 4 | 'title' => $info['title'], |
|
361 | 4 | 'file' => $info['file'], |
|
362 | 4 | 'ext' => $info['ext'], |
|
363 | ]); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Download and then delete an export file. |
||
368 | * |
||
369 | * @param Project $project |
||
370 | * @param string $file |
||
371 | * |
||
372 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
373 | */ |
||
374 | 4 | public function getDownloadExport(Project $project, $file) |
|
382 | } |
||
383 |