Total Complexity | 101 |
Total Lines | 343 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ActiveForm 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 ActiveForm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class ActiveForm extends \InjiObject { |
||
|
|||
15 | |||
16 | /** |
||
17 | * @var \Model |
||
18 | */ |
||
19 | public $model = null; |
||
20 | public $modelName = ''; |
||
21 | public $header = ""; |
||
22 | public $action = ""; |
||
23 | public $form = []; |
||
24 | public $inputs = []; |
||
25 | public $formName = 'noNameForm'; |
||
26 | public $requestFormName = ''; |
||
27 | public $requestFullFormName = ''; |
||
28 | public $parent = null; |
||
29 | |||
30 | public function __construct($model, $form = '') { |
||
62 | } |
||
63 | } |
||
64 | |||
65 | public function getInputs() { |
||
66 | $inputs = !empty($this->form['inputs']) ? $this->form['inputs'] : []; |
||
67 | $modelName = $this->modelName; |
||
68 | foreach ($this->form['map'] as $row) { |
||
69 | foreach ($row as $col) { |
||
70 | if (!$col || !empty($inputs[$col])) { |
||
71 | continue; |
||
72 | } |
||
73 | if (strpos($col, 'form:') === 0) { |
||
74 | $colPath = explode(':', $col); |
||
75 | if ($this->model->{$colPath[1]}) { |
||
76 | $inputs[$col] = new ActiveForm($this->model->{$colPath[1]}, $colPath[2]); |
||
77 | } else { |
||
78 | $relOptions = $modelName::getRelation($colPath[1]); |
||
79 | if (!isset($this->model->_params[$modelName::index()])) { |
||
80 | $this->model->_params[$modelName::index()] = 0; |
||
81 | } |
||
82 | $relOptions['model']::fixPrefix($relOptions['col']); |
||
83 | $inputs[$col] = new ActiveForm(new $relOptions['model'](), $colPath[2]); |
||
84 | } |
||
85 | $inputs[$col]->parent = $this; |
||
86 | |||
87 | $inputs[$col]->parent = $this; |
||
88 | } elseif (!empty($modelName::$cols[$col])) { |
||
89 | $inputs[$col] = $modelName::$cols[$col]; |
||
90 | } |
||
91 | } |
||
92 | } |
||
93 | return $inputs; |
||
94 | } |
||
95 | |||
96 | public function checkRequest($params = [], $ajax = false) { |
||
97 | if (!$this->checkAccess()) { |
||
98 | $this->drawError('you not have access to "' . $this->modelName . '" manager with name: "' . $this->formName . '"'); |
||
99 | return []; |
||
100 | } |
||
101 | $successId = 0; |
||
102 | $modelName = $this->model; |
||
103 | if (!empty($_POST[$this->requestFormName][$this->modelName]) || !empty($_FILES[$this->requestFormName]['tmp_name'][$this->modelName])) { |
||
104 | $request = !empty($_POST[$this->requestFormName][$this->modelName]) ? $_POST[$this->requestFormName][$this->modelName] : []; |
||
105 | if ($this->model) { |
||
106 | if (!empty($this->form['handler']) && empty($_GET['notSave'])) { |
||
107 | |||
108 | $modelName::{$this->form['handler']}($request); |
||
109 | $text = 'Новый элемент был успешно добавлен'; |
||
110 | \Msg::add($text, 'success'); |
||
111 | \Msg::show(); |
||
112 | } else { |
||
113 | $presets = !empty($this->form['preset']) ? $this->form['preset'] : []; |
||
114 | if (!empty($this->form['userGroupPreset'][\Users\User::$cur->group_id])) { |
||
115 | $presets = array_merge($presets, $this->form['userGroupPreset'][\Users\User::$cur->group_id]); |
||
116 | } |
||
117 | $afterSave = []; |
||
118 | $error = false; |
||
119 | foreach ($this->inputs as $col => $param) { |
||
120 | if (!empty($presets[$col])) { |
||
121 | continue; |
||
122 | } |
||
123 | if (is_object($param)) { |
||
124 | $afterSave[$col] = $param; |
||
125 | continue; |
||
126 | } |
||
127 | if (!empty($this->form['userGroupReadonly'][\Users\User::$cur->group_id]) && in_array($col, $this->form['userGroupReadonly'][\Users\User::$cur->group_id])) { |
||
128 | continue; |
||
129 | } |
||
130 | $type = ucfirst($param['type']); |
||
131 | if ($type == 'DynamicType') { |
||
132 | switch ($param['typeSource']) { |
||
133 | case 'selfMethod': |
||
134 | $type = $this->model->{$param['selfMethod']}(); |
||
135 | if (is_array($type)) { |
||
136 | $param = $type; |
||
137 | $type = $type['type']; |
||
138 | } |
||
139 | break; |
||
140 | } |
||
141 | } |
||
142 | $inputClassName = '\Ui\ActiveForm\Input\\' . ucfirst($type); |
||
143 | /** @var \Ui\ActiveForm\Input $input */ |
||
144 | $input = new $inputClassName(); |
||
145 | $input->activeForm = $this; |
||
146 | $input->activeFormParams = $params; |
||
147 | $input->modelName = $this->modelName; |
||
148 | $input->colName = $col; |
||
149 | $input->colParams = $param; |
||
150 | try { |
||
151 | $input->validate($request); |
||
152 | $input->parseRequest($request); |
||
153 | } catch (\Exception $exc) { |
||
154 | \Msg::add($exc->getMessage(), 'danger'); |
||
155 | $error = true; |
||
156 | } |
||
157 | } |
||
158 | if (!$error && empty($_GET['notSave'])) { |
||
159 | foreach ($presets as $col => $preset) { |
||
160 | if (!empty($preset['value'])) { |
||
161 | $this->model->$col = $preset['value']; |
||
162 | } elseif (!empty($preset['userCol'])) { |
||
163 | if (strpos($preset['userCol'], ':')) { |
||
164 | $rel = substr($preset['userCol'], 0, strpos($preset['userCol'], ':')); |
||
165 | $param = substr($preset['userCol'], strpos($preset['userCol'], ':') + 1); |
||
166 | $this->model->$col = \Users\User::$cur->$rel->$param; |
||
167 | } else { |
||
168 | $this->model->$col = \Users\User::$cur->{$preset['userCol']}; |
||
169 | } |
||
170 | } |
||
171 | } |
||
172 | if (!$this->parent) { |
||
173 | if (!empty($this->form['successText'])) { |
||
174 | $text = $this->form['successText']; |
||
175 | } else { |
||
176 | $text = $this->model->pk() ? 'Изменения были успешно сохранены' : 'Новый элемент был успешно добавлен'; |
||
177 | } |
||
178 | \Msg::add($text, 'success'); |
||
179 | } |
||
180 | |||
181 | $this->model->save(!empty($params['dataManagerParams']) ? $params['dataManagerParams'] : []); |
||
182 | foreach ($afterSave as $col => $form) { |
||
183 | if (strpos($col, 'form:') === 0) { |
||
184 | $colPath = explode(':', $col); |
||
185 | if (!$this->model->{$colPath[1]}) { |
||
186 | $relOptions = $modelName::getRelation($colPath[1]); |
||
187 | if (!isset($this->model->_params[$modelName::index()])) { |
||
188 | $this->model->_params[$modelName::index()] = 0; |
||
189 | } |
||
190 | $relOptions['model']::fixPrefix($relOptions['col']); |
||
191 | $form->model->{$relOptions['col']} = $this->model->_params[$modelName::index()]; |
||
192 | } |
||
193 | } |
||
194 | $form->checkRequest(); |
||
195 | } |
||
196 | if ($ajax) { |
||
197 | \Msg::show(); |
||
198 | } elseif (!empty($_GET['redirectUrl'])) { |
||
199 | \Tools::redirect($_GET['redirectUrl'] . (!empty($_GET['dataManagerHash']) ? '#' . $_GET['dataManagerHash'] : '')); |
||
200 | } |
||
201 | $successId = $this->model->pk(); |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | if (!is_array($params) && is_callable($params)) { |
||
206 | $params($request); |
||
207 | } |
||
208 | } |
||
209 | return $successId; |
||
210 | } |
||
211 | |||
212 | public function draw($params = [], $ajax = false) { |
||
213 | if (!$this->checkAccess()) { |
||
214 | $this->drawError('you not have access to "' . $this->modelName . '" form with name: "' . $this->formName . '"'); |
||
215 | return []; |
||
216 | } |
||
217 | $form = new Form(!empty($this->form['formOptions']) ? $this->form['formOptions'] : []); |
||
218 | \App::$cur->view->widget('Ui\ActiveForm', ['form' => $form, 'activeForm' => $this, 'ajax' => $ajax, 'params' => $params]); |
||
219 | } |
||
220 | |||
221 | public function drawCol($colName, $options, $form, $params = []) { |
||
249 | } |
||
250 | |||
251 | public static function getOptionsList($inputParams, $params = [], $modelName = '', $aditionalInputNamePrefix = 'aditional', $options = [],$model=false) { |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * Draw error message |
||
324 | * |
||
325 | * @param string $errorText |
||
326 | */ |
||
327 | public function drawError($errorText) { |
||
328 | echo $errorText; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Check access cur user to form with name in param and $model |
||
333 | * |
||
334 | * @return boolean |
||
335 | */ |
||
336 | public function checkAccess() { |
||
357 | } |
||
358 | } |
||
359 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths