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 | 105 | 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 | 105 | protected function setupValue() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Get the template, can be config variable or view path. |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | abstract protected function getTemplate(); |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | 35 | protected function getViewTemplate() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Render the field. |
||
| 174 | * |
||
| 175 | * @param array $options |
||
| 176 | * @param bool $showLabel |
||
| 177 | * @param bool $showField |
||
| 178 | * @param bool $showError |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | 35 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Return the extra render data for this form field, passed into the field's template directly. |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | 35 | protected function getRenderData() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Get the attribute value from the model by name. |
||
| 234 | * |
||
| 235 | * @param mixed $model |
||
| 236 | * @param string $name |
||
| 237 | * @return mixed |
||
| 238 | */ |
||
| 239 | 96 | protected function getModelValueAttribute($model, $name) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Transform array like syntax to dot syntax. |
||
| 253 | * |
||
| 254 | * @param string $key |
||
| 255 | * @return mixed |
||
| 256 | */ |
||
| 257 | 105 | protected function transformKey($key) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Prepare options for rendering. |
||
| 264 | * |
||
| 265 | * @param array $options |
||
| 266 | * @return array The parsed options |
||
| 267 | */ |
||
| 268 | 105 | protected function prepareOptions(array $options = []) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Normalize and merge rules. |
||
| 336 | * @param array $sourceOptions |
||
| 337 | * @return array |
||
| 338 | */ |
||
| 339 | 105 | protected function prepareRules(array &$sourceOptions = []) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Normalize the the given rule expression to an array. |
||
| 368 | * @param mixed $rules |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | 105 | protected function normalizeRules($rules) |
|
| 392 | |||
| 393 | |||
| 394 | /** |
||
| 395 | * Get name of the field. |
||
| 396 | * |
||
| 397 | * @return string |
||
| 398 | */ |
||
| 399 | 39 | public function getName() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Set name of the field. |
||
| 406 | * |
||
| 407 | * @param string $name |
||
| 408 | * @return $this |
||
| 409 | */ |
||
| 410 | 11 | public function setName($name) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Get dot notation key for fields. |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | **/ |
||
| 422 | 55 | public function getNameKey() |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Get field options. |
||
| 429 | * |
||
| 430 | * @return array |
||
| 431 | */ |
||
| 432 | 12 | public function getOptions() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Get single option from options array. Can be used with dot notation ('attr.class'). |
||
| 439 | * |
||
| 440 | * @param string $option |
||
| 441 | * @param mixed|null $default |
||
| 442 | * @return mixed |
||
| 443 | */ |
||
| 444 | 105 | public function getOption($option, $default = null) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Set field options. |
||
| 451 | * |
||
| 452 | * @param array $options |
||
| 453 | * @return $this |
||
| 454 | */ |
||
| 455 | 11 | public function setOptions($options) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Set single option on the field. |
||
| 464 | * |
||
| 465 | * @param string $name |
||
| 466 | * @param mixed $value |
||
| 467 | * @return $this |
||
| 468 | */ |
||
| 469 | 105 | public function setOption($name, $value) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Get the type of the field. |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | 69 | public function getType() |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Set type of the field. |
||
| 488 | * |
||
| 489 | * @param mixed $type |
||
| 490 | * @return $this |
||
| 491 | */ |
||
| 492 | 1 | public function setType($type) |
|
| 500 | |||
| 501 | /** |
||
| 502 | * @return Form |
||
| 503 | */ |
||
| 504 | 105 | public function getParent() |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Check if the field is rendered. |
||
| 511 | * |
||
| 512 | * @return bool |
||
| 513 | */ |
||
| 514 | 4 | public function isRendered() |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Default options for field. |
||
| 521 | * |
||
| 522 | * @return array |
||
| 523 | */ |
||
| 524 | 76 | protected function getDefaults() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Defaults used across all fields. |
||
| 531 | * |
||
| 532 | * @return array |
||
| 533 | */ |
||
| 534 | 105 | private function allDefaults() |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Get real name of the field without form namespace. |
||
| 556 | * |
||
| 557 | * @return string |
||
| 558 | */ |
||
| 559 | 104 | public function getRealName() |
|
| 563 | |||
| 564 | /** |
||
| 565 | * @param $value |
||
| 566 | * @return $this |
||
| 567 | */ |
||
| 568 | 97 | public function setValue($value) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Set the template property on the object. |
||
| 591 | * |
||
| 592 | * @return void |
||
| 593 | */ |
||
| 594 | 105 | private function setTemplate() |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Add error class to wrapper if validation errors exist. |
||
| 601 | * |
||
| 602 | * @return void |
||
| 603 | */ |
||
| 604 | 105 | protected function addErrorClass() |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Merge all defaults with field specific defaults and set template if passed. |
||
| 633 | * |
||
| 634 | * @param array $options |
||
| 635 | */ |
||
| 636 | 105 | protected function setDefaultOptions(array $options = []) |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Creates default wrapper classes for the form element. |
||
| 649 | * |
||
| 650 | * @param array $options |
||
| 651 | * @return array |
||
| 652 | */ |
||
| 653 | 105 | protected function setDefaultClasses(array $options = []) |
|
| 671 | |||
| 672 | /** |
||
| 673 | * Setup the label for the form field. |
||
| 674 | * |
||
| 675 | * @return void |
||
| 676 | */ |
||
| 677 | 105 | protected function setupLabel() |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Check if fields needs label. |
||
| 700 | * |
||
| 701 | * @return bool |
||
| 702 | */ |
||
| 703 | 35 | protected function needsLabel() |
|
| 714 | |||
| 715 | /** |
||
| 716 | * Disable field. |
||
| 717 | * |
||
| 718 | * @return $this |
||
| 719 | */ |
||
| 720 | 1 | public function disable() |
|
| 726 | |||
| 727 | /** |
||
| 728 | * Enable field. |
||
| 729 | * |
||
| 730 | * @return $this |
||
| 731 | */ |
||
| 732 | 1 | public function enable() |
|
| 738 | |||
| 739 | /** |
||
| 740 | * Get validation rules for a field if any with label for attributes. |
||
| 741 | * |
||
| 742 | * @return array|null |
||
| 743 | */ |
||
| 744 | 10 | public function getValidationRules() |
|
| 770 | |||
| 771 | /** |
||
| 772 | * Get this field's attributes, probably just one. |
||
| 773 | * |
||
| 774 | * @return array |
||
| 775 | */ |
||
| 776 | 3 | public function getAllAttributes() |
|
| 780 | |||
| 781 | /** |
||
| 782 | * Get value property. |
||
| 783 | * |
||
| 784 | * @param mixed|null $default |
||
| 785 | * @return mixed |
||
| 786 | */ |
||
| 787 | 40 | public function getValue($default = null) |
|
| 791 | |||
| 792 | /** |
||
| 793 | * Get default value property. |
||
| 794 | * |
||
| 795 | * @param mixed|null $default |
||
| 796 | * @return mixed |
||
| 797 | */ |
||
| 798 | 35 | public function getDefaultValue($default = null) |
|
| 802 | |||
| 803 | /** |
||
| 804 | * Check if provided value is valid for this type. |
||
| 805 | * |
||
| 806 | * @return bool |
||
| 807 | */ |
||
| 808 | 97 | protected function isValidValue($value) |
|
| 812 | |||
| 813 | /** |
||
| 814 | * Method initFilters used to initialize filters |
||
| 815 | * from field options and bind it to the same. |
||
| 816 | * |
||
| 817 | * @return $this |
||
| 818 | */ |
||
| 819 | 100 | protected function initFilters() |
|
| 838 | |||
| 839 | /** |
||
| 840 | * Method setFilters used to set filters to current filters property. |
||
| 841 | * |
||
| 842 | * @param array $filters |
||
| 843 | * |
||
| 844 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 845 | */ |
||
| 846 | public function setFilters(array $filters) |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Method getFilters returns array of binded filters |
||
| 858 | * if there are any binded. Otherwise empty array. |
||
| 859 | * |
||
| 860 | * @return array |
||
| 861 | */ |
||
| 862 | 23 | public function getFilters() |
|
| 866 | |||
| 867 | /** |
||
| 868 | * @param string|FilterInterface $filter |
||
| 869 | * |
||
| 870 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 871 | * |
||
| 872 | * @throws FilterAlreadyBindedException |
||
| 873 | */ |
||
| 874 | 8 | public function addFilter($filter) |
|
| 902 | |||
| 903 | /** |
||
| 904 | * Method removeFilter used to remove filter by provided alias/name. |
||
| 905 | * |
||
| 906 | * @param string $name |
||
| 907 | * |
||
| 908 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 909 | */ |
||
| 910 | 1 | public function removeFilter($name) |
|
| 920 | |||
| 921 | /** |
||
| 922 | * Method removeFilters used to remove filters by provided aliases/names. |
||
| 923 | * |
||
| 924 | * @param array $filterNames |
||
| 925 | * |
||
| 926 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 927 | */ |
||
| 928 | 1 | public function removeFilters(array $filterNames) |
|
| 940 | |||
| 941 | /** |
||
| 942 | * Method clearFilters used to empty current filters property. |
||
| 943 | * |
||
| 944 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 945 | */ |
||
| 946 | 1 | public function clearFilters() |
|
| 951 | |||
| 952 | /** |
||
| 953 | * Method used to set FiltersOverride status to provided value. |
||
| 954 | * |
||
| 955 | * @param $status |
||
| 956 | * |
||
| 957 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 958 | */ |
||
| 959 | 2 | public function setFiltersOverride($status) |
|
| 964 | |||
| 965 | /** |
||
| 966 | * @return bool |
||
| 967 | */ |
||
| 968 | 9 | public function getFiltersOverride() |
|
| 972 | |||
| 973 | /** |
||
| 974 | * Method used to set Unfiltered/Unmutated field value. |
||
| 975 | * Method is called before field value mutating starts - request value filtering. |
||
| 976 | * |
||
| 977 | * @param mixed $value |
||
| 978 | * |
||
| 979 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 980 | */ |
||
| 981 | 1 | public function setRawValue($value) |
|
| 986 | |||
| 987 | /** |
||
| 988 | * Returns unfiltered raw value of field. |
||
| 989 | * |
||
| 990 | * @return mixed |
||
| 991 | */ |
||
| 992 | public function getRawValue() |
||
| 996 | |||
| 997 | /** |
||
| 998 | * Get config from the form. |
||
| 999 | * |
||
| 1000 | * @return mixed |
||
| 1001 | */ |
||
| 1002 | 105 | private function getConfig($key = null, $default = null) |
|
| 1006 | } |
||
| 1007 |
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.