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) |
|
| 44 | { |
||
| 45 | // Projects should be limited to issue-modify |
||
| 46 | 16 | $projects = null; |
|
| 47 | 16 | if (!$this->auth->guest() && $this->auth->user()->permission('issue-modify')) { |
|
|
|
|||
| 48 | 13 | $projects = $this->auth->user()->projects()->get(); |
|
| 49 | 13 | } |
|
| 50 | |||
| 51 | 16 | return view('project.issue.index', [ |
|
| 52 | 16 | 'issue' => $issue, |
|
| 53 | 16 | 'project' => $project, |
|
| 54 | 16 | 'commentForm' => $form, |
|
| 55 | 16 | 'sidebar' => 'project', |
|
| 56 | 16 | 'projects' => $projects, |
|
| 57 | 16 | ]); |
|
| 58 | 4 | } |
|
| 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 | 5 | public function postAssign(Issue $issue, Request $request) |
|
| 69 | { |
||
| 70 | 1 | $response = ['status' => false]; |
|
| 71 | 1 | if ($issue->reassign((int) $request->input('user_id'), $this->auth->user())) { |
|
| 72 | 3 | $response['status'] = true; |
|
| 73 | 1 | } |
|
| 74 | |||
| 75 | 5 | return response()->json($response); |
|
| 76 | 4 | } |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Ajax: save comment. |
||
| 80 | * |
||
| 81 | * @param Comment $comment |
||
| 82 | * @param Request $request |
||
| 83 | * |
||
| 84 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 85 | */ |
||
| 86 | 3 | public function postEditComment(Comment $comment, Request $request) |
|
| 87 | { |
||
| 88 | 1 | $body = ''; |
|
| 89 | 3 | View Code Duplication | if ($request->has('body')) { |
|
1 ignored issue
–
show
|
|||
| 90 | 1 | $comment->setRelation('user', $this->auth->user()); |
|
| 91 | 2 | $comment->updateBody($request->input('body')); |
|
| 92 | 1 | $body = \Html::format($comment->comment); |
|
| 93 | 1 | } |
|
| 94 | |||
| 95 | 1 | return response()->json(['text' => $body]); |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * To add new comment to an issue. |
||
| 100 | * |
||
| 101 | * @param Project $project |
||
| 102 | * @param Issue $issue |
||
| 103 | * @param Comment $comment |
||
| 104 | * @param FormRequest\Comment $request |
||
| 105 | * |
||
| 106 | * @return \Illuminate\Http\RedirectResponse |
||
| 107 | */ |
||
| 108 | 10 | public function getAddComment(Project $project, Issue $issue, Comment $comment, FormRequest\Comment $request) |
|
| 109 | 10 | { |
|
| 110 | 4 | $comment->setRelation('project', $project); |
|
| 111 | 4 | $comment->setRelation('issue', $issue); |
|
| 112 | 4 | $comment->setRelation('user', $this->auth->user()); |
|
| 113 | 5 | $comment->createComment($request->all()); |
|
| 114 | |||
| 115 | 4 | return redirect($issue->to() . '#comment' . $comment->id) |
|
| 116 | 5 | ->with('notice', trans('tinyissue.your_comment_added')); |
|
| 117 | 1 | } |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Ajax: to delete a comment. |
||
| 121 | * |
||
| 122 | * @param Comment $comment |
||
| 123 | * |
||
| 124 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 125 | */ |
||
| 126 | 2 | public function getDeleteComment(Comment $comment) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * New issue form. |
||
| 136 | * |
||
| 137 | * @param Project $project |
||
| 138 | * @param IssueForm $form |
||
| 139 | * |
||
| 140 | * @return \Illuminate\View\View |
||
| 141 | */ |
||
| 142 | 5 | public function getNew(Project $project, IssueForm $form) |
|
| 143 | { |
||
| 144 | 5 | return view('project.issue.new', [ |
|
| 145 | 5 | 'project' => $project, |
|
| 146 | 5 | 'form' => $form, |
|
| 147 | 5 | 'sidebar' => 'project', |
|
| 148 | 5 | ]); |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * To create a new issue. |
||
| 153 | * |
||
| 154 | * @param Project $project |
||
| 155 | * @param Issue $issue |
||
| 156 | * @param FormRequest\Issue $request |
||
| 157 | * |
||
| 158 | * @return \Illuminate\Http\RedirectResponse |
||
| 159 | */ |
||
| 160 | 2 | View Code Duplication | public function postNew(Project $project, Issue $issue, FormRequest\Issue $request) |
| 169 | |||
| 170 | /** |
||
| 171 | * Edit an existing issue form. |
||
| 172 | * |
||
| 173 | * @param Project $project |
||
| 174 | * @param Issue $issue |
||
| 175 | * @param IssueForm $form |
||
| 176 | * |
||
| 177 | * @return \Illuminate\View\View |
||
| 178 | */ |
||
| 179 | 3 | public function getEdit(Project $project, Issue $issue, IssueForm $form) |
|
| 180 | { |
||
| 181 | // Cannot edit closed issue |
||
| 182 | 2 | if ($issue->status == Issue::STATUS_CLOSED) { |
|
| 183 | 1 | return redirect($issue->to()) |
|
| 184 | 1 | ->with('notice', trans('tinyissue.cant_edit_closed_issue')); |
|
| 185 | } |
||
| 186 | |||
| 187 | 2 | return view('project.issue.edit', [ |
|
| 188 | 2 | 'issue' => $issue, |
|
| 189 | 2 | 'project' => $project, |
|
| 190 | 2 | 'form' => $form, |
|
| 191 | 2 | 'sidebar' => 'project', |
|
| 192 | 3 | ]); |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * To update an existing issue details. |
||
| 197 | * |
||
| 198 | * @param Project $project |
||
| 199 | * @param Issue $issue |
||
| 200 | * @param FormRequest\Issue $request |
||
| 201 | * |
||
| 202 | * @return \Illuminate\Http\RedirectResponse |
||
| 203 | */ |
||
| 204 | 1 | View Code Duplication | public function postEdit(Project $project, Issue $issue, FormRequest\Issue $request) |
| 213 | |||
| 214 | /** |
||
| 215 | * To close or reopen an issue. |
||
| 216 | * |
||
| 217 | * @param Project $project |
||
| 218 | * @param Issue $issue |
||
| 219 | * @param int $status |
||
| 220 | * |
||
| 221 | * @return \Illuminate\Http\RedirectResponse |
||
| 222 | */ |
||
| 223 | 2 | public function getClose(Project $project, Issue $issue, $status = 0) |
|
| 224 | { |
||
| 225 | 2 | if ($status == 0) { |
|
| 226 | 2 | $message = trans('tinyissue.issue_has_been_closed'); |
|
| 227 | 2 | } else { |
|
| 228 | 1 | $message = trans('tinyissue.issue_has_been_reopened'); |
|
| 229 | } |
||
| 230 | |||
| 231 | 2 | $issue->setRelation('project', $project); |
|
| 232 | 2 | $issue->changeStatus($status, $this->auth->user()->id); |
|
| 233 | |||
| 234 | 2 | return redirect($issue->to()) |
|
| 235 | 2 | ->with('notice', $message); |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * To upload an attachment file. |
||
| 240 | * |
||
| 241 | * @param Project $project |
||
| 242 | * @param Attachment $attachment |
||
| 243 | * @param Request $request |
||
| 244 | * |
||
| 245 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 246 | */ |
||
| 247 | 3 | public function postUploadAttachment(Project $project, Attachment $attachment, Request $request) |
|
| 248 | { |
||
| 249 | try { |
||
| 250 | 3 | if (!$this->auth->user()->permission('project-all')) { |
|
| 251 | abort(404); |
||
| 252 | } |
||
| 253 | |||
| 254 | 3 | $attachment->upload($request->all(), $project, $this->auth->user()); |
|
| 255 | |||
| 256 | $response = [ |
||
| 257 | 'upload' => [ |
||
| 258 | [ |
||
| 259 | 3 | 'name' => $attachment->filename, |
|
| 260 | 3 | 'size' => $attachment->filesize, |
|
| 261 | 3 | 'fileId' => $attachment->id, |
|
| 262 | 3 | ], |
|
| 263 | 3 | ], |
|
| 264 | 3 | ]; |
|
| 265 | 3 | } catch (\Exception $exception) { |
|
| 266 | $file = $request->file('upload'); |
||
| 267 | |||
| 268 | $response = [ |
||
| 269 | 'status' => false, |
||
| 270 | 'name' => $file->getClientOriginalName(), |
||
| 271 | 'error' => $exception->getMessage(), |
||
| 272 | 'trace' => $exception->getTraceAsString(), |
||
| 273 | ]; |
||
| 274 | } |
||
| 275 | |||
| 276 | 3 | return response()->json($response); |
|
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Ajax: to remove an attachment file. |
||
| 281 | * |
||
| 282 | * @param Project $project |
||
| 283 | * @param Attachment $attachment |
||
| 284 | * @param Request $request |
||
| 285 | * |
||
| 286 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 287 | */ |
||
| 288 | 1 | public function postRemoveAttachment(Project $project, Attachment $attachment, Request $request) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Display an attachment file such as image. |
||
| 297 | * |
||
| 298 | * @param Project $project |
||
| 299 | * @param Issue $issue |
||
| 300 | * @param Attachment $attachment |
||
| 301 | * @param Request $request |
||
| 302 | * |
||
| 303 | * @return Response |
||
| 304 | */ |
||
| 305 | 2 | public function getDisplayAttachment(Project $project, Issue $issue, Attachment $attachment, Request $request) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Download an attachment file. |
||
| 341 | * |
||
| 342 | * @param Project $project |
||
| 343 | * @param Issue $issue |
||
| 344 | * @param Attachment $attachment |
||
| 345 | * |
||
| 346 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
| 347 | */ |
||
| 348 | 1 | public function getDownloadAttachment(Project $project, Issue $issue, Attachment $attachment) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Ajax: move an issue to another project. |
||
| 360 | * |
||
| 361 | * @param Issue $issue |
||
| 362 | * @param Request $request |
||
| 363 | * |
||
| 364 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 365 | */ |
||
| 366 | 1 | public function postChangeProject(Issue $issue, Request $request) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Ajax: change status of an issue. |
||
| 375 | * |
||
| 376 | * @param Issue $issue |
||
| 377 | * @param Request $request |
||
| 378 | * |
||
| 379 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 380 | */ |
||
| 381 | public function postChangeKanbanTag(Issue $issue, Request $request) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Ajax: returns comments for an issue. |
||
| 393 | * |
||
| 394 | * @param Project $project |
||
| 395 | * @param Issue $issue |
||
| 396 | * |
||
| 397 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 398 | */ |
||
| 399 | 8 | public function getIssueComments(Project $project, Issue $issue) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Ajax: returns activities for an issue excluding comments. |
||
| 430 | * |
||
| 431 | * @param Project $project |
||
| 432 | * @param Issue $issue |
||
| 433 | * |
||
| 434 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 435 | */ |
||
| 436 | 1 | public function getIssueActivity(Project $project, Issue $issue) |
|
| 461 | } |
||
| 462 |
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: