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.

ActionColumn::createUrl()   C
last analyzed

Complexity

Conditions 12
Paths 193

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 6.1916
c 0
b 0
f 0
cc 12
nc 193
nop 8

How to fix   Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace app\backend\components;
4
5
use Closure;
6
use kartik\icons\Icon;
7
use Yii;
8
use yii\grid\Column;
9
use yii\helpers\ArrayHelper;
10
use yii\helpers\Html;
11
use yii\helpers\Url;
12
use app\backend\components\Helper;
13
14
class ActionColumn extends Column
15
{
16
    public $buttons;
17
18
    private $defaultButtons = [];
19
20
    private $callbackButtons;
21
22
    /**
23
     * @var string the ID of the controller that should handle the actions specified here.
24
     * If not set, it will use the currently active controller. This property is mainly used by
25
     * [[urlCreator]] to create URLs for different actions. The value of this property will be prefixed
26
     * to each action name to form the route of the action.
27
     */
28
    public $controller;
29
    /**
30
     * @var callable a callback that creates a button URL using the specified model information.
31
     * The signature of the callback should be the same as that of [[createUrl()]].
32
     * If this property is not set, button URLs will be created using [[createUrl()]].
33
     */
34
    public $urlCreator;
35
36
    public $url_append = '';
37
38
    public $appendReturnUrl = true;
39
40
    public function init()
41
    {
42
        parent::init();
43
44
        $this->defaultButtons = [
45
            [
46
                'url' => 'edit',
47
                'icon' => 'pencil',
48
                'class' => 'btn-primary',
49
                'label' => Yii::t('app', 'Edit'),
50
            ],
51
            [
52
                'url' => 'delete',
53
                'icon' => 'trash-o',
54
                'class' => 'btn-danger',
55
                'label' => Yii::t('app', 'Delete'),
56
                'options' => [
57
                    'data-action' => 'delete',
58
                ],
59
            ]
60
        ];
61
62
63
        if (null === $this->buttons) {
64
            $this->buttons = $this->defaultButtons;
65
        } elseif ($this->buttons instanceof Closure) {
66
            $this->callbackButtons = $this->buttons;
67
        }
68
    }
69
70
    /**
71
     * Creates a URL for the given action and model.
72
     * This method is called for each button and each row.
73
     * @param string $action the button name (or action ID)
74
     * @param \yii\db\ActiveRecord $model the data model
75
     * @param mixed $key the key associated with the data model
76
     * @param integer $index the current row index
77
     * @param bool $appendReturnUrl custom return url for each button
0 ignored issues
show
Documentation introduced by
Should the type for parameter $appendReturnUrl not be boolean|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
78
     * @param string $url_append custom append url for each button
0 ignored issues
show
Documentation introduced by
Should the type for parameter $url_append not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
79
     * @param string $keyParam custom param if $key is string
80
     * @param array $attrs list of model attributes used in route params
81
     * @return string the created URL
82
     */
83
    public function createUrl(
84
        $action,
85
        $model,
86
        $key,
87
        $index,
88
        $appendReturnUrl = null,
89
        $url_append = null,
90
        $keyParam = 'id',
91
        $attrs = []
92
    ) {
93
        if ($this->urlCreator instanceof Closure) {
94
            return call_user_func($this->urlCreator, $action, $model, $key, $index);
95
        } else {
96
            $params = [];
97
            if (is_array($key)) {
98
                $params = $key;
99
            } else {
100
                if (is_null($keyParam) === false) {
101
                    $params = [$keyParam => (string)$key];
102
                }
103
            }
104
            $params[0] = $this->controller ? $this->controller . '/' . $action : $action;
105
            foreach ($attrs as $attrName) {
106
                if ($attrName === 'model') {
107
                    $params['model'] = $model;
108
                } elseif ($attrName === 'mainCategory.category_group_id' && $model->getMainCategory()) {
109
                    $params['category_group_id'] = $model->getMainCategory()->category_group_id;
110
                } else {
111
                    $params[$attrName] = $model->getAttribute($attrName);
112
                }
113
            }
114
            if (is_null($appendReturnUrl) === true) {
115
                $appendReturnUrl = $this->appendReturnUrl;
116
            }
117
            if (is_null($url_append) === true) {
118
                $url_append = $this->url_append;
119
            }
120
            if ($appendReturnUrl) {
121
                $params['returnUrl'] = Helper::getReturnUrl();
122
            }
123
            return Url::toRoute($params) . $url_append;
124
        }
125
    }
126
127
128
    protected function renderDataCellContent($model, $key, $index)
129
    {
130
        if ($this->callbackButtons instanceof Closure) {
131
            $btns = call_user_func($this->callbackButtons, $model, $key, $index, $this);
132
            if (null === $btns) {
133
                $this->buttons = $this->defaultButtons;
134
            } else {
135
                $this->buttons = $btns;
136
            }
137
        }
138
        $min_width = count($this->buttons) * 34; //34 is button-width
139
        $data = Html::beginTag('div', ['class' => 'btn-group', 'style' => 'min-width: ' . $min_width . 'px']);
140
        foreach ($this->buttons as $button) {
141
            $appendReturnUrl = ArrayHelper::getValue($button, 'appendReturnUrl', $this->appendReturnUrl);
142
            $url_append = ArrayHelper::getValue($button, 'url_append', $this->url_append);
143
            $keyParam = ArrayHelper::getValue($button, 'keyParam', 'id');
144
            $attrs = ArrayHelper::getValue($button, 'attrs', []);
145
            Html::addCssClass($button, 'btn');
146
            Html::addCssClass($button, 'btn-sm');
147
            $buttonText = isset($button['text']) ? ' ' . $button['text'] : '';
148
            $data .= Html::a(
149
                    Icon::show($button['icon']) . $buttonText,
150
                    $url = $this->createUrl(
151
                        $button['url'],
152
                        $model,
153
                        $key,
154
                        $index,
155
                        $appendReturnUrl,
156
                        $url_append,
157
                        $keyParam,
158
                        $attrs
159
                    ),
160
                    ArrayHelper::merge(
161
                        isset($button['options']) ? $button['options'] : [],
162
                        [
163
                            'class' => $button['class'],
164
                            'title' => $button['label'],
165
                        ]
166
                    )
167
                ) . ' ';
168
        }
169
        $data .= '</div>';
170
        return $data;
171
    }
172
}
173