Conditions | 35 |
Paths | 22 |
Total Lines | 148 |
Code Lines | 114 |
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 declare(strict_types=1); |
||
84 | public function getItemRating($itemId = 0, $source = 0) |
||
85 | { |
||
86 | $helper = \XoopsModules\Publisher\Helper::getInstance(); |
||
87 | |||
88 | $itemRating = []; |
||
89 | $itemRating['nb_ratings'] = 0; |
||
90 | $uid = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
91 | $voted = false; |
||
92 | $ip = \getenv('REMOTE_ADDR'); |
||
93 | $currentRating = 0; |
||
94 | $count = 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') |
||
111 | ->getObjects($criteria); |
||
112 | $count = \count($ratingObjs); |
||
113 | $itemRating['nb_ratings'] = $count; |
||
114 | |||
115 | foreach ($ratingObjs as $ratingObj) { |
||
116 | $currentRating += $ratingObj->getVar('rate_value'); |
||
117 | if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
||
118 | $voted = true; |
||
|
|||
119 | $itemRating['id'] = $ratingObj->getVar('rate_id'); |
||
120 | } |
||
121 | } |
||
122 | unset($criteria); |
||
123 | |||
124 | $itemRating['avg_rate_value'] = 0; |
||
125 | if ($count > 0) { |
||
126 | $itemRating['avg_rate_value'] = \number_format((float)$currentRating / $count, 2); |
||
127 | } |
||
128 | if (1 == $count) { |
||
129 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_1); |
||
130 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_1); |
||
131 | } else { |
||
132 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_X); |
||
133 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_X); |
||
134 | } |
||
135 | $text = \str_replace('%m', (string)$max_units, $text); |
||
136 | $text = \str_replace('%t', (string)$itemRating['nb_ratings'], $text); |
||
137 | $shorttext = \str_replace('%t', (string)$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') |
||
149 | ->getObjects($criteria); |
||
150 | $count = \count($ratingObjs); |
||
151 | |||
152 | foreach ($ratingObjs as $ratingObj) { |
||
153 | $currentRating += $ratingObj->getVar('rate_value'); |
||
154 | if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
||
155 | $voted = true; |
||
156 | $itemRating['id'] = $ratingObj->getVar('rate_id'); |
||
157 | } |
||
158 | } |
||
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') |
||
168 | ->getObjects($criteria); |
||
169 | $count = \count($ratingObjs); |
||
170 | $currentRating = 0; |
||
171 | foreach ($ratingObjs as $ratingObj) { |
||
172 | $currentRating += $ratingObj->getVar('rate_value'); |
||
173 | if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
||
174 | $voted = true; |
||
175 | $itemRating['id'] = $ratingObj->getVar('rate_id'); |
||
176 | } |
||
177 | } |
||
178 | unset($criteria); |
||
179 | $itemRating['likes'] = $count; |
||
180 | |||
181 | $count = $itemRating['likes'] + $itemRating['dislikes']; |
||
182 | // Facebook Reactions ========================================== |
||
183 | } elseif (Constants::RATING_REACTION === (int)$helper->getConfig('ratingbars')) { |
||
184 | $criteria = new \CriteriaCompo(); |
||
185 | $criteria->add(new \Criteria('rate_itemid', $itemId)); |
||
186 | $criteria->add(new \Criteria('rate_source', $source)); |
||
187 | $criteria->add(new \Criteria('rate_value', 0, '<')); |
||
188 | |||
189 | $ratingObjs = $helper->getHandler('ratings') |
||
190 | ->getObjects($criteria); |
||
191 | $count = \count($ratingObjs); |
||
192 | $itemRating['nb_ratings'] = $count; |
||
193 | |||
194 | foreach ($ratingObjs as $ratingObj) { |
||
195 | $currentRating += $ratingObj->getVar('rate_value'); |
||
196 | if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
||
197 | $voted = true; |
||
198 | $itemRating['id'] = $ratingObj->getVar('rate_id'); |
||
199 | } |
||
200 | } |
||
201 | unset($criteria); |
||
202 | $itemRating['dislikes'] = $count; |
||
203 | |||
204 | $criteria = new \CriteriaCompo(); |
||
205 | $criteria->add(new \Criteria('rate_itemid', $itemId)); |
||
206 | $criteria->add(new \Criteria('rate_source', $source)); |
||
207 | $criteria->add(new \Criteria('rate_value', 0, '>')); |
||
208 | |||
209 | $ratingObjs = $helper->getHandler('ratings') |
||
210 | ->getObjects($criteria); |
||
211 | $count = \count($ratingObjs); |
||
212 | $currentRating = 0; |
||
213 | foreach ($ratingObjs as $ratingObj) { |
||
214 | $currentRating += $ratingObj->getVar('rate_value'); |
||
215 | if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
||
216 | $voted = true; |
||
217 | $itemRating['id'] = $ratingObj->getVar('rate_id'); |
||
218 | } |
||
219 | } |
||
220 | unset($criteria); |
||
221 | $itemRating['likes'] = $count; |
||
222 | |||
223 | $count = $itemRating['likes'] + $itemRating['dislikes']; |
||
224 | } else { |
||
225 | $itemRating['uid'] = $uid; |
||
226 | $itemRating['nb_ratings'] = $count; |
||
227 | $itemRating['voted'] = $voted; |
||
228 | $itemRating['ip'] = $ip; |
||
229 | } |
||
230 | |||
231 | return $itemRating; |
||
232 | } |
||
249 |