Total Complexity | 52 |
Total Lines | 320 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 2 | Features | 0 |
Complex classes like FieldAsTrait 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 FieldAsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | trait FieldAsTrait { |
||
38 | |||
39 | abstract protected function _getFieldIdentifier($prefix, $name = ""); |
||
40 | |||
41 | abstract public function setValueFunction($index, $callback); |
||
42 | |||
43 | abstract protected function _getFieldName($index); |
||
44 | |||
45 | abstract protected function _getFieldCaption($index); |
||
46 | |||
47 | abstract protected function _buttonAsSubmit(BaseHtml &$button, $event, $url, $responseElement = NULL, $parameters = NULL); |
||
48 | |||
49 | /** |
||
50 | * |
||
51 | * @param HtmlFormField $element |
||
52 | * @param array $attributes |
||
53 | */ |
||
54 | protected function _applyAttributes(BaseHtml $element, &$attributes, $index, $instance = null) { |
||
55 | if (isset($attributes["jsCallback"])) { |
||
56 | $callback = $attributes["jsCallback"]; |
||
57 | if (\is_callable($callback)) { |
||
58 | $callback($element, $instance, $index, InstanceViewer::$index); |
||
59 | } |
||
60 | } |
||
61 | unset($attributes["rules"]); |
||
62 | unset($attributes["ajax"]); |
||
63 | unset($attributes["visibleHover"]); |
||
64 | $element->fromArray($attributes); |
||
65 | } |
||
66 | |||
67 | private function _getLabelField($caption, $icon = NULL) { |
||
68 | $label = new HtmlLabel($this->_getFieldIdentifier("lbl"), $caption, $icon); |
||
69 | return $label; |
||
70 | } |
||
71 | |||
72 | protected function _addRules(HtmlFormField $element, &$attributes) { |
||
73 | if (isset($attributes["rules"])) { |
||
74 | $this->_hasRules = true; |
||
75 | $rules = $attributes["rules"]; |
||
76 | if (\is_array($rules)) { |
||
77 | $element->addRules($rules); |
||
78 | } else { |
||
79 | $element->addRule($rules); |
||
80 | } |
||
81 | unset($attributes["rules"]); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | protected function _prepareFormFields(HtmlFormField &$field, $name, &$attributes) { |
||
86 | $field->setName($name); |
||
87 | $this->_addRules($field, $attributes); |
||
88 | return $field; |
||
89 | } |
||
90 | |||
91 | protected function _fieldAs($elementCallback, &$index, $attributes = NULL, $prefix = null) { |
||
92 | $this->setValueFunction($index, function ($value, $instance, $index, $rowIndex) use (&$attributes, $elementCallback, $prefix) { |
||
93 | $caption = $this->_getFieldCaption($index); |
||
94 | $name = $this->_getFieldName($index); |
||
95 | $id = $this->_getFieldIdentifier($prefix, $name); |
||
96 | if (isset($attributes["name"])) { |
||
97 | $name = $attributes["name"]; |
||
98 | unset($attributes["name"]); |
||
99 | } |
||
100 | $element = $elementCallback($id, $name, $value, $caption); |
||
101 | if (\is_array($attributes)) { |
||
102 | $this->_applyAttributes($element, $attributes, $index, $instance); |
||
103 | } |
||
104 | $element->setDisabled(! $this->_edition); |
||
105 | return $element; |
||
106 | }); |
||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | public function fieldAsProgress($index, $label = NULL, $attributes = array()) { |
||
111 | $this->setValueFunction($index, function ($value) use ($label, $attributes) { |
||
112 | $pb = new HtmlProgress($this->_getFieldIdentifier("pb"), $value, $label, $attributes); |
||
113 | return $pb; |
||
114 | }); |
||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | public function fieldAsRating($index, $max = 5, $icon = "") { |
||
119 | $this->setValueFunction($index, function ($value) use ($max, $icon) { |
||
120 | $rating = new HtmlRating($this->_getFieldIdentifier("rat"), $value, $max, $icon); |
||
121 | return $rating; |
||
122 | }); |
||
123 | return $this; |
||
124 | } |
||
125 | |||
126 | public function fieldAsLabel($index, $icon = NULL, $attributes = NULL) { |
||
127 | return $this->_fieldAs(function ($id, $name, $value) use ($icon) { |
||
128 | $lbl = new HtmlLabel($id, $value); |
||
129 | if (isset($icon)) |
||
130 | $lbl->addIcon($icon); |
||
131 | return $lbl; |
||
132 | }, $index, $attributes, "label"); |
||
133 | } |
||
134 | |||
135 | public function fieldAsHeader($index, $niveau = 1, $icon = NULL, $attributes = NULL) { |
||
136 | return $this->_fieldAs(function ($id, $name, $value) use ($niveau, $icon) { |
||
137 | $header = new HtmlHeader($id, $niveau, $value); |
||
138 | if (isset($icon)) |
||
139 | $header->asIcon($icon, $value); |
||
140 | return $header; |
||
141 | }, $index, $attributes, "header"); |
||
142 | } |
||
143 | |||
144 | public function fieldAsImage($index, $size = Size::MINI, $circular = false) { |
||
145 | $this->setValueFunction($index, function ($img) use ($size, $circular) { |
||
146 | $image = new HtmlImage($this->_getFieldIdentifier("image"), $img); |
||
147 | $image->setSize($size); |
||
148 | if ($circular) |
||
149 | $image->setCircular(); |
||
150 | return $image; |
||
151 | }); |
||
152 | return $this; |
||
153 | } |
||
154 | |||
155 | public function fieldAsFlag($index) { |
||
156 | $this->setValueFunction($index, function ($flag) { |
||
157 | $flag = new HtmlFlag($this->_getFieldIdentifier("flag"), $flag); |
||
158 | return $flag; |
||
159 | }); |
||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | public function fieldAsIcon($index) { |
||
164 | $this->setValueFunction($index, function ($icon) { |
||
165 | $icon = new HtmlIcon($this->_getFieldIdentifier("icon"), $icon); |
||
166 | return $icon; |
||
167 | }); |
||
168 | return $this; |
||
169 | } |
||
170 | |||
171 | public function fieldAsAvatar($index, $attributes = NULL) { |
||
172 | return $this->_fieldAs(function ($id, $name, $value) { |
||
173 | $img = new HtmlImage($id, $value); |
||
174 | $img->asAvatar(); |
||
175 | return $img; |
||
176 | }, $index, $attributes, "avatar"); |
||
177 | } |
||
178 | |||
179 | public function fieldAsRadio($index, $attributes = NULL) { |
||
180 | return $this->_fieldAs(function ($id, $name, $value) use ($attributes) { |
||
181 | $input = new HtmlFormRadio($id, $name, $value, $value); |
||
182 | return $this->_prepareFormFields($input, $name, $attributes); |
||
183 | }, $index, $attributes, "radio"); |
||
184 | } |
||
185 | |||
186 | public function fieldAsRadios($index, $elements = [], $attributes = NULL) { |
||
187 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($elements) { |
||
188 | return HtmlFormFields::radios($name, $elements, $caption, $value); |
||
189 | }, $index, $attributes, "radios"); |
||
190 | } |
||
191 | |||
192 | public function fieldAsList($index, $classNames = "", $attributes = NULL) { |
||
193 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($classNames) { |
||
194 | $result = new HtmlList($name, $value); |
||
195 | $result->addClass($classNames); |
||
196 | return $result; |
||
197 | }, $index, $attributes, "list"); |
||
198 | } |
||
199 | |||
200 | public function fieldAsInput($index, $attributes = NULL) { |
||
205 | } |
||
206 | |||
207 | public function fieldAsLabeledInput($index, $attributes = NULL) { |
||
208 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
||
209 | $input = new HtmlFormInput($id, '', 'text', $value, $caption); |
||
210 | $required = ''; |
||
211 | if (isset($attributes['rules'])) { |
||
212 | $rules = json_encode($attributes['rules']); |
||
213 | if (strpos($rules, 'empty') !== false) { |
||
214 | $required = 'required'; |
||
215 | } |
||
216 | } |
||
217 | $input->getField() |
||
218 | ->labeled($caption) |
||
219 | ->setTagName('label') |
||
220 | ->addClass($required); |
||
221 | return $this->_prepareFormFields($input, $name, $attributes); |
||
222 | }, $index, $attributes, 'input'); |
||
223 | } |
||
224 | |||
225 | public function fieldAsDataList($index, ?array $items = [], $attributes = NULL) { |
||
232 | } |
||
233 | |||
234 | public function fieldAsFile($index, $attributes = NULL) { |
||
235 | if (isset($this->_form)) { |
||
236 | $this->_form->setProperty('enctype', 'multipart/form-data'); |
||
237 | } |
||
238 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
||
239 | $input = new HtmlFormInput($id, $caption); |
||
240 | $input->asFile(); |
||
241 | return $this->_prepareFormFields($input, $name, $attributes); |
||
242 | }, $index, $attributes, "input"); |
||
243 | } |
||
244 | |||
245 | public function fieldAsTextarea($index, $attributes = NULL) { |
||
246 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
||
247 | $textarea = new HtmlFormTextarea($id, $caption, $value); |
||
248 | return $this->_prepareFormFields($textarea, $name, $attributes); |
||
249 | }, $index, $attributes, "textarea"); |
||
250 | } |
||
251 | |||
252 | public function fieldAsElement($index, $tagName = "div", $baseClass = "", $attributes = NULL) { |
||
253 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes, $tagName, $baseClass) { |
||
254 | $div = new HtmlSemDoubleElement($id, $tagName, $baseClass); |
||
255 | $div->setContent(\htmlentities($value)); |
||
256 | $textarea = new HtmlFormField("field-" . $id, $div, $caption); |
||
257 | return $this->_prepareFormFields($textarea, $name, $attributes); |
||
258 | }, $index, $attributes, "element"); |
||
259 | } |
||
260 | |||
261 | public function fieldAsHidden($index, $attributes = NULL) { |
||
268 | } |
||
269 | |||
270 | public function fieldAsCheckbox($index, $attributes = NULL) { |
||
271 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) { |
||
272 | if ($caption === null || $caption === "") |
||
273 | $caption = ""; |
||
274 | $input = new HtmlFormCheckbox($id, $caption, $this->_instanceViewer->getIdentifier()); |
||
275 | $input->setChecked(JString::isBooleanTrue($value)); |
||
276 | return $this->_prepareFormFields($input, $name, $attributes); |
||
277 | }, $index, $attributes, "ck"); |
||
278 | } |
||
279 | |||
280 | public function fieldAsDropDown($index, $elements = [], $multiple = false, $attributes = NULL) { |
||
281 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($elements, $multiple, $attributes) { |
||
282 | $dd = new HtmlFormDropdown($id, $elements, $caption, $value ?? ''); |
||
283 | $dd->asSelect($name, $multiple); |
||
284 | return $this->_prepareFormFields($dd, $name, $attributes); |
||
285 | }, $index, $attributes, "dd"); |
||
286 | } |
||
287 | |||
288 | public function fieldAsMessage($index, $attributes = NULL) { |
||
289 | return $this->_fieldAs(function ($id, $name, $value, $caption) { |
||
290 | $mess = new HtmlMessage("message-" . $id, $caption); |
||
291 | $mess->addHeader($value); |
||
292 | return $mess; |
||
293 | }, $index, $attributes, "message"); |
||
294 | } |
||
295 | |||
296 | public function fieldAsLink($index, $attributes = NULL) { |
||
297 | return $this->_fieldAs(function ($id, $name, $value, $caption) { |
||
298 | $lnk = new HtmlLink("message-" . $id, "#", $caption); |
||
299 | return $lnk; |
||
300 | }, $index, $attributes, "link"); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Change fields type |
||
305 | * |
||
306 | * @param array $types |
||
307 | * an array or associative array $type=>$attributes |
||
308 | */ |
||
309 | public function fieldsAs(array $types) { |
||
310 | $i = 0; |
||
311 | if (JArray::isAssociative($types)) { |
||
312 | foreach ($types as $type => $attributes) { |
||
313 | if (\is_int($type)) |
||
314 | $this->fieldAs($i ++, $attributes, []); |
||
315 | else { |
||
316 | $type = preg_replace('/\d/', '', $type); |
||
317 | $this->fieldAs($i ++, $type, $attributes); |
||
318 | } |
||
319 | } |
||
320 | } else { |
||
321 | foreach ($types as $type) { |
||
322 | $this->fieldAs($i ++, $type); |
||
323 | } |
||
324 | } |
||
325 | } |
||
326 | |||
327 | public function fieldAs($index, $type, $attributes = NULL) { |
||
328 | $method = "fieldAs" . \ucfirst($type); |
||
329 | if (\method_exists($this, $method)) { |
||
330 | if (! \is_array($attributes)) { |
||
331 | $attributes = [ |
||
332 | $index |
||
333 | ]; |
||
334 | } else { |
||
335 | \array_unshift($attributes, $index); |
||
336 | } |
||
337 | \call_user_func_array([ |
||
338 | $this, |
||
339 | $method |
||
340 | ], $attributes); |
||
341 | } |
||
342 | } |
||
343 | |||
344 | public function fieldAsSubmit($index, $cssStyle = NULL, $url = NULL, $responseElement = NULL, $attributes = NULL) { |
||
345 | return $this->_fieldAs(function ($id, $name, $value, $caption) use ($url, $responseElement, $cssStyle, $attributes) { |
||
346 | $button = new HtmlButton($id, $caption, $cssStyle); |
||
347 | $this->_buttonAsSubmit($button, "click", $url, $responseElement, @$attributes["ajax"]); |
||
348 | return $button; |
||
349 | }, $index, $attributes, "submit"); |
||
350 | } |
||
351 | |||
352 | public function fieldAsButton($index, $cssStyle = NULL, $attributes = NULL) { |
||
357 | } |
||
358 | } |
||
359 |