ServiceGridView   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 107
Duplicated Lines 13.08 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 9
dl 14
loc 107
ccs 0
cts 92
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B columns() 14 104 7

How to fix   Duplicated Code   

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:

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\MainColumn;
14
use hipanel\grid\RefColumn;
15
use hipanel\helpers\FontIcon;
16
use hipanel\modules\hosting\controllers\IpController;
17
use hipanel\modules\hosting\menus\ServiceActionsMenu;
18
use hipanel\modules\hosting\models\DbSearch;
19
use hipanel\modules\hosting\models\HdomainSearch;
20
use hipanel\modules\hosting\models\Soft;
21
use hipanel\modules\server\grid\ServerColumn;
22
use hipanel\widgets\ArraySpoiler;
23
use hipanel\widgets\State;
24
use hiqdev\yii2\menus\grid\MenuColumn;
25
use Yii;
26
use yii\helpers\Html;
27
28
class ServiceGridView extends \hipanel\grid\BoxedGridView
29
{
30
    public function columns()
31
    {
32
        return array_merge(parent::columns(), [
33
            'service' => [
34
                'class' => MainColumn::class,
35
                'attribute' => 'name',
36
                'filterAttribute' => 'service_like',
37
            ],
38
            'server' => [
39
                'class' => ServerColumn::class,
40
            ],
41
            'object' => [
42
                'format' => 'raw',
43
                'header' => Yii::t('hipanel:hosting', 'Object'),
44
                'value' => function ($model) {
45
                    $html = $model->name . ' ';
46
47
                    if ($model->soft_type === Soft::TYPE_WEB) {
48
                        $url['ok'] = ['@hdomain', (new HdomainSearch())->formName() => ['server' => $model->server, 'service' => $model->name]];
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...
49
                        $url['deleted'] = ['@hdomain', (new HdomainSearch())->formName() => ['server' => $model->server, 'service' => $model->name, 'state' => 'deleted']];
50
                        $type = function ($count) {
51
                            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...
52
                        };
53
                    } elseif ($model->soft_type === Soft::TYPE_DB) {
54
                        $url['ok'] = ['@db', (new DbSearch())->formName() => ['server' => $model->server, 'service' => $model->name]];
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...
55
                        $url['deleted'] = ['@db', (new DbSearch())->formName() => ['server' => $model->server, 'service' => $model->name, 'state' => 'deleted']];
56
                        $type = function ($count) {
57
                            return Yii::t('hipanel:hosting', '{0, plural, one{# DB} other{# DBs}}', (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...
58
                        };
59
                    } else {
60
                        return $html;
61
                    }
62
63 View Code Duplication
                    if ($count = $model->objects_count['ok']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
                        $html .= Html::a(
65
                            (int) $count . '&nbsp;' . FontIcon::i('fa-check') . ' ' . $type($count),
66
                            $url['ok'],
67
                            ['class' => 'btn btn-success btn-xs']
68
                        );
69
                    }
70
                    $html .= ' ';
71 View Code Duplication
                    if (($count = $model->objects_count['deleted']) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
                        $html .= Html::a(
73
                            (int) $count . '&nbsp;' . FontIcon::i('fa-trash') . ' ' . $type($count),
74
                            $url['deleted'],
75
                            ['class' => 'btn btn-xs btn-warning']
76
                        );
77
                    }
78
79
                    return $html;
80
                },
81
            ],
82
            'ip' => [
83
                'format' => 'raw',
84
                'label' => Yii::t('hipanel:hosting', 'IP'),
85
                'value' => function ($model) {
86
                    return ArraySpoiler::widget(['data' => array_unique(array_merge((array) $model->ip, (array) $model->ips))]);
87
                },
88
            ],
89
            'ip_with_link' => [
90
                'format' => 'raw',
91
                'label' => Yii::t('hipanel:hosting', 'IP'),
92
                'value' => function ($model) {
93
                    $ips = Html::tag('span', ArraySpoiler::widget(['data' => array_unique(array_merge((array) $model->ip, (array) $model->ips))]));
94
                    $linkToIPs = Html::a(Yii::t('hipanel', 'Show'), IpController::getSearchUrl([
95
                        'server_in' => $model->server,
96
                        'service_id' => $model->id,
97
                    ]), ['class' => 'btn bg-olive btn-xs btn-flat', 'target' => '_blank', 'data-pjax' => 0]);
98
99
                    return Html::tag('span', sprintf('%s %s', $ips, $linkToIPs), ['style' => 'display: flex; justify-content: space-between;']);
100
                },
101
            ],
102
            'bin' => [
103
                'format' => 'html',
104
                'value' => function ($model) {
105
                    return $model->bin ? Html::tag('code', $model->bin) : '';
106
                },
107
            ],
108
            'etc' => [
109
                'format' => 'html',
110
                'value' => function ($model) {
111
                    return $model->etc ? Html::tag('code', $model->etc) : '';
112
                },
113
            ],
114
            'soft' => [
115
                'value' => function ($model) {
116
                    return $model->soft;
117
                },
118
            ],
119
            'state' => [
120
                'class' => RefColumn::class,
121
                'i18nDictionary' => 'hipanel:hosting',
122
                'format' => 'raw',
123
                'value' => function ($model) {
124
                    return State::widget(compact('model'));
125
                },
126
                'gtype' => 'state,service',
127
            ],
128
            'actions' => [
129
                'class' => MenuColumn::class,
130
                'menuClass' => ServiceActionsMenu::class,
131
            ],
132
        ]);
133
    }
134
}
135