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); |
||
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 | $currentRating = 0; |
||
95 | $count = 0; |
||
96 | |||
97 | if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars') |
||
98 | || Constants::RATING_10STARS === (int)$helper->getConfig('ratingbars') |
||
99 | || Constants::RATING_10NUM === (int)$helper->getConfig('ratingbars')) { |
||
100 | $rating_unitwidth = 25; |
||
101 | if (Constants::RATING_5STARS === (int)$helper->getConfig('ratingbars')) { |
||
102 | $max_units = 5; |
||
103 | } else { |
||
104 | $max_units = 10; |
||
105 | } |
||
106 | |||
107 | $criteria = new \CriteriaCompo(); |
||
108 | $criteria->add(new \Criteria('rate_itemid', $itemId)); |
||
109 | $criteria->add(new \Criteria('rate_source', $source)); |
||
110 | |||
111 | $ratingObjs = $helper->getHandler('ratings') |
||
112 | ->getObjects($criteria); |
||
113 | $count = \count($ratingObjs); |
||
114 | $itemRating['nb_ratings'] = $count; |
||
115 | |||
116 | foreach ($ratingObjs as $ratingObj) { |
||
117 | $currentRating += $ratingObj->getVar('rate_value'); |
||
118 | if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
||
119 | $voted = true; |
||
|
|||
120 | $itemRating['id'] = $ratingObj->getVar('rate_id'); |
||
121 | } |
||
122 | } |
||
123 | unset($criteria); |
||
124 | |||
125 | $itemRating['avg_rate_value'] = 0; |
||
126 | if ($count > 0) { |
||
127 | $itemRating['avg_rate_value'] = \number_format($currentRating / $count, 2); |
||
128 | } |
||
129 | if (1 == $count) { |
||
130 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_1); |
||
131 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_1); |
||
132 | } else { |
||
133 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_X); |
||
134 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_X); |
||
135 | } |
||
136 | $text = \str_replace('%m', (string)$max_units, $text); |
||
137 | $text = \str_replace('%t', (string)$itemRating['nb_ratings'], $text); |
||
138 | $shorttext = \str_replace('%t', (string)$itemRating['nb_ratings'], $shorttext); |
||
139 | $itemRating['text'] = $text; |
||
140 | $itemRating['shorttext'] = $shorttext; |
||
141 | $itemRating['size'] = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px'; |
||
142 | $itemRating['maxsize'] = ($max_units * $rating_unitwidth) . 'px'; |
||
143 | } elseif (Constants::RATING_LIKES === (int)$helper->getConfig('ratingbars')) { |
||
144 | $criteria = new \CriteriaCompo(); |
||
145 | $criteria->add(new \Criteria('rate_itemid', $itemId)); |
||
146 | $criteria->add(new \Criteria('rate_source', $source)); |
||
147 | $criteria->add(new \Criteria('rate_value', 0, '<')); |
||
148 | |||
149 | $ratingObjs = $helper->getHandler('Ratings') |
||
150 | ->getObjects($criteria); |
||
151 | $count = \count($ratingObjs); |
||
152 | |||
153 | foreach ($ratingObjs as $ratingObj) { |
||
154 | $currentRating += $ratingObj->getVar('rate_value'); |
||
155 | if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
||
156 | $voted = true; |
||
157 | $itemRating['id'] = $ratingObj->getVar('rate_id'); |
||
158 | } |
||
159 | } |
||
160 | unset($criteria); |
||
161 | $itemRating['dislikes'] = $count; |
||
162 | |||
163 | $criteria = new \CriteriaCompo(); |
||
164 | $criteria->add(new \Criteria('rate_itemid', $itemId)); |
||
165 | $criteria->add(new \Criteria('rate_source', $source)); |
||
166 | $criteria->add(new \Criteria('rate_value', 0, '>')); |
||
167 | |||
168 | $ratingObjs = $helper->getHandler('ratings') |
||
169 | ->getObjects($criteria); |
||
170 | $count = \count($ratingObjs); |
||
171 | $currentRating = 0; |
||
172 | foreach ($ratingObjs as $ratingObj) { |
||
173 | $currentRating += $ratingObj->getVar('rate_value'); |
||
174 | if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
||
175 | $voted = true; |
||
176 | $itemRating['id'] = $ratingObj->getVar('rate_id'); |
||
177 | } |
||
178 | } |
||
179 | unset($criteria); |
||
180 | $itemRating['likes'] = $count; |
||
181 | |||
182 | $count = $itemRating['likes'] + $itemRating['dislikes']; |
||
183 | // Facebook Reactions ========================================== |
||
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') |
||
191 | ->getObjects($criteria); |
||
192 | $count = \count($ratingObjs); |
||
193 | $itemRating['nb_ratings'] = $count; |
||
194 | |||
195 | foreach ($ratingObjs as $ratingObj) { |
||
196 | $currentRating += $ratingObj->getVar('rate_value'); |
||
197 | if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
||
198 | $voted = true; |
||
199 | $itemRating['id'] = $ratingObj->getVar('rate_id'); |
||
200 | } |
||
201 | } |
||
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') |
||
211 | ->getObjects($criteria); |
||
212 | $count = \count($ratingObjs); |
||
213 | $currentRating = 0; |
||
214 | foreach ($ratingObjs as $ratingObj) { |
||
215 | $currentRating += $ratingObj->getVar('rate_value'); |
||
216 | if (($ratingObj->getVar('rate_ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $ratingObj->getVar('rate_uid'))) { |
||
217 | $voted = true; |
||
218 | $itemRating['id'] = $ratingObj->getVar('rate_id'); |
||
219 | } |
||
220 | } |
||
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 | |||
232 | return $itemRating; |
||
233 | } |
||
250 |