Conditions | 6 |
Paths | 11 |
Total Lines | 59 |
Code Lines | 30 |
Lines | 3 |
Ratio | 5.08 % |
Changes | 5 | ||
Bugs | 2 | 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 |
||
72 | public function actionList($index) |
||
73 | { |
||
74 | // set header |
||
75 | $this->setJsonHeader(); |
||
76 | // get configs |
||
77 | $configs = AppRecord::getConfigs('widget', 'Comments'); |
||
78 | // items per page |
||
79 | $perPage = (int)$configs['perPage']; |
||
80 | // offset can be only integer |
||
81 | $index = (int)$index; |
||
82 | $offset = $perPage * $index; |
||
83 | // get comment target path and check |
||
84 | $path = (string)App::$Request->query->get('path'); |
||
85 | if (Str::likeEmpty($path)) { |
||
86 | throw new NotFoundException('Wrong path'); |
||
87 | } |
||
88 | |||
89 | // select comments from db and check it |
||
90 | $query = CommentPost::where('pathway', '=', $path); |
||
91 | |||
92 | // check if comments is depend of language locale |
||
93 | View Code Duplication | if ((int)$configs['onlyLocale'] === 1) { |
|
94 | $query = $query->where('lang', '=', App::$Request->getLanguage()); |
||
95 | } |
||
96 | |||
97 | // get comments with offset and limit |
||
98 | $records = $query->skip($offset) |
||
99 | ->take($perPage) |
||
100 | ->get(); |
||
101 | |||
102 | // check if records is not empty |
||
103 | if ($records->count() < 1) { |
||
104 | throw new NotFoundException(__('There is no comments found yet. You can be the first!')); |
||
105 | } |
||
106 | |||
107 | // build output json data as array |
||
108 | $data = []; |
||
109 | foreach ($records as $comment) { |
||
110 | // prepare specified data to output response, based on entity model |
||
111 | $commentResponse = new EntityCommentData($comment); |
||
112 | |||
113 | // build output json data |
||
114 | $data[] = $commentResponse->make(); |
||
115 | $commentResponse = null; |
||
116 | } |
||
117 | |||
118 | // calculate comments left count |
||
119 | $count = CommentPost::where('pathway', '=', $path)->count(); |
||
120 | $count -= $offset + $perPage; |
||
121 | if ($count < 0) { |
||
122 | $count = 0; |
||
123 | } |
||
124 | |||
125 | return json_encode([ |
||
126 | 'status' => 1, |
||
127 | 'data' => $data, |
||
128 | 'leftCount' => $count |
||
129 | ]); |
||
130 | } |
||
131 | |||
205 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.