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 |
||
| 30 | class ProjectController extends Controller |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Display activity for a project. |
||
| 34 | * |
||
| 35 | * @param Project $project |
||
| 36 | * |
||
| 37 | * @return \Illuminate\View\View |
||
| 38 | */ |
||
| 39 | 12 | public function getIndex(Project $project) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Display issues for a project. |
||
| 63 | * |
||
| 64 | * @param FilterForm $filterForm |
||
| 65 | * @param Request $request |
||
| 66 | * @param Project $project |
||
| 67 | * @param int $status |
||
| 68 | * |
||
| 69 | * @return \Illuminate\View\View |
||
| 70 | */ |
||
| 71 | 2 | public function getIssues(FilterForm $filterForm, Request $request, Project $project, $status = Issue::STATUS_OPEN) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Display issues assigned to current user for a project. |
||
| 91 | * |
||
| 92 | * @param Project $project |
||
| 93 | * |
||
| 94 | * @return \Illuminate\View\View |
||
| 95 | */ |
||
| 96 | 1 | View Code Duplication | public function getAssigned(Project $project) |
| 108 | |||
| 109 | /** |
||
| 110 | * Display issues created to current user for a project. |
||
| 111 | * |
||
| 112 | * @param Project $project |
||
| 113 | * |
||
| 114 | * @return \Illuminate\View\View |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function getCreated(Project $project) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Display notes for a project. |
||
| 131 | * |
||
| 132 | * @param Project $project |
||
| 133 | * @param NoteForm $form |
||
| 134 | * |
||
| 135 | * @return \Illuminate\View\View |
||
| 136 | */ |
||
| 137 | 7 | public function getNotes(Project $project, NoteForm $form) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param Project $project |
||
| 153 | * @param string $view |
||
| 154 | * @param null $data |
||
| 155 | * @param bool $status |
||
| 156 | * |
||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | 22 | protected function projectMainViewTabs(Project $project, $view, $data = null, $status = false) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Edit the project. |
||
| 220 | * |
||
| 221 | * @param Project $project |
||
| 222 | * @param Form $form |
||
| 223 | * |
||
| 224 | * @return \Illuminate\View\View |
||
| 225 | */ |
||
| 226 | 3 | public function getEdit(Project $project, Form $form) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * To update project details. |
||
| 237 | * |
||
| 238 | * @param Project $project |
||
| 239 | * @param FormRequest\Project $request |
||
| 240 | * |
||
| 241 | * @return \Illuminate\Http\RedirectResponse |
||
| 242 | */ |
||
| 243 | 3 | public function postEdit(Project $project, FormRequest\Project $request) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Ajax: returns list of users that are not in the project. |
||
| 261 | * |
||
| 262 | * @param Project $project |
||
| 263 | * |
||
| 264 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 265 | */ |
||
| 266 | 1 | public function getInactiveUsers(Project $project = null) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Ajax: add user to the project. |
||
| 275 | * |
||
| 276 | * @param Project $project |
||
| 277 | * @param Request $request |
||
| 278 | * |
||
| 279 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 280 | */ |
||
| 281 | 1 | View Code Duplication | public function postAssign(Project $project, Request $request) |
| 291 | |||
| 292 | /** |
||
| 293 | * Ajax: remove user from the project. |
||
| 294 | * |
||
| 295 | * @param Project $project |
||
| 296 | * @param Request $request |
||
| 297 | * |
||
| 298 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 299 | */ |
||
| 300 | 1 | View Code Duplication | public function postUnassign(Project $project, Request $request) |
| 310 | |||
| 311 | /** |
||
| 312 | * To add a new note to the project. |
||
| 313 | * |
||
| 314 | * @param Project $project |
||
| 315 | * @param Note $note |
||
| 316 | * @param FormRequest\Note $request |
||
| 317 | * |
||
| 318 | * @return \Illuminate\Http\RedirectResponse |
||
| 319 | */ |
||
| 320 | 2 | public function postAddNote(Project $project, Note $note, FormRequest\Note $request) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Ajax: To update project note. |
||
| 331 | * |
||
| 332 | * @param Project $project |
||
| 333 | * @param Note $note |
||
| 334 | * @param Request $request |
||
| 335 | * |
||
| 336 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 337 | */ |
||
| 338 | 1 | public function postEditNote(Project $project, Project\Note $note, Request $request) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Ajax: to delete a project note. |
||
| 353 | * |
||
| 354 | * @param Project $project |
||
| 355 | * @param Note $note |
||
| 356 | * |
||
| 357 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 358 | */ |
||
| 359 | 1 | public function getDeleteNote(Project $project, Project\Note $note) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Ajax: generate the issues export file. |
||
| 368 | * |
||
| 369 | * @param Project $project |
||
| 370 | * @param Exporter $exporter |
||
| 371 | * @param Request $request |
||
| 372 | * |
||
| 373 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 374 | */ |
||
| 375 | 4 | public function postExportIssues(Project $project, Exporter $exporter, Request $request) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Download and then delete an export file. |
||
| 401 | * |
||
| 402 | * @param Project $project |
||
| 403 | * @param string $file |
||
| 404 | * |
||
| 405 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
| 406 | */ |
||
| 407 | 4 | public function getDownloadExport(Project $project, $file) |
|
| 415 | } |
||
| 416 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: