| Conditions | 23 |
| Paths | 22 |
| Total Lines | 169 |
| Code Lines | 133 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 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)) { |
||
| 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)->getObjects($criteria); |
||
| 268 | $count = \count($voteObjs); |
||
| 269 | $itemRating['nb_vote'] = $count; |
||
| 270 | |||
| 271 | foreach ($voteObjs as $voteObj) { |
||
| 272 | $currentRating += $voteObj->getVar('rate'); |
||
| 273 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
| 274 | $voted = true; |
||
| 275 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
| 276 | } |
||
| 277 | } |
||
| 278 | unset($criteria); |
||
| 279 | |||
| 280 | $itemRating['avg_rate_value'] = 0; |
||
| 281 | if ($count > 0) { |
||
| 282 | $itemRating['avg_rate_value'] = \number_format($currentRating / $count, 2); |
||
| 283 | } |
||
| 284 | if (1 == $count) { |
||
| 285 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_1); |
||
| 286 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_1); |
||
| 287 | } else { |
||
| 288 | $text = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_X); |
||
| 289 | $shorttext = \str_replace('%c', $itemRating['avg_rate_value'], \_MA_BLOG_RATING_CURRENT_SHORT_X); |
||
| 290 | } |
||
| 291 | $text = \str_replace('%m', $max_units, $text); |
||
| 292 | $text = \str_replace('%t', $itemRating['nb_vote'], $text); |
||
| 293 | $shorttext = \str_replace('%t', $itemRating['nb_vote'], $shorttext); |
||
| 294 | $itemRating['text'] = $text; |
||
| 295 | $itemRating['shorttext'] = $shorttext; |
||
| 296 | $itemRating['size'] = ($itemRating['avg_rate_value'] * $rating_unitwidth) . 'px'; |
||
| 297 | $itemRating['maxsize'] = ($max_units * $rating_unitwidth) . 'px'; |
||
| 298 | |||
| 299 | $itemRating['ip'] = $ip; |
||
| 300 | $itemRating['uid'] = $uid; |
||
| 301 | $itemRating['voted'] = $voted; |
||
| 302 | // YouTube Liking ========================================== |
||
| 303 | } elseif (Constants::RATING_LIKES === $ratingbarsValue) { |
||
| 304 | // get count of "dislikes" |
||
| 305 | $criteria = new \CriteriaCompo(); |
||
| 306 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 307 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 308 | $criteria->add(new \Criteria('rate', 0, '<')); |
||
| 309 | |||
| 310 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 311 | $count = \count($voteObjs); |
||
| 312 | |||
| 313 | foreach ($voteObjs as $voteObj) { |
||
| 314 | $currentRating += $voteObj->getVar('rate'); |
||
| 315 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
| 316 | $voted = true; |
||
| 317 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
| 318 | } |
||
| 319 | } |
||
| 320 | unset($criteria); |
||
| 321 | $itemRating['dislikes'] = $count; |
||
| 322 | |||
| 323 | // get count of "likes" |
||
| 324 | $criteria = new \CriteriaCompo(); |
||
| 325 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 326 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 327 | $criteria->add(new \Criteria('rate', 0, '>')); |
||
| 328 | |||
| 329 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 330 | $count = \count($voteObjs); |
||
| 331 | $currentRating = 0; |
||
| 332 | foreach ($voteObjs as $voteObj) { |
||
| 333 | $currentRating += $voteObj->getVar('rate'); |
||
| 334 | if (($voteObj->getVar('ip') == $ip && 0 == $uid) || ($uid > 0 && $uid == $voteObj->getVar('uid'))) { |
||
| 335 | $voted = true; |
||
| 336 | $itemRating['id'] = $voteObj->getVar('ratingid'); |
||
| 337 | } |
||
| 338 | } |
||
| 339 | unset($criteria); |
||
| 340 | $itemRating['likes'] = $count; |
||
| 341 | |||
| 342 | $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['dislikes']; |
||
| 343 | $itemRating['ip'] = $ip; |
||
| 344 | $itemRating['uid'] = $uid; |
||
| 345 | $itemRating['voted'] = $voted; |
||
| 346 | // Facebook Reactions ========================================== |
||
| 347 | } elseif (Constants::RATING_REACTION === $ratingbarsValue) { |
||
| 348 | $criteria = new \CriteriaCompo(); |
||
| 349 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 350 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 351 | $criteria->add(new \Criteria('rate', 1)); |
||
| 352 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 353 | $count = \count($voteObjs); |
||
| 354 | $itemRating['likes'] = $count; |
||
| 355 | |||
| 356 | $criteria = new \CriteriaCompo(); |
||
| 357 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 358 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 359 | $criteria->add(new \Criteria('rate', 2)); |
||
| 360 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 361 | $count = \count($voteObjs); |
||
| 362 | $itemRating['love'] = $count; |
||
| 363 | |||
| 364 | $criteria = new \CriteriaCompo(); |
||
| 365 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 366 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 367 | $criteria->add(new \Criteria('rate', 3)); |
||
| 368 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 369 | $count = \count($voteObjs); |
||
| 370 | $itemRating['smile'] = $count; |
||
| 371 | |||
| 372 | $criteria = new \CriteriaCompo(); |
||
| 373 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 374 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 375 | $criteria->add(new \Criteria('rate', 4)); |
||
| 376 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 377 | $count = \count($voteObjs); |
||
| 378 | $itemRating['wow'] = $count; |
||
| 379 | |||
| 380 | $criteria = new \CriteriaCompo(); |
||
| 381 | $criteria->add(new \Criteria(static::IDENTIFIER, $itemId)); |
||
| 382 | $criteria->add(new \Criteria(static::SOURCE, $source)); |
||
| 383 | $criteria->add(new \Criteria('rate', 5)); |
||
| 384 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 385 | $count = \count($voteObjs); |
||
| 386 | $itemRating['sad'] = $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', 6)); |
||
| 392 | $voteObjs = $this->helper->getHandler(static::ENTITYNAME)->getObjects($criteria); |
||
| 393 | $count = \count($voteObjs); |
||
| 394 | $itemRating['angry'] = $count; |
||
| 395 | |||
| 396 | |||
| 397 | $itemRating['nb_vote'] = $itemRating['likes'] + $itemRating['love'] + $itemRating['smile'] + $itemRating['wow'] + $itemRating['sad'] + $itemRating['angry']; |
||
| 398 | $itemRating['ip'] = $ip; |
||
| 399 | $itemRating['uid'] = $uid; |
||
| 400 | $itemRating['voted'] = $voted; |
||
| 401 | } else { |
||
| 402 | $itemRating['uid'] = $uid; |
||
| 403 | $itemRating['nb_vote'] = $count; |
||
| 404 | $itemRating['voted'] = $voted; |
||
| 405 | $itemRating['ip'] = $ip; |
||
| 406 | } |
||
| 407 | return $itemRating; |
||
| 408 | } |
||
| 430 |