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 | 103 | 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 | 103 | 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() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Get the attribute value from the model by name. |
||
| 228 | * |
||
| 229 | * @param mixed $model |
||
| 230 | * @param string $name |
||
| 231 | * @return mixed |
||
| 232 | */ |
||
| 233 | 94 | protected function getModelValueAttribute($model, $name) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Transform array like syntax to dot syntax. |
||
| 247 | * |
||
| 248 | * @param string $key |
||
| 249 | * @return mixed |
||
| 250 | */ |
||
| 251 | 103 | protected function transformKey($key) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Prepare options for rendering. |
||
| 258 | * |
||
| 259 | * @param array $options |
||
| 260 | * @return array The parsed options |
||
| 261 | */ |
||
| 262 | 103 | protected function prepareOptions(array $options = []) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Normalize and merge rules. |
||
| 330 | * @param array $sourceOptions |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | 103 | protected function prepareRules(array &$sourceOptions = []) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Normalize the the given rule expression to an array. |
||
| 362 | * @param mixed $rules |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | 103 | protected function normalizeRules($rules) |
|
| 386 | |||
| 387 | |||
| 388 | /** |
||
| 389 | * Get name of the field. |
||
| 390 | * |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | 39 | public function getName() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Set name of the field. |
||
| 400 | * |
||
| 401 | * @param string $name |
||
| 402 | * @return $this |
||
| 403 | */ |
||
| 404 | 11 | public function setName($name) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Get dot notation key for fields. |
||
| 413 | * |
||
| 414 | * @return string |
||
| 415 | **/ |
||
| 416 | 55 | public function getNameKey() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Get field options. |
||
| 423 | * |
||
| 424 | * @return array |
||
| 425 | */ |
||
| 426 | 12 | public function getOptions() |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Get single option from options array. Can be used with dot notation ('attr.class'). |
||
| 433 | * |
||
| 434 | * @param string $option |
||
| 435 | * @param mixed|null $default |
||
| 436 | * @return mixed |
||
| 437 | */ |
||
| 438 | 103 | public function getOption($option, $default = null) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Set field options. |
||
| 445 | * |
||
| 446 | * @param array $options |
||
| 447 | * @return $this |
||
| 448 | */ |
||
| 449 | 11 | public function setOptions($options) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Set single option on the field. |
||
| 458 | * |
||
| 459 | * @param string $name |
||
| 460 | * @param mixed $value |
||
| 461 | * @return $this |
||
| 462 | */ |
||
| 463 | 103 | public function setOption($name, $value) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Get the type of the field. |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | 67 | public function getType() |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Set type of the field. |
||
| 482 | * |
||
| 483 | * @param mixed $type |
||
| 484 | * @return $this |
||
| 485 | */ |
||
| 486 | 1 | public function setType($type) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * @return Form |
||
| 497 | */ |
||
| 498 | 103 | public function getParent() |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Check if the field is rendered. |
||
| 505 | * |
||
| 506 | * @return bool |
||
| 507 | */ |
||
| 508 | 4 | public function isRendered() |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Default options for field. |
||
| 515 | * |
||
| 516 | * @return array |
||
| 517 | */ |
||
| 518 | 76 | protected function getDefaults() |
|
| 522 | |||
| 523 | /** |
||
| 524 | * Defaults used across all fields. |
||
| 525 | * |
||
| 526 | * @return array |
||
| 527 | */ |
||
| 528 | 103 | private function allDefaults() |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Get real name of the field without form namespace. |
||
| 550 | * |
||
| 551 | * @return string |
||
| 552 | */ |
||
| 553 | 102 | public function getRealName() |
|
| 557 | |||
| 558 | /** |
||
| 559 | * @param $value |
||
| 560 | * @return $this |
||
| 561 | */ |
||
| 562 | 95 | public function setValue($value) |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Set the template property on the object. |
||
| 585 | * |
||
| 586 | * @return void |
||
| 587 | */ |
||
| 588 | 103 | private function setTemplate() |
|
| 592 | |||
| 593 | /** |
||
| 594 | * Add error class to wrapper if validation errors exist. |
||
| 595 | * |
||
| 596 | * @return void |
||
| 597 | */ |
||
| 598 | 103 | protected function addErrorClass() |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Merge all defaults with field specific defaults and set template if passed. |
||
| 627 | * |
||
| 628 | * @param array $options |
||
| 629 | */ |
||
| 630 | 103 | protected function setDefaultOptions(array $options = []) |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Creates default wrapper classes for the form element. |
||
| 643 | * |
||
| 644 | * @param array $options |
||
| 645 | * @return array |
||
| 646 | */ |
||
| 647 | 103 | protected function setDefaultClasses(array $options = []) |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Setup the label for the form field. |
||
| 668 | * |
||
| 669 | * @return void |
||
| 670 | */ |
||
| 671 | 103 | protected function setupLabel() |
|
| 691 | |||
| 692 | /** |
||
| 693 | * Check if fields needs label. |
||
| 694 | * |
||
| 695 | * @return bool |
||
| 696 | */ |
||
| 697 | 35 | protected function needsLabel() |
|
| 708 | |||
| 709 | /** |
||
| 710 | * Disable field. |
||
| 711 | * |
||
| 712 | * @return $this |
||
| 713 | */ |
||
| 714 | 1 | public function disable() |
|
| 720 | |||
| 721 | /** |
||
| 722 | * Enable field. |
||
| 723 | * |
||
| 724 | * @return $this |
||
| 725 | */ |
||
| 726 | 1 | public function enable() |
|
| 732 | |||
| 733 | /** |
||
| 734 | * Get validation rules for a field if any with label for attributes. |
||
| 735 | * |
||
| 736 | * @return array|null |
||
| 737 | */ |
||
| 738 | 10 | public function getValidationRules() |
|
| 764 | |||
| 765 | /** |
||
| 766 | * Get this field's attributes, probably just one. |
||
| 767 | * |
||
| 768 | * @return array |
||
| 769 | */ |
||
| 770 | 3 | public function getAllAttributes() |
|
| 774 | |||
| 775 | /** |
||
| 776 | * Get value property. |
||
| 777 | * |
||
| 778 | * @param mixed|null $default |
||
| 779 | * @return mixed |
||
| 780 | */ |
||
| 781 | 38 | public function getValue($default = null) |
|
| 785 | |||
| 786 | /** |
||
| 787 | * Get default value property. |
||
| 788 | * |
||
| 789 | * @param mixed|null $default |
||
| 790 | * @return mixed |
||
| 791 | */ |
||
| 792 | 35 | public function getDefaultValue($default = null) |
|
| 796 | |||
| 797 | /** |
||
| 798 | * Check if provided value is valid for this type. |
||
| 799 | * |
||
| 800 | * @return bool |
||
| 801 | */ |
||
| 802 | 95 | protected function isValidValue($value) |
|
| 806 | |||
| 807 | /** |
||
| 808 | * Method initFilters used to initialize filters |
||
| 809 | * from field options and bind it to the same. |
||
| 810 | * |
||
| 811 | * @return $this |
||
| 812 | */ |
||
| 813 | 98 | protected function initFilters() |
|
| 832 | |||
| 833 | /** |
||
| 834 | * Method setFilters used to set filters to current filters property. |
||
| 835 | * |
||
| 836 | * @param array $filters |
||
| 837 | * |
||
| 838 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 839 | */ |
||
| 840 | public function setFilters(array $filters) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Method getFilters returns array of binded filters |
||
| 852 | * if there are any binded. Otherwise empty array. |
||
| 853 | * |
||
| 854 | * @return array |
||
| 855 | */ |
||
| 856 | 23 | public function getFilters() |
|
| 860 | |||
| 861 | /** |
||
| 862 | * @param string|FilterInterface $filter |
||
| 863 | * |
||
| 864 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 865 | * |
||
| 866 | * @throws FilterAlreadyBindedException |
||
| 867 | */ |
||
| 868 | 8 | public function addFilter($filter) |
|
| 896 | |||
| 897 | /** |
||
| 898 | * Method removeFilter used to remove filter by provided alias/name. |
||
| 899 | * |
||
| 900 | * @param string $name |
||
| 901 | * |
||
| 902 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 903 | */ |
||
| 904 | 1 | public function removeFilter($name) |
|
| 914 | |||
| 915 | /** |
||
| 916 | * Method removeFilters used to remove filters by provided aliases/names. |
||
| 917 | * |
||
| 918 | * @param array $filterNames |
||
| 919 | * |
||
| 920 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 921 | */ |
||
| 922 | 1 | public function removeFilters(array $filterNames) |
|
| 934 | |||
| 935 | /** |
||
| 936 | * Method clearFilters used to empty current filters property. |
||
| 937 | * |
||
| 938 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 939 | */ |
||
| 940 | 1 | public function clearFilters() |
|
| 945 | |||
| 946 | /** |
||
| 947 | * Method used to set FiltersOverride status to provided value. |
||
| 948 | * |
||
| 949 | * @param $status |
||
| 950 | * |
||
| 951 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 952 | */ |
||
| 953 | 2 | public function setFiltersOverride($status) |
|
| 958 | |||
| 959 | /** |
||
| 960 | * @return bool |
||
| 961 | */ |
||
| 962 | 9 | public function getFiltersOverride() |
|
| 966 | |||
| 967 | /** |
||
| 968 | * Method used to set Unfiltered/Unmutated field value. |
||
| 969 | * Method is called before field value mutating starts - request value filtering. |
||
| 970 | * |
||
| 971 | * @param mixed $value |
||
| 972 | * |
||
| 973 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 974 | */ |
||
| 975 | 1 | public function setRawValue($value) |
|
| 980 | |||
| 981 | /** |
||
| 982 | * Returns unfiltered raw value of field. |
||
| 983 | * |
||
| 984 | * @return mixed |
||
| 985 | */ |
||
| 986 | public function getRawValue() |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Get config from the form. |
||
| 993 | * |
||
| 994 | * @return mixed |
||
| 995 | */ |
||
| 996 | 103 | private function getConfig($key = null, $default = null) |
|
| 1000 | } |
||
| 1001 |
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.