| Conditions | 26 |
| Paths | 108 |
| Total Lines | 83 |
| Code Lines | 62 |
| Lines | 10 |
| Ratio | 12.05 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 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 |
||
| 98 | public function checkRequest($params = [], $ajax = false) |
||
|
2 ignored issues
–
show
|
|||
| 99 | { |
||
| 100 | View Code Duplication | if (!$this->checkAccess()) { |
|
| 101 | $this->drawError('you not have access to "' . $this->modelName . '" manager with name: "' . $this->formName . '"'); |
||
| 102 | return []; |
||
| 103 | } |
||
| 104 | $successId = 0; |
||
| 105 | if (!empty($_POST[$this->requestFormName][$this->modelName])) { |
||
| 106 | $request = $_POST[$this->requestFormName][$this->modelName]; |
||
| 107 | if ($this->model) { |
||
| 108 | $presets = !empty($this->form['preset']) ? $this->form['preset'] : []; |
||
| 109 | View Code Duplication | if (!empty($this->form['userGroupPreset'][\Users\User::$cur->group_id])) { |
|
| 110 | $presets = array_merge($presets, $this->form['userGroupPreset'][\Users\User::$cur->group_id]); |
||
| 111 | } |
||
| 112 | $afterSave = []; |
||
| 113 | $error = false; |
||
| 114 | foreach ($this->inputs as $col => $param) { |
||
| 115 | if (!empty($presets[$col])) { |
||
| 116 | continue; |
||
| 117 | } |
||
| 118 | if (is_object($param)) { |
||
| 119 | $afterSave[] = $param; |
||
| 120 | continue; |
||
| 121 | } |
||
| 122 | View Code Duplication | if (!empty($this->form['userGroupReadonly'][\Users\User::$cur->group_id]) && in_array($col, $this->form['userGroupReadonly'][\Users\User::$cur->group_id])) { |
|
| 123 | continue; |
||
| 124 | } |
||
| 125 | $inputClassName = '\Ui\ActiveForm\Input\\' . ucfirst($param['type']); |
||
| 126 | $input = new $inputClassName(); |
||
| 127 | $input->activeForm = $this; |
||
| 128 | $input->activeFormParams = $params; |
||
| 129 | $input->modelName = $this->modelName; |
||
| 130 | $input->colName = $col; |
||
| 131 | $input->colParams = $param; |
||
| 132 | try { |
||
| 133 | $input->validate($request); |
||
| 134 | $input->parseRequest($request); |
||
| 135 | } catch (\Exception $exc) { |
||
| 136 | \Msg::add($exc->getMessage(), 'danger'); |
||
| 137 | $error = true; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | if (!$error) { |
||
| 141 | foreach ($presets as $col => $preset) { |
||
| 142 | if (!empty($preset['value'])) { |
||
| 143 | $this->model->$col = $preset['value']; |
||
| 144 | } elseif (!empty($preset['userCol'])) { |
||
| 145 | if (strpos($preset['userCol'], ':')) { |
||
| 146 | $rel = substr($preset['userCol'], 0, strpos($preset['userCol'], ':')); |
||
| 147 | $param = substr($preset['userCol'], strpos($preset['userCol'], ':') + 1); |
||
| 148 | $this->model->$col = \Users\User::$cur->$rel->$param; |
||
| 149 | } else { |
||
| 150 | $this->model->$col = \Users\User::$cur->{$preset['userCol']}; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | if (!$this->parent) { |
||
| 155 | if (!empty($this->form['successText'])) { |
||
| 156 | $text = $this->form['successText']; |
||
| 157 | } else { |
||
| 158 | $text = $this->model->pk() ? 'Изменения были успешно сохранены' : 'Новый элемент был успешно добавлен'; |
||
| 159 | } |
||
| 160 | \Msg::add($text, 'success'); |
||
| 161 | } |
||
| 162 | |||
| 163 | $this->model->save(!empty($params['dataManagerParams']) ? $params['dataManagerParams'] : []); |
||
| 164 | foreach ($afterSave as $form) { |
||
| 165 | $form->checkRequest(); |
||
| 166 | } |
||
| 167 | if ($ajax) { |
||
| 168 | \Msg::show(); |
||
| 169 | } elseif (!empty($_GET['redirectUrl'])) { |
||
| 170 | \Tools::redirect($_GET['redirectUrl']); |
||
| 171 | } |
||
| 172 | $successId = $this->model->pk(); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | if (!is_array($params) && is_callable($params)) { |
||
| 176 | $params($request); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | return $successId; |
||
| 180 | } |
||
| 181 | |||
| 319 |
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: