Passed
Push — v5 ( e348bf...8a8f01 )
by Alexey
06:35
created

Input   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 161
rs 5.5555
c 0
b 0
f 0
wmc 56

9 Methods

Rating   Name   Duplication   Size   Complexity  
B value() 0 8 7
B validate() 0 18 11
C readOnly() 0 24 9
B defaultValue() 0 21 6
B preset() 0 19 7
B parseRequest() 0 17 5
A colName() 0 2 2
B draw() 0 20 5
A colLabel() 0 3 4

How to fix   Complexity   

Complex Class

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.

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
15
class Input extends \InjiObject {
0 ignored issues
show
Bug introduced by
The type InjiObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
    public $form = null;
18
    /**
19
     * @var \Ui\ActiveForm
20
     */
21
    public $activeForm = null;
22
    public $activeFormParams = [];
23
    public $modelName = '';
24
    public $colName = '';
25
    public $colParams = [];
26
    public $options = [];
27
28
    public function draw() {
29
        $inputName = $this->colName();
30
        $inputLabel = $this->colLabel();
31
        $inputOptions = $this->options;
32
        $inputOptions['value'] = $this->value();
33
        $inputOptions['disabled'] = $this->readOnly();
34
        if (!empty($this->colParams['required']) || (!empty($this->colParams['requiredOnNew']) && !$this->activeForm->model->pk())) {
35
            $inputOptions['required'] = true;
36
        }
37
38
        $preset = $this->preset();
39
        if ($preset !== null) {
40
            $inputOptions['disabled'] = true;
41
            $this->form->input('hidden', $inputName, '', $inputOptions);
42
            return true;
43
        }
44
        $classPath = explode('\\', get_called_class());
45
        $inputType = lcfirst(array_pop($classPath));
46
        $this->form->input($inputType, $inputName, $inputLabel, $inputOptions);
47
        return true;
48
    }
49
50
    public function parseRequest($request) {
51
        $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
52
        if ($this->readOnly()) {
53
            if ($this->activeForm->model->pk()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
54
            } else {
55
                $this->activeForm->model->{$colName} = $this->defaultValue();
56
            }
57
            return true;
58
        }
59
60
        if (isset($request[$this->colName])) {
61
            $this->activeForm->model->{$colName} = $request[$this->colName];
62
        } else {
63
            $this->activeForm->model->{$colName} = 0;
64
            $this->activeForm->model->{$colName} = '';
65
        }
66
        return true;
67
    }
68
69
    public function value() {
70
        $value = $this->defaultValue();
71
        if ($this->activeForm) {
72
            $colName = empty($this->colParams['col']) ? $this->colName : $this->colParams['col'];
73
            $value = ($this->activeForm && $this->activeForm->model && isset($this->activeForm->model->{$colName})) ? $this->activeForm->model->{$colName} : $value;
74
        }
75
        $value = isset($this->colParams['value']) ? $this->colParams['value'] : $value;
76
        return $value;
77
    }
78
79
    public function defaultValue($value = '') {
80
        if (isset($this->colParams['default'])) {
81
            if (is_array($this->colParams['default'])) {
82
                switch ($this->colParams['default']['type']) {
83
                    case 'relPath':
84
                        $val = $this->activeForm->model;
85
                        foreach (explode(':', $this->colParams['default']['relPath']) as $path) {
86
                            if ($val->$path) {
87
                                $val = $val->$path;
88
                            } else {
89
                                break 2;
90
                            }
91
                            $value = $val;
92
                        }
93
                        break;
94
                }
95
            } else {
96
                $value = $this->colParams['default'];
97
            }
98
        }
99
        return $value;
100
    }
101
102
    public function preset() {
103
        $preset = !empty($this->activeForm->form['preset'][$this->colName]) ? $this->activeForm->form['preset'][$this->colName] : [];
104
        if (!empty($this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName])) {
105
            $preset = array_merge($preset, $this->activeForm->form['userGroupPreset'][\Users\User::$cur->group_id][$this->colName]);
106
        }
107
        if ($preset) {
108
            $value = '';
109
            if (!empty($preset['value'])) {
110
                $value = $preset['value'];
111
            } elseif (!empty($preset['userCol'])) {
112
                if (strpos($preset['userCol'], ':')) {
113
                    $rel = substr($preset['userCol'], 0, strpos($preset['userCol'], ':'));
114
                    $param = substr($preset['userCol'], strpos($preset['userCol'], ':') + 1);
115
                    $value = \Users\User::$cur->$rel->$param;
116
                }
117
            }
118
            return $value;
119
        }
120
        return null;
121
    }
122
123
    public function colName() {
124
        return "{$this->activeForm->requestFormName}[{$this->activeForm->modelName}]" . (stristr($this->colName, '[') ? $this->colName : "[{$this->colName}]");
125
    }
126
127
    public function colLabel() {
128
        $modelName = $this->modelName;
129
        return isset($this->colParams['label']) ? $this->colParams['label'] : (($this->activeForm->model && !empty($modelName::$labels[$this->colName])) ? $modelName::$labels[$this->colName] : $this->colName);
0 ignored issues
show
Bug introduced by
The property labels does not exist on string.
Loading history...
130
    }
131
132
    public function readOnly() {
133
        if (!empty($this->colParams['readonly'])) {
134
            if (is_bool($this->colParams['readonly'])) {
135
                return true;
136
            }
137
            $readonly = true;
138
            if (is_array($this->colParams['readonly'])) {
139
                switch ($this->colParams['readonly']['cond']) {
140
                    case 'colValue':
141
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['col']} == $this->colParams['readonly']['value'];
142
                        if (!empty($this->colParams['readonly']['reverse'])) {
143
                            $readonly = !$readonly;
144
                        }
145
                        break;
146
                    case 'itemMethod':
147
                        $readonly = $this->activeForm->model->{$this->colParams['readonly']['method']}();
148
                        break;
149
                }
150
            }
151
            if ($readonly) {
152
                return true;
153
            }
154
        }
155
        return !empty($this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]) && in_array($this->colName, $this->activeForm->form['userGroupReadonly'][\Users\User::$cur->group_id]);
156
    }
157
158
    public function validate(&$request) {
159
        if (!empty($this->colParams['required']) && empty($request[$this->colName])) {
160
            throw new \Exception('Вы не заполнили: ' . $this->colLabel());
161
        }
162
        if (!empty($this->colParams['requiredOnNew']) && !$this->activeForm->model->pk() && empty($request[$this->colName])) {
163
            throw new \Exception('Вы не заполнили: ' . $this->colLabel());
164
        }
165
        if (!empty($this->colParams['unique']) && is_string($request[$this->colName])) {
166
            $modelName = $this->activeForm->modelName;
167
            $item = $modelName::get($request[$this->colName], $this->colName);
168
            if ($item && $item->id != $this->activeForm->model->id) {
169
                throw new \Exception($modelName::objectName() . ' с ' . $this->colLabel() . ' "' . $request[$this->colName] . '" уже существует');
170
            }
171
        }
172
        if (!empty($this->colParams['validator'])) {
173
            $modelName = $this->modelName;
174
            $validator = $modelName::validator($this->colParams['validator']);
175
            $validator($this->activeForm, $request);
176
        }
177
    }
178
}