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 | 101 | public function __construct($name, $type, Form $parent, array $options = []) |
|
| 115 | { |
||
| 116 | 101 | $this->name = $name; |
|
| 117 | 101 | $this->type = $type; |
|
| 118 | 101 | $this->parent = $parent; |
|
| 119 | 101 | $this->formHelper = $this->parent->getFormHelper(); |
|
| 120 | 101 | $this->setTemplate(); |
|
| 121 | 101 | $this->setDefaultOptions($options); |
|
| 122 | 101 | $this->setupValue(); |
|
| 123 | 96 | $this->initFilters(); |
|
| 124 | 96 | } |
|
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Setup the value of the form field. |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | 101 | protected function setupValue() |
|
| 133 | { |
||
| 134 | 101 | $value = $this->getOption($this->valueProperty); |
|
| 135 | 101 | $isChild = $this->getOption('is_child'); |
|
| 136 | |||
| 137 | 101 | if ($value instanceof \Closure) { |
|
| 138 | $this->valueClosure = $value; |
||
| 139 | } |
||
| 140 | |||
| 141 | 101 | if (($value === null || $value instanceof \Closure) && !$isChild) { |
|
| 142 | 90 | $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name)); |
|
| 143 | 23 | } elseif (!$isChild) { |
|
| 144 | 14 | $this->hasDefault = true; |
|
| 145 | } |
||
| 146 | 96 | } |
|
| 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 | 35 | 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 | 35 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Return the extra render data for this form field, passed into the field's template directly. |
||
| 215 | * |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | 35 | protected function getRenderData() { |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Get the attribute value from the model by name. |
||
| 224 | * |
||
| 225 | * @param mixed $model |
||
| 226 | * @param string $name |
||
| 227 | * @return mixed |
||
| 228 | */ |
||
| 229 | 92 | protected function getModelValueAttribute($model, $name) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Transform array like syntax to dot syntax. |
||
| 243 | * |
||
| 244 | * @param string $key |
||
| 245 | * @return mixed |
||
| 246 | */ |
||
| 247 | 101 | protected function transformKey($key) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Prepare options for rendering. |
||
| 254 | * |
||
| 255 | * @param array $options |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | 101 | protected function prepareOptions(array $options = []) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Get name of the field. |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | 39 | public function getName() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Set name of the field. |
||
| 333 | * |
||
| 334 | * @param string $name |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | 12 | public function setName($name) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Get dot notation key for fields. |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | **/ |
||
| 349 | 54 | public function getNameKey() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Get field options. |
||
| 356 | * |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | 12 | public function getOptions() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Get single option from options array. Can be used with dot notation ('attr.class'). |
||
| 366 | * |
||
| 367 | * @param string $option |
||
| 368 | * @param mixed|null $default |
||
| 369 | * @return mixed |
||
| 370 | */ |
||
| 371 | 101 | public function getOption($option, $default = null) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Set field options. |
||
| 378 | * |
||
| 379 | * @param array $options |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | 12 | public function setOptions($options) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Set single option on the field. |
||
| 391 | * |
||
| 392 | * @param string $name |
||
| 393 | * @param mixed $value |
||
| 394 | * @return $this |
||
| 395 | */ |
||
| 396 | 101 | public function setOption($name, $value) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Get the type of the field. |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | 65 | public function getType() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Set type of the field. |
||
| 415 | * |
||
| 416 | * @param mixed $type |
||
| 417 | * @return $this |
||
| 418 | */ |
||
| 419 | 1 | public function setType($type) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * @return Form |
||
| 430 | */ |
||
| 431 | 101 | public function getParent() |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Check if the field is rendered. |
||
| 438 | * |
||
| 439 | * @return bool |
||
| 440 | */ |
||
| 441 | 4 | public function isRendered() |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Default options for field. |
||
| 448 | * |
||
| 449 | * @return array |
||
| 450 | */ |
||
| 451 | 76 | protected function getDefaults() |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Defaults used across all fields. |
||
| 458 | * |
||
| 459 | * @return array |
||
| 460 | */ |
||
| 461 | 101 | private function allDefaults() |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Get real name of the field without form namespace. |
||
| 483 | * |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | 100 | public function getRealName() |
|
| 490 | |||
| 491 | /** |
||
| 492 | * @param $value |
||
| 493 | * @return $this |
||
| 494 | */ |
||
| 495 | 94 | public function setValue($value) |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Set the template property on the object. |
||
| 518 | * |
||
| 519 | * @return void |
||
| 520 | */ |
||
| 521 | 101 | private function setTemplate() |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Add error class to wrapper if validation errors exist. |
||
| 528 | * |
||
| 529 | * @return void |
||
| 530 | */ |
||
| 531 | 101 | protected function addErrorClass() |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Merge all defaults with field specific defaults and set template if passed. |
||
| 557 | * |
||
| 558 | * @param array $options |
||
| 559 | */ |
||
| 560 | 101 | protected function setDefaultOptions(array $options = []) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Creates default wrapper classes for the form element. |
||
| 573 | * |
||
| 574 | * @param array $options |
||
| 575 | * @return array |
||
| 576 | */ |
||
| 577 | 101 | protected function setDefaultClasses(array $options = []) |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Setup the label for the form field. |
||
| 598 | * |
||
| 599 | * @return void |
||
| 600 | */ |
||
| 601 | 101 | protected function setupLabel() |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Check if fields needs label. |
||
| 624 | * |
||
| 625 | * @return bool |
||
| 626 | */ |
||
| 627 | 35 | protected function needsLabel() |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Disable field. |
||
| 641 | * |
||
| 642 | * @return $this |
||
| 643 | */ |
||
| 644 | 1 | public function disable() |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Enable field. |
||
| 653 | * |
||
| 654 | * @return $this |
||
| 655 | */ |
||
| 656 | 1 | public function enable() |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Get validation rules for a field if any with label for attributes. |
||
| 665 | * |
||
| 666 | * @return array|null |
||
| 667 | */ |
||
| 668 | 9 | public function getValidationRules() |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Get this field's attributes, probably just one. |
||
| 707 | * |
||
| 708 | * @return array |
||
| 709 | */ |
||
| 710 | 3 | public function getAllAttributes() |
|
| 714 | |||
| 715 | /** |
||
| 716 | * Get value property. |
||
| 717 | * |
||
| 718 | * @param mixed|null $default |
||
| 719 | * @return mixed |
||
| 720 | */ |
||
| 721 | 38 | public function getValue($default = null) |
|
| 725 | |||
| 726 | /** |
||
| 727 | * Get default value property. |
||
| 728 | * |
||
| 729 | * @param mixed|null $default |
||
| 730 | * @return mixed |
||
| 731 | */ |
||
| 732 | 35 | public function getDefaultValue($default = null) |
|
| 736 | |||
| 737 | /** |
||
| 738 | * Check if provided value is valid for this type. |
||
| 739 | * |
||
| 740 | * @return bool |
||
| 741 | */ |
||
| 742 | 94 | protected function isValidValue($value) |
|
| 746 | |||
| 747 | /** |
||
| 748 | * Method initFilters used to initialize filters |
||
| 749 | * from field options and bind it to the same. |
||
| 750 | * |
||
| 751 | * @return $this |
||
| 752 | */ |
||
| 753 | 96 | protected function initFilters() |
|
| 772 | |||
| 773 | /** |
||
| 774 | * Method setFilters used to set filters to current filters property. |
||
| 775 | * |
||
| 776 | * @param array $filters |
||
| 777 | * |
||
| 778 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 779 | */ |
||
| 780 | public function setFilters(array $filters) |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Method getFilters returns array of binded filters |
||
| 792 | * if there are any binded. Otherwise empty array. |
||
| 793 | * |
||
| 794 | * @return array |
||
| 795 | */ |
||
| 796 | 23 | public function getFilters() |
|
| 800 | |||
| 801 | /** |
||
| 802 | * @param string|FilterInterface $filter |
||
| 803 | * |
||
| 804 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 805 | * |
||
| 806 | * @throws FilterAlreadyBindedException |
||
| 807 | */ |
||
| 808 | 8 | public function addFilter($filter) |
|
| 836 | |||
| 837 | /** |
||
| 838 | * Method removeFilter used to remove filter by provided alias/name. |
||
| 839 | * |
||
| 840 | * @param string $name |
||
| 841 | * |
||
| 842 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 843 | */ |
||
| 844 | 1 | public function removeFilter($name) |
|
| 854 | |||
| 855 | /** |
||
| 856 | * Method removeFilters used to remove filters by provided aliases/names. |
||
| 857 | * |
||
| 858 | * @param array $filterNames |
||
| 859 | * |
||
| 860 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 861 | */ |
||
| 862 | 1 | public function removeFilters(array $filterNames) |
|
| 874 | |||
| 875 | /** |
||
| 876 | * Method clearFilters used to empty current filters property. |
||
| 877 | * |
||
| 878 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 879 | */ |
||
| 880 | 1 | public function clearFilters() |
|
| 885 | |||
| 886 | /** |
||
| 887 | * Method used to set FiltersOverride status to provided value. |
||
| 888 | * |
||
| 889 | * @param $status |
||
| 890 | * |
||
| 891 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 892 | */ |
||
| 893 | 2 | public function setFiltersOverride($status) |
|
| 898 | |||
| 899 | /** |
||
| 900 | * @return bool |
||
| 901 | */ |
||
| 902 | 9 | public function getFiltersOverride() |
|
| 906 | |||
| 907 | /** |
||
| 908 | * Method used to set Unfiltered/Unmutated field value. |
||
| 909 | * Method is called before field value mutating starts - request value filtering. |
||
| 910 | * |
||
| 911 | * @param mixed $value |
||
| 912 | * |
||
| 913 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 914 | */ |
||
| 915 | 1 | public function setRawValue($value) |
|
| 920 | |||
| 921 | /** |
||
| 922 | * Returns unfiltered raw value of field. |
||
| 923 | * |
||
| 924 | * @return mixed |
||
| 925 | */ |
||
| 926 | public function getRawValue() |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Get config from the form. |
||
| 933 | * |
||
| 934 | * @return mixed |
||
| 935 | */ |
||
| 936 | 101 | private function getConfig($key = null, $default = null) |
|
| 940 | } |
||
| 941 |
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.