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 |
||
25 | class TopicRepository extends BaseRepository implements TopicRepositoryInterface |
||
26 | { |
||
27 | use IncludeUserTrait, DispatchesJobs; |
||
28 | |||
29 | protected $favorite_table = 'favorites'; |
||
30 | protected $attention_table = 'attentions'; |
||
31 | |||
32 | /** |
||
33 | * Specify Validator Rules. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $rules = [ |
||
38 | ValidatorInterface::RULE_CREATE => [ |
||
39 | 'title' => 'required|min:2', |
||
40 | 'body' => 'required|min:2', |
||
41 | 'node_id' => 'required|integer|exists:nodes,id', |
||
42 | ], |
||
43 | ValidatorInterface::RULE_UPDATE => [ |
||
44 | |||
45 | ], |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * Specify Model class name. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | public function model() |
||
54 | { |
||
55 | return Topic::class; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * 引入帖子最后评论者. |
||
60 | * |
||
61 | * @param $default_columns |
||
62 | */ |
||
63 | View Code Duplication | public function includeLastReplyUser($default_columns) |
|
|
|||
64 | { |
||
65 | $available_include = Includable::make('last_reply_user') |
||
66 | ->setDefaultColumns($default_columns) |
||
67 | ->setAllowColumns(Reply::$includable) |
||
68 | ->setForeignKey('last_reply_user_id'); |
||
69 | |||
70 | app(IncludeManager::class)->add($available_include); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * 引入帖子所属节点. |
||
75 | * |
||
76 | * @param $default_columns |
||
77 | */ |
||
78 | View Code Duplication | public function includeNode($default_columns) |
|
87 | |||
88 | /** |
||
89 | * 引入帖子的评论. |
||
90 | * |
||
91 | * @param $default_columns |
||
92 | */ |
||
93 | View Code Duplication | public function includeReplies($default_columns) |
|
102 | |||
103 | /** |
||
104 | * 引入帖子每个的评论发布者. |
||
105 | * |
||
106 | * @param $default_columns |
||
107 | */ |
||
108 | public function includeRepliesUser($default_columns) |
||
117 | |||
118 | /** |
||
119 | * Boot up the repository, pushing criteria. |
||
120 | */ |
||
121 | public function boot() |
||
125 | |||
126 | /** |
||
127 | * 支持帖子. |
||
128 | * |
||
129 | * @param Topic $topic |
||
130 | * |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function voteUp(Topic $topic) |
||
160 | |||
161 | /** |
||
162 | * 反对帖子. |
||
163 | * |
||
164 | * @param Topic $topic |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function voteDown(Topic $topic) |
||
193 | |||
194 | /** |
||
195 | * 重置投票. |
||
196 | * |
||
197 | * @param $topic_id |
||
198 | * @param $user_id |
||
199 | * |
||
200 | * @return mixed |
||
201 | */ |
||
202 | protected function resetVote($topic_id, $user_id) |
||
210 | |||
211 | /** |
||
212 | * 是否已经支持帖子. |
||
213 | * |
||
214 | * @param $topic_id |
||
215 | * @param $user_id |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | View Code Duplication | public function userUpVoted($topic_id, $user_id) |
|
228 | |||
229 | /** |
||
230 | * 是否已经反对帖子. |
||
231 | * |
||
232 | * @param $topic_id |
||
233 | * @param $user_id |
||
234 | * |
||
235 | * @return bool |
||
236 | */ |
||
237 | View Code Duplication | public function userDownVoted($topic_id, $user_id) |
|
246 | |||
247 | /** |
||
248 | * 用户是否已经收藏帖子. |
||
249 | * |
||
250 | * @param $topic_id |
||
251 | * @param $user_id |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function userFavorite($topic_id, $user_id) |
||
259 | |||
260 | /** |
||
261 | * 用户是否已经关注帖子. |
||
262 | * |
||
263 | * @param $topic_id |
||
264 | * @param $user_id |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function userAttention($topic_id, $user_id) |
||
272 | |||
273 | /** |
||
274 | * 用户收藏的帖子. |
||
275 | * |
||
276 | * @param $user_id |
||
277 | * @param $columns |
||
278 | * |
||
279 | * @return Paginator |
||
280 | */ |
||
281 | public function favoriteTopicsWithPaginator($user_id, $columns = ['*']) |
||
285 | |||
286 | /** |
||
287 | * 用户关注的帖子. |
||
288 | * |
||
289 | * @param $user_id |
||
290 | * @param $columns |
||
291 | * |
||
292 | * @return Paginator |
||
293 | */ |
||
294 | public function attentionTopicsWithPaginator($user_id, $columns = ['*']) |
||
298 | |||
299 | /** |
||
300 | * 通过 attentions 或 favorites 查找帖子. |
||
301 | * |
||
302 | * @param $table |
||
303 | * @param $user_id |
||
304 | * @param $columns |
||
305 | * |
||
306 | * @return Paginator |
||
307 | */ |
||
308 | public function getTopicsWithPaginatorBy($table, $user_id, $columns = ['*']) |
||
331 | |||
332 | /** |
||
333 | * 添加 node_id 过滤条件. |
||
334 | * |
||
335 | * @param $node_id |
||
336 | * |
||
337 | * @return $this |
||
338 | */ |
||
339 | public function byNodeId($node_id) |
||
345 | |||
346 | /** |
||
347 | * 添加 user_id 过滤条件. |
||
348 | * |
||
349 | * @param $user_id |
||
350 | * |
||
351 | * @return $this |
||
352 | */ |
||
353 | public function byUserId($user_id) |
||
359 | |||
360 | /** |
||
361 | * 收藏帖子. |
||
362 | * |
||
363 | * @param $topic_id |
||
364 | * @param $user_id |
||
365 | */ |
||
366 | View Code Duplication | public function favorite($topic_id, $user_id) |
|
377 | |||
378 | /** |
||
379 | * 取消收藏帖子. |
||
380 | * |
||
381 | * @param $topic_id |
||
382 | * @param $user_id |
||
383 | */ |
||
384 | public function unFavorite($topic_id, $user_id) |
||
390 | |||
391 | /** |
||
392 | * 关注帖子. |
||
393 | * |
||
394 | * @param $topic_id |
||
395 | * @param $user_id |
||
396 | */ |
||
397 | View Code Duplication | public function attention($topic_id, $user_id) |
|
408 | |||
409 | /** |
||
410 | * 取消关注帖子. |
||
411 | * |
||
412 | * @param $topic_id |
||
413 | * @param $user_id |
||
414 | */ |
||
415 | public function unAttention($topic_id, $user_id) |
||
421 | } |
||
422 |
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.