| Conditions | 33 |
| Paths | 22 |
| Total Lines | 157 |
| Code Lines | 119 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | 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 |
||
| 74 | public function getItemRating($itemId = null, $source = null): array |
||
| 75 | { |
||
| 76 | $itemId = $itemId ?? 0; |
||
| 77 | $source = $source ?? 0; |
||
| 78 | $xoopsUser = $GLOBALS['xoopsUser']; |
||
| 79 | |||
| 80 | $itemRating = []; |
||
| 81 | $itemRating['nb_vote'] = 0; |
||
| 82 | $uid = \is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0; |
||
| 83 | $voted = false; |
||
| 84 | $ip = \getenv('REMOTE_ADDR'); |
||
| 85 | $currentRating = 0; |
||
| 86 | $count = 0; |
||
| 87 | |||
| 88 | $max_units = 10; |
||
| 89 | $ratingbarsValue = (int)$this->helper->getConfig('ratingbars'); |
||
| 90 | $ratingArray = [Constants::RATING_5STARS, Constants::RATING_10STARS, Constants::RATING_10NUM]; |
||
| 91 | |||
| 92 | if (in_array($ratingbarsValue, $ratingArray)) { |
||
| 93 | $rating_unitwidth = 25; |
||
| 94 | if (Constants::RATING_5STARS === (int)$this->helper->getConfig('ratingbars')) { |
||
| 95 | $max_units = 5; |
||
| 96 | } |
||
| 97 | |||
| 98 | $criteria = new \CriteriaCompo(); |
||
| 99 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 100 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 101 | |||
| 102 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
|
|
|||
| 103 | $count = \count($voteObjs); |
||
| 104 | $itemRating['nb_vote'] = $count; |
||
| 105 | |||
| 106 | foreach ($voteObjs as $voteObj) { |
||
| 107 | $currentRating += $voteObj->getVar('rate'); |
||
| 108 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
| 109 | $voted = true; |
||
| 110 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | unset($criteria); |
||
| 114 | |||
| 115 | $itemRating['avg_rate_value'] = 0; |
||
| 116 | if ($count > 0) { |
||
| 117 | $itemRating['avg_rate_value'] = \number_format($currentRating / $count, 2); |
||
| 118 | } |
||
| 119 | if (1 == $count) { |
||
| 120 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_1); |
||
| 121 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_1); |
||
| 122 | } else { |
||
| 123 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_X); |
||
| 124 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_X); |
||
| 125 | } |
||
| 126 | $text = \str_replace('%m', $max_units, $text); |
||
| 127 | $text = \str_replace('%t', $itemRating['nb_vote'], $text); |
||
| 128 | $shorttext = \str_replace('%t', $itemRating['nb_vote'], $shorttext); |
||
| 129 | $itemRating['text'] = $text; |
||
| 130 | $itemRating['shorttext'] = $shorttext; |
||
| 131 | $itemRating['size'] = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px'; |
||
| 132 | $itemRating['maxsize'] = ($max_units * $rating_unitwidth) . 'px'; |
||
| 133 | |||
| 134 | $itemRating['ip'] = $ip; |
||
| 135 | $itemRating['uid'] = $uid; |
||
| 136 | $itemRating['voted'] = $voted; |
||
| 137 | // YouTube Liking ========================================== |
||
| 138 | } elseif (Constants::RATING_LIKES === (int)$this->helper->getConfig('ratingbars')) { |
||
| 139 | // get count of "dislikes" |
||
| 140 | $criteria = new \CriteriaCompo(); |
||
| 141 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 142 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 143 | $criteria->add(new \Criteria('rate', 0, '<')); |
||
| 144 | |||
| 145 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 146 | $count = \count($voteObjs); |
||
| 147 | |||
| 148 | foreach ($voteObjs as $voteObj) { |
||
| 149 | $currentRating += $voteObj->getVar('rate'); |
||
| 150 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
| 151 | $voted = true; |
||
| 152 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | unset($criteria); |
||
| 156 | $itemRating['dislikes'] = $count; |
||
| 157 | |||
| 158 | // get count of "likes" |
||
| 159 | $criteria = new \CriteriaCompo(); |
||
| 160 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 161 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 162 | $criteria->add(new \Criteria('rate', 0, '>')); |
||
| 163 | |||
| 164 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 165 | $count = \count($voteObjs); |
||
| 166 | $currentRating = 0; |
||
| 167 | foreach ($voteObjs as $voteObj) { |
||
| 168 | $currentRating += $voteObj->getVar('rate'); |
||
| 169 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
| 170 | $voted = true; |
||
| 171 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | unset($criteria); |
||
| 175 | $itemRating['likes'] = $count; |
||
| 176 | |||
| 177 | $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes']; |
||
| 178 | $itemRating['ip'] = $ip; |
||
| 179 | $itemRating['uid'] = $uid; |
||
| 180 | $itemRating['voted'] = $voted; |
||
| 181 | // Facebook Reactions ========================================== |
||
| 182 | } elseif (Constants::RATING_REACTION === (int)$this->helper->getConfig('ratingbars')) { |
||
| 183 | $criteria = new \CriteriaCompo(); |
||
| 184 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 185 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 186 | $criteria->add(new \Criteria('rate', 0, '<')); |
||
| 187 | |||
| 188 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 189 | $count = \count($voteObjs); |
||
| 190 | $itemRating['nb_vote'] = $count; |
||
| 191 | |||
| 192 | foreach ($voteObjs as $voteObj) { |
||
| 193 | $currentRating += $voteObj->getVar('rate'); |
||
| 194 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
| 195 | $voted = true; |
||
| 196 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | unset($criteria); |
||
| 200 | $itemRating['dislikes'] = $count; |
||
| 201 | |||
| 202 | $criteria = new \CriteriaCompo(); |
||
| 203 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 204 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 205 | $criteria->add(new \Criteria('rate', 0, '>')); |
||
| 206 | |||
| 207 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 208 | $count = \count($voteObjs); |
||
| 209 | $currentRating = 0; |
||
| 210 | foreach ($voteObjs as $voteObj) { |
||
| 211 | $currentRating += $voteObj->getVar('rate'); |
||
| 212 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
| 213 | $voted = true; |
||
| 214 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | unset($criteria); |
||
| 218 | $itemRating['likes'] = $count; |
||
| 219 | |||
| 220 | $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes']; |
||
| 221 | $itemRating['ip'] = $ip; |
||
| 222 | $itemRating['uid'] = $uid; |
||
| 223 | $itemRating['voted'] = $voted; |
||
| 224 | } else { |
||
| 225 | $itemRating['uid'] = $uid; |
||
| 226 | $itemRating['nb_vote'] = $count; |
||
| 227 | $itemRating['voted'] = $voted; |
||
| 228 | $itemRating['ip'] = $ip; |
||
| 229 | } |
||
| 230 | return $itemRating; |
||
| 231 | } |
||
| 430 |