RequestGridView::columns()   B
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 0
cts 63
cp 0
rs 8.6327
c 0
b 0
f 0
cc 3
nc 1
nop 0
crap 12

How to fix   Long Method   

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
 * 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\RefColumn;
14
use hipanel\modules\hosting\menus\RequestActionsMenu;
15
use hipanel\modules\hosting\models\Request;
16
use hipanel\modules\server\grid\ServerColumn;
17
use hipanel\widgets\gridLegend\ColorizeGrid;
18
use hiqdev\yii2\menus\grid\MenuColumn;
19
use Yii;
20
use yii\helpers\Html;
21
use yii\helpers\UnsetArrayValue;
22
23
class RequestGridView extends \hipanel\grid\BoxedGridView
24
{
25
    use ColorizeGrid;
26
27
    public function columns()
28
    {
29
        return array_merge(parent::columns(), [
30
            'classes' => [
31
                'label' => Yii::t('hipanel:hosting', 'Action'),
32
                'filter' => false,
33
                'enableSorting' => false,
34
                'value' => function ($model) {
35
                    return sprintf('%s, %s', $model->object_class, $model->action);
36
                },
37
            ],
38
            'server' => class_exists(ServerColumn::class) ? [
39
                'sortAttribute' => 'server',
40
                'attribute' => 'server_id',
41
                'class' => ServerColumn::class,
42
            ] : [
43
                'visible' => false,
44
            ],
45
            'account' => [
46
                'enableSorting' => false,
47
                'class' => AccountColumn::class,
48
            ],
49
            'object' => [
50
                'enableSorting' => false,
51
                'filter' => false,
52
                'format' => 'raw',
53
                'value' => function (Request $model): string {
54
                    return Html::a('<i class="fa fa-external-link"></i>&nbsp;' . $model->object,
0 ignored issues
show
Documentation introduced by
The property object does not exist on object<hipanel\modules\hosting\models\Request>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
                        ['/hosting/' . $model->object_class . '/view', 'id' => $model->object_id],
0 ignored issues
show
Documentation introduced by
The property object_class does not exist on object<hipanel\modules\hosting\models\Request>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property object_id does not exist on object<hipanel\modules\hosting\models\Request>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56
                        ['data-pjax' => 0]
57
                    );
58
                },
59
            ],
60
            'time' => [
61
                'filter' => false,
62
                'value' => function ($model) {
63
                    return Yii::$app->formatter->asDatetime($model->time);
64
                },
65
            ],
66
            'state' => [
67
                'class' => RefColumn::class,
68
                'i18nDictionary' => 'hipanel:hosting',
69
                'gtype' => 'state,request',
70
                'format' => 'raw',
71
                'value' => function ($model) {
72
                    $colors = [
73
                        'error' => 'danger',
74
                        'progress' => 'info',
75
                        'done' => 'success',
76
                    ];
77
78
                    return Html::tag('span', Yii::t('hipanel:hosting', $model->state_label), [
79
                        'class' => 'text-' . (isset($colors[$model->state]) ? $colors[$model->state] : 'default'),
80
                    ]);
81
                },
82
                'filterOverrides' => [
83
                    'done' => new UnsetArrayValue(),
84
                ],
85
            ],
86
            'parent' => [
87
                'format' => 'raw',
88
                'value' => function (Request $model): string {
89
                    return Html::a($model->parent, ['@request/view', 'id' => $model->parent_id]);
0 ignored issues
show
Documentation introduced by
The property parent does not exist on object<hipanel\modules\hosting\models\Request>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property parent_id does not exist on object<hipanel\modules\hosting\models\Request>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
                },
91
            ],
92
            'actions' => [
93
                'class' => MenuColumn::class,
94
                'menuClass' => RequestActionsMenu::class,
95
            ],
96
        ]);
97
    }
98
}
99