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 |
||
| 32 | class IssueController extends Controller |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Project issue index page (List project issues). |
||
| 36 | * |
||
| 37 | * @param Project $project |
||
| 38 | * @param Issue $issue |
||
| 39 | * @param CommentForm $form |
||
| 40 | * |
||
| 41 | * @return \Illuminate\View\View |
||
| 42 | */ |
||
| 43 | 16 | public function getIndex(Project $project, Issue $issue, CommentForm $form) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Ajax: Assign new user to an issue. |
||
| 62 | * |
||
| 63 | * @param Issue $issue |
||
| 64 | * @param Request $request |
||
| 65 | * |
||
| 66 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 67 | */ |
||
| 68 | 1 | View Code Duplication | public function postAssign(Issue $issue, Request $request) |
| 77 | |||
| 78 | /** |
||
| 79 | * Ajax: save comment. |
||
| 80 | * |
||
| 81 | * @param Comment $comment |
||
| 82 | * @param Request $request |
||
| 83 | * |
||
| 84 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 85 | */ |
||
| 86 | 1 | public function postEditComment(Comment $comment, Request $request) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * To add new comment to an issue. |
||
| 99 | * |
||
| 100 | * @param Project $project |
||
| 101 | * @param Issue $issue |
||
| 102 | * @param Comment $comment |
||
| 103 | * @param FormRequest\Comment $request |
||
| 104 | * |
||
| 105 | * @return \Illuminate\Http\RedirectResponse |
||
| 106 | */ |
||
| 107 | 4 | public function getAddComment(Project $project, Issue $issue, Comment $comment, FormRequest\Comment $request) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Ajax: to delete a comment. |
||
| 120 | * |
||
| 121 | * @param Comment $comment |
||
| 122 | * |
||
| 123 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 124 | */ |
||
| 125 | 1 | public function getDeleteComment(Comment $comment) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * New issue form. |
||
| 134 | * |
||
| 135 | * @param Project $project |
||
| 136 | * @param IssueForm $form |
||
| 137 | * |
||
| 138 | * @return \Illuminate\View\View |
||
| 139 | */ |
||
| 140 | 5 | public function getNew(Project $project, IssueForm $form) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * To create a new issue. |
||
| 151 | * |
||
| 152 | * @param Project $project |
||
| 153 | * @param Issue $issue |
||
| 154 | * @param FormRequest\Issue $request |
||
| 155 | * |
||
| 156 | * @return \Illuminate\Http\RedirectResponse |
||
| 157 | */ |
||
| 158 | 2 | View Code Duplication | public function postNew(Project $project, Issue $issue, FormRequest\Issue $request) |
| 167 | |||
| 168 | /** |
||
| 169 | * Edit an existing issue form. |
||
| 170 | * |
||
| 171 | * @param Project $project |
||
| 172 | * @param Issue $issue |
||
| 173 | * @param IssueForm $form |
||
| 174 | * |
||
| 175 | * @return \Illuminate\View\View |
||
| 176 | */ |
||
| 177 | 2 | public function getEdit(Project $project, Issue $issue, IssueForm $form) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * To update an existing issue details. |
||
| 195 | * |
||
| 196 | * @param Project $project |
||
| 197 | * @param Issue $issue |
||
| 198 | * @param FormRequest\Issue $request |
||
| 199 | * |
||
| 200 | * @return \Illuminate\Http\RedirectResponse |
||
| 201 | */ |
||
| 202 | 1 | View Code Duplication | public function postEdit(Project $project, Issue $issue, FormRequest\Issue $request) |
| 211 | |||
| 212 | /** |
||
| 213 | * To close or reopen an issue. |
||
| 214 | * |
||
| 215 | * @param Project $project |
||
| 216 | * @param Issue $issue |
||
| 217 | * @param int $status |
||
| 218 | * |
||
| 219 | * @return \Illuminate\Http\RedirectResponse |
||
| 220 | */ |
||
| 221 | 2 | public function getClose(Project $project, Issue $issue, $status = 0) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * To upload an attachment file. |
||
| 238 | * |
||
| 239 | * @param Project $project |
||
| 240 | * @param Attachment $attachment |
||
| 241 | * @param Request $request |
||
| 242 | * |
||
| 243 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 244 | */ |
||
| 245 | 3 | public function postUploadAttachment(Project $project, Attachment $attachment, Request $request) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Ajax: to remove an attachment file. |
||
| 279 | * |
||
| 280 | * @param Project $project |
||
| 281 | * @param Attachment $attachment |
||
| 282 | * @param Request $request |
||
| 283 | * |
||
| 284 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 285 | */ |
||
| 286 | 1 | public function postRemoveAttachment(Project $project, Attachment $attachment, Request $request) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Display an attachment file such as image. |
||
| 295 | * |
||
| 296 | * @param Project $project |
||
| 297 | * @param Issue $issue |
||
| 298 | * @param Attachment $attachment |
||
| 299 | * @param Request $request |
||
| 300 | * |
||
| 301 | * @return Response |
||
| 302 | */ |
||
| 303 | 2 | public function getDisplayAttachment(Project $project, Issue $issue, Attachment $attachment, Request $request) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Download an attachment file. |
||
| 339 | * |
||
| 340 | * @param Project $project |
||
| 341 | * @param Issue $issue |
||
| 342 | * @param Attachment $attachment |
||
| 343 | * |
||
| 344 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
| 345 | */ |
||
| 346 | 1 | public function getDownloadAttachment(Project $project, Issue $issue, Attachment $attachment) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Ajax: move an issue to another project. |
||
| 358 | * |
||
| 359 | * @param Issue $issue |
||
| 360 | * @param Request $request |
||
| 361 | * |
||
| 362 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 363 | */ |
||
| 364 | 1 | public function postChangeProject(Issue $issue, Request $request) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Ajax: change status of an issue. |
||
| 373 | * |
||
| 374 | * @param Issue $issue |
||
| 375 | * @param Request $request |
||
| 376 | * |
||
| 377 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 378 | */ |
||
| 379 | public function postChangeKanbanTag(Issue $issue, Request $request) |
||
| 388 | |||
| 389 | 8 | public function getIssueComments(Project $project, Issue $issue) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Ajax: returns activities for an issue excluding comments. |
||
| 416 | * |
||
| 417 | * @param Project $project |
||
| 418 | * @param Issue $issue |
||
| 419 | * |
||
| 420 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 421 | */ |
||
| 422 | 1 | public function getIssueActivity(Project $project, Issue $issue) |
|
| 443 | } |
||
| 444 |
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: