Conditions | 7 |
Paths | 10 |
Total Lines | 68 |
Code Lines | 42 |
Lines | 27 |
Ratio | 39.71 % |
Changes | 2 | ||
Bugs | 1 | Features | 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 |
||
19 | public function actionList($index) |
||
20 | { |
||
21 | // set header |
||
22 | $this->setJsonHeader(); |
||
23 | // get config count per page |
||
24 | $perPage = (int)AppRecord::getConfig('widget', 'Comments', 'perPage'); |
||
25 | // offset can be only integer |
||
26 | $index = (int)$index; |
||
27 | $offset = $perPage * $index; |
||
28 | // get comment target path and check |
||
29 | $path = (string)App::$Request->query->get('path'); |
||
30 | if (Str::likeEmpty($path)) { |
||
31 | throw new JsonException('Wrong path'); |
||
32 | } |
||
33 | |||
34 | // select comments from db and check it |
||
35 | $records = CommentPost::where('pathway', '=', $path) |
||
36 | ->skip($offset) |
||
37 | ->take($perPage) |
||
38 | ->get(); |
||
39 | |||
40 | if ($records->count() < 1) { |
||
41 | throw new JsonException('No comments is found'); |
||
42 | } |
||
43 | |||
44 | // build output json data as array |
||
45 | $data = []; |
||
46 | View Code Duplication | foreach ($records as $comment) { |
|
|
|||
47 | // comment can be passed from registered user (with unique ID) or from guest (without ID) |
||
48 | $userName = __('Unknown'); |
||
49 | $userAvatar = App::$Alias->scriptUrl . '/upload/user/avatar/small/default.jpg'; |
||
50 | $userObject = $comment->getUser(); |
||
51 | if ($userObject !== null) { |
||
52 | $userName = $userObject->getProfile()->nick; |
||
53 | $userAvatar = $userObject->getProfile()->getAvatarUrl('small'); |
||
54 | } else { |
||
55 | if (!Str::likeEmpty($comment->guest_name)) { |
||
56 | $userName = App::$Security->strip_tags($comment->guest_name); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | // build output json data |
||
61 | $data[] = [ |
||
62 | 'id' => $comment->id, |
||
63 | 'text' => $comment->message, |
||
64 | 'date' => Date::convertToDatetime($comment->created_at, Date::FORMAT_TO_HOUR), |
||
65 | 'user' => [ |
||
66 | 'id' => $comment->user_id, |
||
67 | 'name' => $userName, |
||
68 | 'avatar' => $userAvatar |
||
69 | ], |
||
70 | 'answers' => $comment->getAnswerCount() |
||
71 | ]; |
||
72 | } |
||
73 | |||
74 | // calculate comments left count |
||
75 | $count = CommentPost::where('pathway', '=', $path)->count(); |
||
76 | $count -= $offset + $perPage; |
||
77 | if ($count < 0) { |
||
78 | $count = 0; |
||
79 | } |
||
80 | |||
81 | return json_encode([ |
||
82 | 'status' => 1, |
||
83 | 'data' => $data, |
||
84 | 'leftCount' => $count |
||
85 | ]); |
||
86 | } |
||
87 | |||
136 | } |
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.