Total Complexity | 45 |
Total Lines | 219 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 1 |
Complex classes like FormElement 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 FormElement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | abstract class FormElement |
||
9 | { |
||
10 | use GluesAttributes; |
||
11 | |||
12 | /** @var array */ |
||
13 | protected $params; |
||
14 | |||
15 | /** @var Form */ |
||
16 | protected $form; |
||
17 | |||
18 | /** @var object */ |
||
19 | public $model = null; |
||
20 | |||
21 | /** @var string */ |
||
22 | public $id; |
||
23 | |||
24 | /** @var string */ |
||
25 | public $name; |
||
26 | |||
27 | /** @var mixed */ |
||
28 | public $value; |
||
29 | |||
30 | /** @var string */ |
||
31 | public $label; |
||
32 | |||
33 | /** @var array */ |
||
34 | public $class = []; |
||
35 | |||
36 | /** @var array */ |
||
37 | public $labelClass = []; |
||
38 | |||
39 | /** @var bool */ |
||
40 | public $required = false; |
||
41 | |||
42 | /** @var bool */ |
||
43 | public $disabled = false; |
||
44 | |||
45 | /** @var bool */ |
||
46 | public $readonly = false; |
||
47 | |||
48 | /** @var bool */ |
||
49 | public $autocomplete; |
||
50 | |||
51 | /** @var array */ |
||
52 | public $attributesList = []; |
||
53 | |||
54 | /** @var string */ |
||
55 | public $desc; |
||
56 | |||
57 | /** @var string */ |
||
58 | public $help; |
||
59 | |||
60 | /** @var array */ |
||
61 | public $customAttributes; |
||
62 | |||
63 | public function __construct(array $params) |
||
64 | { |
||
65 | $this->params = $params; |
||
66 | |||
67 | $this->setForm(); |
||
68 | $this->setModel(); |
||
69 | $this->setCommonAttributes(); |
||
70 | $this->setSpecificAttributes(); |
||
71 | } |
||
72 | |||
73 | protected function setForm() |
||
74 | { |
||
75 | $this->form = app(Form::class); |
||
76 | } |
||
77 | |||
78 | protected function setModel() |
||
79 | { |
||
80 | if (isset($this->params['model']) && ! empty($this->params['model'])) { |
||
81 | $this->model = $this->params['model']; |
||
82 | } |
||
83 | |||
84 | // Check to see if the model was added on the form opening tag |
||
85 | if (is_null($this->model) && ! is_null($this->form->model)) { |
||
86 | $this->model = $this->form->model; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | protected function setCommonAttributes() |
||
91 | { |
||
92 | $this->setName(); |
||
93 | $this->setLabel(); |
||
94 | $this->setValue(); |
||
95 | $this->setClass(); |
||
96 | $this->setRequired(); |
||
97 | $this->setDisabled(); |
||
98 | $this->setReadonly(); |
||
99 | $this->setAutocomplete(); |
||
100 | $this->setDesc(); |
||
101 | $this->setHelp(); |
||
102 | |||
103 | // Additional, custom attributes set by the user (eg: data, v-model) |
||
104 | $this->setCustomAttributes(); |
||
105 | } |
||
106 | |||
107 | protected function setSpecificAttributes() |
||
108 | { |
||
109 | // ... |
||
110 | } |
||
111 | |||
112 | protected function setName() |
||
113 | { |
||
114 | if (isset($this->params['name']) && ! empty($this->params['name'])) { |
||
115 | $this->name = $this->id = $this->params['name']; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | protected function setLabel() |
||
138 | } |
||
139 | } |
||
140 | |||
141 | protected function setRequired() |
||
142 | { |
||
143 | if (isset($this->params['required']) && $this->params['required'] == true) { |
||
144 | $this->required = true; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | protected function setDisabled() |
||
149 | { |
||
150 | if (isset($this->params['disabled']) && $this->params['disabled'] == true) { |
||
151 | $this->disabled = true; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | protected function setReadonly() |
||
156 | { |
||
157 | if (isset($this->params['readonly']) && $this->params['readonly'] == true) { |
||
158 | $this->readonly = true; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | protected function setValue() |
||
163 | { |
||
164 | if (is_null($this->name)) { |
||
165 | return; |
||
166 | } |
||
167 | |||
168 | if (! is_null($this->value)) { |
||
169 | $computedValue = $this->value; |
||
170 | } elseif (! is_null($this->model) && isset($this->model->{str_replace('[]', '', $this->name)})) { |
||
171 | $computedValue = $this->model->{str_replace('[]', '', $this->name)}; |
||
172 | } |
||
173 | |||
174 | $this->value = old($this->name, $computedValue ?? ''); |
||
175 | } |
||
176 | |||
177 | protected function setAutocomplete() |
||
178 | { |
||
179 | // Set default autocomplete option (true/false) from cofing file |
||
180 | $this->autocomplete = config('blade-form-components.autocomplete'); |
||
181 | |||
182 | if (isset($this->params['autocomplete'])) { |
||
183 | $this->autocomplete = $this->params['autocomplete']; |
||
184 | } |
||
185 | } |
||
186 | |||
187 | protected function setDesc() |
||
191 | } |
||
192 | } |
||
193 | |||
194 | protected function setHelp() |
||
195 | { |
||
196 | if (isset($this->params['help'])) { |
||
197 | $this->help = $this->params['help']; |
||
198 | } |
||
199 | } |
||
200 | |||
201 | protected function setClass() |
||
202 | { |
||
203 | $this->setDefaultClass(); |
||
204 | |||
205 | // Attach the error class if an error is displayed against this field |
||
206 | $errors = session()->get('errors', app(ViewErrorBag::class)); |
||
207 | if ($errors->has($this->name)) { |
||
208 | $this->class[] = config('blade-form-components.styles.field.error'); |
||
209 | } |
||
210 | |||
211 | // Attach other user-defined classes |
||
212 | if (isset($this->params['class'])) { |
||
213 | $this->class[] = $this->params['class']; |
||
214 | } |
||
215 | } |
||
216 | |||
217 | protected function setDefaultClass() |
||
221 | } |
||
222 | |||
223 | protected function setCustomAttributes() |
||
224 | { |
||
225 | if (isset($this->params['attributes']['input']) && ! empty($this->params['attributes']['input'])) { |
||
226 | $this->customAttributes = $this->params['attributes']['input']; |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.