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 |
||
24 | class CommentController extends AbstractApiController |
||
25 | { |
||
26 | use DispatchesJobs; |
||
27 | |||
28 | /** |
||
29 | * Get all comments. |
||
30 | * |
||
31 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
32 | * @param \Illuminate\Contracts\Auth\Guard $auth |
||
33 | * |
||
34 | * @return \Illuminate\Http\JsonResponse |
||
35 | */ |
||
36 | public function getComments(Request $request, Guard $auth) |
||
42 | |||
43 | /** |
||
44 | * Get a single comment. |
||
45 | * |
||
46 | * @param \Gitamin\Models\Comment $comment |
||
47 | * |
||
48 | * @return \Illuminate\Http\JsonResponse |
||
49 | */ |
||
50 | public function getComment(Comment $comment) |
||
54 | |||
55 | /** |
||
56 | * Create a new comment. |
||
57 | * |
||
58 | * @param \Illuminate\Contracts\Auth\Guard $auth |
||
59 | * |
||
60 | * @return \Illuminate\Http\JsonResponse |
||
61 | */ |
||
62 | public function postComments(Request $request, Guard $auth) |
||
63 | { |
||
64 | try { |
||
65 | $comment = $this->dispatch(new AddCommentCommand( |
||
66 | $request->input('message'), |
||
67 | $request->input('target_type'), |
||
68 | $request->input('target_id'), |
||
69 | $request->input('author_id'), |
||
70 | $request->input('project_id') |
||
71 | )); |
||
72 | } catch (QueryException $e) { |
||
73 | throw new BadRequestHttpException(); |
||
74 | } |
||
75 | |||
76 | return $this->item($comment); |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Update an existing comment. |
||
81 | * |
||
82 | * @param \Gitamin\Models\Inicdent $comment |
||
83 | * |
||
84 | * @return \Illuminate\Http\JsonResponse |
||
85 | */ |
||
86 | View Code Duplication | public function putComment(Request $request, Comment $comment) |
|
99 | |||
100 | /** |
||
101 | * Delete an existing comment. |
||
102 | * |
||
103 | * @param \Gitamin\Models\Comment $comment |
||
104 | * |
||
105 | * @return \Illuminate\Http\JsonResponse |
||
106 | */ |
||
107 | public function deleteComment(Comment $comment) |
||
113 | } |
||
114 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.