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

ProductController   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 448
Duplicated Lines 16.52 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 56
c 5
b 1
f 1
lcom 1
cbo 14
dl 74
loc 448
rs 6.5957

4 Methods

Rating   Name   Duplication   Size   Complexity  
F actionList() 20 196 32
B actionShow() 20 75 6
B actionSearch() 23 79 4
C buildBreadcrumbsArray() 11 62 14

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ProductController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ProductController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace app\modules\shop\controllers;
4
5
use app\components\Controller;
6
use app\extensions\DefaultTheme\components\BaseWidget;
7
use app\extensions\DefaultTheme\models\ThemeActiveWidgets;
8
use app\extensions\DefaultTheme\models\ThemeWidgets;
9
use app\extensions\DefaultTheme\widgets\FilterSets\Widget;
10
use app\models\Object;
11
use app\models\PropertyStaticValues;
12
use app\models\Search;
13
use app\modules\core\helpers\ContentBlockHelper;
14
use app\modules\core\helpers\EventTriggeringHelper;
15
use app\modules\core\models\ContentBlock;
16
use app\modules\shop\events\ProductPageShowed;
17
use app\modules\shop\exceptions\EmptyFilterHttpException;
18
use app\modules\shop\models\Category;
19
use app\modules\shop\models\Product;
20
use app\traits\DynamicContentTrait;
21
use devgroup\TagDependencyHelper\ActiveRecordHelper;
22
use Yii;
23
use yii\caching\TagDependency;
24
use yii\data\Pagination;
25
use yii\helpers\ArrayHelper;
26
use yii\helpers\Json;
27
use yii\helpers\Url;
28
use yii\web\ForbiddenHttpException;
29
use yii\web\NotFoundHttpException;
30
use yii\web\Response;
31
use yii\web\ServerErrorHttpException;
32
33
class ProductController extends Controller
34
{
35
    use DynamicContentTrait;
36
37
    /**
38
     * Products listing by category with filtration support.
39
     *
40
     * @return string
41
     * @throws \Exception
42
     * @throws NotFoundHttpException
43
     * @throws ServerErrorHttpException
44
     */
45
    public function actionList()
0 ignored issues
show
Coding Style introduced by
actionList uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
actionList uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
46
    {
47
48
        $request = Yii::$app->request;
49
50
        if (null === $request->get('category_group_id')) {
51
            throw new NotFoundHttpException;
52
        }
53
54
        if (null === $object = Object::getForClass(Product::className())) {
55
            throw new ServerErrorHttpException('Object not found.');
56
        }
57
58
        $category_group_id = intval($request->get('category_group_id', 0));
59
60
        $title_append = $request->get('title_append', '');
61
        if (!empty($title_append)) {
62
            $title_append = is_array($title_append) ? implode(' ', $title_append) : $title_append;
63
            unset($_GET['title_append']);
64
        }
65
66
        $title_prepend = $request->get("title_prepend", "");
67
        if (!empty($title_prepend)) {
68
            $title_prepend = is_array($title_prepend) ? implode(" ", $title_prepend) : $title_prepend;
69
            unset($_GET["title_prepend"]);
70
        }
71
72
        $values_by_property_id = $request->get('properties', []);
73
        if (!is_array($values_by_property_id)) {
74
            $values_by_property_id = [$values_by_property_id];
75
        }
76
77
        if (Yii::$app->request->isPost && isset($_POST['properties'])) {
78
            if (is_array($_POST['properties'])) {
79
                foreach ($_POST['properties'] as $key => $value) {
80
                    if (isset($values_by_property_id[$key])) {
81
                        $values_by_property_id[$key] = array_unique(
82
                            ArrayHelper::merge(
83
                                $values_by_property_id[$key],
84
                                $value
85
                            )
86
                        );
87
                    } else {
88
                        $values_by_property_id[$key] = array_unique($value);
89
                    }
90
                }
91
            }
92
        }
93
94
        $selected_category_ids = $request->get('categories', []);
95
        if (!is_array($selected_category_ids)) {
96
            $selected_category_ids = [$selected_category_ids];
97
        }
98
99
        if (null !== $selected_category_id = $request->get('last_category_id')) {
100
            $selected_category_id = intval($selected_category_id);
101
        }
102
103
        $result = Product::filteredProducts(
104
            $category_group_id,
105
            $values_by_property_id,
106
            $selected_category_id,
107
            false,
108
            null,
109
            true,
110
            false
111
        );
112
        /** @var Pagination $pages */
113
        $pages = $result['pages'];
114
        if (Yii::$app->response->is_prefiltered_page) {
115
            $pages->route = '/' . Yii::$app->request->pathInfo;
116
            $pages->params = [
117
118
            ];
119
        }
120
        $allSorts = $result['allSorts'];
121
        $products = $result['products'];
122
123
        // throw 404 if we are at filtered page without any products
124
        if (!Yii::$app->request->isAjax && !empty($values_by_property_id) && empty($products)) {
125
            throw new EmptyFilterHttpException();
126
        }
127
128
        if (null !== $selected_category = $selected_category_id) {
129
            if ($selected_category_id > 0) {
130
                if (null !== $selected_category = Category::findById($selected_category_id, null)) {
131 View Code Duplication
                    if (!empty($selected_category->meta_description)) {
132
                        $this->view->registerMetaTag(
133
                            [
134
                                'name' => 'description',
135
                                'content' => ContentBlockHelper::compileContentString(
136
                                    $selected_category->meta_description,
137
                                    Product::className() . ":{$selected_category->id}:meta_description",
138
                                    new TagDependency(
139
                                        [
140
                                            'tags' => [
141
                                                ActiveRecordHelper::getCommonTag(ContentBlock::className()),
142
                                                ActiveRecordHelper::getCommonTag(Category::className())
143
                                            ]
144
                                        ]
145
                                    )
146
                                )
147
                            ],
148
                            'meta_description'
149
                        );
150
                    }
151
152
                    $this->view->title = $selected_category->title;
153
                }
154
            }
155
        }
156
        if (is_null($selected_category) || !$selected_category->active) {
157
            throw new NotFoundHttpException;
158
        }
159
160
        if (!empty($title_append)) {
161
            $this->view->title .= " " . $title_append;
162
        }
163
164
        if (!empty($title_prepend)) {
165
            $this->view->title = "{$title_prepend} {$this->view->title}";
166
        }
167
168
        $this->view->blocks['h1'] = $selected_category->h1;
169
        $this->view->blocks['announce'] = $selected_category->announce;
170
        $this->view->blocks['content'] = $selected_category->content;
171
172
        $this->loadDynamicContent($object->id, 'shop/product/list', $request->get());
173
174
        $params = [
175
            'model' => $selected_category,
176
            'selected_category' => $selected_category,
177
            'selected_category_id' => $selected_category_id,
178
            'selected_category_ids' => $selected_category_ids,
179
            'values_by_property_id' => $values_by_property_id,
180
            'products' => $products,
181
            'object' => $object,
182
            'category_group_id' => $category_group_id,
183
            'pages' => $pages,
184
            'title_append' => $title_append,
185
            'selections' => $request->get(),
186
            'breadcrumbs' => $this->buildBreadcrumbsArray($selected_category, null, $values_by_property_id),
187
            'allSorts' => $allSorts,
188
        ];
189
        $viewFile = $this->computeViewFile($selected_category, 'list');
190
191
        if (Yii::$app->request->isAjax) {
192
            Yii::$app->response->format = Response::FORMAT_JSON;
193
194
            $content = $this->renderAjax(
195
                $viewFile,
196
                $params
197
            );
198
            $filters = '';
199
            $activeWidgets = ThemeActiveWidgets::getActiveWidgets();
200
            foreach ($activeWidgets as $activeWidget) {
201
                if ($activeWidget->widget->widget == Widget::className()) {
202
                    /** @var ThemeWidgets $widgetModel */
203
                    $widgetModel = $activeWidget->widget;
204
                    /** @var BaseWidget $widgetClassName */
205
                    $widgetClassName =  $widgetModel->widget;
206
                    $widgetConfiguration = Json::decode($widgetModel->configuration_json, true);
207
                    if (!is_array($widgetConfiguration)) {
208
                        $widgetConfiguration = [];
209
                    }
210
                    $activeWidgetConfiguration = Json::decode($activeWidget->configuration_json, true);
211
                    if (!is_array($activeWidgetConfiguration)) {
212
                        $activeWidgetConfiguration  = [];
213
                    }
214
                    $config = ArrayHelper::merge($widgetConfiguration, $activeWidgetConfiguration);
215
                    $config['themeWidgetModel'] = $widgetModel;
216
                    $config['partRow'] = $activeWidget->part;
217
                    $config['activeWidget'] = $activeWidget;
218
                    $filters = $widgetClassName::widget($config);
219
                }
220
            }
221
            return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('content' =...lues_by_property_id))); (array) is incompatible with the return type documented by app\modules\shop\control...tController::actionList of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
222
                'content' => $content,
223
                'filters' => $filters,
224
                'title' => $this->view->title,
225
                'url' => Url::to(
226
                    [
227
                        '/shop/product/list',
228
                        'last_category_id' => $selected_category_id,
229
                        'category_group_id' => $category_group_id,
230
                        'properties' => $values_by_property_id
231
                    ]
232
                ),
233
            ];
234
        } else {
235
            return $this->render(
236
                $viewFile,
237
                $params
238
            );
239
        }
240
    }
241
242
    /**
243
     * Product page view
244
     *
245
     * @param null $model_id
246
     * @return string
247
     * @throws NotFoundHttpException
248
     * @throws ServerErrorHttpException
249
     */
250
    public function actionShow($model_id = null)
251
    {
252
        if (null === $object = Object::getForClass(Product::className())) {
253
            throw new ServerErrorHttpException('Object not found.');
254
        }
255
256
        $product = Product::findById($model_id);
257
258
        $request = Yii::$app->request;
259
260
        $values_by_property_id = $request->get('properties', []);
261
        if (!is_array($values_by_property_id)) {
262
            $values_by_property_id = [$values_by_property_id];
263
        }
264
265
        $selected_category_id = $request->get('last_category_id');
266
267
        $selected_category_ids = $request->get('categories', []);
268
        if (!is_array($selected_category_ids)) {
269
            $selected_category_ids = [$selected_category_ids];
270
        }
271
272
        $category_group_id = intval($request->get('category_group_id', 0));
273
274
        // trigger that we are to show product to user!
275
        // wow! such product! very events!
276
        $specialEvent = new ProductPageShowed([
277
            'product_id' => $product->id,
278
        ]);
279
        EventTriggeringHelper::triggerSpecialEvent($specialEvent);
280
281 View Code Duplication
        if (!empty($product->meta_description)) {
282
            $this->view->registerMetaTag(
283
                [
284
                    'name' => 'description',
285
                    'content' => ContentBlockHelper::compileContentString(
286
                        $product->meta_description,
287
                        Product::className() . ":{$product->id}:meta_description",
288
                        new TagDependency(
289
                            [
290
                                'tags' => [
291
                                    ActiveRecordHelper::getCommonTag(ContentBlock::className()),
292
                                    ActiveRecordHelper::getCommonTag(Product::className())
293
                                ]
294
                            ]
295
                        )
296
                    )
297
                ],
298
                'meta_description'
299
            );
300
        }
301
302
        $selected_category = ($selected_category_id > 0) ? Category::findById($selected_category_id) : null;
303
304
        $this->view->title = $product->title;
305
        $this->view->blocks['h1'] = $product->h1;
306
        $this->view->blocks['announce'] = $product->announce;
307
        $this->view->blocks['content'] = $product->content;
308
        $this->view->blocks['title'] = $product->title;
309
310
311
        return $this->render(
312
            $this->computeViewFile($product, 'show'),
313
            [
314
                'model' => $product,
315
                'category_group_id' => $category_group_id,
316
                'values_by_property_id' => $values_by_property_id,
317
                'selected_category_id' => $selected_category_id,
318
                'selected_category' => $selected_category,
319
                'selected_category_ids' => $selected_category_ids,
320
                'object' => $object,
321
                'breadcrumbs' => $this->buildBreadcrumbsArray($selected_category, $product)
322
            ]
323
        );
324
    }
325
326
    /**
327
     * Search handler
328
     * @return array
329
     * @throws ForbiddenHttpException
330
     */
331
    public function actionSearch()
332
    {
333
        $headers = Yii::$app->response->getHeaders();
334
        $headers->set('X-Robots-Tag', 'none');
335
        $headers->set('X-Frame-Options', 'SAMEORIGIN');
336
        $headers->set('X-Content-Type-Options', 'nosniff');
337
        if (!Yii::$app->request->isAjax) {
338
            throw new ForbiddenHttpException();
339
        }
340
        $model = new Search();
341
        $model->load(Yii::$app->request->get());
342
        $cacheKey = 'ProductSearchIds: ' . $model->q;
343
        $ids = Yii::$app->cache->get($cacheKey);
344
        if ($ids === false) {
345
            $ids = ArrayHelper::merge(
346
                $model->searchProductsByDescription(),
347
                $model->searchProductsByProperty()
348
            );
349
            Yii::$app->cache->set(
350
                $cacheKey,
351
                $ids,
352
                86400,
353
                new TagDependency(
354
                    [
355
                        'tags' => ActiveRecordHelper::getCommonTag(Product::className()),
356
                    ]
357
                )
358
            );
359
        }
360
361
        /** @var \app\modules\shop\ShopModule $module */
362
        $module = Yii::$app->modules['shop'];
363
364
        $pages = new Pagination(
365
            [
366
                'defaultPageSize' => $module->searchResultsLimit,
367
                'forcePageParam' => false,
368
                'totalCount' => count($ids),
369
            ]
370
        );
371
        $cacheKey .= ' : ' . $pages->offset;
372
        $products = Yii::$app->cache->get($cacheKey);
373 View Code Duplication
        if ($products === false) {
374
            $products = Product::find()->where(
375
                [
376
                    'in',
377
                    '`id`',
378
                    array_slice(
379
                        $ids,
380
                        $pages->offset,
381
                        $pages->limit
382
                    )
383
                ]
384
            )->addOrderBy('sort_order')->with('images')->all();
385
            Yii::$app->cache->set(
386
                $cacheKey,
387
                $products,
388
                86400,
389
                new TagDependency(
390
                    [
391
                        'tags' => ActiveRecordHelper::getCommonTag(Product::className()),
392
                    ]
393
                )
394
            );
395
        }
396
        Yii::$app->response->format = Response::FORMAT_JSON;
397
        return [
398
            'view' => $this->renderAjax(
399
                'search',
400
                [
401
                    'model' => $model,
402
                    'pages' => $pages,
403
                    'products' => $products,
404
                ]
405
            ),
406
            'totalCount' => count($ids),
407
        ];
408
409
    }
410
411
    /**
412
    * This function build array for widget "Breadcrumbs"
413
    * @param Category $selCat - model of current category
414
    * @param Product|null $product - model of product, if current page is a page of product
415
    * @param array $properties - array of properties and static values
416
    * Return an array for widget or empty array
417
    */
418
    private function buildBreadcrumbsArray($selCat, $product = null, $properties = [])
419
    {
420
        if ($selCat === null) {
421
            return [];
422
        }
423
424
        // init
425
        $breadcrumbs = [];
426
        if ($product !== null) {
427
            $crumbs[$product->slug] = !empty($product->breadcrumbs_label) ? $product->breadcrumbs_label : '';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$crumbs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $crumbs = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
428
        }
429
        $crumbs[$selCat->slug] = $selCat->breadcrumbs_label;
0 ignored issues
show
Bug introduced by
The variable $crumbs does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
430
431
        // get basic data
432
        $parent = $selCat->parent_id > 0 ? Category::findById($selCat->parent_id) : null;
433 View Code Duplication
        while ($parent !== null) {
434
            $crumbs[$parent->slug] = $parent->breadcrumbs_label;
435
            $parent = $parent->parent;
436
        }
437
438
        // build array for widget
439
        $url = '';
440
        $crumbs = array_reverse($crumbs, true);
441 View Code Duplication
        foreach ($crumbs as $slug => $label) {
442
            $url .= '/' . $slug;
443
            $breadcrumbs[] = [
444
                'label' => $label,
445
                'url' => $url
446
            ];
447
        }
448
        if (is_null($product) && $this->module->showFiltersInBreadcrumbs && !empty($properties)) {
449
            $route = [
450
                '@category',
451
                'last_category_id' => $selCat->id,
452
                'category_group_id' => $selCat->category_group_id,
453
            ];
454
            $params = [];
455
            foreach ($properties as $propertyId => $propertyStaticValues) {
456
                $localParams = $params;
457
                foreach ($propertyStaticValues as $propertyStaticValue) {
458
                    $psv = PropertyStaticValues::findById($propertyStaticValue);
459
                    if (is_null($psv)) {
460
                        continue;
461
                    }
462
                    $localParams[$propertyId][] = $propertyStaticValue;
463
                    $breadcrumbs[] = [
464
                        'label' => $psv['name'],
465
                        'url' => array_merge($route, ['properties' => $localParams]),
466
                    ];
467
                }
468
                $params[$propertyId] = $propertyStaticValues;
469
            }
470
        }
471
        unset($breadcrumbs[count($breadcrumbs) - 1]['url']); // last item is not a link
472
473
        if (isset(Yii::$app->response->blocks['breadcrumbs_label'])) {
474
            // last item label rewrited through prefiltered page or something similar
475
            $breadcrumbs[count($breadcrumbs) - 1]['label'] = Yii::$app->response->blocks['breadcrumbs_label'];
476
        }
477
478
        return $breadcrumbs;
479
    }
480
}
481