| Conditions | 9 |
| Paths | 34 |
| Total Lines | 63 |
| Code Lines | 45 |
| 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 |
||
| 64 | public function show_star_rating($item = null, $registerScript = true) { |
||
| 65 | $this->list_for_show = $this->rating_model->get_settings(); |
||
| 66 | if ($this->list_for_show == null) { |
||
| 67 | $this->list_for_show = []; |
||
| 68 | } |
||
| 69 | $id = $this->core->core_data['id']; |
||
| 70 | $type = $this->core->core_data['data_type']; |
||
| 71 | |||
| 72 | // product rating |
||
| 73 | if ($item != null && $item instanceof SProducts) { |
||
| 74 | if ($item->getRating() != null) { |
||
| 75 | $rating_s = (int) $item->getRating() * 20; // rating in percent |
||
| 76 | } else { |
||
| 77 | $rating_s = 0; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** Берем все комментарии по данному товару*/ |
||
| 81 | $count_commment = $this->db->select('rate') |
||
| 82 | ->from('comments') |
||
| 83 | ->where('item_id', $item->getId()) |
||
| 84 | ->count_all_results(); |
||
| 85 | |||
| 86 | $data = [ |
||
| 87 | 'id_type' => $item->getId(), |
||
| 88 | 'type' => 'product', |
||
| 89 | 'votes' => $count_commment, |
||
| 90 | 'rating' => $rating_s, |
||
| 91 | ]; |
||
| 92 | $template = 'product_star_rating'; |
||
| 93 | } else { |
||
| 94 | if (in_array($type, array_keys($this->list_for_show))) { |
||
| 95 | $rating = $this->rating_model->get_rating($id, $type); |
||
| 96 | if ($rating->votes != 0) { |
||
| 97 | $rating_s = $rating->rating / $rating->votes * 20; //rating in percent |
||
| 98 | } else { |
||
| 99 | $rating_s = 0; |
||
| 100 | } |
||
| 101 | $data = [ |
||
| 102 | 'id' => $rating->id, |
||
| 103 | 'type' => $rating->type, |
||
| 104 | 'votes' => $rating->votes, |
||
| 105 | 'rating' => $rating_s, |
||
| 106 | ]; |
||
| 107 | |||
| 108 | $template = 'star_rating'; |
||
| 109 | } else { |
||
| 110 | $template = null; |
||
| 111 | return false; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | //Show template with prepared parameters |
||
| 116 | if ($template !== null) { |
||
| 117 | $renderTemplate = assetManager::create(); |
||
| 118 | } |
||
| 119 | $renderTemplate->setData($data) |
||
| 120 | ->registerStyle('style'); |
||
| 121 | if ($registerScript) { |
||
| 122 | $renderTemplate->registerScript('scripts'); |
||
| 123 | } |
||
| 124 | $renderTemplate->render($template, true); |
||
| 125 | return $this; |
||
| 126 | } |
||
| 127 | |||
| 239 | } |