Passed
Push — master ( de9f16...6836c0 )
by Aleksandr
02:35
created

TestingGridView::init()   C

Complexity

Conditions 9
Paths 1

Size

Total Lines 58
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 32
nc 1
nop 0
dl 0
loc 58
rs 6.9928
c 0
b 0
f 0

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
4
namespace carono\exchange1c\widgets;
5
6
use carono\exchange1c\models\TestingClass;
7
use yii\grid\ActionColumn;
8
use yii\grid\GridView;
9
use yii\helpers\Html;
10
11
class TestingGridView extends GridView
12
{
13
    public function init()
14
    {
15
        $this->rowOptions = function ($data) {
16
            /**
17
             * @var TestingClass $data
18
             */
19
            $result = $data->testing();
20
            if (($result === true || $data->hasResult()) && !$data->hasErrors()) {
21
                return ['class' => 'success'];
22
            } elseif ($result === false || $data->hasErrors()) {
23
                return ['class' => 'danger'];
24
            } else {
25
                return ['class' => 'warning'];
26
            }
27
        };
28
29
        $this->columns = [
30
            'name',
31
            'expect',
32
            [
33
                'attribute' => 'comment',
34
                'value' => function ($data) {
35
                    /**
36
                     * @var TestingClass $data
37
                     */
38
                    if ($data->hasErrors()) {
39
                        return Html::errorSummary($data);
40
                    }
41
                    return $data->comment;
42
                }
43
            ],
44
            [
45
                'class' => ActionColumn::className(),
46
                'header' => 'Result',
47
                'template' => '{view}',
48
                'buttons' => [
49
                    'view' => function ($url, $data) {
50
                        /**
51
                         * @var TestingClass $data
52
                         */
53
                        if ($data->method && !$data->isAutoTest()) {
54
                            $span = Html::tag('i', '', [
55
                                'class' => 'glyphicon glyphicon-eye-open',
56
                                'title' => "Выполнить метод {$data->method}"
57
                            ]);
58
                            return Html::a($span, [
59
                                'testing/index',
60
                                'class' => (new \ReflectionClass($data))->getShortName(),
61
                                'result' => $data->method
62
                            ]);
63
                        } else {
64
                            return $data->result;
65
                        }
66
                    }
67
                ]
68
            ]
69
        ];
70
        parent::init();
71
    }
72
}