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 string |
||
| 22 | */ |
||
| 23 | protected $name; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Type of the field. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $type; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * All options for the field. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 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 | * |
||
| 78 | * @var bool|false |
||
| 79 | */ |
||
| 80 | protected $hasDefault = false; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var \Closure|null |
||
| 84 | */ |
||
| 85 | protected $valueClosure = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Array of filters key(alias/name) => objects. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected $filters = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Raw/unfiltered field value. |
||
| 96 | * |
||
| 97 | * @var mixed $rawValues |
||
| 98 | */ |
||
| 99 | protected $rawValue; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Override filters with same alias/name for field. |
||
| 103 | * |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | protected $filtersOverride = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $name |
||
| 110 | * @param string $type |
||
| 111 | * @param Form $parent |
||
| 112 | * @param array $options |
||
| 113 | */ |
||
| 114 | 97 | public function __construct($name, $type, Form $parent, array $options = []) |
|
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Setup the value of the form field. |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | 97 | protected function setupValue() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Get the template, can be config variable or view path. |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | abstract protected function getTemplate(); |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | 34 | protected function getViewTemplate() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Render the field. |
||
| 165 | * |
||
| 166 | * @param array $options |
||
| 167 | * @param bool $showLabel |
||
| 168 | * @param bool $showField |
||
| 169 | * @param bool $showError |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | 34 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Return the extra render data for this form field, passed into the field's template directly. |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | 34 | protected function getRenderData() { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Get the attribute value from the model by name. |
||
| 222 | * |
||
| 223 | * @param mixed $model |
||
| 224 | * @param string $name |
||
| 225 | * @return mixed |
||
| 226 | */ |
||
| 227 | 88 | protected function getModelValueAttribute($model, $name) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Transform array like syntax to dot syntax. |
||
| 241 | * |
||
| 242 | * @param string $key |
||
| 243 | * @return mixed |
||
| 244 | */ |
||
| 245 | 97 | protected function transformKey($key) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Prepare options for rendering. |
||
| 252 | * |
||
| 253 | * @param array $options |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | 97 | protected function prepareOptions(array $options = []) |
|
| 257 | { |
||
| 258 | 97 | $helper = $this->formHelper; |
|
| 259 | 97 | $rulesParser = $helper->createRulesParser($this); |
|
| 260 | 97 | $rules = $this->getOption('rules'); |
|
| 261 | 97 | $parsedRules = $rules ? $rulesParser->parse($rules) : []; |
|
| 262 | |||
| 263 | 97 | $this->options = $helper->mergeOptions($this->options, $options); |
|
| 264 | |||
| 265 | 97 | foreach (['attr', 'label_attr', 'wrapper'] as $appendable) { |
|
| 266 | // Append values to the 'class' attribute |
||
| 267 | 97 | if ($this->getOption("{$appendable}.class_append")) { |
|
| 268 | // Combine the current class attribute with the appends |
||
| 269 | 3 | $append = $this->getOption("{$appendable}.class_append"); |
|
| 270 | 3 | $classAttribute = $this->getOption("{$appendable}.class", '').' '.$append; |
|
| 271 | 3 | $this->setOption("{$appendable}.class", $classAttribute); |
|
| 272 | |||
| 273 | // Then remove the class_append option to prevent it from showing up as an attribute in the HTML |
||
| 274 | 97 | $this->setOption("{$appendable}.class_append", null); |
|
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | 97 | if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) { |
|
| 279 | 2 | $this->name = $this->name.'[]'; |
|
| 280 | 2 | $this->setOption('tmp.multipleBracesSet', true); |
|
| 281 | } |
||
| 282 | |||
| 283 | 97 | if ($this->parent->haveErrorsEnabled()) { |
|
| 284 | 97 | $this->addErrorClass(); |
|
| 285 | } |
||
| 286 | |||
| 287 | 97 | if ($this->getOption('required') === true || isset($parsedRules['required'])) { |
|
| 288 | 4 | $lblClass = $this->getOption('label_attr.class', ''); |
|
| 289 | 4 | $requiredClass = $helper->getConfig('defaults.required_class', 'required'); |
|
| 290 | |||
| 291 | 4 | if (! str_contains($lblClass, $requiredClass)) { |
|
| 292 | 4 | $lblClass .= ' '.$requiredClass; |
|
| 293 | 4 | $this->setOption('label_attr.class', $lblClass); |
|
| 294 | } |
||
| 295 | |||
| 296 | 4 | if ($this->parent->clientValidationEnabled()) { |
|
| 297 | 3 | $this->setOption('attr.required', 'required'); |
|
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | 97 | if ($this->parent->clientValidationEnabled() && $parsedRules) { |
|
|
|
|||
| 302 | 1 | $attrs = $this->getOption('attr') + $parsedRules; |
|
| 303 | 1 | $this->setOption('attr', $attrs); |
|
| 304 | } |
||
| 305 | |||
| 306 | 97 | $this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper'))); |
|
| 307 | 97 | $this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors'))); |
|
| 308 | |||
| 309 | 97 | if ($this->getOption('help_block.text')) { |
|
| 310 | 1 | $this->setOption( |
|
| 311 | 1 | 'help_block.helpBlockAttrs', |
|
| 312 | 1 | $helper->prepareAttributes($this->getOption('help_block.attr')) |
|
| 313 | ); |
||
| 314 | } |
||
| 315 | |||
| 316 | 97 | return $this->options; |
|
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Get name of the field. |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | 39 | public function getName() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Set name of the field. |
||
| 331 | * |
||
| 332 | * @param string $name |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | 12 | public function setName($name) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Get dot notation key for fields. |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | **/ |
||
| 347 | 53 | public function getNameKey() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Get field options. |
||
| 354 | * |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | 12 | public function getOptions() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Get single option from options array. Can be used with dot notation ('attr.class'). |
||
| 364 | * |
||
| 365 | * @param string $option |
||
| 366 | * @param mixed|null $default |
||
| 367 | * @return mixed |
||
| 368 | */ |
||
| 369 | 97 | public function getOption($option, $default = null) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Set field options. |
||
| 376 | * |
||
| 377 | * @param array $options |
||
| 378 | * @return $this |
||
| 379 | */ |
||
| 380 | 12 | public function setOptions($options) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Set single option on the field. |
||
| 389 | * |
||
| 390 | * @param string $name |
||
| 391 | * @param mixed $value |
||
| 392 | * @return $this |
||
| 393 | */ |
||
| 394 | 97 | public function setOption($name, $value) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Get the type of the field. |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | 62 | public function getType() |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Set type of the field. |
||
| 413 | * |
||
| 414 | * @param mixed $type |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | 1 | public function setType($type) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * @return Form |
||
| 428 | */ |
||
| 429 | 97 | public function getParent() |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Check if the field is rendered. |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | 4 | public function isRendered() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Default options for field. |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | 75 | protected function getDefaults() |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Defaults used across all fields. |
||
| 456 | * |
||
| 457 | * @return array |
||
| 458 | */ |
||
| 459 | 97 | private function allDefaults() |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Get real name of the field without form namespace. |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | 96 | public function getRealName() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * @param $value |
||
| 491 | * @return $this |
||
| 492 | */ |
||
| 493 | 90 | public function setValue($value) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Set the template property on the object. |
||
| 516 | * |
||
| 517 | * @return void |
||
| 518 | */ |
||
| 519 | 97 | private function setTemplate() |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Add error class to wrapper if validation errors exist. |
||
| 526 | * |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | 97 | protected function addErrorClass() |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Merge all defaults with field specific defaults and set template if passed. |
||
| 546 | * |
||
| 547 | * @param array $options |
||
| 548 | */ |
||
| 549 | 97 | protected function setDefaultOptions(array $options = []) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Creates default wrapper classes for the form element. |
||
| 562 | * |
||
| 563 | * @param array $options |
||
| 564 | * @return array |
||
| 565 | */ |
||
| 566 | 97 | protected function setDefaultClasses(array $options = []) |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Setup the label for the form field. |
||
| 587 | * |
||
| 588 | * @return void |
||
| 589 | */ |
||
| 590 | 97 | protected function setupLabel() |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Check if fields needs label. |
||
| 607 | * |
||
| 608 | * @return bool |
||
| 609 | */ |
||
| 610 | 34 | protected function needsLabel() |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Disable field. |
||
| 624 | * |
||
| 625 | * @return $this |
||
| 626 | */ |
||
| 627 | 1 | public function disable() |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Enable field. |
||
| 636 | * |
||
| 637 | * @return $this |
||
| 638 | */ |
||
| 639 | 1 | public function enable() |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Get validation rules for a field if any with label for attributes. |
||
| 648 | * |
||
| 649 | * @return array|null |
||
| 650 | */ |
||
| 651 | 9 | public function getValidationRules() |
|
| 687 | |||
| 688 | /** |
||
| 689 | * Get this field's attributes, probably just one. |
||
| 690 | * |
||
| 691 | * @return array |
||
| 692 | */ |
||
| 693 | 3 | public function getAllAttributes() |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Get value property. |
||
| 700 | * |
||
| 701 | * @param mixed|null $default |
||
| 702 | * @return mixed |
||
| 703 | */ |
||
| 704 | 37 | public function getValue($default = null) |
|
| 708 | |||
| 709 | /** |
||
| 710 | * Get default value property. |
||
| 711 | * |
||
| 712 | * @param mixed|null $default |
||
| 713 | * @return mixed |
||
| 714 | */ |
||
| 715 | 34 | public function getDefaultValue($default = null) |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Check if provided value is valid for this type. |
||
| 722 | * |
||
| 723 | * @return bool |
||
| 724 | */ |
||
| 725 | 92 | protected function isValidValue($value) |
|
| 729 | |||
| 730 | /** |
||
| 731 | * Method initFilters used to initialize filters |
||
| 732 | * from field options and bind it to the same. |
||
| 733 | * |
||
| 734 | * @return $this |
||
| 735 | */ |
||
| 736 | 92 | protected function initFilters() |
|
| 755 | |||
| 756 | /** |
||
| 757 | * Method setFilters used to set filters to current filters property. |
||
| 758 | * |
||
| 759 | * @param array $filters |
||
| 760 | * |
||
| 761 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 762 | */ |
||
| 763 | public function setFilters(array $filters) |
||
| 772 | |||
| 773 | /** |
||
| 774 | * Method getFilters returns array of binded filters |
||
| 775 | * if there are any binded. Otherwise empty array. |
||
| 776 | * |
||
| 777 | * @return array |
||
| 778 | */ |
||
| 779 | 23 | public function getFilters() |
|
| 783 | |||
| 784 | /** |
||
| 785 | * @param string|FilterInterface $filter |
||
| 786 | * |
||
| 787 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 788 | * |
||
| 789 | * @throws FilterAlreadyBindedException |
||
| 790 | */ |
||
| 791 | 8 | public function addFilter($filter) |
|
| 819 | |||
| 820 | /** |
||
| 821 | * Method removeFilter used to remove filter by provided alias/name. |
||
| 822 | * |
||
| 823 | * @param string $name |
||
| 824 | * |
||
| 825 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 826 | */ |
||
| 827 | 1 | public function removeFilter($name) |
|
| 837 | |||
| 838 | /** |
||
| 839 | * Method removeFilters used to remove filters by provided aliases/names. |
||
| 840 | * |
||
| 841 | * @param array $filterNames |
||
| 842 | * |
||
| 843 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 844 | */ |
||
| 845 | 1 | public function removeFilters(array $filterNames) |
|
| 857 | |||
| 858 | /** |
||
| 859 | * Method clearFilters used to empty current filters property. |
||
| 860 | * |
||
| 861 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 862 | */ |
||
| 863 | 1 | public function clearFilters() |
|
| 868 | |||
| 869 | /** |
||
| 870 | * Method used to set FiltersOverride status to provided value. |
||
| 871 | * |
||
| 872 | * @param $status |
||
| 873 | * |
||
| 874 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 875 | */ |
||
| 876 | 2 | public function setFiltersOverride($status) |
|
| 881 | |||
| 882 | /** |
||
| 883 | * @return bool |
||
| 884 | */ |
||
| 885 | 9 | public function getFiltersOverride() |
|
| 889 | |||
| 890 | /** |
||
| 891 | * Method used to set Unfiltered/Unmutated field value. |
||
| 892 | * Method is called before field value mutating starts - request value filtering. |
||
| 893 | * |
||
| 894 | * @param mixed $value |
||
| 895 | * |
||
| 896 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 897 | */ |
||
| 898 | 1 | public function setRawValue($value) |
|
| 903 | |||
| 904 | /** |
||
| 905 | * Returns unfiltered raw value of field. |
||
| 906 | * |
||
| 907 | * @return mixed |
||
| 908 | */ |
||
| 909 | public function getRawValue() |
||
| 913 | } |
||
| 914 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.