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