GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — filters-dev ( b32603 )
by
unknown
11:53
created

RatingShowWidget::run()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 47
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 47
rs 6.7272
cc 7
eloc 30
nc 5
nop 0
1
<?php
2
3
namespace app\modules\review\widgets\rating;
4
5
use app\modules\review\models\RatingItem;
6
use app\modules\review\models\RatingValues;
7
use app\modules\review\models\Review;
8
use Yii;
9
use yii\base\Widget;
10
use yii\helpers\ArrayHelper;
11
12
class RatingShowWidget extends Widget
13
{
14
    public $objectModel;
15
    public $calcFunction;
16
    public $viewFile = 'rating-show';
17
    /**
18
     * @var Review|null
19
     */
20
    public $reviewModel = null;
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function init()
26
    {
27
        parent::init();
28
        RatingAsset::register($this->view);
29
        if (is_array($this->calcFunction) && count($this->calcFunction) >= 2) {
30
            $class = array_shift($this->calcFunction);
31
            $method = array_shift($this->calcFunction);
32
            if (class_exists($class) && method_exists($class, $method)) {
33
                $this->calcFunction = [$class, $method];
34
            } else {
35
                $this->calcFunction = [$this, 'calculateRating'];
36
            }
37
        } elseif (!$this->calcFunction instanceof \Closure) {
38
            $this->calcFunction = [$this, 'calculateRating'];
39
        }
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function run()
46
    {
47
        parent::run();
48
        if (!is_null($this->objectModel)) {
49
            $groupedRatingValues = RatingValues::getValuesByObjectModel($this->objectModel);
50
            if (!empty($groupedRatingValues)) {
51
                $groups = [];
52
                foreach ($groupedRatingValues as $groupId => $group) {
53
                    $ratingItem = RatingItem::findById($groupId);
54
                    $groups[] = [
55
                        'name' => !is_null($ratingItem) ? $ratingItem->name : Yii::t('app', 'Unknown rating'),
56
                        'rating' => call_user_func($this->calcFunction, $group),
57
                        'votes' => count($group),
58
                    ];
59
                }
60
            } else {
61
                $groups = null;
62
            }
63
            return $this->render(
64
                $this->viewFile,
65
                [
66
                    'groups' => $groups,
67
                ]
68
            );
69
        } elseif (!is_null($this->reviewModel)) {
70
            $value = RatingValues::findOne(['rating_id' => $this->reviewModel->rating_id]);
71
            $groups = [];
72
73
            $ratingItem = RatingItem::findById(ArrayHelper::getValue($value, 'rating_item_id', 0));
0 ignored issues
show
Bug introduced by
It seems like $value defined by \app\modules\review\mode...eviewModel->rating_id)) on line 70 can also be of type boolean; however, yii\helpers\BaseArrayHelper::getValue() does only seem to accept array|object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
            $group = [ArrayHelper::getValue($value, 'value')];
0 ignored issues
show
Bug introduced by
It seems like $value defined by \app\modules\review\mode...eviewModel->rating_id)) on line 70 can also be of type boolean; however, yii\helpers\BaseArrayHelper::getValue() does only seem to accept array|object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
75
76
            $groups[] = [
77
                'name' => !is_null($ratingItem) ? $ratingItem->name : Yii::t('app', 'Unknown rating'),
78
                'rating' => call_user_func($this->calcFunction, $group),
79
            ];
80
81
            return $this->render(
82
                $this->viewFile,
83
                [
84
                    'groups' => $groups,
85
                ]
86
            );
87
        } else {
88
            return '';
89
        }
90
91
    }
92
93
    /**
94
     * @param $values
95
     * @return float|int
96
     */
97
    private function calculateRating($values)
98
    {
99
        $count = count($values);
100
        if (0 === $sum = array_sum($values)) {
101
            return 0;
102
        }
103
        return $sum / $count;
104
    }
105
}
106