| Conditions | 11 |
| Paths | 25 |
| Total Lines | 63 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 24 | public function save(Request $request, $entityId, $contentId) |
||
| 25 | { |
||
| 26 | $result = $this->checkParam($entityId, $contentId); |
||
| 27 | if ($result !== true) { |
||
| 28 | return $result; |
||
| 29 | } |
||
| 30 | |||
| 31 | $content = (string) $request->post('content', ''); |
||
| 32 | // 暂不支持html |
||
| 33 | $content = strip_tags($content); |
||
| 34 | if ($content === '') { |
||
| 35 | return [ |
||
| 36 | 'code' => 5, |
||
| 37 | 'msg' => '评论内容不能为空', |
||
| 38 | ]; |
||
| 39 | } |
||
| 40 | if (mb_strlen($content) > 1024) { |
||
| 41 | return [ |
||
| 42 | 'code' => 6, |
||
| 43 | 'msg' => '评论内容过长', |
||
| 44 | ]; |
||
| 45 | } |
||
| 46 | $pid = (int) $request->post('pid', 0); |
||
| 47 | if ($pid < 0) { |
||
| 48 | return [ |
||
| 49 | 'code' => 7, |
||
| 50 | 'msg' => 'invalid pid', |
||
| 51 | ]; |
||
| 52 | } |
||
| 53 | if ($pid > 0 && !($parentComment = \App\Repository\Admin\CommentRepository::find($pid))) { |
||
| 54 | return [ |
||
| 55 | 'code' => 8, |
||
| 56 | 'msg' => '引用评论不存在', |
||
| 57 | ]; |
||
| 58 | } |
||
| 59 | |||
| 60 | try { |
||
| 61 | $rid = $pid === 0 ? $pid : ($parentComment->rid === 0 ? $parentComment->id : $parentComment->rid); |
||
|
|
|||
| 62 | \App\Repository\Admin\CommentRepository::add([ |
||
| 63 | 'entity_id' => $entityId, |
||
| 64 | 'content_id' => $contentId, |
||
| 65 | 'pid' => $pid, |
||
| 66 | 'rid' => $rid, |
||
| 67 | 'content' => $content, |
||
| 68 | 'user_id' => Auth::guard('member')->id(), |
||
| 69 | ]); |
||
| 70 | if ($rid > 0) { |
||
| 71 | // 清除缓存 |
||
| 72 | Cache::forget('comment_replay:' . $rid); |
||
| 73 | |||
| 74 | // 回复数+1 |
||
| 75 | CommentRepository::addReplyCount($rid); |
||
| 76 | } |
||
| 77 | return [ |
||
| 78 | 'code' => 0, |
||
| 79 | 'msg' => '', |
||
| 80 | 'reload' => true, |
||
| 81 | ]; |
||
| 82 | } catch (Throwable $e) { |
||
| 83 | Log::error($e); |
||
| 84 | return [ |
||
| 85 | 'code' => 500, |
||
| 86 | 'msg' => '评论失败:内部错误', |
||
| 87 | ]; |
||
| 164 |