IpGridView   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 12
dl 0
loc 137
ccs 0
cts 121
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C columns() 0 130 12
1
<?php
2
/**
3
 * Hosting Plugin for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-hosting
6
 * @package   hipanel-module-hosting
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\hosting\grid;
12
13
use hipanel\grid\BoxedGridView;
14
use hipanel\grid\MainColumn;
15
use hipanel\grid\XEditableColumn;
16
use hipanel\helpers\FontIcon;
17
use hipanel\helpers\Url;
18
use hipanel\modules\hosting\menus\IpActionsMenu;
19
use hipanel\modules\hosting\models\HdomainSearch;
20
use hipanel\modules\hosting\models\Ip;
21
use hipanel\modules\hosting\widgets\ip\ApplyPtrChange;
22
use hipanel\modules\hosting\widgets\ip\IpTag;
23
use hipanel\widgets\ArraySpoiler;
24
use hipanel\widgets\XEditable;
25
use hiqdev\yii2\menus\grid\MenuColumn;
26
use Yii;
27
use yii\base\InvalidParamException;
28
use yii\helpers\Html;
29
30
class IpGridView extends BoxedGridView
31
{
32
    public $controllerUrl = '@ip';
33
34
    public $ipTags = [];
35
36
    public function columns()
37
    {
38
        return array_merge(parent::columns(), [
39
            'ip' => [
40
                'class' => MainColumn::class,
41
                'filterAttribute' => 'ip_like',
42
            ],
43
            'note' => [
44
                'class' => XEditableColumn::class,
45
                'pluginOptions' => [
46
                    'url'       => Url::to('set-note'),
47
                ],
48
                'widgetOptions' => [
49
                    'linkOptions' => [
50
                        'data-type' => 'textarea',
51
                    ],
52
                ],
53
                'visible' => Yii::$app->user->can('admin'),
54
            ],
55
            'tags' => [
56
                'format' => 'raw',
57
                'attribute' => 'tag',
58
                'header' => Yii::t('hipanel:hosting', 'Tags'),
59
                'visible' => Yii::$app->user->can('admin'),
60
                'filter' => function ($column, $model) {
61
                    return Html::activeDropDownList($model, 'tag_in', array_merge(['' => Yii::t('hipanel', '---')], $this->ipTags), ['class' => 'form-control']);
62
                },
63
                'value' => function ($model) {
64
                    $labels = [];
65
                    foreach ($model->tags as $tag) {
66
                        $labels[] = IpTag::widget(['tag' => $tag]);
67
                    }
68
69
                    return implode(' ', $labels);
70
                },
71
            ],
72
            'counters' => [
73
                'format' => 'html',
74
                'header' => Yii::t('hipanel:hosting', 'Counters'),
75
                'value' => function ($model) {
76
                    $html = '';
77
                    foreach ($model->objects_count as $count) {
78
                        if ($count['type'] === 'hdomain') {
79
                            $url['ok'] = ['@hdomain', (new HdomainSearch())->formName() => ['ip_like' => $model->ip]];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$url was never initialized. Although not strictly required by PHP, it is generally a good practice to add $url = 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...
80
                            $url['deleted'] = ['@hdomain', (new HdomainSearch())->formName() => ['ip_like' => $model->ip, 'state' => 'deleted']];
0 ignored issues
show
Bug introduced by
The variable $url 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...
81
                            $type = function ($count) {
82
                                return Yii::t('hipanel:hosting', '{0, plural, one{domain} other{domains}}', (int) $count);
0 ignored issues
show
Documentation introduced by
(int) $count is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
                            };
84
                        } else {
85
                            throw new InvalidParamException('The object type is not supported', $model);
0 ignored issues
show
Deprecated Code introduced by
The class yii\base\InvalidParamException has been deprecated with message: since 2.0.14. Use [[InvalidArgumentException]] instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
86
                        }
87
88
                        if ($count['ok']) {
89
                            $html .= Html::a(
90
                                (int) $count['ok'] . '&nbsp;' . FontIcon::i('fa-check') . ' ' . $type($count['ok']),
91
                                $url['ok'],
92
                                ['class' => 'btn btn-success btn-xs']
93
                            );
94
                        }
95
                        $html .= ' ';
96
                        if ($count['deleted'] > 0) {
97
                            $html .= Html::a(
98
                                (int) $count['deleted'] . '&nbsp;' . FontIcon::i('fa-trash') . ' ' . $type($count['deleted']),
99
                                $url['deleted'],
100
                                ['class' => 'btn btn-xs btn-warning']
101
                            );
102
                        }
103
                    }
104
105
                    return $html;
106
                },
107
            ],
108
            'links' => [
109
                'format' => 'html',
110
                'value' => function ($model) {
111
                    $items = [];
112
                    foreach ($model->links as $link) {
113
                        $item = Html::a($link->device, ['@server/view', 'id' => $link->device_id]);
114
                        if ($link->service_id) {
115
                            $item .= '&nbsp;' . FontIcon::i('fa-long-arrow-right');
116
                            $item .= '&nbsp;' . Html::a($link->service ?: $link->soft, ['@service/view', 'id' => $link->service_id]);
117
                        }
118
                        $items[] = $item;
119
                    }
120
121
                    return ArraySpoiler::widget(['data' => $items, 'visibleCount' => 3]);
122
                },
123
            ],
124
            'services' => [
125
                'attribute' => 'links',
126
                'format' => 'html',
127
                'label' => Yii::t('hipanel:server', 'Services'),
128
                'value' => function ($model) {
129
                    return ArraySpoiler::widget([
130
                        'data' => $model->links,
131
                        'formatter' => function ($link) {
132
                            if (Yii::$app->user->can('support') && Yii::getAlias('@service', false)) {
133
                                return Html::a($link->service, ['@service/view', 'id' => $link->service_id]);
134
                            }
135
136
                            return $link->service;
137
                        },
138
                    ]);
139
                },
140
            ],
141
            'actions' => [
142
                'class' => MenuColumn::class,
143
                'menuClass' => IpActionsMenu::class,
144
            ],
145
            'ptr' => [
146
                'options' => [
147
                    'style' => 'width: 40%',
148
                ],
149
                'format' => 'raw',
150
                'value' => static function (Ip $model): string {
151
                    if ($model->canSetPtr()) {
152
                        return XEditable::widget([
153
                            'model' => $model,
154
                            'attribute' => 'ptr',
155
                            'pluginOptions' => [
156
                                'url' => Url::to('@ip/set-ptr'),
157
                            ],
158
                        ]);
159
                    }
160
161
                    return ApplyPtrChange::widget(compact('model'));
162
                },
163
            ],
164
        ]);
165
    }
166
}
167