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\Contracts\View\Factory|\Illuminate\View\View |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function getIndex(Project $project) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Display issues for a project. |
||
| 52 | * |
||
| 53 | * @param FilterForm $filterForm |
||
| 54 | * @param Request $request |
||
| 55 | * @param Project $project |
||
| 56 | * @param int $status |
||
| 57 | * |
||
| 58 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 59 | */ |
||
| 60 | public function getIssues(FilterForm $filterForm, Request $request, Project $project, $status = Issue::STATUS_OPEN) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Display open issues. |
||
| 71 | * |
||
| 72 | * @param FilterForm $filterForm |
||
| 73 | * @param Request $request |
||
| 74 | * @param Project $project |
||
| 75 | * |
||
| 76 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function getOpenIssues(FilterForm $filterForm, Request $request, Project $project) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Display closed issues. |
||
| 93 | * |
||
| 94 | * @param FilterForm $filterForm |
||
| 95 | * @param Request $request |
||
| 96 | * @param Project $project |
||
| 97 | * |
||
| 98 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 99 | */ |
||
| 100 | View Code Duplication | public function getClosedIssues(FilterForm $filterForm, Request $request, Project $project) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Display issues assigned to current user for a project. |
||
| 115 | * |
||
| 116 | * @param Project $project |
||
| 117 | * |
||
| 118 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 119 | */ |
||
| 120 | View Code Duplication | public function getAssigned(Project $project) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Display issues created to current user for a project. |
||
| 135 | * |
||
| 136 | * @param Project $project |
||
| 137 | * |
||
| 138 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function getCreated(Project $project) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Display notes for a project. |
||
| 155 | * |
||
| 156 | * @param Project $project |
||
| 157 | * @param NoteForm $form |
||
| 158 | * |
||
| 159 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 160 | */ |
||
| 161 | View Code Duplication | public function getNotes(Project $project, NoteForm $form) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @param mixed $data |
||
| 177 | * @param string $active |
||
| 178 | * @param Project $project |
||
| 179 | * |
||
| 180 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 181 | */ |
||
| 182 | protected function indexView($data, $active, Project $project) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Edit the project. |
||
| 201 | * |
||
| 202 | * @param Project $project |
||
| 203 | * @param Form $form |
||
| 204 | * |
||
| 205 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
| 206 | */ |
||
| 207 | public function getEdit(Project $project, Form $form) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * To update project details. |
||
| 218 | * |
||
| 219 | * @param Project $project |
||
| 220 | * @param FormRequest\Project $request |
||
| 221 | * |
||
| 222 | * @return \Illuminate\Http\RedirectResponse |
||
| 223 | */ |
||
| 224 | public function postEdit(Project $project, FormRequest\Project $request) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Ajax: returns list of users that are not in the project. |
||
| 242 | * |
||
| 243 | * @param Project $project |
||
| 244 | * |
||
| 245 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 246 | */ |
||
| 247 | public function getInactiveUsers(Project $project) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Ajax: add user to the project. |
||
| 256 | * |
||
| 257 | * @param Project $project |
||
| 258 | * @param Request $request |
||
| 259 | * |
||
| 260 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 261 | */ |
||
| 262 | public function postAssign(Project $project, Request $request) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Ajax: remove user from the project. |
||
| 271 | * |
||
| 272 | * @param Project $project |
||
| 273 | * @param Request $request |
||
| 274 | * |
||
| 275 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 276 | */ |
||
| 277 | public function postUnassign(Project $project, Request $request) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * To add a new note to the project. |
||
| 286 | * |
||
| 287 | * @param Project $project |
||
| 288 | * @param Note $note |
||
| 289 | * @param FormRequest\Note $request |
||
| 290 | * |
||
| 291 | * @return \Illuminate\Http\RedirectResponse |
||
| 292 | */ |
||
| 293 | View Code Duplication | public function postAddNote(Project $project, Note $note, FormRequest\Note $request) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Ajax: To update project note. |
||
| 304 | * |
||
| 305 | * @param Project $project |
||
| 306 | * @param Note $note |
||
| 307 | * @param Request $request |
||
| 308 | * |
||
| 309 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 310 | */ |
||
| 311 | public function postEditNote(Project $project, Note $note, Request $request) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Ajax: to delete a project note. |
||
| 326 | * |
||
| 327 | * @param Project $project |
||
| 328 | * @param Note $note |
||
| 329 | * |
||
| 330 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 331 | */ |
||
| 332 | public function getDeleteNote(Project $project, Note $note) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Ajax: generate the issues export file. |
||
| 342 | * |
||
| 343 | * @param Project $project |
||
| 344 | * @param Exporter $exporter |
||
| 345 | * @param Request $request |
||
| 346 | * |
||
| 347 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 348 | */ |
||
| 349 | public function postExportIssues(Project $project, Exporter $exporter, Request $request) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Download and then delete an export file. |
||
| 375 | * |
||
| 376 | * @param Project $project |
||
| 377 | * @param string $file |
||
| 378 | * |
||
| 379 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
| 380 | */ |
||
| 381 | public function getDownloadExport(Project $project, $file) |
||
| 389 | } |
||
| 390 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.