Complex classes like FormField 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 FormField, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | abstract class FormField |
||
17 | { |
||
18 | /** |
||
19 | * Name of the field |
||
20 | * |
||
21 | * @var |
||
22 | */ |
||
23 | protected $name; |
||
24 | |||
25 | /** |
||
26 | * Type of the field |
||
27 | * |
||
28 | * @var |
||
29 | */ |
||
30 | protected $type; |
||
31 | |||
32 | /** |
||
33 | * All options for the field |
||
34 | * |
||
35 | * @var |
||
36 | */ |
||
37 | protected $options = []; |
||
38 | |||
39 | /** |
||
40 | * Is field rendered |
||
41 | * |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $rendered = false; |
||
45 | |||
46 | /** |
||
47 | * @var Form |
||
48 | */ |
||
49 | protected $parent; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $template; |
||
55 | |||
56 | /** |
||
57 | * @var FormHelper |
||
58 | */ |
||
59 | protected $formHelper; |
||
60 | |||
61 | /** |
||
62 | * Name of the property for value setting |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $valueProperty = 'value'; |
||
67 | |||
68 | /** |
||
69 | * Name of the property for default value |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $defaultValueProperty = 'default_value'; |
||
74 | |||
75 | /** |
||
76 | * Is default value set? |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $hasDefault = false; |
||
80 | |||
81 | /** |
||
82 | * @var \Closure|null |
||
83 | */ |
||
84 | protected $valueClosure = null; |
||
85 | |||
86 | /** |
||
87 | * @param $name |
||
88 | * @param $type |
||
89 | * @param Form $parent |
||
90 | * @param array $options |
||
91 | */ |
||
92 | 67 | public function __construct($name, $type, Form $parent, array $options = []) |
|
93 | { |
||
94 | 67 | $this->name = $name; |
|
95 | 67 | $this->type = $type; |
|
96 | 67 | $this->parent = $parent; |
|
97 | 67 | $this->formHelper = $this->parent->getFormHelper(); |
|
98 | 67 | $this->setTemplate(); |
|
99 | 67 | $this->setDefaultOptions($options); |
|
100 | 67 | $this->setupValue(); |
|
101 | 62 | } |
|
102 | |||
103 | 67 | protected function setupValue() |
|
104 | { |
||
105 | 67 | $value = $this->getOption($this->valueProperty); |
|
106 | 67 | $isChild = $this->getOption('is_child'); |
|
107 | |||
108 | 67 | if ($value instanceof \Closure) { |
|
109 | $this->valueClosure = $value; |
||
110 | } |
||
111 | |||
112 | 67 | if (($value === null || $value instanceof \Closure) && !$isChild) { |
|
113 | 58 | $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name)); |
|
114 | 62 | } elseif (!$isChild) { |
|
115 | 12 | $this->hasDefault = true; |
|
116 | 12 | } |
|
117 | 62 | } |
|
118 | |||
119 | /** |
||
120 | * Get the template, can be config variable or view path |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | abstract protected function getTemplate(); |
||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | 26 | protected function getViewTemplate() |
|
133 | |||
134 | /** |
||
135 | * @param array $options |
||
136 | * @param bool $showLabel |
||
137 | * @param bool $showField |
||
138 | * @param bool $showError |
||
139 | * @return string |
||
140 | */ |
||
141 | 26 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
|
142 | { |
||
143 | 26 | $this->prepareOptions($options); |
|
144 | 26 | $value = $this->getValue(); |
|
145 | 26 | $defaultValue = $this->getDefaultValue(); |
|
146 | |||
147 | 26 | if ($showField) { |
|
148 | 26 | $this->rendered = true; |
|
149 | 26 | } |
|
150 | |||
151 | // Override default value with value |
||
152 | 26 | if (!$this->isValidValue($value) && $this->isValidValue($defaultValue)) { |
|
153 | $this->setOption($this->valueProperty, $defaultValue); |
||
154 | } |
||
155 | |||
156 | 26 | if (!$this->needsLabel()) { |
|
157 | 8 | $showLabel = false; |
|
158 | 8 | } |
|
159 | |||
160 | 26 | if ($showError) { |
|
161 | 25 | $showError = $this->parent->haveErrorsEnabled(); |
|
162 | 25 | } |
|
163 | |||
164 | 26 | return $this->formHelper->getView()->make( |
|
165 | 26 | $this->getViewTemplate(), |
|
166 | [ |
||
167 | 26 | 'name' => $this->name, |
|
168 | 26 | 'nameKey' => $this->getNameKey(), |
|
169 | 26 | 'type' => $this->type, |
|
170 | 26 | 'options' => $this->options, |
|
171 | 26 | 'showLabel' => $showLabel, |
|
172 | 26 | 'showField' => $showField, |
|
173 | 'showError' => $showError |
||
174 | 26 | ] |
|
175 | 26 | )->render(); |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Get the attribute value from the model by name |
||
180 | * |
||
181 | * @param mixed $model |
||
182 | * @param string $name |
||
183 | * @return mixed |
||
184 | */ |
||
185 | 60 | protected function getModelValueAttribute($model, $name) |
|
186 | { |
||
187 | 60 | $transformedName = $this->transformKey($name); |
|
188 | 60 | if (is_string($model)) { |
|
189 | return $model; |
||
190 | 60 | } elseif (is_object($model)) { |
|
191 | 2 | return object_get($model, $transformedName); |
|
192 | 60 | } elseif (is_array($model)) { |
|
193 | 59 | return array_get($model, $transformedName); |
|
194 | } |
||
195 | 5 | } |
|
196 | |||
197 | /** |
||
198 | * Transform array like syntax to dot syntax |
||
199 | * |
||
200 | * @param $key |
||
201 | * @return mixed |
||
202 | */ |
||
203 | 67 | protected function transformKey($key) |
|
204 | { |
||
205 | 67 | return $this->formHelper->transformToDotSyntax($key); |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Prepare options for rendering |
||
210 | * |
||
211 | * @param array $options |
||
212 | * @return array |
||
213 | */ |
||
214 | 67 | protected function prepareOptions(array $options = []) |
|
215 | { |
||
216 | 67 | $helper = $this->formHelper; |
|
217 | 67 | $rulesParser = new RulesParser($this); |
|
218 | 67 | $rules = $this->getOption('rules'); |
|
219 | 67 | $parsedRules = $rules ? $rulesParser->parse($rules) : []; |
|
220 | |||
221 | 67 | $this->options = $helper->mergeOptions($this->options, $options); |
|
222 | |||
223 | 67 | if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) { |
|
224 | 2 | $this->name = $this->name.'[]'; |
|
225 | 2 | $this->setOption('tmp.multipleBracesSet', true); |
|
226 | 2 | } |
|
227 | |||
228 | 67 | if ($this->parent->haveErrorsEnabled()) { |
|
229 | 67 | $this->addErrorClass(); |
|
230 | 67 | } |
|
231 | |||
232 | 67 | if ($this->getOption('required') === true || isset($parsedRules['required'])) { |
|
233 | 3 | $lblClass = $this->getOption('label_attr.class', ''); |
|
234 | 3 | $requiredClass = $helper->getConfig('defaults.required_class', 'required'); |
|
235 | 3 | if (!str_contains($lblClass, $requiredClass)) { |
|
236 | 3 | $lblClass .= ' ' . $requiredClass; |
|
237 | 3 | $this->setOption('label_attr.class', $lblClass); |
|
238 | 3 | $this->setOption('attr.required', 'required'); |
|
239 | 3 | } |
|
240 | 3 | } |
|
241 | |||
242 | 67 | if ($this->parent->clientValidationEnabled() && $rules) { |
|
243 | 1 | $attrs = $this->getOption('attr') + $parsedRules; |
|
244 | 1 | $this->setOption('attr', $attrs); |
|
245 | 1 | } |
|
246 | |||
247 | 67 | $this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper'))); |
|
248 | 67 | $this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors'))); |
|
249 | |||
250 | 67 | if ($this->getOption('is_child')) { |
|
251 | 16 | $this->setOption('labelAttrs', $helper->prepareAttributes($this->getOption('label_attr'))); |
|
252 | 16 | } |
|
253 | |||
254 | 67 | if ($this->getOption('help_block.text')) { |
|
255 | 1 | $this->setOption( |
|
256 | 1 | 'help_block.helpBlockAttrs', |
|
257 | 1 | $helper->prepareAttributes($this->getOption('help_block.attr')) |
|
258 | 1 | ); |
|
259 | 1 | } |
|
260 | |||
261 | 67 | return $this->options; |
|
262 | } |
||
263 | |||
264 | /** |
||
265 | * Get name of the field |
||
266 | * |
||
267 | * @return string |
||
268 | */ |
||
269 | 23 | public function getName() |
|
273 | |||
274 | /** |
||
275 | * Set name of the field |
||
276 | * |
||
277 | * @param string $name |
||
278 | * @return $this |
||
279 | */ |
||
280 | 11 | public function setName($name) |
|
281 | { |
||
282 | 11 | $this->name = $name; |
|
283 | |||
284 | 11 | return $this; |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * Get dot notation key for fields |
||
289 | * |
||
290 | * @return string |
||
291 | **/ |
||
292 | 37 | public function getNameKey() |
|
296 | |||
297 | /** |
||
298 | * Get field options |
||
299 | * |
||
300 | * @return array |
||
301 | */ |
||
302 | 9 | public function getOptions() |
|
306 | |||
307 | /** |
||
308 | * Get single option from options array. Can be used with dot notation ('attr.class') |
||
309 | * |
||
310 | * @param $option |
||
311 | * @param mixed $default |
||
312 | * |
||
313 | * @return mixed |
||
314 | */ |
||
315 | 67 | public function getOption($option, $default = null) |
|
316 | { |
||
317 | 67 | return array_get($this->options, $option, $default); |
|
318 | } |
||
319 | |||
320 | /** |
||
321 | * Set field options |
||
322 | * |
||
323 | * @param array $options |
||
324 | * @return $this |
||
325 | */ |
||
326 | 11 | public function setOptions($options) |
|
327 | { |
||
328 | 11 | $this->options = $this->prepareOptions($options); |
|
329 | |||
330 | 11 | return $this; |
|
331 | } |
||
332 | |||
333 | /** |
||
334 | * Set single option on the field |
||
335 | * |
||
336 | * @param string $name |
||
337 | * @param mixed $value |
||
338 | * @return $this |
||
339 | */ |
||
340 | 67 | public function setOption($name, $value) |
|
341 | { |
||
342 | 67 | array_set($this->options, $name, $value); |
|
343 | |||
344 | 67 | return $this; |
|
345 | } |
||
346 | |||
347 | /** |
||
348 | * Get the type of the field |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | 38 | public function getType() |
|
356 | |||
357 | /** |
||
358 | * Set type of the field |
||
359 | * |
||
360 | * @param mixed $type |
||
361 | * @return $this |
||
362 | */ |
||
363 | 1 | public function setType($type) |
|
364 | { |
||
365 | 1 | if ($this->formHelper->getFieldType($type)) { |
|
366 | 1 | $this->type = $type; |
|
367 | 1 | } |
|
368 | |||
369 | 1 | return $this; |
|
370 | } |
||
371 | |||
372 | /** |
||
373 | * @return Form |
||
374 | */ |
||
375 | 67 | public function getParent() |
|
379 | |||
380 | /** |
||
381 | * Check if the field is rendered |
||
382 | * |
||
383 | * @return bool |
||
384 | */ |
||
385 | 4 | public function isRendered() |
|
389 | |||
390 | /** |
||
391 | * Default options for field |
||
392 | * |
||
393 | * @return array |
||
394 | */ |
||
395 | 47 | protected function getDefaults() |
|
399 | |||
400 | /** |
||
401 | * Defaults used across all fields |
||
402 | * |
||
403 | * @return array |
||
404 | */ |
||
405 | 67 | private function allDefaults() |
|
406 | { |
||
407 | return [ |
||
408 | 67 | 'wrapper' => ['class' => $this->formHelper->getConfig('defaults.wrapper_class')], |
|
409 | 67 | 'attr' => ['class' => $this->formHelper->getConfig('defaults.field_class')], |
|
410 | 67 | 'help_block' => ['text' => null, 'tag' => 'p', 'attr' => [ |
|
411 | 67 | 'class' => $this->formHelper->getConfig('defaults.help_block_class') |
|
412 | 67 | ]], |
|
413 | 67 | 'value' => null, |
|
414 | 67 | 'default_value' => null, |
|
415 | 67 | 'label' => $this->getLabel(), |
|
416 | 67 | 'is_child' => false, |
|
417 | 67 | 'label_attr' => ['class' => $this->formHelper->getConfig('defaults.label_class')], |
|
418 | 67 | 'errors' => ['class' => $this->formHelper->getConfig('defaults.error_class')], |
|
419 | 67 | 'rules' => [] |
|
420 | 67 | ]; |
|
421 | } |
||
422 | |||
423 | /** |
||
424 | * Get real name of the field without form namespace |
||
425 | * |
||
426 | * @return string |
||
427 | */ |
||
428 | 67 | public function getRealName() |
|
432 | |||
433 | /** |
||
434 | * @param $value |
||
435 | * @return $this |
||
436 | */ |
||
437 | 61 | public function setValue($value) |
|
438 | { |
||
439 | 61 | if ($this->hasDefault) { |
|
440 | 1 | return $this; |
|
441 | } |
||
442 | |||
443 | 61 | $closure = $this->valueClosure; |
|
444 | |||
445 | 61 | if ($closure instanceof \Closure) { |
|
446 | $value = $closure($value ?: null); |
||
447 | } |
||
448 | |||
449 | 61 | if (!$this->isValidValue($value)) { |
|
450 | 59 | $value = $this->getOption($this->defaultValueProperty); |
|
451 | 59 | } |
|
452 | |||
453 | 61 | $this->options[$this->valueProperty] = $value; |
|
454 | |||
455 | 61 | return $this; |
|
456 | } |
||
457 | |||
458 | /** |
||
459 | * Set the template property on the object |
||
460 | */ |
||
461 | 67 | private function setTemplate() |
|
465 | |||
466 | /** |
||
467 | * Add error class to wrapper if validation errors exist |
||
468 | */ |
||
469 | 67 | protected function addErrorClass() |
|
470 | { |
||
471 | 67 | $errors = $this->parent->getRequest()->session()->get('errors'); |
|
472 | |||
473 | 67 | if ($errors && $errors->has($this->getNameKey())) { |
|
474 | $errorClass = $this->formHelper->getConfig('defaults.wrapper_error_class'); |
||
475 | $wrapperClass = $this->getOption('wrapper.class'); |
||
476 | |||
477 | if ($this->getOption('wrapper') && !str_contains($wrapperClass, $errorClass)) { |
||
478 | $wrapperClass .= ' ' . $errorClass; |
||
479 | $this->setOption('wrapper.class', $wrapperClass); |
||
480 | } |
||
481 | } |
||
482 | 67 | } |
|
483 | |||
484 | |||
485 | /** |
||
486 | * Merge all defaults with field specific defaults and set template if passed |
||
487 | * |
||
488 | * @param array $options |
||
489 | */ |
||
490 | 67 | protected function setDefaultOptions(array $options = []) |
|
491 | { |
||
492 | 67 | $this->options = $this->formHelper->mergeOptions($this->allDefaults(), $this->getDefaults()); |
|
493 | 67 | $this->options = $this->prepareOptions($options); |
|
494 | 67 | } |
|
495 | |||
496 | /** |
||
497 | * Check if fields needs label |
||
498 | * |
||
499 | * @return bool |
||
500 | */ |
||
501 | 26 | protected function needsLabel() |
|
512 | |||
513 | /** |
||
514 | * Disable field |
||
515 | * |
||
516 | * @return $this |
||
517 | */ |
||
518 | 1 | public function disable() |
|
519 | { |
||
520 | 1 | $this->setOption('attr.disabled', 'disabled'); |
|
521 | |||
522 | 1 | return $this; |
|
523 | } |
||
524 | |||
525 | /** |
||
526 | * Enable field |
||
527 | * |
||
528 | * @return $this |
||
529 | */ |
||
530 | 1 | public function enable() |
|
531 | { |
||
532 | 1 | array_forget($this->options, 'attr.disabled'); |
|
533 | |||
534 | 1 | return $this; |
|
535 | } |
||
536 | |||
537 | /** |
||
538 | * Get validation rules for a field if any with label for attributes |
||
539 | * |
||
540 | * @return array|null |
||
541 | */ |
||
542 | 3 | public function getValidationRules() |
|
543 | { |
||
544 | 3 | $rules = $this->getOption('rules', []); |
|
545 | 3 | $name = $this->getNameKey(); |
|
546 | |||
547 | 3 | if (!$rules) { |
|
548 | return []; |
||
549 | } |
||
550 | |||
551 | return [ |
||
552 | 3 | 'rules' => [$name => $rules], |
|
553 | 3 | 'attributes' => [$name => $this->getOption('label')] |
|
554 | 3 | ]; |
|
555 | } |
||
556 | |||
557 | /** |
||
558 | * Get value property |
||
559 | * |
||
560 | * @param mixed|null $default |
||
561 | * @return mixed |
||
562 | */ |
||
563 | 29 | public function getValue($default = null) |
|
567 | |||
568 | /** |
||
569 | * Get default value property |
||
570 | * |
||
571 | * @param mixed|null $default |
||
572 | * @return mixed |
||
573 | */ |
||
574 | 26 | public function getDefaultValue($default = null) |
|
578 | |||
579 | /** |
||
580 | * Check if provided value is valid for this type |
||
581 | * |
||
582 | * @return bool |
||
583 | */ |
||
584 | 62 | protected function isValidValue($value) |
|
588 | |||
589 | /** |
||
590 | * Get label for the field |
||
591 | */ |
||
592 | 67 | public function getLabel() |
|
593 | { |
||
594 | 67 | return $this->formHelper->formatLabel($this->getLabelRaw()); |
|
595 | } |
||
596 | |||
597 | /** |
||
598 | * Get formatted label for the field |
||
599 | */ |
||
600 | 67 | public function getLabelRaw() |
|
601 | { |
||
602 | 67 | $label = $this->getOption('label', $this->getRealName()); |
|
603 | |||
604 | 67 | if ($langName = $this->parent->getLanguageName()) { |
|
605 | 3 | return sprintf('%s.%s', $langName, $label); |
|
606 | } |
||
607 | |||
611 | } |
||
612 |