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 | 4 | public function getIndex(Project $project) |
|
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 | public function getIssues(FilterForm $filterForm, Request $request, Project $project, $status = Issue::STATUS_OPEN) |
||
66 | { |
||
67 | $active = $status == Issue::STATUS_OPEN ? 'open_issue' : 'closed_issue'; |
||
68 | $issues = $project->listIssues($status, $request->all()); |
||
69 | |||
70 | return view('project.index', [ |
||
71 | 'tabs' => $this->projectMainViewTabs($project, 'issues', $issues, $status), |
||
72 | 'project' => $project, |
||
73 | 'active' => $active, |
||
74 | 'issues' => $issues, |
||
75 | 'sidebar' => 'project', |
||
76 | '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 | public function getAssigned(Project $project) |
||
88 | { |
||
89 | $issues = $project->listAssignedIssues($this->auth->user()->id); |
||
90 | |||
91 | return view('project.index', [ |
||
92 | 'tabs' => $this->projectMainViewTabs($project, 'assigned', $issues), |
||
93 | 'project' => $project, |
||
94 | 'active' => 'issue_assigned_to_you', |
||
95 | 'issues' => $issues, |
||
96 | '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 | 6 | public function getNotes(Project $project, NoteForm $form) |
|
121 | |||
122 | /** |
||
123 | * @param Project $project |
||
124 | * @param $view |
||
125 | * @param null $data |
||
126 | * @param bool $status |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 10 | protected function projectMainViewTabs(Project $project, $view, $data = null, $status = false) |
|
131 | { |
||
132 | 10 | $notesCount = $view === 'note' ? $data->count() : $project->notes()->count(); |
|
133 | |||
134 | 10 | $assignedIssuesCount = 0; |
|
135 | 10 | if ($view !== 'assigned' && !$this->auth->guest()) { |
|
136 | 10 | $assignedIssuesCount = $this->auth->user()->assignedIssuesCount($project->id); |
|
137 | } elseif ($view === 'assigned') { |
||
138 | $assignedIssuesCount = $data->count(); |
||
139 | } |
||
140 | |||
141 | 10 | if ($view === 'issues') { |
|
142 | if ($status == Issue::STATUS_OPEN) { |
||
143 | $closedIssuesCount = $project->closedIssuesCount()->count(); |
||
144 | $openIssuesCount = $data->count(); |
||
145 | } else { |
||
146 | $closedIssuesCount = $data->count(); |
||
147 | $openIssuesCount = $project->openIssuesCount()->count(); |
||
148 | } |
||
149 | } else { |
||
150 | 10 | $openIssuesCount = $project->openIssuesCount()->count(); |
|
151 | 10 | $closedIssuesCount = $project->closedIssuesCount()->count(); |
|
152 | } |
||
153 | |||
154 | 10 | $tabs = []; |
|
155 | 10 | $tabs[] = [ |
|
156 | 10 | 'url' => $project->to(), |
|
157 | 10 | 'page' => 'activity', |
|
158 | ]; |
||
159 | 10 | $tabs[] = [ |
|
160 | 10 | 'url' => $project->to('issues'), |
|
161 | 10 | 'page' => 'open_issue', |
|
162 | 10 | 'prefix' => $openIssuesCount, |
|
163 | ]; |
||
164 | 10 | $tabs[] = [ |
|
165 | 10 | 'url' => $project->to('issues') . '/0', |
|
166 | 10 | 'page' => 'closed_issue', |
|
167 | 10 | 'prefix' => $closedIssuesCount, |
|
168 | ]; |
||
169 | 10 | if (!$this->auth->guest()) { |
|
170 | 10 | $tabs[] = [ |
|
171 | 10 | 'url' => $project->to('assigned'), |
|
172 | 10 | 'page' => 'issue_assigned_to_you', |
|
173 | 10 | 'prefix' => $assignedIssuesCount, |
|
174 | ]; |
||
175 | } |
||
176 | 10 | $tabs[] = [ |
|
177 | 10 | 'url' => $project->to('notes'), |
|
178 | 10 | 'page' => 'notes', |
|
179 | 10 | 'prefix' => $notesCount, |
|
180 | ]; |
||
181 | |||
182 | 10 | 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) |
|
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) |
|
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) |
|
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) |
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) |
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) |
|
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) |
|
315 | |||
316 | /** |
||
317 | * Ajax: to delete a project note. |
||
318 | * |
||
319 | * @param Project $project |
||
320 | * @param Note $note |
||
321 | * |
||
322 | * @return \Symfony\Component\HttpFoundation\Response |
||
323 | */ |
||
324 | public function getDeleteNote(Project $project, Project\Note $note) |
||
330 | |||
331 | /** |
||
332 | * Ajax: generate the issues export file. |
||
333 | * |
||
334 | * @param Project $project |
||
335 | * @param Exporter $exporter |
||
336 | * @param Request $request |
||
337 | * |
||
338 | * @return \Symfony\Component\HttpFoundation\Response |
||
339 | */ |
||
340 | public function postExportIssues(Project $project, Exporter $exporter, Request $request) |
||
363 | |||
364 | /** |
||
365 | * Download and then delete an export file. |
||
366 | * |
||
367 | * @param Project $project |
||
368 | * @param string $file |
||
369 | * |
||
370 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
371 | */ |
||
372 | public function getDownloadExport(Project $project, $file) |
||
380 | } |
||
381 |