Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 23 | class Comments extends ApiController |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Add comment or answer via ajax. |
||
| 27 | * @return string |
||
| 28 | * @throws NativeException |
||
| 29 | */ |
||
| 30 | public function actionAdd() |
||
| 31 | { |
||
| 32 | $this->setJsonHeader(); |
||
| 33 | $configs = AppRecord::getConfigs('widget', 'Comments'); |
||
| 34 | |||
| 35 | $replayTo = (int)$this->request->request->get('replay-to'); |
||
|
|
|||
| 36 | $model = null; |
||
| 37 | // check if its a answer (comment answer type) |
||
| 38 | if ($replayTo > 0) { |
||
| 39 | $model = new CommentAnswerAdd($configs); |
||
| 40 | $model->replayTo = $replayTo; |
||
| 41 | } else { // sounds like new comment row |
||
| 42 | $model = new CommentPostAdd($configs); |
||
| 43 | $model->pathway = App::$Security->strip_tags($this->request->request->get('pathway')); |
||
| 44 | } |
||
| 45 | |||
| 46 | // pass general comment params to model |
||
| 47 | $model->message = App::$Security->secureHtml((string)$this->request->request->get('message')); |
||
| 48 | $model->guestName = App::$Security->strip_tags($this->request->request->get('guest-name')); |
||
| 49 | |||
| 50 | // check model conditions before add new row |
||
| 51 | if ($model === null || !$model->check()) { |
||
| 52 | throw new NativeException('Unknown error'); |
||
| 53 | } |
||
| 54 | |||
| 55 | // add comment post or answer to database and get response active record row |
||
| 56 | $record = $model->buildRecord(); |
||
| 57 | // pass row to entity builder model |
||
| 58 | $response = new EntityCommentData($record); |
||
| 59 | |||
| 60 | return json_encode([ |
||
| 61 | 'status' => 1, |
||
| 62 | 'data' => $response->make() // build row to standard format |
||
| 63 | ]); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * List comments as json object with defined offset index |
||
| 68 | * @param int $index |
||
| 69 | * @return string |
||
| 70 | * @throws NotFoundException |
||
| 71 | */ |
||
| 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 | |||
| 133 | /** |
||
| 134 | * List answers by comment id as json object |
||
| 135 | * @param int $commentId |
||
| 136 | * @return string |
||
| 137 | * @throws ForbiddenException |
||
| 138 | * @throws NotFoundException |
||
| 139 | */ |
||
| 140 | public function actionShowanswers($commentId) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get commentaries count for pathway. Pathway should be array [itemId => pathway] |
||
| 178 | * @throws NativeException |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function actionCount() |
||
| 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.