Conditions | 29 |
Paths | 110 |
Total Lines | 90 |
Code Lines | 69 |
Lines | 10 |
Ratio | 11.11 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
96 | public function checkRequest($params = [], $ajax = false) { |
||
2 ignored issues
–
show
|
|||
97 | View Code Duplication | 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 | if (!empty($_POST[$this->requestFormName][$this->modelName])) { |
||
103 | $request = $_POST[$this->requestFormName][$this->modelName]; |
||
104 | if ($this->model) { |
||
105 | if (!empty($this->form['handler'])) { |
||
106 | $modelName = $this->model; |
||
107 | $modelName::{$this->form['handler']}($request); |
||
108 | $text = 'Новый элемент был успешно добавлен'; |
||
109 | \Msg::add($text, 'success'); |
||
110 | \Msg::show(); |
||
111 | } else { |
||
112 | $presets = !empty($this->form['preset']) ? $this->form['preset'] : []; |
||
113 | View Code Duplication | if (!empty($this->form['userGroupPreset'][\Users\User::$cur->group_id])) { |
|
114 | $presets = array_merge($presets, $this->form['userGroupPreset'][\Users\User::$cur->group_id]); |
||
115 | } |
||
116 | $afterSave = []; |
||
117 | $error = false; |
||
118 | foreach ($this->inputs as $col => $param) { |
||
119 | if (!empty($presets[$col])) { |
||
120 | continue; |
||
121 | } |
||
122 | if (is_object($param)) { |
||
123 | $afterSave[] = $param; |
||
124 | continue; |
||
125 | } |
||
126 | View Code Duplication | if (!empty($this->form['userGroupReadonly'][\Users\User::$cur->group_id]) && in_array($col, $this->form['userGroupReadonly'][\Users\User::$cur->group_id])) { |
|
127 | continue; |
||
128 | } |
||
129 | $inputClassName = '\Ui\ActiveForm\Input\\' . ucfirst($param['type']); |
||
130 | $input = new $inputClassName(); |
||
131 | $input->activeForm = $this; |
||
132 | $input->activeFormParams = $params; |
||
133 | $input->modelName = $this->modelName; |
||
134 | $input->colName = $col; |
||
135 | $input->colParams = $param; |
||
136 | try { |
||
137 | $input->validate($request); |
||
138 | $input->parseRequest($request); |
||
139 | } catch (\Exception $exc) { |
||
140 | \Msg::add($exc->getMessage(), 'danger'); |
||
141 | $error = true; |
||
142 | } |
||
143 | } |
||
144 | if (!$error && empty($_GET['notSave'])) { |
||
145 | foreach ($presets as $col => $preset) { |
||
146 | if (!empty($preset['value'])) { |
||
147 | $this->model->$col = $preset['value']; |
||
148 | } elseif (!empty($preset['userCol'])) { |
||
149 | if (strpos($preset['userCol'], ':')) { |
||
150 | $rel = substr($preset['userCol'], 0, strpos($preset['userCol'], ':')); |
||
151 | $param = substr($preset['userCol'], strpos($preset['userCol'], ':') + 1); |
||
152 | $this->model->$col = \Users\User::$cur->$rel->$param; |
||
153 | } else { |
||
154 | $this->model->$col = \Users\User::$cur->{$preset['userCol']}; |
||
155 | } |
||
156 | } |
||
157 | } |
||
158 | if (!$this->parent) { |
||
159 | if (!empty($this->form['successText'])) { |
||
160 | $text = $this->form['successText']; |
||
161 | } else { |
||
162 | $text = $this->model->pk() ? 'Изменения были успешно сохранены' : 'Новый элемент был успешно добавлен'; |
||
163 | } |
||
164 | \Msg::add($text, 'success'); |
||
165 | } |
||
166 | |||
167 | $this->model->save(!empty($params['dataManagerParams']) ? $params['dataManagerParams'] : []); |
||
168 | foreach ($afterSave as $form) { |
||
169 | $form->checkRequest(); |
||
170 | } |
||
171 | if ($ajax) { |
||
172 | \Msg::show(); |
||
173 | } elseif (!empty($_GET['redirectUrl'])) { |
||
174 | \Tools::redirect($_GET['redirectUrl'] . (!empty($_GET['dataManagerHash']) ? '#' . $_GET['dataManagerHash'] : '')); |
||
175 | } |
||
176 | $successId = $this->model->pk(); |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | if (!is_array($params) && is_callable($params)) { |
||
181 | $params($request); |
||
182 | } |
||
183 | } |
||
184 | return $successId; |
||
185 | } |
||
186 | |||
323 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: