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 |
||
12 | class RepliesController extends Controller |
||
13 | { |
||
14 | /** |
||
15 | * @var ReplyRepositoryInterface |
||
16 | */ |
||
17 | private $replies; |
||
18 | |||
19 | /** |
||
20 | * TopicController constructor. |
||
21 | * |
||
22 | * @param ReplyRepositoryInterface $repository |
||
23 | */ |
||
24 | public function __construct(ReplyRepositoryInterface $repository) |
||
28 | |||
29 | /** |
||
30 | * 获取指定帖子的回复. |
||
31 | * |
||
32 | * @param $topic_id |
||
33 | * |
||
34 | * @return \Illuminate\Http\Response |
||
35 | */ |
||
36 | View Code Duplication | public function indexByTopicId($topic_id) |
|
|
|||
37 | { |
||
38 | $this->replies->addAvailableInclude('user', ['name', 'avatar']); |
||
39 | |||
40 | $data = $this->replies |
||
41 | ->byTopicId($topic_id) |
||
42 | ->autoWith() |
||
43 | ->autoWithRootColumns(['id', 'vote_count', 'created_at']) |
||
44 | ->paginate(per_page()); |
||
45 | |||
46 | return $this->response()->paginator($data, new ReplyTransformer()); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * 获取指定用户的回复. |
||
51 | * |
||
52 | * @param $user_id |
||
53 | * |
||
54 | * @return \Dingo\Api\Http\Response |
||
55 | */ |
||
56 | View Code Duplication | public function indexByUserId($user_id) |
|
57 | { |
||
58 | $this->replies->addAvailableInclude('user', ['name', 'avatar']); |
||
59 | |||
60 | $data = $this->replies |
||
61 | ->byUserId($user_id) |
||
62 | ->autoWith() |
||
63 | ->autoWithRootColumns(['id', 'vote_count']) |
||
64 | ->paginate(per_page()); |
||
65 | |||
66 | return $this->response()->paginator($data, new ReplyTransformer()); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * 发布一条新回复. |
||
71 | * |
||
72 | * @param \Illuminate\Http\Request $request |
||
73 | * |
||
74 | * @return \Illuminate\Http\Response |
||
75 | */ |
||
76 | View Code Duplication | public function store(Request $request) |
|
77 | { |
||
78 | try { |
||
79 | $reply = $this->replies->store($request->all()); |
||
80 | |||
81 | return $this->response()->item($reply, new ReplyTransformer()); |
||
82 | } catch (ValidatorException $e) { |
||
83 | throw new StoreResourceFailedException('Could not create new topic.', $e->getMessageBag()->all()); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * 更新回复. |
||
89 | * |
||
90 | * @param \Illuminate\Http\Request $request |
||
91 | * @param int $id |
||
92 | * |
||
93 | * @return \Illuminate\Http\Response |
||
94 | */ |
||
95 | public function update(Request $request, $id) |
||
96 | { |
||
97 | // |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * 删除一条回复. |
||
102 | * |
||
103 | * @param int $id |
||
104 | * |
||
105 | * @return \Illuminate\Http\Response |
||
106 | */ |
||
107 | public function destroy($id) |
||
108 | { |
||
109 | // |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * 指定帖子下评论列表的 Web View. |
||
114 | * |
||
115 | * @param $topic_id |
||
116 | * |
||
117 | * @return \Illuminate\View\View |
||
118 | */ |
||
119 | public function indexWebViewByTopic($topic_id) |
||
131 | |||
132 | /** |
||
133 | * 指定帖子下评论列表的 Web View. |
||
134 | * |
||
135 | * @param $user_id |
||
136 | * |
||
137 | * @return \Illuminate\View\View |
||
138 | */ |
||
139 | public function indexWebViewByUser($user_id) |
||
148 | } |
||
149 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.