Completed
Push — master ( 690aba...fbb2e2 )
by Andrii
15:44
created

ServerGridView   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 201
Duplicated Lines 5.97 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 0%

Importance

Changes 12
Bugs 1 Features 1
Metric Value
wmc 14
c 12
b 1
f 1
lcom 1
cbo 13
dl 12
loc 201
ccs 0
cts 151
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B formatTariff() 0 12 5
A setOsImages() 0 4 1
B defaultColumns() 12 153 6
A defaultRepresentations() 0 21 2

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
/*
4
 * Server module for HiPanel
5
 *
6
 * @link      https://github.com/hiqdev/hipanel-module-server
7
 * @package   hipanel-module-server
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\modules\server\grid;
13
14
use hipanel\grid\ActionColumn;
15
use hipanel\grid\MainColumn;
16
use hipanel\grid\RefColumn;
17
use hipanel\grid\XEditableColumn;
18
use hipanel\helpers\Url;
19
use hipanel\modules\server\widgets\DiscountFormatter;
20
use hipanel\modules\server\widgets\Expires;
21
use hipanel\modules\server\widgets\OSFormatter;
22
use hipanel\modules\server\widgets\State;
23
use hipanel\widgets\ArraySpoiler;
24
use Yii;
25
use yii\helpers\ArrayHelper;
26
use yii\helpers\Html;
27
28
class ServerGridView extends \hipanel\grid\BoxedGridView
29
{
30
    /**
31
     * @var array
32
     */
33
    public static $osImages;
34
35
    public static function setOsImages($osImages)
36
    {
37
        static::$osImages = $osImages;
38
    }
39
40
    public static function formatTariff($model)
41
    {
42
        if (Yii::$app->user->can('support')) {
43
            if ($model->parent_tariff && $model->parent_tariff !== $model->tariff) {
44
                return Html::tag('abbr', $model->parent_tariff, ['title' => $model->tariff, 'data-toggle' => 'tooltip']);
45
            } else {
46
                return $model->tariff;
47
            }
48
        } else {
49
            return !empty($model->parent_tariff) ? $model->parent_tariff : $model->tariff;
50
        }
51
    }
52
53
    public static function defaultColumns()
54
    {
55
        $osImages = self::$osImages;
56
57
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('server' =>...'rack']['switch']; })); (array<string,array>) is incompatible with the return type of the parent method hipanel\grid\GridView::defaultColumns of type array<string,array<string,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...
58
            'server' => [
59
                'class' => MainColumn::className(),
60
                'attribute' => 'name',
61
                'filterAttribute' => 'name_like',
62
                'note' => Yii::$app->user->can('support') ? 'label' : 'note',
63
                'noteOptions' => [
64
                    'url' => Yii::$app->user->can('support') ? Url::to('set-label') : Url::to('set-note'),
65
                ],
66
            ],
67
            'state' => [
68
                'class' => RefColumn::className(),
69
                'format' => 'raw',
70
                'gtype' => 'state,device',
71
                'value' => function ($model) {
72
                    $html = State::widget(compact('model'));
73
                    if ($model->status_time) {
74
                        $html .= ' '. Html::tag('nobr', Yii::t('hipanel/server', 'since {date}', ['date' => Yii::$app->formatter->asDate($model->status_time)]));
75
                    }
76
                    return $html;
77
                },
78
            ],
79
            'panel' => [
80
                'attribute' => 'panel',
81
                'format' => 'text',
82
                'contentOptions' => ['class' => 'text-uppercase'],
83
                'value' => function ($model) {
84
                    return $model->panel ? Yii::t('hipanel/server/panel', $model->panel) : Yii::t('hipanel/server/panel', 'No control panel');
85
                },
86
            ],
87
            'os' => [
88
                'attribute' => 'os',
89
                'format' => 'raw',
90
                'value' => function ($model) use ($osImages) {
91
                    return OSFormatter::widget([
92
                        'osimages' => $osImages,
93
                        'imageName' => $model->osimage,
94
                    ]);
95
                },
96
            ],
97
            'os_and_panel' => [
98
                'attribute' => 'os',
99
                'format' => 'raw',
100
                'value' => function ($model) use ($osImages) {
101
                    $html = OSFormatter::widget([
102
                        'osimages' => $osImages,
103
                        'imageName' => $model->osimage,
104
                    ]);
105
                    $html .= ' ' . $model->panel ?: '';
106
                    return $html;
107
                },
108
            ],
109
            'tariff_and_discount' => [
110
                'attribute' => 'tariff',
111
                'format' => 'raw',
112 View Code Duplication
                'value' => function ($model) {
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...
113
                    return self::formatTariff($model) . ' ' . DiscountFormatter::widget([
114
                        'current' => $model->discounts['fee']['current'],
115
                        'next' => $model->discounts['fee']['next'],
116
                    ]);
117
                },
118
            ],
119
            'discount' => [
120
                'attribute' => 'discount',
121
                'label' => Yii::t('hipanel/server', 'Discount'),
122
                'format' => 'raw',
123
                'headerOptions' => ['style' => 'width: 1em'],
124 View Code Duplication
                'value' => function ($model) {
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...
125
                    return DiscountFormatter::widget([
126
                        'current' => $model->discounts['fee']['current'],
127
                        'next' => $model->discounts['fee']['next'],
128
                    ]);
129
                },
130
            ],
131
            'actions' => [
132
                'class' => ActionColumn::className(),
133
                'template' => '{view}',
134
                'header' => Yii::t('hipanel', 'Actions'),
135
            ],
136
            'expires' => [
137
                'filter' => false,
138
                'format' => 'raw',
139
                'headerOptions' => ['style' => 'width: 1em'],
140
                'value' => function ($model) {
141
                    return Expires::widget(compact('model'));
142
                },
143
            ],
144
            'tariff' => [
145
                'format' => 'raw',
146
                'attribute' => 'tariff',
147
                'value' => function ($model) {
148
                    return self::formatTariff($model);
149
                },
150
            ],
151
            'tariff_note' => [
152
                'attribute' => 'tariff_note',
153
                'value' => function ($model) {
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
154
155
                },
156
            ],
157
            'ips' => [
158
                'format' => 'raw',
159
                'attribute' => 'ips',
160
                'label' => Yii::t('hipanel/server', 'IP addresses'),
161
                'value' => function ($model) {
162
                    return ArraySpoiler::widget([
163
                        'data' => ArrayHelper::getColumn($model->ips, 'ip'),
164
                        'delimiter' => '<br />',
165
                        'visibleCount' => 3,
166
                        'button' => ['popoverOptions' => ['html' => true]],
167
                    ]);
168
                },
169
            ],
170
            'sale_time' => [
171
                'attribute' => 'sale_time',
172
                'format' => 'date',
173
            ],
174
            'note' => [
175
                'class' => XEditableColumn::class,
176
                'pluginOptions' => [
177
                    'url'       => Url::to('set-note'),
178
                ],
179
                'widgetOptions' => [
180
                    'linkOptions' => [
181
                        'data-type' => 'textarea',
182
                    ],
183
                ],
184
            ],
185
            'label' => [
186
                'class' => XEditableColumn::class,
187
                'visible' => Yii::$app->user->can('support'),
188
                'pluginOptions' => [
189
                    'url'       => Url::to('set-label'),
190
                ],
191
                'widgetOptions' => [
192
                    'linkOptions' => [
193
                        'data-type' => 'textarea',
194
                    ],
195
                ],
196
            ],
197
            'rack' => [
198
                'format' => 'html',
199
                'filterAttribute' => 'rack_like',
200
                'value'  => function ($model) {
201
                    return $model->switches['rack']['switch'];
202
                },
203
            ],
204
        ];
205
    }
206
207
    public static function defaultRepresentations()
208
    {
209
        return [
210
            'common' => [
211
                'label'   => Yii::t('hipanel', 'common'),
212
                'columns' => [
213
                    'checkbox',
214
                    'server', 'client_id', 'seller_id',
215
                    'ips', 'state', 'expires',
216
                    'tariff_and_discount',
217
                ],
218
            ],
219
            'manager' => Yii::$app->user->can('support') ? [
220
                'label'   => Yii::t('hipanel/server', 'manager'),
221
                'columns' => [
222
                    'checkbox', 'client_id',
223
                    'rack', 'dc', 'server', 'tariff', 'hwsummary',
224
                ],
225
            ] : null,
226
        ];
227
    }
228
}
229