| Conditions | 13 |
| Paths | 98 |
| Total Lines | 51 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 182 |
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 |
||
| 118 | protected function getFormElement() |
||
| 119 | { |
||
| 120 | if (! empty($this->config['form_type'])) { |
||
| 121 | if (! in_array($this->config['form_type'], $this->databaseTypeToFormType)) { |
||
| 122 | throw new FactoryException("Unknown form type " . $this->config['form_type']); |
||
| 123 | } |
||
| 124 | |||
| 125 | $formElementType = $this->config['form_type']; |
||
| 126 | } else { |
||
| 127 | if (! array_key_exists($this->column->getType()->getName(), $this->databaseTypeToFormType)) { |
||
| 128 | throw new FactoryException("No form type found for database type " . $this->column->getType()->getName()); |
||
| 129 | } |
||
| 130 | |||
| 131 | $formElementType = $this->databaseTypeToFormType[$this->column->getType()->getName()]; |
||
| 132 | } |
||
| 133 | |||
| 134 | if (isset($this->config['defaults']) && is_array($this->config['defaults'])) { |
||
| 135 | $formElementType = 'select'; |
||
| 136 | } |
||
| 137 | |||
| 138 | |||
| 139 | $formElement = $this->factory->get($formElementType, []); |
||
| 140 | |||
| 141 | if (! empty($this->config['attr']) && is_array($this->config['attr'])) { |
||
| 142 | $formElement->attr($this->config['attr']); |
||
| 143 | } |
||
| 144 | |||
| 145 | if ($formElementType !== 'hidden') { |
||
| 146 | $formElement->class('form-control') |
||
| 147 | ->label($this->getPresentation()) |
||
| 148 | ->placeholder($this->getPresentation()); |
||
| 149 | } |
||
| 150 | |||
| 151 | if ($formElementType === 'textarea') { |
||
| 152 | $formElement->class('form-control ' . config('anavel-crud.text_editor')); |
||
| 153 | } |
||
| 154 | |||
| 155 | if (isset($this->config['defaults'])) { |
||
| 156 | if (! is_array($this->config['defaults'])) { |
||
| 157 | $formElement->val(transcrud($this->config['defaults'])); |
||
| 158 | } else { |
||
| 159 | $defaults = []; |
||
| 160 | foreach ($this->config['defaults'] as $key => $default) { |
||
| 161 | $defaults[$key] = transcrud($default); |
||
| 162 | } |
||
| 163 | $formElement->options($defaults); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | return $formElement; |
||
| 168 | } |
||
| 169 | |||
| 176 |