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 | 101 | 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 | 101 | 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() { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Get the attribute value from the model by name. |
||
| 227 | * |
||
| 228 | * @param mixed $model |
||
| 229 | * @param string $name |
||
| 230 | * @return mixed |
||
| 231 | */ |
||
| 232 | 92 | protected function getModelValueAttribute($model, $name) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Transform array like syntax to dot syntax. |
||
| 246 | * |
||
| 247 | * @param string $key |
||
| 248 | * @return mixed |
||
| 249 | */ |
||
| 250 | 101 | protected function transformKey($key) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Prepare options for rendering. |
||
| 257 | * |
||
| 258 | * @param array $options |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 101 | protected function prepareOptions(array $options = []) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Get name of the field. |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | 38 | public function getName() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Set name of the field. |
||
| 336 | * |
||
| 337 | * @param string $name |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | 11 | public function setName($name) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Get dot notation key for fields. |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | **/ |
||
| 352 | 54 | public function getNameKey() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Get field options. |
||
| 359 | * |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | 12 | public function getOptions() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Get single option from options array. Can be used with dot notation ('attr.class'). |
||
| 369 | * |
||
| 370 | * @param string $option |
||
| 371 | * @param mixed|null $default |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | 101 | public function getOption($option, $default = null) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Set field options. |
||
| 381 | * |
||
| 382 | * @param array $options |
||
| 383 | * @return $this |
||
| 384 | */ |
||
| 385 | 11 | public function setOptions($options) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Set single option on the field. |
||
| 394 | * |
||
| 395 | * @param string $name |
||
| 396 | * @param mixed $value |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | 101 | public function setOption($name, $value) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Get the type of the field. |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | 65 | public function getType() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Set type of the field. |
||
| 418 | * |
||
| 419 | * @param mixed $type |
||
| 420 | * @return $this |
||
| 421 | */ |
||
| 422 | 1 | public function setType($type) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @return Form |
||
| 433 | */ |
||
| 434 | 101 | public function getParent() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * Check if the field is rendered. |
||
| 441 | * |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | 4 | public function isRendered() |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Default options for field. |
||
| 451 | * |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | 75 | protected function getDefaults() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Defaults used across all fields. |
||
| 461 | * |
||
| 462 | * @return array |
||
| 463 | */ |
||
| 464 | 101 | private function allDefaults() |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Get real name of the field without form namespace. |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | 100 | public function getRealName() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * @param $value |
||
| 496 | * @return $this |
||
| 497 | */ |
||
| 498 | 93 | public function setValue($value) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Set the template property on the object. |
||
| 521 | * |
||
| 522 | * @return void |
||
| 523 | */ |
||
| 524 | 101 | private function setTemplate() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Add error class to wrapper if validation errors exist. |
||
| 531 | * |
||
| 532 | * @return void |
||
| 533 | */ |
||
| 534 | 101 | protected function addErrorClass() |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Merge all defaults with field specific defaults and set template if passed. |
||
| 560 | * |
||
| 561 | * @param array $options |
||
| 562 | */ |
||
| 563 | 101 | protected function setDefaultOptions(array $options = []) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Creates default wrapper classes for the form element. |
||
| 576 | * |
||
| 577 | * @param array $options |
||
| 578 | * @return array |
||
| 579 | */ |
||
| 580 | 101 | protected function setDefaultClasses(array $options = []) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Setup the label for the form field. |
||
| 601 | * |
||
| 602 | * @return void |
||
| 603 | */ |
||
| 604 | 101 | protected function setupLabel() |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Check if fields needs label. |
||
| 627 | * |
||
| 628 | * @return bool |
||
| 629 | */ |
||
| 630 | 35 | protected function needsLabel() |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Disable field. |
||
| 644 | * |
||
| 645 | * @return $this |
||
| 646 | */ |
||
| 647 | 1 | public function disable() |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Enable field. |
||
| 656 | * |
||
| 657 | * @return $this |
||
| 658 | */ |
||
| 659 | 1 | public function enable() |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Get validation rules for a field if any with label for attributes. |
||
| 668 | * |
||
| 669 | * @return array|null |
||
| 670 | */ |
||
| 671 | 9 | public function getValidationRules() |
|
| 672 | { |
||
| 673 | 9 | $rules = $this->getOption('rules', []); |
|
| 674 | 9 | $name = $this->getNameKey(); |
|
| 675 | 9 | $messages = $this->getOption('error_messages', []); |
|
| 676 | 9 | $formName = $this->formHelper->transformToDotSyntax($this->parent->getName()); |
|
| 677 | |||
| 678 | 9 | if ($messages && $formName) { |
|
| 679 | 1 | $newMessages = []; |
|
| 680 | 1 | foreach ($messages as $messageKey => $message) { |
|
| 681 | 1 | $messageKey = sprintf('%s.%s', $formName, $messageKey); |
|
| 682 | 1 | $newMessages[$messageKey] = $message; |
|
| 683 | } |
||
| 684 | 1 | $messages = $newMessages; |
|
| 685 | } |
||
| 686 | |||
| 687 | 9 | if (!$rules) { |
|
| 688 | 2 | return (new Rules([]))->setFieldName($this->getNameKey()); |
|
| 689 | } |
||
| 690 | |||
| 691 | 8 | if (is_array($rules)) { |
|
| 692 | 2 | $rules = array_map(function ($rule) use ($name) { |
|
| 693 | 2 | if ($rule instanceof \Closure) { |
|
| 694 | return $rule($name); |
||
| 695 | } |
||
| 696 | |||
| 697 | 2 | return $rule; |
|
| 698 | 2 | }, $rules); |
|
| 699 | } |
||
| 700 | |||
| 701 | 8 | return (new Rules( |
|
| 702 | 8 | [$name => $rules], |
|
| 703 | 8 | [$name => $this->getOption('label')], |
|
| 704 | 8 | $messages |
|
| 705 | 8 | ))->setFieldName($this->getNameKey()); |
|
| 706 | } |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Get this field's attributes, probably just one. |
||
| 710 | * |
||
| 711 | * @return array |
||
| 712 | */ |
||
| 713 | 3 | public function getAllAttributes() |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Get value property. |
||
| 720 | * |
||
| 721 | * @param mixed|null $default |
||
| 722 | * @return mixed |
||
| 723 | */ |
||
| 724 | 38 | public function getValue($default = null) |
|
| 728 | |||
| 729 | /** |
||
| 730 | * Get default value property. |
||
| 731 | * |
||
| 732 | * @param mixed|null $default |
||
| 733 | * @return mixed |
||
| 734 | */ |
||
| 735 | 35 | public function getDefaultValue($default = null) |
|
| 739 | |||
| 740 | /** |
||
| 741 | * Check if provided value is valid for this type. |
||
| 742 | * |
||
| 743 | * @return bool |
||
| 744 | */ |
||
| 745 | 93 | protected function isValidValue($value) |
|
| 749 | |||
| 750 | /** |
||
| 751 | * Method initFilters used to initialize filters |
||
| 752 | * from field options and bind it to the same. |
||
| 753 | * |
||
| 754 | * @return $this |
||
| 755 | */ |
||
| 756 | 96 | protected function initFilters() |
|
| 775 | |||
| 776 | /** |
||
| 777 | * Method setFilters used to set filters to current filters property. |
||
| 778 | * |
||
| 779 | * @param array $filters |
||
| 780 | * |
||
| 781 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 782 | */ |
||
| 783 | public function setFilters(array $filters) |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Method getFilters returns array of binded filters |
||
| 795 | * if there are any binded. Otherwise empty array. |
||
| 796 | * |
||
| 797 | * @return array |
||
| 798 | */ |
||
| 799 | 23 | public function getFilters() |
|
| 803 | |||
| 804 | /** |
||
| 805 | * @param string|FilterInterface $filter |
||
| 806 | * |
||
| 807 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 808 | * |
||
| 809 | * @throws FilterAlreadyBindedException |
||
| 810 | */ |
||
| 811 | 8 | public function addFilter($filter) |
|
| 839 | |||
| 840 | /** |
||
| 841 | * Method removeFilter used to remove filter by provided alias/name. |
||
| 842 | * |
||
| 843 | * @param string $name |
||
| 844 | * |
||
| 845 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 846 | */ |
||
| 847 | 1 | public function removeFilter($name) |
|
| 857 | |||
| 858 | /** |
||
| 859 | * Method removeFilters used to remove filters by provided aliases/names. |
||
| 860 | * |
||
| 861 | * @param array $filterNames |
||
| 862 | * |
||
| 863 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 864 | */ |
||
| 865 | 1 | public function removeFilters(array $filterNames) |
|
| 877 | |||
| 878 | /** |
||
| 879 | * Method clearFilters used to empty current filters property. |
||
| 880 | * |
||
| 881 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 882 | */ |
||
| 883 | 1 | public function clearFilters() |
|
| 888 | |||
| 889 | /** |
||
| 890 | * Method used to set FiltersOverride status to provided value. |
||
| 891 | * |
||
| 892 | * @param $status |
||
| 893 | * |
||
| 894 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 895 | */ |
||
| 896 | 2 | public function setFiltersOverride($status) |
|
| 901 | |||
| 902 | /** |
||
| 903 | * @return bool |
||
| 904 | */ |
||
| 905 | 9 | public function getFiltersOverride() |
|
| 909 | |||
| 910 | /** |
||
| 911 | * Method used to set Unfiltered/Unmutated field value. |
||
| 912 | * Method is called before field value mutating starts - request value filtering. |
||
| 913 | * |
||
| 914 | * @param mixed $value |
||
| 915 | * |
||
| 916 | * @return \Kris\LaravelFormBuilder\Fields\FormField |
||
| 917 | */ |
||
| 918 | 1 | public function setRawValue($value) |
|
| 923 | |||
| 924 | /** |
||
| 925 | * Returns unfiltered raw value of field. |
||
| 926 | * |
||
| 927 | * @return mixed |
||
| 928 | */ |
||
| 929 | public function getRawValue() |
||
| 933 | |||
| 934 | /** |
||
| 935 | * Get config from the form. |
||
| 936 | * |
||
| 937 | * @return mixed |
||
| 938 | */ |
||
| 939 | 101 | private function getConfig($key = null, $default = null) |
|
| 943 | } |
||
| 944 |
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.