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