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.
Test Setup Failed
Push — feature/editReviewsInFloatingP... ( a11aa2 )
by Alexander
28:22
created

FloatingPanel::run()   D

Complexity

Conditions 14
Paths 24

Size

Total Lines 108
Code Lines 67

Duplication

Lines 29
Ratio 26.85 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 29
loc 108
rs 4.9516
cc 14
eloc 67
nc 24
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
3
namespace app\backend\widgets;
4
5
use app\models\Object;
6
use app\modules\review\models\Review;
7
use Yii;
8
use yii\base\Widget;
9
use app;
10
use kartik\icons\Icon;
11
use yii\helpers\Json;
12
13
class FloatingPanel extends Widget
14
{
15
    public $bottom = false;
16
17
    /**
18
     * Check if model has any reviews and if so - generate link to edit them
19
     *
20
     * @param string $objectName
21
     * @param int $modelId
22
     * @return array
23
     */
24
    private function getReviewEditParams($objectName, $modelId)
25
    {
26
        $objectId = array_search($objectName, Object::getSelectArray());
27
        $reviews = Review::getForObjectModel($modelId, $objectId, 1);
28
        if (!empty($reviews)) {
29
            return [
30
                "label" => Icon::show("pencil") . Yii::t("app", "Edit reviews"),
31
                "url" => [
32
                    "/review/backend-review/index",
33
                    "SearchModel" => [
34
                        "object_id" => $objectId,
35
                        "object_model_id" => $modelId
36
                    ]
37
                ]
38
            ];
39
        } else {
40
            return [];
41
        }
42
    }
43
44
    public function run()
0 ignored issues
show
Coding Style introduced by
run 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...
45
    {
46
        app\backend\assets\FrontendEditingAsset::register($this->view);
47
48
        $items = [
49
            [
50
                'label' => Icon::show('dashboard') . ' ' . Yii::t('app', 'Backend'),
51
                'url' => ['/backend/'],
52
            ]
53
        ];
54
55
        switch (Yii::$app->requestedRoute) {
56
            case 'shop/product/list':
57
                if (isset($_GET['properties'])) {
58
                    $apply_if_params = [];
59
                    foreach ($_GET['properties'] as $property_id => $values) {
60
                        if (isset($values[0])) {
61
                            $apply_if_params[$property_id] = $values[0];
62
                        }
63
                    }
64
                    if (Yii::$app->response->dynamic_content_trait === true) {
65
                        $items[] = [
66
                            'label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Edit Dynamic Content'),
67
                            'url' => [
68
                                '/backend/dynamic-content/edit',
69
                                'id' => Yii::$app->response->matched_dynamic_content_trait_model->id,
70
                            ],
71
                        ];
72
                    } else {
73
                        if (isset($_GET['properties'], $_GET['last_category_id'])) {
74
                            $items[] = [
75
                                'label' => Icon::show('puzzle') . ' ' . Yii::t('app', 'Add Dynamic Content'),
76
                                'url' => [
77
                                    '/backend/dynamic-content/edit',
78
                                    'DynamicContent' => [
79
                                        'apply_if_params' => Json::encode($apply_if_params),
80
                                        'apply_if_last_category_id' => $_GET['last_category_id'],
81
                                        'object_id' => Object::getForClass(app\modules\shop\models\Product::className())->id,
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 125 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
82
                                        'route' => 'shop/product/list',
83
                                    ]
84
                                ],
85
                            ];
86
87
                        }
88
89
                    }
90 View Code Duplication
                } else {
91
                    // no properties selected - go to category edit page
92
93
                    if (isset($_GET['last_category_id'])) {
94
                        $reviewsLink = $this->getReviewEditParams("Category", intval($_GET['last_category_id']));
95
                        $cat = app\modules\shop\models\Category::findById($_GET['last_category_id']);
96
                        $items[] = [
97
                            'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit category'),
98
                            'url' => [
99
                                '/shop/backend-category/edit',
100
                                'id' => $cat->id,
101
                                'parent_id' => $cat->parent_id,
102
                            ],
103
                        ];
104
                    }
105
                }
106
107
                break;
108
            case 'shop/product/show':
109
                if (isset($_GET['model_id'])) {
110
                    $reviewsLink = $this->getReviewEditParams("Product", intval($_GET['model_id']));
111
                    $items[] = [
112
                        'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit product'),
113
                        'url' => [
114
                            '/shop/backend-product/edit',
115
                            'id' => intval($_GET['model_id'])
116
                        ],
117
                    ];
118
                }
119
                break;
120
            
121
            case '/page/page/show':
122
            case '/page/page/list':
123 View Code Duplication
                if (isset($_GET['id'])) {
124
                    $page = app\modules\page\models\Page::findById($_GET['id']);
125
                    $reviewsLink = $this->getReviewEditParams("Page", $_GET['id']);
126
                    $items[] = [
127
                        'label' => Icon::show('pencil') . ' ' . Yii::t('app', 'Edit page'),
128
                        'url' => [
129
                            '/page/backend/edit',
130
                            'id' => $page->id,
131
                            'parent_id' =>$page->parent_id,
132
133
                        ],
134
                    ];
135
                }
136
                break;
137
        }
138
139
        if (!empty($reviewsLink)) {
140
            $items[] = $reviewsLink;
141
        }
142
143
        return $this->render(
144
            'floating-panel',
145
            [
146
                'items' => $items,
147
                'bottom' => $this->bottom,
148
            ]
149
        );
150
151
    }
152
}