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) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Transform array like syntax to dot syntax. |
||
| 239 | * |
||
| 240 | * @param string $key |
||
| 241 | * @return mixed |
||
| 242 | */ |
||
| 243 | 54 | protected function transformKey($key) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Prepare options for rendering. |
||
| 250 | * |
||
| 251 | * @param array $options |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | 101 | protected function prepareOptions(array $options = []) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Get name of the field. |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | 39 | public function getName() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Set name of the field. |
||
| 329 | * |
||
| 330 | * @param string $name |
||
| 331 | * @return $this |
||
| 332 | */ |
||
| 333 | 12 | public function setName($name) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Get dot notation key for fields. |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | **/ |
||
| 345 | 54 | public function getNameKey() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Get field options. |
||
| 352 | * |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | 12 | public function getOptions() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Get single option from options array. Can be used with dot notation ('attr.class'). |
||
| 362 | * |
||
| 363 | * @param string $option |
||
| 364 | * @param mixed|null $default |
||
| 365 | * @return mixed |
||
| 366 | */ |
||
| 367 | 101 | public function getOption($option, $default = null) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Set field options. |
||
| 374 | * |
||
| 375 | * @param array $options |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | 12 | public function setOptions($options) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Set single option on the field. |
||
| 387 | * |
||
| 388 | * @param string $name |
||
| 389 | * @param mixed $value |
||
| 390 | * @return $this |
||
| 391 | */ |
||
| 392 | 101 | public function setOption($name, $value) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Get the type of the field. |
||
| 401 | * |
||
| 402 | * @return string |
||
| 403 | */ |
||
| 404 | 65 | public function getType() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Set type of the field. |
||
| 411 | * |
||
| 412 | * @param mixed $type |
||
| 413 | * @return $this |
||
| 414 | */ |
||
| 415 | 1 | public function setType($type) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @return Form |
||
| 426 | */ |
||
| 427 | 101 | public function getParent() |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Check if the field is rendered. |
||
| 434 | * |
||
| 435 | * @return bool |
||
| 436 | */ |
||
| 437 | 4 | public function isRendered() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Default options for field. |
||
| 444 | * |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | 76 | protected function getDefaults() |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Defaults used across all fields. |
||
| 454 | * |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | 101 | private function allDefaults() |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Get real name of the field without form namespace. |
||
| 479 | * |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | 100 | public function getRealName() |
|
| 486 | |||
| 487 | /** |
||
| 488 | * @param $value |
||
| 489 | * @return $this |
||
| 490 | */ |
||
| 491 | 94 | public function setValue($value) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Set the template property on the object. |
||
| 514 | * |
||
| 515 | * @return void |
||
| 516 | */ |
||
| 517 | 101 | private function setTemplate() |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Add error class to wrapper if validation errors exist. |
||
| 524 | * |
||
| 525 | * @return void |
||
| 526 | */ |
||
| 527 | 101 | protected function addErrorClass() |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Merge all defaults with field specific defaults and set template if passed. |
||
| 553 | * |
||
| 554 | * @param array $options |
||
| 555 | */ |
||
| 556 | 101 | protected function setDefaultOptions(array $options = []) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * Creates default wrapper classes for the form element. |
||
| 569 | * |
||
| 570 | * @param array $options |
||
| 571 | * @return array |
||
| 572 | */ |
||
| 573 | 101 | protected function setDefaultClasses(array $options = []) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Setup the label for the form field. |
||
| 594 | * |
||
| 595 | * @return void |
||
| 596 | */ |
||
| 597 | 101 | protected function setupLabel() |
|
| 617 | |||
| 618 | /** |
||
| 619 | * Check if fields needs label. |
||
| 620 | * |
||
| 621 | * @return bool |
||
| 622 | */ |
||
| 623 | 35 | protected function needsLabel() |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Disable field. |
||
| 637 | * |
||
| 638 | * @return $this |
||
| 639 | */ |
||
| 640 | 1 | public function disable() |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Enable field. |
||
| 649 | * |
||
| 650 | * @return $this |
||
| 651 | */ |
||
| 652 | 1 | public function enable() |
|
| 658 | |||
| 659 | /** |
||
| 660 | * Get validation rules for a field if any with label for attributes. |
||
| 661 | * |
||
| 662 | * @return array|null |
||
| 663 | */ |
||
| 664 | 9 | public function getValidationRules() |
|
| 700 | |||
| 701 | /** |
||
| 702 | * Get this field's attributes, probably just one. |
||
| 703 | * |
||
| 704 | * @return array |
||
| 705 | */ |
||
| 706 | 3 | public function getAllAttributes() |
|
| 710 | |||
| 711 | /** |
||
| 712 | * Get value property. |
||
| 713 | * |
||
| 714 | * @param mixed|null $default |
||
| 715 | * @return mixed |
||
| 716 | */ |
||
| 717 | 38 | public function getValue($default = null) |
|
| 721 | |||
| 722 | /** |
||
| 723 | * Get default value property. |
||
| 724 | * |
||
| 725 | * @param mixed|null $default |
||
| 726 | * @return mixed |
||
| 727 | */ |
||
| 728 | 35 | public function getDefaultValue($default = null) |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Check if provided value is valid for this type. |
||
| 735 | * |
||
| 736 | * @return bool |
||
| 737 | */ |
||
| 738 | 94 | protected function isValidValue($value) |
|
| 742 | |||
| 743 | /** |
||
| 744 | * Method initFilters used to initialize filters |
||
| 745 | * from field options and bind it to the same. |
||
| 746 | * |
||
| 747 | * @return $this |
||
| 748 | */ |
||
| 749 | 96 | protected function initFilters() |
|
| 768 | |||
| 769 | /** |
||
| 770 | * Method setFilters used to set filters to current filters property. |
||
| 771 | * |
||
| 772 | * @param array $filters |
||
| 773 | * |
||
| 774 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 775 | */ |
||
| 776 | public function setFilters(array $filters) |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Method getFilters returns array of binded filters |
||
| 788 | * if there are any binded. Otherwise empty array. |
||
| 789 | * |
||
| 790 | * @return array |
||
| 791 | */ |
||
| 792 | 23 | public function getFilters() |
|
| 796 | |||
| 797 | /** |
||
| 798 | * @param string|FilterInterface $filter |
||
| 799 | * |
||
| 800 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 801 | * |
||
| 802 | * @throws FilterAlreadyBindedException |
||
| 803 | */ |
||
| 804 | 8 | public function addFilter($filter) |
|
| 832 | |||
| 833 | /** |
||
| 834 | * Method removeFilter used to remove filter by provided alias/name. |
||
| 835 | * |
||
| 836 | * @param string $name |
||
| 837 | * |
||
| 838 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 839 | */ |
||
| 840 | 1 | public function removeFilter($name) |
|
| 850 | |||
| 851 | /** |
||
| 852 | * Method removeFilters used to remove filters by provided aliases/names. |
||
| 853 | * |
||
| 854 | * @param array $filterNames |
||
| 855 | * |
||
| 856 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 857 | */ |
||
| 858 | 1 | public function removeFilters(array $filterNames) |
|
| 870 | |||
| 871 | /** |
||
| 872 | * Method clearFilters used to empty current filters property. |
||
| 873 | * |
||
| 874 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 875 | */ |
||
| 876 | 1 | public function clearFilters() |
|
| 881 | |||
| 882 | /** |
||
| 883 | * Method used to set FiltersOverride status to provided value. |
||
| 884 | * |
||
| 885 | * @param $status |
||
| 886 | * |
||
| 887 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 888 | */ |
||
| 889 | 2 | public function setFiltersOverride($status) |
|
| 894 | |||
| 895 | /** |
||
| 896 | * @return bool |
||
| 897 | */ |
||
| 898 | 9 | public function getFiltersOverride() |
|
| 902 | |||
| 903 | /** |
||
| 904 | * Method used to set Unfiltered/Unmutated field value. |
||
| 905 | * Method is called before field value mutating starts - request value filtering. |
||
| 906 | * |
||
| 907 | * @param mixed $value |
||
| 908 | * |
||
| 909 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 910 | */ |
||
| 911 | 1 | public function setRawValue($value) |
|
| 916 | |||
| 917 | /** |
||
| 918 | * Returns unfiltered raw value of field. |
||
| 919 | * |
||
| 920 | * @return mixed |
||
| 921 | */ |
||
| 922 | public function getRawValue() |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Get config from the form. |
||
| 929 | * |
||
| 930 | * @return mixed |
||
| 931 | */ |
||
| 932 | 101 | private function getConfig($key = null, $default = null) |
|
| 936 | } |
||
| 937 |
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.