Conditions | 23 |
Paths | 22 |
Total Lines | 178 |
Code Lines | 142 |
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 declare(strict_types=1); |
||
240 | public function getItemRating5($itemObj = null, $source = null): array |
||
241 | { |
||
242 | $itemId = $itemObj->itemid(); |
||
243 | $source = $source ?? 0; |
||
244 | $xoopsUser = $GLOBALS['xoopsUser']; |
||
245 | |||
246 | $itemRating = []; |
||
247 | $itemRating['nb_vote'] = 0; |
||
248 | $uid = \is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0; |
||
249 | $voted = false; |
||
250 | $ip = \getenv('REMOTE_ADDR'); |
||
251 | $currentRating = 0; |
||
252 | $count = 0; |
||
253 | |||
254 | $max_units = 10; |
||
255 | $ratingbarsValue = $itemObj->votetype(); |
||
256 | $ratingArray = [Constants::RATING_5STARS, Constants::RATING_10STARS, Constants::RATING_10NUM]; |
||
257 | |||
258 | if (\in_array($ratingbarsValue, $ratingArray, true)) { |
||
259 | $rating_unitwidth = 25; |
||
260 | if (Constants::RATING_5STARS === $ratingbarsValue) { |
||
261 | $max_units = 5; |
||
262 | } |
||
263 | |||
264 | $criteria = new \CriteriaCompo(); |
||
265 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
266 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
267 | |||
268 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
269 | ->getObjects($criteria); |
||
270 | $count = \count($voteObjs); |
||
271 | $itemRating['nb_vote'] = $count; |
||
272 | |||
273 | foreach ($voteObjs as $voteObj) { |
||
274 | $currentRating += $voteObj->getVar('rate'); |
||
275 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
276 | $voted = true; |
||
277 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
278 | } |
||
279 | } |
||
280 | unset($criteria); |
||
281 | |||
282 | $itemRating['avg_rate_value'] = 0; |
||
283 | if ($count > 0) { |
||
284 | $itemRating['avg_rate_value'] = \number_format($currentRating / $count, 2); |
||
285 | } |
||
286 | if (1 == $count) { |
||
287 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_1); |
||
288 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_1); |
||
289 | } else { |
||
290 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_X); |
||
291 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_PUBLISHER_RATING_CURRENT_SHORT_X); |
||
292 | } |
||
293 | $text = \str_replace('%m', (string)$max_units, $text); |
||
294 | $text = \str_replace('%t', (string)$itemRating['nb_vote'], $text); |
||
295 | $shorttext = \str_replace('%t', (string)$itemRating['nb_vote'], $shorttext); |
||
296 | $itemRating['text'] = $text; |
||
297 | $itemRating['shorttext'] = $shorttext; |
||
298 | $itemRating['size'] = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px'; |
||
299 | $itemRating['maxsize'] = ($max_units * $rating_unitwidth) . 'px'; |
||
300 | |||
301 | $itemRating['ip'] = $ip; |
||
302 | $itemRating['uid'] = $uid; |
||
303 | $itemRating['voted'] = $voted; |
||
304 | // YouTube Liking ========================================== |
||
305 | } elseif (Constants::RATING_LIKES === $ratingbarsValue) { |
||
306 | // get count of "dislikes" |
||
307 | $criteria = new \CriteriaCompo(); |
||
308 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
309 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
310 | $criteria->add(new \Criteria('rate', 0, '<')); |
||
311 | |||
312 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
313 | ->getObjects($criteria); |
||
314 | $count = \count($voteObjs); |
||
315 | |||
316 | foreach ($voteObjs as $voteObj) { |
||
317 | $currentRating += $voteObj->getVar('rate'); |
||
318 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
319 | $voted = true; |
||
320 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
321 | } |
||
322 | } |
||
323 | unset($criteria); |
||
324 | $itemRating['dislikes'] = $count; |
||
325 | |||
326 | // get count of "likes" |
||
327 | $criteria = new \CriteriaCompo(); |
||
328 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
329 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
330 | $criteria->add(new \Criteria('rate', 0, '>')); |
||
331 | |||
332 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
333 | ->getObjects($criteria); |
||
334 | $count = \count($voteObjs); |
||
335 | $currentRating = 0; |
||
336 | foreach ($voteObjs as $voteObj) { |
||
337 | $currentRating += $voteObj->getVar('rate'); |
||
338 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
339 | $voted = true; |
||
340 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
341 | } |
||
342 | } |
||
343 | unset($criteria); |
||
344 | $itemRating['likes'] = $count; |
||
345 | |||
346 | $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes']; |
||
347 | $itemRating['ip'] = $ip; |
||
348 | $itemRating['uid'] = $uid; |
||
349 | $itemRating['voted'] = $voted; |
||
350 | // Facebook Reactions ========================================== |
||
351 | } elseif (Constants::RATING_REACTION === $ratingbarsValue) { |
||
352 | $criteria = new \CriteriaCompo(); |
||
353 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
354 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
355 | $criteria->add(new \Criteria('rate', 1)); |
||
356 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
357 | ->getObjects($criteria); |
||
358 | $count = \count($voteObjs); |
||
359 | $itemRating['likes'] = $count; |
||
360 | |||
361 | $criteria = new \CriteriaCompo(); |
||
362 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
363 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
364 | $criteria->add(new \Criteria('rate', 2)); |
||
365 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
366 | ->getObjects($criteria); |
||
367 | $count = \count($voteObjs); |
||
368 | $itemRating['love'] = $count; |
||
369 | |||
370 | $criteria = new \CriteriaCompo(); |
||
371 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
372 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
373 | $criteria->add(new \Criteria('rate', 3)); |
||
374 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
375 | ->getObjects($criteria); |
||
376 | $count = \count($voteObjs); |
||
377 | $itemRating['smile'] = $count; |
||
378 | |||
379 | $criteria = new \CriteriaCompo(); |
||
380 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
381 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
382 | $criteria->add(new \Criteria('rate', 4)); |
||
383 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
384 | ->getObjects($criteria); |
||
385 | $count = \count($voteObjs); |
||
386 | $itemRating['wow'] = $count; |
||
387 | |||
388 | $criteria = new \CriteriaCompo(); |
||
389 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
390 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
391 | $criteria->add(new \Criteria('rate', 5)); |
||
392 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
393 | ->getObjects($criteria); |
||
394 | $count = \count($voteObjs); |
||
395 | $itemRating['sad'] = $count; |
||
396 | |||
397 | $criteria = new \CriteriaCompo(); |
||
398 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
399 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
400 | $criteria->add(new \Criteria('rate', 6)); |
||
401 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME) |
||
402 | ->getObjects($criteria); |
||
403 | $count = \count($voteObjs); |
||
404 | $itemRating['angry'] = $count; |
||
405 | |||
406 | $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['love'] + $itemRating['smile'] + $itemRating['wow'] + $itemRating['sad'] + $itemRating['angry']; |
||
407 | $itemRating['ip'] = $ip; |
||
408 | $itemRating['uid'] = $uid; |
||
409 | $itemRating['voted'] = $voted; |
||
410 | } else { |
||
411 | $itemRating['uid'] = $uid; |
||
412 | $itemRating['nb_vote'] = $count; |
||
413 | $itemRating['voted'] = $voted; |
||
414 | $itemRating['ip'] = $ip; |
||
415 | } |
||
416 | |||
417 | return $itemRating; |
||
418 | } |
||
534 |