Completed
Push — master ( f3cddb...22e068 )
by Dmitry
51:40 queued 36:46
created

AccountGridView::columns()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 0
cts 40
cp 0
rs 8.9381
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2

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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\hosting\grid;
12
13
use hipanel\grid\ActionColumn;
14
use hipanel\grid\MainColumn;
15
use hipanel\grid\RefColumn;
16
use hipanel\modules\hosting\models\Account;
17
use hipanel\modules\hosting\widgets\account\State;
18
use hipanel\modules\hosting\widgets\account\Type;
19
use hipanel\modules\server\grid\ServerColumn;
20
use hipanel\widgets\ArraySpoiler;
21
use Yii;
22
use yii\helpers\Html;
23
24
class AccountGridView extends \hipanel\grid\BoxedGridView
25
{
26
    public function columns()
27
    {
28
        return array_merge(parent::columns(), [
29
            'account' => [
30
                'class'             => MainColumn::class,
31
                'label'             => Yii::t('hipanel', 'Account'),
32
                'attribute'         => 'login',
33
                'filterAttribute'   => 'login_like',
34
            ],
35
            'state' => [
36
                'class'             => RefColumn::class,
37
                'format'            => 'raw',
38
                'i18nDictionary'    => 'hipanel:hosting',
39
                'value'             => function ($model) {
40
                    return State::widget(compact('model'));
41
                },
42
                'gtype'             => 'state,account',
43
            ],
44
            'server' => [
45
                'class'             => ServerColumn::class,
46
            ],
47
            'sshftp_ips' => [
48
                'attribute'         => 'sshftp_ips',
49
                'format'            => 'raw',
50
                'value'             => function ($model) {
51
                    return ArraySpoiler::widget([
52
                        'data'         => $model->sshftp_ips,
53
                        'visibleCount' => 3,
54
                    ]);
55
                },
56
            ],
57
            'actions' => [
58
                'class'             => ActionColumn::class,
59
                'template'          => '{view} {delete}',
60
            ],
61
            'type' => [
62
                'class'             => RefColumn::class,
63
                'i18nDictionary'    => 'hipanel:hosting',
64
                'format'            => 'raw',
65
                'value'             => function ($model) {
66
                    return Type::widget(compact('model'));
67
                },
68
                'gtype'             => 'type,account',
69
            ],
70
            'access_data' => [
71
                'format' => 'raw',
72
                'label' => Yii::t('hipanel:hosting:account', 'Access data'),
73
                'value' => function (Account $model): string {
74
                    return Yii::t('hipanel:hosting:account', '{ip_label} {ip} {login_label} {login}', [
75
                        'ip_label' => Html::tag('b', Yii::t('hipanel', 'IP:')),
76
                        'login_label' => Html::tag('b', Yii::t('hipanel', 'Login:')),
77
                        'ip' => $model->ip,
0 ignored issues
show
Documentation introduced by
The property ip does not exist on object<hipanel\modules\hosting\models\Account>. 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...
78
                        'login' => $model->login]);
0 ignored issues
show
Documentation introduced by
The property login does not exist on object<hipanel\modules\hosting\models\Account>. 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...
79
                }
80
            ]
81
        ]);
82
    }
83
}
84