Test Failed
Push — master ( 2e584a...5aebc0 )
by Alexey
10:12
created

Input   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 139
Duplicated Lines 8.63 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 139
rs 8.3396
wmc 44
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A draw() 0 19 2
A parseRequest() 12 12 4
D value() 0 28 12
B preset() 0 20 7
A colName() 0 3 2
A colLabel() 0 4 4
D readOnly() 0 25 9
A validate() 0 10 4

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Input often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Input, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Active form input
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Ui\ActiveForm;
13
14
class Input extends \Object {
15
16
    public $form = null;
17
    public $activeForm = null;
18
    public $activeFormParams = [];
19
    public $modelName = '';
20
    public $colName = '';
21
    public $colParams = [];
22
    public $options = [];
23
24
    public function draw() {
25
        $inputName = $this->colName();
26
        $inputLabel = $this->colLabel();
27
28
        $inputOptions = $this->options;
29
        $inputOptions['value'] = $this->value();
30
        $inputOptions['disabled'] = $this->readOnly();
31
32
        $preset = $this->preset();
33
        if ($preset !== null) {
34
            $inputOptions['disabled'] = true;
35
            $this->form->input('hidden', $inputName, '', $inputOptions);
36
            return true;
37
        }
38
        $classPath = explode('\\', get_called_class());
39
        $inputType = lcfirst(array_pop($classPath));
40
        $this->form->input($inputType, $inputName, $inputLabel, $inputOptions);
41
        return true;
42
    }
43
44 View Code Duplication
    public function parseRequest($request) {
45
        if ($this->readOnly()) {
46
            return false;
47
        }
48
        $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
49
        if (isset($request[$this->colName])) {
50
            $this->activeForm->model->{$colName} = $request[$this->colName];
51
        } else {
52
            $this->activeForm->model->{$colName} = 0;
53
            $this->activeForm->model->{$colName} = '';
54
        }
55
    }
56
57
    public function value() {
58
        $value = '';
59
        if (isset($this->colParams['default'])) {
60
            if (is_array($this->colParams['default'])) {
61
                switch ($this->colParams['default']['type']) {
62
                    case 'relPath':
63
                        $val = $this->activeForm->model;
64
                        foreach (explode(':', $this->colParams['default']['relPath']) as $path) {
65
                            if ($val->$path) {
66
                                $val = $val->$path;
67
                            } else {
68
                                break 2;
69
                            }
70
                            $value = $val;
71
                        }
72
                        break;
73
                }
74
            } else {
75
                $value = $this->colParams['default'];
76
            }
77
        }
78
        if ($this->activeForm) {
79
            $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
80
            $value = ($this->activeForm && $this->activeForm->model && isset($this->activeForm->model->{$colName})) ? $this->activeForm->model->{$colName} : $value;
81
        }
82
        $value = isset($this->colParams['value']) ? $this->colParams['value'] : $value;
83
        return $value;
84
    }
85
86
    public function preset() {
87
        $preset = !empty($this->activeForm->form['preset'][$this->colName]) ? $this->activeForm->form['preset'][$this->colName] : [];
88
        if (!empty($this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName])) {
89
            $preset = array_merge($preset, $this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName]);
90
        }
91
        if ($preset) {
92
            $value = '';
93
            if (!empty($preset['value'])) {
94
                $value = $preset['value'];
95
            } elseif (!empty($preset['userCol'])) {
96
                if (strpos($preset['userCol'], ':')) {
97
                    $rel = substr($preset['userCol'], 0, strpos($preset['userCol'], ':'));
98
                    $param = substr($preset['userCol'], strpos($preset['userCol'], ':') + 1);
99
                    $value = \Users\User::$cur->$rel->$param;
100
                }
101
            }
102
            return $value;
103
        }
104
        return null;
105
    }
106
107
    public function colName() {
108
        return "{$this->activeForm->requestFormName}[{$this->activeForm->modelName}]" . (stristr($this->colName, '[') ? $this->colName : "[{$this->colName}]");
109
    }
110
111
    public function colLabel() {
112
        $modelName = $this->modelName;
113
        return isset($this->colParams['label']) ? $this->colParams['label'] : (($this->activeForm->model && !empty($modelName::$labels[$this->colName])) ? $modelName::$labels[$this->colName] : $this->colName);
114
    }
115
116
    public function readOnly() {
117
        if (!empty($this->colParams['readonly'])) {
118
            if (is_bool($this->colParams['readonly'])) {
119
                return true;
120
            }
121
            $readonly = true;
122
            if (is_array($this->colParams['readonly'])) {
123
                switch ($this->colParams['readonly']['cond']) {
124
                    case 'colValue':
125
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['col']} == $this->colParams['readonly']['value'];
126
                        if (!empty($this->colParams['readonly']['reverse'])) {
127
                            $readonly = !$readonly;
128
                        }
129
                        break;
130
                    case 'itemMethod':
131
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['method']}();
132
                        break;
133
                }
134
            }
135
            if ($readonly) {
136
                return true;
137
            }
138
        }
139
        return !empty($this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]) && in_array($this->colName, $this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]);
140
    }
141
142
    public function validate(&$request) {
143
        if (empty($request[$this->colName]) && !empty($this->colParams['required'])) {
144
            throw new \Exception('Вы не заполнили: ' . $this->colLabel());
145
        }
146
        if (!empty($this->colParams['validator'])) {
147
            $modelName = $this->modelName;
148
            $validator = $modelName::validator($this->colParams['validator']);
149
            $validator($this->activeForm, $request);
150
        }
151
    }
152
}