| Conditions | 6 |
| Paths | 11 |
| Total Lines | 60 |
| Code Lines | 31 |
| Lines | 3 |
| Ratio | 5 % |
| Changes | 7 | ||
| Bugs | 3 | 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)$this->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 | ->where('moderate', '=', 0); |
||
| 92 | |||
| 93 | // check if comments is depend of language locale |
||
| 94 | View Code Duplication | if ((int)$configs['onlyLocale'] === 1) { |
|
| 95 | $query = $query->where('lang', '=', $this->request->getLanguage()); |
||
| 96 | } |
||
| 97 | |||
| 98 | // get comments with offset and limit |
||
| 99 | $records = $query->skip($offset) |
||
| 100 | ->take($perPage) |
||
| 101 | ->get(); |
||
| 102 | |||
| 103 | // check if records is not empty |
||
| 104 | if ($records->count() < 1) { |
||
| 105 | throw new NotFoundException(__('There is no comments found yet. You can be the first!')); |
||
| 106 | } |
||
| 107 | |||
| 108 | // build output json data as array |
||
| 109 | $data = []; |
||
| 110 | foreach ($records as $comment) { |
||
| 111 | // prepare specified data to output response, based on entity model |
||
| 112 | $commentResponse = new EntityCommentData($comment); |
||
| 113 | |||
| 114 | // build output json data |
||
| 115 | $data[] = $commentResponse->make(); |
||
| 116 | $commentResponse = null; |
||
| 117 | } |
||
| 118 | |||
| 119 | // calculate comments left count |
||
| 120 | $count = CommentPost::where('pathway', '=', $path)->where('moderate', '=', 0)->count(); |
||
| 121 | $count -= $offset + $perPage; |
||
| 122 | if ($count < 0) { |
||
| 123 | $count = 0; |
||
| 124 | } |
||
| 125 | |||
| 126 | return json_encode([ |
||
| 127 | 'status' => 1, |
||
| 128 | 'data' => $data, |
||
| 129 | 'leftCount' => $count |
||
| 130 | ]); |
||
| 131 | } |
||
| 132 | |||
| 207 | } |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.