Test Setup Failed
Push — master ( 594b61...d5d43f )
by eXeCUT
13:04
created

DetailView   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 172
Duplicated Lines 17.44 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 0
dl 30
loc 172
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 11 1
A __construct() 0 43 1
A init() 0 15 1
A getAction() 0 10 2
A runWidget() 0 5 1
A renderAlertBlock() 30 30 4
A renderSubmitButtons() 0 29 4

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
 * Created by PhpStorm.
4
 * User: execut
5
 * Date: 12/26/17
6
 * Time: 3:37 PM
7
 */
8
9
namespace execut\actions\widgets;
10
11
12
use execut\loadingOverlay\LoadingOverlay;
13
use execut\yii\jui\WidgetTrait;
14
use yii\helpers\ArrayHelper;
15
use yii\helpers\Html;
16
use yii\helpers\Url;
17
use kartik\alert\Alert;
18
19
class DetailView extends \kartik\detail\DetailView
20
{
21
    use WidgetTrait;
22
    const DEFAULT_BUTTONS_TEMPLATE = '{save}&nbsp;&nbsp;{apply}&nbsp;&nbsp;{cancel}';
23
    const All_BUTTONS_TEMPLATE = '{check}&nbsp;&nbsp;{save}&nbsp;&nbsp;{apply}&nbsp;&nbsp;{cancel}';
24
    public $uniqueId = null;
25
    public $action = null;
26
    public $buttonsTemplate = self::DEFAULT_BUTTONS_TEMPLATE;
27
    public $saveButton = '<input type="submit" name="save" value="Отправить" class="btn btn-primary" href="" title="Сохранить и вернуться">';
28
    public $checkButton = '<input type="submit" name="check" value="Проверить" class="btn btn-info" href="" title="Проверить">';
29
    public $applyButton = '<input type="submit" name="apply" value="Применить" class="btn btn-primary" href="" title="Сохранить изменения">';
30
    public $cancelButton = '<a class="btn btn-default" href="{backUrl}">Вернуться к списку</a>';
31
    public $backUrl = null;
32
    public $alertBlockAddon = null;
33
34
    public function __construct($config = [])
35
    {
36
        $config = ArrayHelper::merge([
37
            'panel'=> false,
38
//            [
0 ignored issues
show
Unused Code Comprehensibility introduced by
48% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
//                'heading'=> '',
40
//                'footer' => '&nbsp;{buttons}',
41
////                'type'=>\kartik\detail\DetailView::TYPE_PRIMARY,
42
//                'headingOptions' => [
43
//                    'template' => ''
44
//                ],
45
//                'footerOptions' => [
46
//                    'style' => 'height: 31px',
47
//                ],
48
//            ],
49
            'buttons1' => '',
50
            'buttonContainer' => [
51
                'class' => 'buttons-container',
52
            ],
53
            'mainTemplate' => $this->renderAlertBlock() . '{detail}{buttons}',
54
            'bordered' => true,
55
            'striped' => true,
56
            'condensed' => true,
57
            'responsive' => false,
58
            'hideIfEmpty' => true,
59
            'hover' => true,
60
//                        'hAlign'=> true,
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
//                        'vAlign'=> true,
62
//                        'fadeDelay'=> 2000,
63
//                        'container' => ['id'=>'kv-demo'],
64
            'formOptions' => [
65
                'enableAjaxValidation' => false,
66
                'validateOnChange' => false,
67
                'validateOnSubmit' => false,
68
                'validateOnBlur' => false,
69
                'options' => [
70
                    'enctype'=>'multipart/form-data',
71
                ],
72
            ],
73
        ], $config);
74
75
        parent::__construct($config);
76
    }
77
78
    public function run() {
79
        echo LoadingOverlay::widget();
80
81
        $this->_registerBundle();
82
        parent::registerPlugin('DetailView');
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (registerPlugin() instead of run()). Are you sure this is correct? If so, you might want to change this to $this->registerPlugin().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
83
        echo $this->_beginContainer();
84
        $r = parent::run();
85
        echo $this->_endContainer();
86
87
        return $r;
88
    }
89
90
    public function init()
91
    {
92
        $this->attributes = $this->model->getFormFields();
93
        $this->formOptions['action'] = $this->getAction();
94
        $this->deleteOptions = [
95
            'url' => Url::to([
96
                $this->uniqueId . '/delete',
97
                'id' => $this->model->primaryKey,
98
            ]),
99
            'kvdelete' => true
100
        ];
101
        $this->formOptions['validationUrl'] = $this->getAction();
102
103
        parent::init();
104
    }
105
106
    protected function getAction() {
107
        if ($this->action !== null) {
108
            return $this->action;
109
        }
110
111
        return Url::to([
112
            $this->uniqueId . '/update',
113
            'id' => $this->model->primaryKey,
114
        ]);
115
    }
116
117
    public function runWidget()
118
    {
119
        $this->buttons2 = $this->renderSubmitButtons();
120
        parent::runWidget(); // TODO: Change the autogenerated stub
121
    }
122
123
    /**
124
     * Initializes and renders alert container block
125
     */
126 View Code Duplication
    protected function renderAlertBlock()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
127
    {
128
        $session = \Yii::$app->session;
129
        $flashes = $session->getAllFlashes();
130
        $alertContainerOptions = [
131
            'style' => 'max-width:400px'
132
        ];
133
        if (count($flashes) === 0) {
134
            Html::addCssStyle($alertContainerOptions, 'display:none;');
135
        }
136
        $out = Html::beginTag('div', $alertContainerOptions);
137
        foreach ($flashes as $type => $message) {
138
            if (is_array($message)) {
139
                $message = implode('<br>', $message);
140
            }
141
142
            $alertWidgetOptions = [];
143
            $alertWidgetOptions['body'] = $message;
144
            $alertWidgetOptions['options'] = [
145
                'class' => ['alert', 'alert-success'],
146
                'style' => 'padding-left:10px;padding-right:10px;'
147
            ];
148
            $out .= "\n" . Alert::widget($alertWidgetOptions);
149
            $session->removeFlash($type);
150
        }
151
152
        $out .= "\n</div>";
153
154
        return $this->alertBlockAddon . $out;
155
    }
156
157
    /**
158
     * @param $cancelUrl
159
     * @return string
160
     */
161
    public function renderSubmitButtons()
162
    {
163
        $cancelButton = $this->cancelButton;
164
        $backUrl = $this->backUrl;
165
        if ($backUrl === null) {
166
            $backUrlParts = explode('/', $this->uniqueId);
167
            unset($backUrlParts[count($backUrlParts) - 1]);
168
            $backUrl = [implode('/', $backUrlParts)];
169
        }
170
        if (is_array($backUrl)) {
171
            $backUrl = Url::to($backUrl);
172
        }
173
174
        $cancelButton = strtr($cancelButton, [
175
            '{backUrl}' => $backUrl,
176
        ]);
177
178
179
        if (is_callable($this->buttonsTemplate)) {
180
            $this->buttonsTemplate = call_user_func($this->buttonsTemplate, $this->model);
181
        }
182
183
        return strtr($this->buttonsTemplate, [
184
            '{check}' => $this->checkButton,
185
            '{save}' => $this->saveButton,
186
            '{apply}' => $this->applyButton,
187
            '{cancel}' => $cancelButton,
188
        ]);
189
    }
190
}