Conditions | 12 |
Paths | 25 |
Total Lines | 64 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
25 | public function save(Request $request, $entityId, $contentId) |
||
26 | { |
||
27 | $result = $this->checkParam($entityId, $contentId); |
||
28 | if ($result !== true) { |
||
29 | return $result; |
||
30 | } |
||
31 | |||
32 | $content = (string) $request->post('content', ''); |
||
33 | // 暂不支持html |
||
34 | $content = strip_tags($content); |
||
35 | if ($content === '') { |
||
36 | return [ |
||
37 | 'code' => 5, |
||
38 | 'msg' => '评论内容不能为空', |
||
39 | ]; |
||
40 | } |
||
41 | if (mb_strlen($content) > 1024) { |
||
42 | return [ |
||
43 | 'code' => 6, |
||
44 | 'msg' => '评论内容过长', |
||
45 | ]; |
||
46 | } |
||
47 | $pid = (int) $request->post('pid', 0); |
||
48 | if ($pid < 0) { |
||
49 | return [ |
||
50 | 'code' => 7, |
||
51 | 'msg' => 'invalid pid', |
||
52 | ]; |
||
53 | } |
||
54 | if ($pid > 0 && !($parentComment = \App\Repository\Admin\CommentRepository::find($pid))) { |
||
55 | return [ |
||
56 | 'code' => 8, |
||
57 | 'msg' => '引用评论不存在', |
||
58 | ]; |
||
59 | } |
||
60 | |||
61 | try { |
||
62 | $rid = $pid === 0 ? $pid : ($parentComment->rid == 0 ? $parentComment->id : $parentComment->rid); |
||
|
|||
63 | \App\Repository\Admin\CommentRepository::add([ |
||
64 | 'entity_id' => $entityId, |
||
65 | 'content_id' => $contentId, |
||
66 | 'pid' => $pid, |
||
67 | 'rid' => $rid, |
||
68 | 'content' => $content, |
||
69 | 'user_id' => Auth::guard('member')->id(), |
||
70 | 'reply_user_id' => $pid > 0 ? $parentComment->user_id : 0, |
||
71 | ]); |
||
72 | if ($rid > 0) { |
||
73 | // 清除缓存 |
||
74 | Cache::forget('comment_replay:' . $rid); |
||
75 | |||
76 | // 回复数+1 |
||
77 | CommentRepository::addReplyCount($rid); |
||
78 | } |
||
79 | return [ |
||
80 | 'code' => 0, |
||
81 | 'msg' => '', |
||
82 | 'reload' => true, |
||
83 | ]; |
||
84 | } catch (Throwable $e) { |
||
85 | Log::error($e); |
||
86 | return [ |
||
87 | 'code' => 500, |
||
88 | 'msg' => '评论失败:内部错误', |
||
89 | ]; |
||
192 |