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 — master ( b9b347...45f141 )
by Ivan
24s
created

PriceSliderRangeWidget::init()   C

Complexity

Conditions 10
Paths 15

Size

Total Lines 61
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 1
Metric Value
c 3
b 2
f 1
dl 0
loc 61
rs 6.4757
cc 10
eloc 41
nc 15
nop 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
namespace app\modules\shop\widgets;
3
4
use yii\db\Query;
5
use yii\helpers\ArrayHelper;
6
use Yii;
7
8
9
class PriceSliderRangeWidget extends SliderRangeWidget
10
{
11
12
    public $attributeName = 'Цена';
13
14
    public $minAttribute = 'price_min';
15
    public $maxAttribute = 'price_max';
16
    public $changeFlagAttribute = 'price_change_flag';
17
18
    public $categoryId;
19
20
21
    public function init()
22
    {
23
        $cacheKey = 'priceRangeCategory' . $this->categoryId;
24
25
        if (!$data = Yii::$app->cache->get($cacheKey)) {
26
            $dataMin = (new Query())->select([
27
                    'MIN(product.price / currency.convert_nominal * currency.convert_rate) AS min_price',
28
                    'product.currency_id AS min_currency',
29
                ])
30
                ->from(['product', 'product_category', 'currency'])
31
                ->where('product.id = product_category.object_model_id AND (product.currency_id = currency.id)')
32
                ->andWhere(
33
                    [
34
                        'product.active' => 1,
35
                        'product_category.category_id' => $this->categoryId
36
                    ]
37
                )->one();
38
39
            $dataMax = (new Query())->select([
40
                'MAX(product.price / currency.convert_nominal * currency.convert_rate) AS max_price',
41
                'product.currency_id AS max_currency',
42
            ])
43
                ->from(['product', 'product_category', 'currency'])
44
                ->where('product.id = product_category.object_model_id AND (product.currency_id = currency.id)')
45
                ->andWhere(
46
                        [
47
                            'product.active' => 1,
48
                            'product_category.category_id' => $this->categoryId
49
                        ]
50
                )->one();
51
52
            $data = ArrayHelper::merge($dataMax, $dataMin);
53
            if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
54
                $data['min_price'] = (int) $data['min_price'];
55
                $data['max_price'] = (int) $data['max_price'];
56
                Yii::$app->cache->set($cacheKey, $data, 86400);
57
            }
58
59
        }
60
        if ($data && isset($data['min_price']) && isset($data['max_price'])) {
61
            $this->minValue = $data['min_price'];
62
            $this->maxValue = $data['max_price'];
63
            $get = ArrayHelper::merge(Yii::$app->request->get(), Yii::$app->request->post());
64
65
            if (isset($get[$this->minAttribute]) && is_numeric($get[$this->minAttribute])) {
66
                $this->changeFlagDefaultValue = 1;
67
                $this->minValueNow = $get[$this->minAttribute];
68
            } else {
69
                $this->minValueNow = $this->minValue;
70
            }
71
72
            if (isset($get[$this->maxAttribute]) && is_numeric($get[$this->maxAttribute])) {
73
                $this->changeFlagDefaultValue = 1;
74
                $this->maxValueNow = $get[$this->maxAttribute];
75
            } else {
76
                $this->maxValueNow = $this->maxValue;
77
            }
78
        }
79
        return parent::init();
80
81
    }
82
83
84
}