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 | 96 | 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 | 96 | 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 | 33 | 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 | 33 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Return the extra render data for this form field, passed into the field's template directly. |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | 33 | protected function getRenderData() { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Get the attribute value from the model by name. |
||
| 222 | * |
||
| 223 | * @param mixed $model |
||
| 224 | * @param string $name |
||
| 225 | * @return mixed |
||
| 226 | */ |
||
| 227 | 88 | protected function getModelValueAttribute($model, $name) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Transform array like syntax to dot syntax. |
||
| 241 | * |
||
| 242 | * @param string $key |
||
| 243 | * @return mixed |
||
| 244 | */ |
||
| 245 | 96 | protected function transformKey($key) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Prepare options for rendering. |
||
| 252 | * |
||
| 253 | * @param array $options |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | 96 | protected function prepareOptions(array $options = []) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Get name of the field. |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | 39 | public function getName() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Set name of the field. |
||
| 331 | * |
||
| 332 | * @param string $name |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | 12 | public function setName($name) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Get dot notation key for fields. |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | **/ |
||
| 347 | 52 | public function getNameKey() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Get field options. |
||
| 354 | * |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | 12 | public function getOptions() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Get single option from options array. Can be used with dot notation ('attr.class'). |
||
| 364 | * |
||
| 365 | * @param string $option |
||
| 366 | * @param mixed|null $default |
||
| 367 | * @return mixed |
||
| 368 | */ |
||
| 369 | 96 | public function getOption($option, $default = null) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Set field options. |
||
| 376 | * |
||
| 377 | * @param array $options |
||
| 378 | * @return $this |
||
| 379 | */ |
||
| 380 | 12 | public function setOptions($options) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Set single option on the field. |
||
| 389 | * |
||
| 390 | * @param string $name |
||
| 391 | * @param mixed $value |
||
| 392 | * @return $this |
||
| 393 | */ |
||
| 394 | 96 | public function setOption($name, $value) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Get the type of the field. |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | 62 | public function getType() |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Set type of the field. |
||
| 413 | * |
||
| 414 | * @param mixed $type |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | 1 | public function setType($type) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * @return Form |
||
| 428 | */ |
||
| 429 | 96 | public function getParent() |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Check if the field is rendered. |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | 4 | public function isRendered() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Default options for field. |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | 75 | protected function getDefaults() |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Defaults used across all fields. |
||
| 456 | * |
||
| 457 | * @return array |
||
| 458 | */ |
||
| 459 | 96 | private function allDefaults() |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Get real name of the field without form namespace. |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | 95 | public function getRealName() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * @param $value |
||
| 491 | * @return $this |
||
| 492 | */ |
||
| 493 | 90 | public function setValue($value) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * Set the template property on the object. |
||
| 516 | * |
||
| 517 | * @return void |
||
| 518 | */ |
||
| 519 | 96 | private function setTemplate() |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Add error class to wrapper if validation errors exist. |
||
| 526 | * |
||
| 527 | * @return void |
||
| 528 | */ |
||
| 529 | 96 | protected function addErrorClass() |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Merge all defaults with field specific defaults and set template if passed. |
||
| 546 | * |
||
| 547 | * @param array $options |
||
| 548 | */ |
||
| 549 | 96 | protected function setDefaultOptions(array $options = []) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Creates default wrapper classes for the form element. |
||
| 562 | * |
||
| 563 | * @param array $options |
||
| 564 | * @return array |
||
| 565 | */ |
||
| 566 | 96 | protected function setDefaultClasses(array $options = []) |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Setup the label for the form field. |
||
| 587 | * |
||
| 588 | * @return void |
||
| 589 | */ |
||
| 590 | 96 | protected function setupLabel() |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Check if fields needs label. |
||
| 607 | * |
||
| 608 | * @return bool |
||
| 609 | */ |
||
| 610 | 33 | protected function needsLabel() |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Disable field. |
||
| 624 | * |
||
| 625 | * @return $this |
||
| 626 | */ |
||
| 627 | 1 | public function disable() |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Enable field. |
||
| 636 | * |
||
| 637 | * @return $this |
||
| 638 | */ |
||
| 639 | 1 | public function enable() |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Get validation rules for a field if any with label for attributes. |
||
| 648 | * |
||
| 649 | * @return array|null |
||
| 650 | */ |
||
| 651 | 9 | public function getValidationRules() |
|
| 677 | |||
| 678 | /** |
||
| 679 | * Get this field's attributes, probably just one. |
||
| 680 | * |
||
| 681 | * @return array |
||
| 682 | */ |
||
| 683 | 3 | public function getAllAttributes() |
|
| 687 | |||
| 688 | /** |
||
| 689 | * Get value property. |
||
| 690 | * |
||
| 691 | * @param mixed|null $default |
||
| 692 | * @return mixed |
||
| 693 | */ |
||
| 694 | 36 | public function getValue($default = null) |
|
| 698 | |||
| 699 | /** |
||
| 700 | * Get default value property. |
||
| 701 | * |
||
| 702 | * @param mixed|null $default |
||
| 703 | * @return mixed |
||
| 704 | */ |
||
| 705 | 33 | public function getDefaultValue($default = null) |
|
| 709 | |||
| 710 | /** |
||
| 711 | * Check if provided value is valid for this type. |
||
| 712 | * |
||
| 713 | * @return bool |
||
| 714 | */ |
||
| 715 | 91 | protected function isValidValue($value) |
|
| 719 | |||
| 720 | /** |
||
| 721 | * Method initFilters used to initialize filters |
||
| 722 | * from field options and bind it to the same. |
||
| 723 | * |
||
| 724 | * @return $this |
||
| 725 | */ |
||
| 726 | 91 | protected function initFilters() |
|
| 745 | |||
| 746 | /** |
||
| 747 | * Method setFilters used to set filters to current filters property. |
||
| 748 | * |
||
| 749 | * @param array $filters |
||
| 750 | * |
||
| 751 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 752 | */ |
||
| 753 | public function setFilters(array $filters) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Method getFilters returns array of binded filters |
||
| 765 | * if there are any binded. Otherwise empty array. |
||
| 766 | * |
||
| 767 | * @return array |
||
| 768 | */ |
||
| 769 | 23 | public function getFilters() |
|
| 773 | |||
| 774 | /** |
||
| 775 | * @param string|FilterInterface $filter |
||
| 776 | * |
||
| 777 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 778 | * |
||
| 779 | * @throws FilterAlreadyBindedException |
||
| 780 | */ |
||
| 781 | 8 | public function addFilter($filter) |
|
| 809 | |||
| 810 | /** |
||
| 811 | * Method removeFilter used to remove filter by provided alias/name. |
||
| 812 | * |
||
| 813 | * @param string $name |
||
| 814 | * |
||
| 815 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 816 | */ |
||
| 817 | 1 | public function removeFilter($name) |
|
| 827 | |||
| 828 | /** |
||
| 829 | * Method removeFilters used to remove filters by provided aliases/names. |
||
| 830 | * |
||
| 831 | * @param array $filterNames |
||
| 832 | * |
||
| 833 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 834 | */ |
||
| 835 | 1 | public function removeFilters(array $filterNames) |
|
| 847 | |||
| 848 | /** |
||
| 849 | * Method clearFilters used to empty current filters property. |
||
| 850 | * |
||
| 851 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 852 | */ |
||
| 853 | 1 | public function clearFilters() |
|
| 858 | |||
| 859 | /** |
||
| 860 | * Method used to set FiltersOverride status to provided value. |
||
| 861 | * |
||
| 862 | * @param $status |
||
| 863 | * |
||
| 864 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 865 | */ |
||
| 866 | 2 | public function setFiltersOverride($status) |
|
| 871 | |||
| 872 | /** |
||
| 873 | * @return bool |
||
| 874 | */ |
||
| 875 | 9 | public function getFiltersOverride() |
|
| 879 | |||
| 880 | /** |
||
| 881 | * Method used to set Unfiltered/Unmutated field value. |
||
| 882 | * Method is called before field value mutating starts - request value filtering. |
||
| 883 | * |
||
| 884 | * @param mixed $value |
||
| 885 | * |
||
| 886 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 887 | */ |
||
| 888 | 1 | public function setRawValue($value) |
|
| 893 | |||
| 894 | /** |
||
| 895 | * Returns unfiltered raw value of field. |
||
| 896 | * |
||
| 897 | * @return mixed |
||
| 898 | */ |
||
| 899 | public function getRawValue() |
||
| 903 | } |
||
| 904 |
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.