| Total Complexity | 56 |
| Total Lines | 161 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 15 | class Input extends \Inji\InjiObject { |
||
| 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()) { |
||
| 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'][\Inji\Users\User::$cur->group_id][$this->colName])) { |
||
| 105 | $preset = array_merge($preset, $this->activeForm->form['userGroupPreset'][\Inji\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 = \Inji\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); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function readOnly() { |
||
| 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])) { |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
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