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