Conditions | 9 |
Paths | 17 |
Total Lines | 62 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | 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 |
||
132 | public function ajax_rate() { |
||
133 | |||
134 | $id = $this->input->post('cid'); |
||
135 | $type = $this->input->post('type'); |
||
136 | $rating = (int) $this->input->post('val'); |
||
137 | |||
138 | if ($id != null && $type != null && !$this->session->userdata('voted_g' . $id . $type) == true) { |
||
139 | //Check if rating exists |
||
140 | $check = $this->rating_model->get_rating($id, $type); |
||
141 | if ($check != null) { |
||
142 | $this->new_votes = $check->votes + 1; |
||
143 | $this->new_rating = $check->rating + $rating; |
||
144 | $data = [ |
||
145 | 'votes' => $this->new_votes, |
||
146 | 'rating' => $this->new_rating, |
||
147 | ]; |
||
148 | $rating_res = $this->new_rating / $this->new_votes * 20; |
||
149 | $votes_res = $this->new_votes; |
||
150 | $this->rating_model->update_rating($id, $type, $data); |
||
151 | } else { |
||
152 | $data = [ |
||
153 | 'id_type' => $id, |
||
154 | 'type' => $type, |
||
155 | 'votes' => 1, |
||
156 | 'rating' => $rating, |
||
157 | ]; |
||
158 | $votes_res = 1; |
||
159 | $rating_res = $rating * 20; |
||
160 | $this->rating_model->insert_rating($data); |
||
161 | } |
||
162 | //Change rating for product |
||
163 | if ($type == 'product') { |
||
164 | if (SProductsQuery::create()->findPk($id) !== null) { |
||
165 | $model = SProductsRatingQuery::create()->findPk($id); |
||
166 | if ($model === null) { |
||
167 | $model = new SProductsRating; |
||
168 | $model->setProductId($id); |
||
169 | } |
||
170 | $rating_res = (($model->getRating() + $rating) / ($model->getVotes() + 1)) * 20; |
||
171 | $votes_res = $model->getVotes() + 1; |
||
172 | |||
173 | $model->setVotes($model->getVotes() + 1); |
||
174 | $model->setRating($model->getRating() + $rating); |
||
175 | $model->save(); |
||
176 | } |
||
177 | } |
||
178 | //Save in session user's info |
||
179 | $this->session->set_userdata('voted_g' . $id . $type, true); |
||
180 | |||
181 | //If ajax request than return data for with new rating and votes |
||
182 | if ($this->input->is_ajax_request()) { |
||
183 | return json_encode( |
||
184 | [ |
||
185 | 'rate' => "$rating_res", |
||
186 | 'votes' => "$votes_res", |
||
187 | ] |
||
188 | ); |
||
189 | } |
||
190 | } else { |
||
191 | return json_encode(['rate' => null]); |
||
192 | } |
||
193 | } |
||
194 | |||
239 | } |