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 |
||
| 17 | abstract class FormField |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Name of the field. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $name; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Type of the field. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $type; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * All options for the field. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $options = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Is field rendered. |
||
| 42 | * |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $rendered = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var Form |
||
| 49 | */ |
||
| 50 | protected $parent; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $template; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var FormHelper |
||
| 59 | */ |
||
| 60 | protected $formHelper; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Name of the property for value setting. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $valueProperty = 'value'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Name of the property for default value. |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $defaultValueProperty = 'default_value'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Is default value set? |
||
| 78 | * |
||
| 79 | * @var bool|false |
||
| 80 | */ |
||
| 81 | protected $hasDefault = false; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var \Closure|null |
||
| 85 | */ |
||
| 86 | protected $valueClosure = null; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Array of filters key(alias/name) => objects. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $filters = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Raw/unfiltered field value. |
||
| 97 | * |
||
| 98 | * @var mixed $rawValues |
||
| 99 | */ |
||
| 100 | protected $rawValue; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Override filters with same alias/name for field. |
||
| 104 | * |
||
| 105 | * @var bool |
||
| 106 | */ |
||
| 107 | protected $filtersOverride = false; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $name |
||
| 111 | * @param string $type |
||
| 112 | * @param Form $parent |
||
| 113 | * @param array $options |
||
| 114 | */ |
||
| 115 | 101 | public function __construct($name, $type, Form $parent, array $options = []) |
|
| 126 | |||
| 127 | |||
| 128 | /** |
||
| 129 | * Setup the value of the form field. |
||
| 130 | * |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | 101 | protected function setupValue() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Get the template, can be config variable or view path. |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | abstract protected function getTemplate(); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | 35 | protected function getViewTemplate() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Render the field. |
||
| 166 | * |
||
| 167 | * @param array $options |
||
| 168 | * @param bool $showLabel |
||
| 169 | * @param bool $showField |
||
| 170 | * @param bool $showError |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | 35 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Return the extra render data for this form field, passed into the field's template directly. |
||
| 216 | * |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | 35 | protected function getRenderData() { |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Get the attribute value from the model by name. |
||
| 225 | * |
||
| 226 | * @param mixed $model |
||
| 227 | * @param string $name |
||
| 228 | * @return mixed |
||
| 229 | */ |
||
| 230 | 92 | protected function getModelValueAttribute($model, $name) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Transform array like syntax to dot syntax. |
||
| 244 | * |
||
| 245 | * @param string $key |
||
| 246 | * @return mixed |
||
| 247 | */ |
||
| 248 | 101 | protected function transformKey($key) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Prepare options for rendering. |
||
| 255 | * |
||
| 256 | * @param array $options |
||
| 257 | * @return array |
||
| 258 | */ |
||
| 259 | 101 | protected function prepareOptions(array $options = []) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get name of the field. |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | 39 | public function getName() |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Set name of the field. |
||
| 334 | * |
||
| 335 | * @param string $name |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | 12 | public function setName($name) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Get dot notation key for fields. |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | **/ |
||
| 350 | 54 | public function getNameKey() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Get field options. |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | 12 | public function getOptions() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Get single option from options array. Can be used with dot notation ('attr.class'). |
||
| 367 | * |
||
| 368 | * @param string $option |
||
| 369 | * @param mixed|null $default |
||
| 370 | * @return mixed |
||
| 371 | */ |
||
| 372 | 101 | public function getOption($option, $default = null) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Set field options. |
||
| 379 | * |
||
| 380 | * @param array $options |
||
| 381 | * @return $this |
||
| 382 | */ |
||
| 383 | 12 | public function setOptions($options) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Set single option on the field. |
||
| 392 | * |
||
| 393 | * @param string $name |
||
| 394 | * @param mixed $value |
||
| 395 | * @return $this |
||
| 396 | */ |
||
| 397 | 101 | public function setOption($name, $value) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Get the type of the field. |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | 65 | public function getType() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Set type of the field. |
||
| 416 | * |
||
| 417 | * @param mixed $type |
||
| 418 | * @return $this |
||
| 419 | */ |
||
| 420 | 1 | public function setType($type) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * @return Form |
||
| 431 | */ |
||
| 432 | 101 | public function getParent() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Check if the field is rendered. |
||
| 439 | * |
||
| 440 | * @return bool |
||
| 441 | */ |
||
| 442 | 4 | public function isRendered() |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Default options for field. |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | 76 | protected function getDefaults() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Defaults used across all fields. |
||
| 459 | * |
||
| 460 | * @return array |
||
| 461 | */ |
||
| 462 | 101 | private function allDefaults() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Get real name of the field without form namespace. |
||
| 484 | * |
||
| 485 | * @return string |
||
| 486 | */ |
||
| 487 | 100 | public function getRealName() |
|
| 491 | |||
| 492 | /** |
||
| 493 | * @param $value |
||
| 494 | * @return $this |
||
| 495 | */ |
||
| 496 | 94 | public function setValue($value) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Set the template property on the object. |
||
| 519 | * |
||
| 520 | * @return void |
||
| 521 | */ |
||
| 522 | 101 | private function setTemplate() |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Add error class to wrapper if validation errors exist. |
||
| 529 | * |
||
| 530 | * @return void |
||
| 531 | */ |
||
| 532 | 101 | protected function addErrorClass() |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Merge all defaults with field specific defaults and set template if passed. |
||
| 558 | * |
||
| 559 | * @param array $options |
||
| 560 | */ |
||
| 561 | 101 | protected function setDefaultOptions(array $options = []) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * Creates default wrapper classes for the form element. |
||
| 574 | * |
||
| 575 | * @param array $options |
||
| 576 | * @return array |
||
| 577 | */ |
||
| 578 | 101 | protected function setDefaultClasses(array $options = []) |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Setup the label for the form field. |
||
| 599 | * |
||
| 600 | * @return void |
||
| 601 | */ |
||
| 602 | 101 | protected function setupLabel() |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Check if fields needs label. |
||
| 625 | * |
||
| 626 | * @return bool |
||
| 627 | */ |
||
| 628 | 35 | protected function needsLabel() |
|
| 639 | |||
| 640 | /** |
||
| 641 | * Disable field. |
||
| 642 | * |
||
| 643 | * @return $this |
||
| 644 | */ |
||
| 645 | 1 | public function disable() |
|
| 651 | |||
| 652 | /** |
||
| 653 | * Enable field. |
||
| 654 | * |
||
| 655 | * @return $this |
||
| 656 | */ |
||
| 657 | 1 | public function enable() |
|
| 663 | |||
| 664 | /** |
||
| 665 | * Get validation rules for a field if any with label for attributes. |
||
| 666 | * |
||
| 667 | * @return array|null |
||
| 668 | */ |
||
| 669 | 9 | public function getValidationRules() |
|
| 705 | |||
| 706 | /** |
||
| 707 | * Get this field's attributes, probably just one. |
||
| 708 | * |
||
| 709 | * @return array |
||
| 710 | */ |
||
| 711 | 3 | public function getAllAttributes() |
|
| 715 | |||
| 716 | /** |
||
| 717 | * Get value property. |
||
| 718 | * |
||
| 719 | * @param mixed|null $default |
||
| 720 | * @return mixed |
||
| 721 | */ |
||
| 722 | 38 | public function getValue($default = null) |
|
| 726 | |||
| 727 | /** |
||
| 728 | * Get default value property. |
||
| 729 | * |
||
| 730 | * @param mixed|null $default |
||
| 731 | * @return mixed |
||
| 732 | */ |
||
| 733 | 35 | public function getDefaultValue($default = null) |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Check if provided value is valid for this type. |
||
| 740 | * |
||
| 741 | * @return bool |
||
| 742 | */ |
||
| 743 | 94 | protected function isValidValue($value) |
|
| 747 | |||
| 748 | /** |
||
| 749 | * Method initFilters used to initialize filters |
||
| 750 | * from field options and bind it to the same. |
||
| 751 | * |
||
| 752 | * @return $this |
||
| 753 | */ |
||
| 754 | 96 | protected function initFilters() |
|
| 773 | |||
| 774 | /** |
||
| 775 | * Method setFilters used to set filters to current filters property. |
||
| 776 | * |
||
| 777 | * @param array $filters |
||
| 778 | * |
||
| 779 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 780 | */ |
||
| 781 | public function setFilters(array $filters) |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Method getFilters returns array of binded filters |
||
| 793 | * if there are any binded. Otherwise empty array. |
||
| 794 | * |
||
| 795 | * @return array |
||
| 796 | */ |
||
| 797 | 23 | public function getFilters() |
|
| 801 | |||
| 802 | /** |
||
| 803 | * @param string|FilterInterface $filter |
||
| 804 | * |
||
| 805 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 806 | * |
||
| 807 | * @throws FilterAlreadyBindedException |
||
| 808 | */ |
||
| 809 | 8 | public function addFilter($filter) |
|
| 837 | |||
| 838 | /** |
||
| 839 | * Method removeFilter used to remove filter by provided alias/name. |
||
| 840 | * |
||
| 841 | * @param string $name |
||
| 842 | * |
||
| 843 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 844 | */ |
||
| 845 | 1 | public function removeFilter($name) |
|
| 855 | |||
| 856 | /** |
||
| 857 | * Method removeFilters used to remove filters by provided aliases/names. |
||
| 858 | * |
||
| 859 | * @param array $filterNames |
||
| 860 | * |
||
| 861 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 862 | */ |
||
| 863 | 1 | public function removeFilters(array $filterNames) |
|
| 875 | |||
| 876 | /** |
||
| 877 | * Method clearFilters used to empty current filters property. |
||
| 878 | * |
||
| 879 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 880 | */ |
||
| 881 | 1 | public function clearFilters() |
|
| 886 | |||
| 887 | /** |
||
| 888 | * Method used to set FiltersOverride status to provided value. |
||
| 889 | * |
||
| 890 | * @param $status |
||
| 891 | * |
||
| 892 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 893 | */ |
||
| 894 | 2 | public function setFiltersOverride($status) |
|
| 899 | |||
| 900 | /** |
||
| 901 | * @return bool |
||
| 902 | */ |
||
| 903 | 9 | public function getFiltersOverride() |
|
| 907 | |||
| 908 | /** |
||
| 909 | * Method used to set Unfiltered/Unmutated field value. |
||
| 910 | * Method is called before field value mutating starts - request value filtering. |
||
| 911 | * |
||
| 912 | * @param mixed $value |
||
| 913 | * |
||
| 914 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 915 | */ |
||
| 916 | 1 | public function setRawValue($value) |
|
| 921 | |||
| 922 | /** |
||
| 923 | * Returns unfiltered raw value of field. |
||
| 924 | * |
||
| 925 | * @return mixed |
||
| 926 | */ |
||
| 927 | public function getRawValue() |
||
| 931 | |||
| 932 | /** |
||
| 933 | * Get config from the form. |
||
| 934 | * |
||
| 935 | * @return mixed |
||
| 936 | */ |
||
| 937 | 101 | private function getConfig($key = null, $default = null) |
|
| 941 | } |
||
| 942 |
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.