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 | 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) |
|
87 | { |
||
88 | 1 | $body = ''; |
|
89 | 1 | View Code Duplication | if ($request->has('body')) { |
1 ignored issue
–
show
|
|||
90 | 1 | $comment->setRelation('user', $this->auth->user()); |
|
91 | 1 | $comment->updateBody($request->input('body')); |
|
92 | 1 | $body = \Html::format($comment->comment); |
|
93 | } |
||
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 | 4 | public function getAddComment(Project $project, Issue $issue, Comment $comment, FormRequest\Comment $request) |
|
109 | { |
||
110 | 4 | $comment->setRelation('project', $project); |
|
111 | 4 | $comment->setRelation('issue', $issue); |
|
112 | 4 | $comment->setRelation('user', $this->auth->user()); |
|
113 | 4 | $comment->createComment($request->all()); |
|
114 | |||
115 | 4 | return redirect($issue->to() . '#comment' . $comment->id) |
|
116 | 4 | ->with('notice', trans('tinyissue.your_comment_added')); |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Ajax: to delete a comment. |
||
121 | * |
||
122 | * @param Comment $comment |
||
123 | * |
||
124 | * @return \Symfony\Component\HttpFoundation\Response |
||
125 | */ |
||
126 | 1 | public function getDeleteComment(Comment $comment) |
|
127 | { |
||
128 | 1 | $comment->setRelation('user', $this->auth->user()); |
|
129 | 1 | $comment->deleteComment(); |
|
130 | |||
131 | 1 | return response()->json(['status' => true]); |
|
132 | } |
||
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 | ]); |
||
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) |
161 | { |
||
162 | 2 | $issue->setRelation('project', $project); |
|
163 | 2 | $issue->setRelation('user', $this->auth->user()); |
|
164 | 2 | $issue->createIssue($request->all()); |
|
165 | |||
166 | 2 | return redirect($issue->to()) |
|
167 | 2 | ->with('notice', trans('tinyissue.issue_has_been_created')); |
|
168 | } |
||
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 | 2 | 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 | ]); |
||
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) |
205 | { |
||
206 | 1 | $issue->setRelation('project', $project); |
|
207 | 1 | $issue->setRelation('updatedBy', $this->auth->user()); |
|
208 | 1 | $issue->updateIssue($request->all()); |
|
209 | |||
210 | 1 | return redirect($issue->to()) |
|
211 | 1 | ->with('notice', trans('tinyissue.issue_has_been_updated')); |
|
212 | } |
||
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 | } 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 | ], |
||
263 | ], |
||
264 | ]; |
||
265 | } 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) |
|
289 | { |
||
290 | 1 | $attachment->remove($request->all(), $project, $this->auth->user()); |
|
291 | |||
292 | 1 | return response()->json(['status' => true]); |
|
293 | } |
||
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) |
|
306 | { |
||
307 | 2 | $issue->setRelation('project', $project); |
|
308 | 2 | $attachment->setRelation('issue', $issue); |
|
309 | |||
310 | 2 | $path = config('tinyissue.uploads_dir') . '/' . $issue->project_id . '/' . $attachment->upload_token . '/' . $attachment->filename; |
|
311 | 2 | $storage = \Storage::disk('local'); |
|
312 | 2 | $length = $storage->size($path); |
|
313 | 2 | $time = $storage->lastModified($path); |
|
314 | 2 | $type = $storage->getDriver()->getMimetype($path); |
|
315 | |||
316 | 2 | $response = new Response(); |
|
317 | 2 | $response->setEtag(md5($time . $path)); |
|
318 | 2 | $response->setExpires(new \DateTime('@' . ($time + 60))); |
|
319 | 2 | $response->setLastModified(new \DateTime('@' . $time)); |
|
320 | 2 | $response->setPublic(); |
|
321 | 2 | $response->setStatusCode(200); |
|
322 | |||
323 | 2 | $response->header('Content-Type', $type); |
|
324 | 2 | $response->header('Content-Length', $length); |
|
325 | 2 | $response->header('Content-Disposition', 'inline; filename="' . $attachment->filename . '"'); |
|
326 | 2 | $response->header('Cache-Control', 'must-revalidate'); |
|
327 | |||
328 | 2 | if ($response->isNotModified($request)) { |
|
329 | // Return empty response if not modified |
||
330 | return $response; |
||
331 | } |
||
332 | |||
333 | // Return file if first request / modified |
||
334 | 2 | $response->setContent($storage->get($path)); |
|
335 | |||
336 | 2 | return $response; |
|
337 | } |
||
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) |
|
349 | { |
||
350 | 1 | $issue->setRelation('project', $project); |
|
351 | 1 | $attachment->setRelation('issue', $issue); |
|
352 | |||
353 | 1 | $path = config('filesystems.disks.local.root') . '/' . config('tinyissue.uploads_dir') . '/' . $issue->project_id . '/' . $attachment->upload_token . '/' . $attachment->filename; |
|
354 | |||
355 | 1 | return response()->download($path, $attachment->filename); |
|
356 | } |
||
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) |
|
367 | { |
||
368 | 1 | $issue->changeProject($request->input('project_id')); |
|
369 | |||
370 | 1 | return response()->json(['status' => true, 'url' => $issue->to()]); |
|
371 | } |
||
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) |
|
400 | { |
||
401 | $issue->attachments->each(function (Attachment $attachment) use ($issue) { |
||
402 | $attachment->setRelation('issue', $issue); |
||
403 | 8 | }); |
|
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: