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 ( f95cc0...b08239 )
by Ivan
10:56
created

FilterWidget   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 29
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 233
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 34 1
F getPossibleSelections() 0 169 28
1
<?php
2
3
namespace app\widgets\filter;
4
5
use app\models\Object;
6
use app\models\ObjectStaticValues;
7
use app\models\Property;
8
use app\models\PropertyGroup;
9
use app\models\PropertyStaticValues;
10
use app\modules\Shop\models\Product;
11
use Yii;
12
use yii\base\Widget;
13
use yii\caching\TagDependency;
14
use yii\db\Query;
15
use yii\helpers\ArrayHelper;
16
use yii\helpers\Json;
17
18
class FilterWidget extends Widget
19
{
20
    protected $possibleSelections = null;
21
    public $categoryGroupId = 0;
22
    public $currentSelections = [];
23
    public $goBackAlignment = 'left';
24
    public $objectId = null;
25
    public $onlyAvailableFilters = true;
26
    public $disableInsteadOfHide = false;
27
    public $route = '/shop/product/list';
28
    public $title = 'Filter';
29
    public $viewFile = 'filterWidget';
30
    protected $disabled_ids = [];
31
    public $render_dynamic = true;
32
33
    /**
34
     * @var null|array Array of group ids to display in filter, null to display all available for Object
35
     */
36
    public $onlyGroupsIds = null;
37
38
    /**
39
     * @var array Additional params passed to filter view
40
     */
41
    public $additionalViewParams = [];
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function run()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
47
    {
48
        Yii::beginProfile("FilterWidget");
49
50
        $view = $this->getView();
51
        FilterWidgetAsset::register($view);
52
        $view->registerJs(
53
            "jQuery('#{$this->id}').getFilters();"
54
        );
55
56
        Yii::beginProfile("GetPossibleSelections");
57
        $this->getPossibleSelections();
58
        Yii::endProfile("GetPossibleSelections");
59
60
        $result = $this->render(
61
            $this->viewFile,
62
            ArrayHelper::merge([
63
                'id' => $this->id,
64
                'current_selections' => $this->currentSelections,
65
                'possible_selections' => $this->possibleSelections,
66
                'object_id' => $this->objectId,
67
                'title' => $this->title,
68
                'go_back_alignment' => $this->goBackAlignment,
69
                'route' => $this->route,
70
                'category_group_id' => $this->categoryGroupId,
71
                'disabled_ids' => $this->disabled_ids,
72
                'render_dynamic' => $this->render_dynamic,
73
            ], $this->additionalViewParams)
74
        );
75
76
        Yii::endProfile("FilterWidget");
77
78
        return $result;
79
    }
80
81
    public function getPossibleSelections()
82
    {
83
        $data = [
84
            'propertyIds' => [],
85
            'propertyStaticValueIds' => [],
86
        ];
87
        if ($this->onlyAvailableFilters) {
88
            Yii::beginProfile("onlyAvailableFilters");
89
            $object = Object::findById($this->objectId);
90
            if (!is_null($object) && isset($this->currentSelections['last_category_id'])) {
91
92
                $cacheKey = 'FilterWidget: ' . $object->id . ':' . $this->currentSelections['last_category_id'] . ':'
93
                    . Json::encode($this->currentSelections['properties']);
94
                $data = Yii::$app->cache->get($cacheKey);
95
                if ($data === false) {
96
                    $data = [];
97
                    Yii::beginProfile("ObjectIds for this category");
98
99
                    $psv = [];
100
                    array_walk_recursive($this->currentSelections['properties'], function ($item) use (&$psv) {
101
                        if (is_array($item)) {
102
                            continue;
103
                        }
104
                        $psv[] = $item;
105
                    });
106
107
                    $query = (new Query)
108
                        ->select('pc.object_model_id')
109
                        ->from($object->categories_table_name . ' pc')
110
                        ->innerJoin(Product::tableName() . ' product', 'product.id = pc.object_model_id')
111
                        ->where([
112
                            'pc.category_id' => $this->currentSelections['last_category_id'],
113
                            'product.active' => 1,
114
                        ]);
115
116
                    if (count($this->currentSelections['properties'])) {
117
                        $query->innerJoin([
118
                            'osvm' => (new Query)
119
                                ->select('object_model_id')
120
                                ->distinct()
121
                                ->from(ObjectStaticValues::tableName())
122
                                ->where([
123
                                    'object_id' => $object->id,
124
                                    'property_static_value_id' => $psv
125
                                ])
126
                                ->groupBy('object_model_id')
127
                                ->having(['count(object_model_id)' => count($psv)])
128
                            ],
129
                            'osvm.object_model_id = pc.object_model_id'
130
                        );
131
                    }
132
133
                    Yii::endProfile("ObjectIds for this category");
134
135
                    $ids = array_map('intval', $query->column());
136
                    $query = null;
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
137
138
                    Yii::beginProfile("all PSV ids");
139
                    $data['propertyStaticValueIds'] = [];
140
                    if (count($ids) !== 0) {
141
                        $q4psv = (new Query())
142
                            ->select('property_static_value_id')
143
                            ->from(ObjectStaticValues::tableName())
144
                            ->distinct()
145
                            ->where(['object_id' => $object->id])
146
                            ->andWhere('object_model_id in (' . implode(',', $ids) . ')');
147
148
149
                        $data['propertyStaticValueIds'] = array_map('intval', $q4psv->column());
150
                    }
151
                    Yii::endProfile("all PSV ids");
152
153
154
                    $ids = null;
0 ignored issues
show
Unused Code introduced by
$ids is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
155
156
                    Yii::beginProfile("Property ids from PSV ids");
157
                    $data['propertyIds'] = [];
158
                    if (count($data['propertyStaticValueIds']) !== 0) {
159
                        $data['propertyIds'] = PropertyStaticValues::find()
160
                            ->select('property_id')
161
                            ->distinct()
162
                            ->where(['dont_filter' => 0])
163
                            ->andWhere('id IN (' . implode(',', $data['propertyStaticValueIds']) . ')')
164
                            ->asArray()
165
                            ->column();
166
                    }
167
                    Yii::endProfile("Property ids from PSV ids");
168
169
                    Yii::$app->cache->set(
170
                        $cacheKey,
171
                        $data,
172
                        86400,
173
                        new TagDependency([
174
                            'tags' => [
175
                                \devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag($object->object_class)
176
                            ],
177
                        ])
178
                    );
179
                    $object = null;
0 ignored issues
show
Unused Code introduced by
$object is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
180
                }
181
            }
182
            Yii::endProfile("onlyAvailableFilters");
183
        }
184
185
        $this->possibleSelections = [];
186
187
        $groups = PropertyGroup::getForObjectId($this->objectId);
188
189
        foreach ($groups as $group) {
190
191
            if ($this->onlyGroupsIds !== null) {
192
                if (in_array($group->id, $this->onlyGroupsIds) === false) {
193
                    // skip this group
194
                    continue;
195
                }
196
            }
197
198
            if ($group->is_internal) {
199
                continue;
200
            }
201
            $this->possibleSelections[$group->id] = [
202
                'group' => $group,
203
                'selections' => [],
204
                'static_selections' => [],
205
                'dynamic_selections' => [],
206
            ];
207
            $props = Property::getForGroupId($group->id);
208
            foreach ($props as $p) {
0 ignored issues
show
Bug introduced by
The expression $props of type null|array<integer,object<app\models\Property>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
209
210
                if ($this->onlyAvailableFilters && !in_array($p->id, $data['propertyIds'])) {
211
                    if ($this->disableInsteadOfHide === false) {
212
                        continue;
213
                    }
214
                }
215
                if ($p->dont_filter) {
216
                    continue;
217
                }
218
                if ($p->has_static_values) {
219
                    $propertyStaticValues = PropertyStaticValues::getValuesForPropertyId($p->id);
220
                    foreach ($propertyStaticValues as $key => $propertyStaticValue) {
221
222
                        if ($propertyStaticValue['dont_filter']) {
223
                            unset($propertyStaticValues[$key]);
224
                        }
225
                    }
226
                    if ($this->onlyAvailableFilters) {
227
                        foreach ($propertyStaticValues as $key => $propertyStaticValue) {
228
229
                            if (!in_array($propertyStaticValue['id'], $data['propertyStaticValueIds'])) {
230
                                if ($this->disableInsteadOfHide === true) {
231
                                    $this->disabled_ids[]=$propertyStaticValue['id'];
232
                                } else {
233
                                    unset($propertyStaticValues[$key]);
234
                                }
235
                            }
236
                        }
237
                    }
238
239
                    $this->possibleSelections[$group->id]['static_selections'][$p->id] = $propertyStaticValues;
240
                } elseif ($p->is_column_type_stored && $p->value_type == 'NUMBER') {
241
                    $this->possibleSelections[$group->id]['dynamic_selections'][] = $p->id;
242
                }
243
244
            }
245
            if (count($this->possibleSelections[$group->id]) === 0) {
246
                unset($this->possibleSelections[$group->id]);
247
            }
248
        }
249
    }
250
}
251