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