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 |
||
| 19 | abstract class FormField |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * Name of the field. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $name; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Type of the field. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $type; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * All options for the field. |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $options = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Is field rendered. |
||
| 44 | * |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $rendered = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Form |
||
| 51 | */ |
||
| 52 | protected $parent; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $template; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var FormHelper |
||
| 61 | */ |
||
| 62 | protected $formHelper; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Name of the property for value setting. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $valueProperty = 'value'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Name of the property for default value. |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $defaultValueProperty = 'default_value'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Is default value set? |
||
| 80 | * |
||
| 81 | * @var bool|false |
||
| 82 | */ |
||
| 83 | protected $hasDefault = false; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var \Closure|null |
||
| 87 | */ |
||
| 88 | protected $valueClosure = null; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Array of filters key(alias/name) => objects. |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $filters = []; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Raw/unfiltered field value. |
||
| 99 | * |
||
| 100 | * @var mixed $rawValues |
||
| 101 | */ |
||
| 102 | protected $rawValue; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Override filters with same alias/name for field. |
||
| 106 | * |
||
| 107 | * @var bool |
||
| 108 | */ |
||
| 109 | protected $filtersOverride = false; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $name |
||
| 113 | * @param string $type |
||
| 114 | * @param Form $parent |
||
| 115 | * @param array $options |
||
| 116 | */ |
||
| 117 | 101 | public function __construct($name, $type, Form $parent, array $options = []) |
|
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Setup the value of the form field. |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | 101 | protected function setupValue() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Get the template, can be config variable or view path. |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | abstract protected function getTemplate(); |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | 35 | protected function getViewTemplate() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Render the field. |
||
| 168 | * |
||
| 169 | * @param array $options |
||
| 170 | * @param bool $showLabel |
||
| 171 | * @param bool $showField |
||
| 172 | * @param bool $showError |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 35 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Return the extra render data for this form field, passed into the field's template directly. |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | 35 | protected function getRenderData() { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Get the attribute value from the model by name. |
||
| 227 | * |
||
| 228 | * @param mixed $model |
||
| 229 | * @param string $name |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | 92 | protected function getModelValueAttribute($model, $name) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Transform array like syntax to dot syntax. |
||
| 246 | * |
||
| 247 | * @param string $key |
||
| 248 | * @return mixed |
||
| 249 | */ |
||
| 250 | 101 | protected function transformKey($key) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Prepare options for rendering. |
||
| 257 | * |
||
| 258 | * @param array $options |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 101 | protected function prepareOptions(array $options = []) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Get name of the field. |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | 39 | public function getName() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Set name of the field. |
||
| 336 | * |
||
| 337 | * @param string $name |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | 12 | public function setName($name) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Get dot notation key for fields. |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | **/ |
||
| 352 | 54 | public function getNameKey() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Get field options. |
||
| 359 | * |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | 12 | public function getOptions() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Get single option from options array. Can be used with dot notation ('attr.class'). |
||
| 369 | * |
||
| 370 | * @param string $option |
||
| 371 | * @param mixed|null $default |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | 101 | public function getOption($option, $default = null) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Set field options. |
||
| 381 | * |
||
| 382 | * @param array $options |
||
| 383 | * @return $this |
||
| 384 | */ |
||
| 385 | 12 | public function setOptions($options) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Set single option on the field. |
||
| 394 | * |
||
| 395 | * @param string $name |
||
| 396 | * @param mixed $value |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | 101 | public function setOption($name, $value) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Get the type of the field. |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | 65 | public function getType() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Set type of the field. |
||
| 418 | * |
||
| 419 | * @param mixed $type |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | 1 | public function setType($type) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @return Form |
||
| 433 | */ |
||
| 434 | 101 | public function getParent() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Check if the field is rendered. |
||
| 441 | * |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | 4 | public function isRendered() |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Default options for field. |
||
| 451 | * |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | 76 | protected function getDefaults() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Defaults used across all fields. |
||
| 461 | * |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | 101 | private function allDefaults() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Get real name of the field without form namespace. |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | 100 | public function getRealName() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * @param $value |
||
| 496 | * @return $this |
||
| 497 | */ |
||
| 498 | 94 | public function setValue($value) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Set the template property on the object. |
||
| 521 | * |
||
| 522 | * @return void |
||
| 523 | */ |
||
| 524 | 101 | private function setTemplate() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Add error class to wrapper if validation errors exist. |
||
| 531 | * |
||
| 532 | * @return void |
||
| 533 | */ |
||
| 534 | 101 | protected function addErrorClass() |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Merge all defaults with field specific defaults and set template if passed. |
||
| 563 | * |
||
| 564 | * @param array $options |
||
| 565 | */ |
||
| 566 | 101 | protected function setDefaultOptions(array $options = []) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Creates default wrapper classes for the form element. |
||
| 579 | * |
||
| 580 | * @param array $options |
||
| 581 | * @return array |
||
| 582 | */ |
||
| 583 | 101 | protected function setDefaultClasses(array $options = []) |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Setup the label for the form field. |
||
| 604 | * |
||
| 605 | * @return void |
||
| 606 | */ |
||
| 607 | 101 | protected function setupLabel() |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Check if fields needs label. |
||
| 630 | * |
||
| 631 | * @return bool |
||
| 632 | */ |
||
| 633 | 35 | protected function needsLabel() |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Disable field. |
||
| 647 | * |
||
| 648 | * @return $this |
||
| 649 | */ |
||
| 650 | 1 | public function disable() |
|
| 656 | |||
| 657 | /** |
||
| 658 | * Enable field. |
||
| 659 | * |
||
| 660 | * @return $this |
||
| 661 | */ |
||
| 662 | 1 | public function enable() |
|
| 668 | |||
| 669 | /** |
||
| 670 | * Get validation rules for a field if any with label for attributes. |
||
| 671 | * |
||
| 672 | * @return array|null |
||
| 673 | */ |
||
| 674 | 9 | public function getValidationRules() |
|
| 710 | |||
| 711 | /** |
||
| 712 | * Get this field's attributes, probably just one. |
||
| 713 | * |
||
| 714 | * @return array |
||
| 715 | */ |
||
| 716 | 3 | public function getAllAttributes() |
|
| 720 | |||
| 721 | /** |
||
| 722 | * Get value property. |
||
| 723 | * |
||
| 724 | * @param mixed|null $default |
||
| 725 | * @return mixed |
||
| 726 | */ |
||
| 727 | 38 | public function getValue($default = null) |
|
| 731 | |||
| 732 | /** |
||
| 733 | * Get default value property. |
||
| 734 | * |
||
| 735 | * @param mixed|null $default |
||
| 736 | * @return mixed |
||
| 737 | */ |
||
| 738 | 35 | public function getDefaultValue($default = null) |
|
| 742 | |||
| 743 | /** |
||
| 744 | * Check if provided value is valid for this type. |
||
| 745 | * |
||
| 746 | * @return bool |
||
| 747 | */ |
||
| 748 | 94 | protected function isValidValue($value) |
|
| 752 | |||
| 753 | /** |
||
| 754 | * Method initFilters used to initialize filters |
||
| 755 | * from field options and bind it to the same. |
||
| 756 | * |
||
| 757 | * @return $this |
||
| 758 | */ |
||
| 759 | 96 | protected function initFilters() |
|
| 778 | |||
| 779 | /** |
||
| 780 | * Method setFilters used to set filters to current filters property. |
||
| 781 | * |
||
| 782 | * @param array $filters |
||
| 783 | * |
||
| 784 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 785 | */ |
||
| 786 | public function setFilters(array $filters) |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Method getFilters returns array of binded filters |
||
| 798 | * if there are any binded. Otherwise empty array. |
||
| 799 | * |
||
| 800 | * @return array |
||
| 801 | */ |
||
| 802 | 23 | public function getFilters() |
|
| 806 | |||
| 807 | /** |
||
| 808 | * @param string|FilterInterface $filter |
||
| 809 | * |
||
| 810 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 811 | * |
||
| 812 | * @throws FilterAlreadyBindedException |
||
| 813 | */ |
||
| 814 | 8 | public function addFilter($filter) |
|
| 842 | |||
| 843 | /** |
||
| 844 | * Method removeFilter used to remove filter by provided alias/name. |
||
| 845 | * |
||
| 846 | * @param string $name |
||
| 847 | * |
||
| 848 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 849 | */ |
||
| 850 | 1 | public function removeFilter($name) |
|
| 860 | |||
| 861 | /** |
||
| 862 | * Method removeFilters used to remove filters by provided aliases/names. |
||
| 863 | * |
||
| 864 | * @param array $filterNames |
||
| 865 | * |
||
| 866 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 867 | */ |
||
| 868 | 1 | public function removeFilters(array $filterNames) |
|
| 880 | |||
| 881 | /** |
||
| 882 | * Method clearFilters used to empty current filters property. |
||
| 883 | * |
||
| 884 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 885 | */ |
||
| 886 | 1 | public function clearFilters() |
|
| 891 | |||
| 892 | /** |
||
| 893 | * Method used to set FiltersOverride status to provided value. |
||
| 894 | * |
||
| 895 | * @param $status |
||
| 896 | * |
||
| 897 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 898 | */ |
||
| 899 | 2 | public function setFiltersOverride($status) |
|
| 904 | |||
| 905 | /** |
||
| 906 | * @return bool |
||
| 907 | */ |
||
| 908 | 9 | public function getFiltersOverride() |
|
| 912 | |||
| 913 | /** |
||
| 914 | * Method used to set Unfiltered/Unmutated field value. |
||
| 915 | * Method is called before field value mutating starts - request value filtering. |
||
| 916 | * |
||
| 917 | * @param mixed $value |
||
| 918 | * |
||
| 919 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 920 | */ |
||
| 921 | 1 | public function setRawValue($value) |
|
| 926 | |||
| 927 | /** |
||
| 928 | * Returns unfiltered raw value of field. |
||
| 929 | * |
||
| 930 | * @return mixed |
||
| 931 | */ |
||
| 932 | public function getRawValue() |
||
| 936 | |||
| 937 | /** |
||
| 938 | * Get config from the form. |
||
| 939 | * |
||
| 940 | * @return mixed |
||
| 941 | */ |
||
| 942 | 101 | private function getConfig($key = null, $default = null) |
|
| 946 | } |
||
| 947 |
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.