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 = []) |
|
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * Setup the value of the form field. |
||
| 129 | * |
||
| 130 | * @return void |
||
| 131 | */ |
||
| 132 | 101 | protected function setupValue() |
|
| 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) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Return the extra render data for this form field, passed into the field's template directly. |
||
| 214 | * |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | 35 | protected function getRenderData() { |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Get the attribute value from the model by name. |
||
| 223 | * |
||
| 224 | * @param mixed $model |
||
| 225 | * @param string $name |
||
| 226 | * @return mixed |
||
| 227 | */ |
||
| 228 | 92 | protected function getModelValueAttribute($model, $name) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Transform array like syntax to dot syntax. |
||
| 242 | * |
||
| 243 | * @param string $key |
||
| 244 | * @return mixed |
||
| 245 | */ |
||
| 246 | 101 | protected function transformKey($key) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Prepare options for rendering. |
||
| 253 | * |
||
| 254 | * @param array $options |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | 101 | protected function prepareOptions(array $options = []) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Get name of the field. |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | 39 | public function getName() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Set name of the field. |
||
| 332 | * |
||
| 333 | * @param string $name |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | 12 | public function setName($name) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Get dot notation key for fields. |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | **/ |
||
| 348 | 54 | public function getNameKey() |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Get field options. |
||
| 355 | * |
||
| 356 | * @return array |
||
| 357 | */ |
||
| 358 | 12 | public function getOptions() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Get single option from options array. Can be used with dot notation ('attr.class'). |
||
| 365 | * |
||
| 366 | * @param string $option |
||
| 367 | * @param mixed|null $default |
||
| 368 | * @return mixed |
||
| 369 | */ |
||
| 370 | 101 | public function getOption($option, $default = null) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Set field options. |
||
| 377 | * |
||
| 378 | * @param array $options |
||
| 379 | * @return $this |
||
| 380 | */ |
||
| 381 | 12 | public function setOptions($options) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Set single option on the field. |
||
| 390 | * |
||
| 391 | * @param string $name |
||
| 392 | * @param mixed $value |
||
| 393 | * @return $this |
||
| 394 | */ |
||
| 395 | 101 | public function setOption($name, $value) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Get the type of the field. |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | 65 | public function getType() |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Set type of the field. |
||
| 414 | * |
||
| 415 | * @param mixed $type |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | 1 | public function setType($type) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @return Form |
||
| 429 | */ |
||
| 430 | 101 | public function getParent() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Check if the field is rendered. |
||
| 437 | * |
||
| 438 | * @return bool |
||
| 439 | */ |
||
| 440 | 4 | public function isRendered() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Default options for field. |
||
| 447 | * |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | 76 | protected function getDefaults() |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Defaults used across all fields. |
||
| 457 | * |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | 101 | private function allDefaults() |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Get real name of the field without form namespace. |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | 100 | public function getRealName() |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @param $value |
||
| 492 | * @return $this |
||
| 493 | */ |
||
| 494 | 94 | public function setValue($value) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Set the template property on the object. |
||
| 517 | * |
||
| 518 | * @return void |
||
| 519 | */ |
||
| 520 | 101 | private function setTemplate() |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Add error class to wrapper if validation errors exist. |
||
| 527 | * |
||
| 528 | * @return void |
||
| 529 | */ |
||
| 530 | 101 | protected function addErrorClass() |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Merge all defaults with field specific defaults and set template if passed. |
||
| 555 | * |
||
| 556 | * @param array $options |
||
| 557 | */ |
||
| 558 | 101 | protected function setDefaultOptions(array $options = []) |
|
| 568 | |||
| 569 | /** |
||
| 570 | * Creates default wrapper classes for the form element. |
||
| 571 | * |
||
| 572 | * @param array $options |
||
| 573 | * @return array |
||
| 574 | */ |
||
| 575 | 101 | protected function setDefaultClasses(array $options = []) |
|
| 593 | |||
| 594 | /** |
||
| 595 | * Setup the label for the form field. |
||
| 596 | * |
||
| 597 | * @return void |
||
| 598 | */ |
||
| 599 | 101 | protected function setupLabel() |
|
| 619 | |||
| 620 | /** |
||
| 621 | * Check if fields needs label. |
||
| 622 | * |
||
| 623 | * @return bool |
||
| 624 | */ |
||
| 625 | 35 | protected function needsLabel() |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Disable field. |
||
| 639 | * |
||
| 640 | * @return $this |
||
| 641 | */ |
||
| 642 | 1 | public function disable() |
|
| 648 | |||
| 649 | /** |
||
| 650 | * Enable field. |
||
| 651 | * |
||
| 652 | * @return $this |
||
| 653 | */ |
||
| 654 | 1 | public function enable() |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Get validation rules for a field if any with label for attributes. |
||
| 663 | * |
||
| 664 | * @return array|null |
||
| 665 | */ |
||
| 666 | 9 | public function getValidationRules() |
|
| 702 | |||
| 703 | /** |
||
| 704 | * Get this field's attributes, probably just one. |
||
| 705 | * |
||
| 706 | * @return array |
||
| 707 | */ |
||
| 708 | 3 | public function getAllAttributes() |
|
| 712 | |||
| 713 | /** |
||
| 714 | * Get value property. |
||
| 715 | * |
||
| 716 | * @param mixed|null $default |
||
| 717 | * @return mixed |
||
| 718 | */ |
||
| 719 | 38 | public function getValue($default = null) |
|
| 723 | |||
| 724 | /** |
||
| 725 | * Get default value property. |
||
| 726 | * |
||
| 727 | * @param mixed|null $default |
||
| 728 | * @return mixed |
||
| 729 | */ |
||
| 730 | 35 | public function getDefaultValue($default = null) |
|
| 734 | |||
| 735 | /** |
||
| 736 | * Check if provided value is valid for this type. |
||
| 737 | * |
||
| 738 | * @return bool |
||
| 739 | */ |
||
| 740 | 94 | protected function isValidValue($value) |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Method initFilters used to initialize filters |
||
| 747 | * from field options and bind it to the same. |
||
| 748 | * |
||
| 749 | * @return $this |
||
| 750 | */ |
||
| 751 | 96 | protected function initFilters() |
|
| 770 | |||
| 771 | /** |
||
| 772 | * Method setFilters used to set filters to current filters property. |
||
| 773 | * |
||
| 774 | * @param array $filters |
||
| 775 | * |
||
| 776 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 777 | */ |
||
| 778 | public function setFilters(array $filters) |
||
| 787 | |||
| 788 | /** |
||
| 789 | * Method getFilters returns array of binded filters |
||
| 790 | * if there are any binded. Otherwise empty array. |
||
| 791 | * |
||
| 792 | * @return array |
||
| 793 | */ |
||
| 794 | 23 | public function getFilters() |
|
| 798 | |||
| 799 | /** |
||
| 800 | * @param string|FilterInterface $filter |
||
| 801 | * |
||
| 802 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 803 | * |
||
| 804 | * @throws FilterAlreadyBindedException |
||
| 805 | */ |
||
| 806 | 8 | public function addFilter($filter) |
|
| 834 | |||
| 835 | /** |
||
| 836 | * Method removeFilter used to remove filter by provided alias/name. |
||
| 837 | * |
||
| 838 | * @param string $name |
||
| 839 | * |
||
| 840 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 841 | */ |
||
| 842 | 1 | public function removeFilter($name) |
|
| 852 | |||
| 853 | /** |
||
| 854 | * Method removeFilters used to remove filters by provided aliases/names. |
||
| 855 | * |
||
| 856 | * @param array $filterNames |
||
| 857 | * |
||
| 858 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 859 | */ |
||
| 860 | 1 | public function removeFilters(array $filterNames) |
|
| 872 | |||
| 873 | /** |
||
| 874 | * Method clearFilters used to empty current filters property. |
||
| 875 | * |
||
| 876 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 877 | */ |
||
| 878 | 1 | public function clearFilters() |
|
| 883 | |||
| 884 | /** |
||
| 885 | * Method used to set FiltersOverride status to provided value. |
||
| 886 | * |
||
| 887 | * @param $status |
||
| 888 | * |
||
| 889 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 890 | */ |
||
| 891 | 2 | public function setFiltersOverride($status) |
|
| 896 | |||
| 897 | /** |
||
| 898 | * @return bool |
||
| 899 | */ |
||
| 900 | 9 | public function getFiltersOverride() |
|
| 904 | |||
| 905 | /** |
||
| 906 | * Method used to set Unfiltered/Unmutated field value. |
||
| 907 | * Method is called before field value mutating starts - request value filtering. |
||
| 908 | * |
||
| 909 | * @param mixed $value |
||
| 910 | * |
||
| 911 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 912 | */ |
||
| 913 | 1 | public function setRawValue($value) |
|
| 918 | |||
| 919 | /** |
||
| 920 | * Returns unfiltered raw value of field. |
||
| 921 | * |
||
| 922 | * @return mixed |
||
| 923 | */ |
||
| 924 | public function getRawValue() |
||
| 928 | } |
||
| 929 |
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.