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 |
||
16 | class TopicsController extends Controller |
||
17 | { |
||
18 | /** |
||
19 | * @var TopicRepositoryInterface |
||
20 | */ |
||
21 | private $topics; |
||
22 | |||
23 | /** |
||
24 | * TopicController constructor. |
||
25 | * |
||
26 | * @param TopicRepositoryInterface $repository |
||
27 | */ |
||
28 | public function __construct(TopicRepositoryInterface $repository) |
||
32 | |||
33 | /** |
||
34 | * 默认帖子列表. |
||
35 | * |
||
36 | * @return \Illuminate\Http\Response |
||
37 | */ |
||
38 | public function index() |
||
42 | |||
43 | /** |
||
44 | * 获取指定用户发布的帖子. |
||
45 | * |
||
46 | * @param $user_id |
||
47 | * |
||
48 | * @return \Dingo\Api\Http\Response |
||
49 | */ |
||
50 | public function indexByUserId($user_id) |
||
56 | |||
57 | /** |
||
58 | * 获取指定节点下的帖子. |
||
59 | * |
||
60 | * @param $node_id |
||
61 | * |
||
62 | * @return \Dingo\Api\Http\Response |
||
63 | */ |
||
64 | public function indexByNodeId($node_id) |
||
70 | |||
71 | /** |
||
72 | * 用户收藏的帖子列表. |
||
73 | * |
||
74 | * @param $user_id |
||
75 | * |
||
76 | * @return \Dingo\Api\Http\Response |
||
77 | */ |
||
78 | View Code Duplication | public function indexByUserFavorite($user_id) |
|
88 | |||
89 | /** |
||
90 | * 用户关注的帖子列表. |
||
91 | * |
||
92 | * @param $user_id |
||
93 | * |
||
94 | * @return \Dingo\Api\Http\Response |
||
95 | */ |
||
96 | View Code Duplication | public function indexByUserAttention($user_id) |
|
106 | |||
107 | /** |
||
108 | * 发布新帖子. |
||
109 | * |
||
110 | * @param \Illuminate\Http\Request $request |
||
111 | * |
||
112 | * @return \Illuminate\Http\Response |
||
113 | */ |
||
114 | View Code Duplication | public function store(Request $request) |
|
124 | |||
125 | /** |
||
126 | * 获取指定帖子的详细. |
||
127 | * |
||
128 | * @param int $id |
||
129 | * |
||
130 | * @return \Illuminate\Http\Response |
||
131 | */ |
||
132 | public function show($id) |
||
150 | |||
151 | /** |
||
152 | * 更新帖子. |
||
153 | * |
||
154 | * @param \Illuminate\Http\Request $request |
||
155 | * @param int $id |
||
156 | * |
||
157 | * @return \Illuminate\Http\Response |
||
158 | */ |
||
159 | public function update(Request $request, $id) |
||
163 | |||
164 | /** |
||
165 | * 删除帖子. |
||
166 | * |
||
167 | * @param int $id |
||
168 | * |
||
169 | * @return \Illuminate\Http\Response |
||
170 | */ |
||
171 | public function destroy($id) |
||
181 | |||
182 | /** |
||
183 | * 支持帖子. |
||
184 | * |
||
185 | * @param $id |
||
186 | * |
||
187 | * @return \Illuminate\Http\Response |
||
188 | */ |
||
189 | public function voteUp($id) |
||
198 | |||
199 | /** |
||
200 | * 反对帖子. |
||
201 | * |
||
202 | * @param $id |
||
203 | * |
||
204 | * @return \Illuminate\Http\Response |
||
205 | */ |
||
206 | public function voteDown($id) |
||
215 | |||
216 | /** |
||
217 | * 所有帖子列表接口的通用部分. |
||
218 | * |
||
219 | * @return \Dingo\Api\Http\Response |
||
220 | */ |
||
221 | protected function commonIndex() |
||
241 | |||
242 | /** |
||
243 | * 用于客户端的帖子详细 Web View. |
||
244 | * |
||
245 | * @param $id |
||
246 | * |
||
247 | * @return \Illuminate\View\View |
||
248 | */ |
||
249 | public function showWebView($id) |
||
255 | |||
256 | /** |
||
257 | * 收藏帖子. |
||
258 | * |
||
259 | * @param $topic_id |
||
260 | * |
||
261 | * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
||
262 | */ |
||
263 | View Code Duplication | public function favorite($topic_id) |
|
275 | |||
276 | /** |
||
277 | * 取消收藏帖子. |
||
278 | * |
||
279 | * @param $topic_id |
||
280 | * |
||
281 | * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
||
282 | */ |
||
283 | View Code Duplication | public function unFavorite($topic_id) |
|
295 | |||
296 | /** |
||
297 | * 关注帖子. |
||
298 | * |
||
299 | * @param $topic_id |
||
300 | * |
||
301 | * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
||
302 | */ |
||
303 | View Code Duplication | public function attention($topic_id) |
|
315 | |||
316 | /** |
||
317 | * 取消关注帖子. |
||
318 | * |
||
319 | * @param $topic_id |
||
320 | * |
||
321 | * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response |
||
322 | */ |
||
323 | View Code Duplication | public function unAttention($topic_id) |
|
335 | |||
336 | /** |
||
337 | * 注册帖子列表接口通用的引入项. |
||
338 | */ |
||
339 | protected function registerListApiIncludes() |
||
345 | } |
||
346 |
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.