Passed
Push — master ( 227fa5...84d0c3 )
by Alexey
06:13
created

Input   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 120
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 12
loc 120
rs 8.2608
c 0
b 0
f 0
wmc 40
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A draw() 0 19 2
A parseRequest() 12 12 4
B value() 0 9 8
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 = isset($this->colParams['default']) ? $this->colParams['default'] : '';
59
        if ($this->activeForm) {
60
            $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
61
            $value = ($this->activeForm && $this->activeForm->model && isset($this->activeForm->model->{$colName})) ? $this->activeForm->model->{$colName} : $value;
62
        }
63
        $value = isset($this->colParams['value']) ? $this->colParams['value'] : $value;
64
        return $value;
65
    }
66
67
    public function preset() {
68
        $preset = !empty($this->activeForm->form['preset'][$this->colName]) ? $this->activeForm->form['preset'][$this->colName] : [];
69
        if (!empty($this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName])) {
70
            $preset = array_merge($preset, $this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName]);
71
        }
72
        if ($preset) {
73
            $value = '';
74
            if (!empty($preset['value'])) {
75
                $value = $preset['value'];
76
            } elseif (!empty($preset['userCol'])) {
77
                if (strpos($preset['userCol'], ':')) {
78
                    $rel = substr($preset['userCol'], 0, strpos($preset['userCol'], ':'));
79
                    $param = substr($preset['userCol'], strpos($preset['userCol'], ':') + 1);
80
                    $value = \Users\User::$cur->$rel->$param;
81
                }
82
            }
83
            return $value;
84
        }
85
        return null;
86
    }
87
88
    public function colName() {
89
        return "{$this->activeForm->requestFormName}[{$this->activeForm->modelName}]" . (stristr($this->colName, '[') ? $this->colName : "[{$this->colName}]");
90
    }
91
92
    public function colLabel() {
93
        $modelName = $this->modelName;
94
        return isset($this->colParams['label']) ? $this->colParams['label'] : (($this->activeForm->model && !empty($modelName::$labels[$this->colName])) ? $modelName::$labels[$this->colName] : $this->colName);
95
    }
96
97
    public function readOnly() {
98
        if (!empty($this->colParams['readonly'])) {
99
            if (is_bool($this->colParams['readonly'])) {
100
                return true;
101
            }
102
            $readonly = true;
103
            if (is_array($this->colParams['readonly'])) {
104
                switch ($this->colParams['readonly']['cond']) {
105
                    case 'colValue':
106
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['col']} == $this->colParams['readonly']['value'];
107
                        if (!empty($this->colParams['readonly']['reverse'])) {
108
                            $readonly = !$readonly;
109
                        }
110
                        break;
111
                    case 'itemMethod':
112
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['method']}();
113
                        break;
114
                }
115
            }
116
            if ($readonly) {
117
                return true;
118
            }
119
        }
120
        return !empty($this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]) && in_array($this->colName, $this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]);
121
    }
122
123
    public function validate(&$request) {
124
        if (empty($request[$this->colName]) && !empty($this->colParams['required'])) {
125
            throw new \Exception('Вы не заполнили: ' . $this->colLabel());
126
        }
127
        if (!empty($this->colParams['validator'])) {
128
            $modelName = $this->modelName;
129
            $validator = $modelName::validator($this->colParams['validator']);
130
            $validator($this->activeForm, $request);
131
        }
132
    }
133
}