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) |
|
| 173 | { |
||
| 174 | 35 | $this->prepareOptions($options); |
|
| 175 | 35 | $value = $this->getValue(); |
|
| 176 | 35 | $defaultValue = $this->getDefaultValue(); |
|
| 177 | |||
| 178 | 35 | if ($showField) { |
|
| 179 | 35 | $this->rendered = true; |
|
| 180 | } |
||
| 181 | |||
| 182 | // Override default value with value |
||
| 183 | 35 | if (!$this->isValidValue($value) && $this->isValidValue($defaultValue)) { |
|
| 184 | $this->setOption($this->valueProperty, $defaultValue); |
||
| 185 | } |
||
| 186 | |||
| 187 | 35 | if (!$this->needsLabel()) { |
|
| 188 | 11 | $showLabel = false; |
|
| 189 | } |
||
| 190 | |||
| 191 | 35 | if ($showError) { |
|
| 192 | 34 | $showError = $this->parent->haveErrorsEnabled(); |
|
| 193 | } |
||
| 194 | |||
| 195 | 35 | $data = $this->getRenderData(); |
|
| 196 | |||
| 197 | 35 | return $this->formHelper->getView()->make( |
|
| 198 | 35 | $this->getViewTemplate(), |
|
| 199 | $data + [ |
||
| 200 | 35 | 'name' => $this->name, |
|
| 201 | 35 | 'nameKey' => $this->getNameKey(), |
|
| 202 | 35 | 'type' => $this->type, |
|
| 203 | 35 | 'options' => $this->options, |
|
| 204 | 35 | 'showLabel' => $showLabel, |
|
| 205 | 35 | 'showField' => $showField, |
|
| 206 | 35 | 'showError' => $showError, |
|
| 207 | 35 | 'errorBag' => $this->parent->getErrorBag(), |
|
| 208 | 35 | 'translationTemplate' => $this->parent->getTranslationTemplate(), |
|
| 209 | ] |
||
| 210 | 35 | )->render(); |
|
| 211 | } |
||
| 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() { |
|
| 219 | 35 | return []; |
|
| 220 | } |
||
| 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) |
|
| 230 | { |
||
| 231 | 92 | $transformedName = $this->transformKey($name); |
|
| 232 | 92 | if (is_string($model)) { |
|
| 233 | return $model; |
||
| 234 | 92 | } elseif (is_object($model)) { |
|
| 235 | 3 | return object_get($model, $transformedName); |
|
| 236 | 92 | } elseif (is_array($model)) { |
|
| 237 | 91 | return array_get($model, $transformedName); |
|
| 238 | } |
||
| 239 | 5 | } |
|
| 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) |
|
| 248 | { |
||
| 249 | 101 | return $this->formHelper->transformToDotSyntax($key); |
|
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Prepare options for rendering. |
||
| 254 | * |
||
| 255 | * @param array $options |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | 101 | protected function prepareOptions(array $options = []) |
|
| 259 | { |
||
| 260 | 101 | $helper = $this->formHelper; |
|
| 261 | 101 | $rulesParser = $helper->createRulesParser($this); |
|
| 262 | 101 | $rules = $this->getOption('rules'); |
|
| 263 | 101 | $parsedRules = $rules ? $rulesParser->parse($rules) : []; |
|
| 264 | |||
| 265 | 101 | $this->options = $helper->mergeOptions($this->options, $options); |
|
| 266 | |||
| 267 | 101 | foreach (['attr', 'label_attr', 'wrapper'] as $appendable) { |
|
| 268 | // Append values to the 'class' attribute |
||
| 269 | 101 | if ($this->getOption("{$appendable}.class_append")) { |
|
| 270 | // Combine the current class attribute with the appends |
||
| 271 | 3 | $append = $this->getOption("{$appendable}.class_append"); |
|
| 272 | 3 | $classAttribute = $this->getOption("{$appendable}.class", '').' '.$append; |
|
| 273 | 3 | $this->setOption("{$appendable}.class", $classAttribute); |
|
| 274 | |||
| 275 | // Then remove the class_append option to prevent it from showing up as an attribute in the HTML |
||
| 276 | 101 | $this->setOption("{$appendable}.class_append", null); |
|
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | 101 | if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) { |
|
| 281 | 2 | $this->name = $this->name.'[]'; |
|
| 282 | 2 | $this->setOption('tmp.multipleBracesSet', true); |
|
| 283 | } |
||
| 284 | |||
| 285 | 101 | if ($this->parent->haveErrorsEnabled()) { |
|
| 286 | 101 | $this->addErrorClass(); |
|
| 287 | } |
||
| 288 | |||
| 289 | 101 | if ($this->getOption('required') === true || isset($parsedRules['required'])) { |
|
| 290 | 4 | $lblClass = $this->getOption('label_attr.class', ''); |
|
| 291 | 4 | $requiredClass = $helper->getConfig('defaults.required_class', 'required'); |
|
| 292 | |||
| 293 | 4 | if (! str_contains($lblClass, $requiredClass)) { |
|
| 294 | 4 | $lblClass .= ' '.$requiredClass; |
|
| 295 | 4 | $this->setOption('label_attr.class', $lblClass); |
|
| 296 | } |
||
| 297 | |||
| 298 | 4 | if ($this->parent->clientValidationEnabled()) { |
|
| 299 | 3 | $this->setOption('attr.required', 'required'); |
|
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | 101 | if ($this->parent->clientValidationEnabled() && $parsedRules) { |
|
|
|
|||
| 304 | 1 | $attrs = $this->getOption('attr') + $parsedRules; |
|
| 305 | 1 | $this->setOption('attr', $attrs); |
|
| 306 | } |
||
| 307 | |||
| 308 | 101 | $this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper'))); |
|
| 309 | 101 | $this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors'))); |
|
| 310 | |||
| 311 | 101 | if ($this->getOption('help_block.text')) { |
|
| 312 | 1 | $this->setOption( |
|
| 313 | 1 | 'help_block.helpBlockAttrs', |
|
| 314 | 1 | $helper->prepareAttributes($this->getOption('help_block.attr')) |
|
| 315 | ); |
||
| 316 | } |
||
| 317 | |||
| 318 | 101 | return $this->options; |
|
| 319 | } |
||
| 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 | 18 | 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 |
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.