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) |
|
| 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 | View Code Duplication | public function getAssigned(Project $project) |
| 99 | |||
| 100 | /** |
||
| 101 | * Display issues created to current user for a project. |
||
| 102 | * |
||
| 103 | * @param Project $project |
||
| 104 | * |
||
| 105 | * @return \Illuminate\View\View |
||
| 106 | */ |
||
| 107 | View Code Duplication | public function getCreated(Project $project) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Display notes for a project. |
||
| 122 | * |
||
| 123 | * @param Project $project |
||
| 124 | * @param NoteForm $form |
||
| 125 | * |
||
| 126 | * @return \Illuminate\View\View |
||
| 127 | */ |
||
| 128 | 7 | public function getNotes(Project $project, NoteForm $form) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @param Project $project |
||
| 144 | * @param string $view |
||
| 145 | * @param null $data |
||
| 146 | * @param bool $status |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | 21 | protected function projectMainViewTabs(Project $project, $view, $data = null, $status = false) |
|
| 151 | { |
||
| 152 | 21 | $notesCount = $view === 'note' ? $data->count() : $project->notes()->count(); |
|
| 153 | |||
| 154 | 21 | $assignedIssuesCount = 0; |
|
| 155 | 21 | if ($view !== 'assigned' && !$this->auth->guest()) { |
|
| 156 | 19 | $method = auth()->user()->isUser() ? 'createdIssuesCount' : 'assignedIssuesCount'; |
|
| 157 | 19 | $assignedIssuesCount = $this->auth->user()->$method($project->id); |
|
| 158 | 2 | } elseif ($view === 'assigned' || $view === 'created') { |
|
| 159 | 1 | $assignedIssuesCount = $data->count(); |
|
| 160 | } |
||
| 161 | |||
| 162 | 21 | if ($view === 'issues') { |
|
| 163 | 2 | if ($status == Issue::STATUS_OPEN) { |
|
| 164 | 2 | $closedIssuesCount = $project->closedIssuesCount()->count(); |
|
| 165 | 2 | $openIssuesCount = $data->count(); |
|
| 166 | } else { |
||
| 167 | 1 | $closedIssuesCount = $data->count(); |
|
| 168 | 2 | $openIssuesCount = $project->openIssuesCount()->count(); |
|
| 169 | } |
||
| 170 | } else { |
||
| 171 | 19 | $openIssuesCount = $project->openIssuesCount()->count(); |
|
| 172 | 19 | $closedIssuesCount = $project->closedIssuesCount()->count(); |
|
| 173 | } |
||
| 174 | |||
| 175 | 21 | $tabs = []; |
|
| 176 | 21 | $tabs[] = [ |
|
| 177 | 21 | 'url' => $project->to(), |
|
| 178 | 21 | 'page' => 'activity', |
|
| 179 | ]; |
||
| 180 | 21 | $tabs[] = [ |
|
| 181 | 21 | 'url' => $project->to('issues'), |
|
| 182 | 21 | 'page' => 'open_issue', |
|
| 183 | 21 | 'prefix' => $openIssuesCount, |
|
| 184 | ]; |
||
| 185 | 21 | $tabs[] = [ |
|
| 186 | 21 | 'url' => $project->to('issues') . '/0', |
|
| 187 | 21 | 'page' => 'closed_issue', |
|
| 188 | 21 | 'prefix' => $closedIssuesCount, |
|
| 189 | ]; |
||
| 190 | 21 | if (!$this->auth->guest()) { |
|
| 191 | 20 | $tabs[] = [ |
|
| 192 | 20 | 'url' => $project->to(auth()->user()->isUser() ? 'created' : 'assigned'), |
|
| 193 | 20 | 'page' => (auth()->user()->isUser() ? 'issue_created_by_you' : 'issue_assigned_to_you'), |
|
| 194 | 20 | 'prefix' => $assignedIssuesCount, |
|
| 195 | ]; |
||
| 196 | } |
||
| 197 | 21 | $tabs[] = [ |
|
| 198 | 21 | 'url' => $project->to('notes'), |
|
| 199 | 21 | 'page' => 'notes', |
|
| 200 | 21 | 'prefix' => $notesCount, |
|
| 201 | ]; |
||
| 202 | |||
| 203 | 21 | return $tabs; |
|
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Edit the project. |
||
| 208 | * |
||
| 209 | * @param Project $project |
||
| 210 | * @param Form $form |
||
| 211 | * |
||
| 212 | * @return \Illuminate\View\View |
||
| 213 | */ |
||
| 214 | 2 | public function getEdit(Project $project, Form $form) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * To update project details. |
||
| 225 | * |
||
| 226 | * @param Project $project |
||
| 227 | * @param FormRequest\Project $request |
||
| 228 | * |
||
| 229 | * @return \Illuminate\Http\RedirectResponse |
||
| 230 | */ |
||
| 231 | 2 | public function postEdit(Project $project, FormRequest\Project $request) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Ajax: returns list of users that are not in the project. |
||
| 249 | * |
||
| 250 | * @param Project $project |
||
| 251 | * |
||
| 252 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 253 | */ |
||
| 254 | 1 | public function getInactiveUsers(Project $project = null) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Ajax: add user to the project. |
||
| 263 | * |
||
| 264 | * @param Project $project |
||
| 265 | * @param Request $request |
||
| 266 | * |
||
| 267 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 268 | */ |
||
| 269 | 1 | View Code Duplication | public function postAssign(Project $project, Request $request) |
| 279 | |||
| 280 | /** |
||
| 281 | * Ajax: remove user from the project. |
||
| 282 | * |
||
| 283 | * @param Project $project |
||
| 284 | * @param Request $request |
||
| 285 | * |
||
| 286 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 287 | */ |
||
| 288 | 1 | View Code Duplication | public function postUnassign(Project $project, Request $request) |
| 298 | |||
| 299 | /** |
||
| 300 | * To add a new note to the project. |
||
| 301 | * |
||
| 302 | * @param Project $project |
||
| 303 | * @param Note $note |
||
| 304 | * @param FormRequest\Note $request |
||
| 305 | * |
||
| 306 | * @return \Illuminate\Http\RedirectResponse |
||
| 307 | */ |
||
| 308 | 2 | public function postAddNote(Project $project, Note $note, FormRequest\Note $request) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Ajax: To update project note. |
||
| 319 | * |
||
| 320 | * @param Project $project |
||
| 321 | * @param Note $note |
||
| 322 | * @param Request $request |
||
| 323 | * |
||
| 324 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 325 | */ |
||
| 326 | 1 | public function postEditNote(Project $project, Project\Note $note, Request $request) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Ajax: to delete a project note. |
||
| 341 | * |
||
| 342 | * @param Project $project |
||
| 343 | * @param Note $note |
||
| 344 | * |
||
| 345 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 346 | */ |
||
| 347 | 1 | public function getDeleteNote(Project $project, Project\Note $note) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Ajax: generate the issues export file. |
||
| 356 | * |
||
| 357 | * @param Project $project |
||
| 358 | * @param Exporter $exporter |
||
| 359 | * @param Request $request |
||
| 360 | * |
||
| 361 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 362 | */ |
||
| 363 | 4 | public function postExportIssues(Project $project, Exporter $exporter, Request $request) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Download and then delete an export file. |
||
| 389 | * |
||
| 390 | * @param Project $project |
||
| 391 | * @param string $file |
||
| 392 | * |
||
| 393 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
| 394 | */ |
||
| 395 | 4 | public function getDownloadExport(Project $project, $file) |
|
| 403 | } |
||
| 404 |