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 |
||
| 22 | */ |
||
| 23 | protected $name; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Type of the field |
||
| 27 | * |
||
| 28 | * @var |
||
| 29 | */ |
||
| 30 | protected $type; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * All options for the field |
||
| 34 | * |
||
| 35 | * @var |
||
| 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 | * @var bool |
||
| 78 | */ |
||
| 79 | protected $hasDefault = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var \Closure|null |
||
| 83 | */ |
||
| 84 | protected $valueClosure = null; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param $name |
||
| 88 | * @param $type |
||
| 89 | * @param Form $parent |
||
| 90 | * @param array $options |
||
| 91 | */ |
||
| 92 | 84 | public function __construct($name, $type, Form $parent, array $options = []) |
|
| 93 | { |
||
| 94 | 84 | $this->name = $name; |
|
| 95 | 84 | $this->type = $type; |
|
| 96 | 84 | $this->parent = $parent; |
|
| 97 | 84 | $this->formHelper = $this->parent->getFormHelper(); |
|
| 98 | 84 | $this->setTemplate(); |
|
| 99 | 84 | $this->setDefaultOptions($options); |
|
| 100 | 84 | $this->setupValue(); |
|
| 101 | 78 | } |
|
| 102 | |||
| 103 | 84 | protected function setupValue() |
|
| 104 | { |
||
| 105 | 84 | $value = $this->getOption($this->valueProperty); |
|
| 106 | 84 | $isChild = $this->getOption('is_child'); |
|
| 107 | |||
| 108 | 84 | if ($value instanceof \Closure) { |
|
| 109 | $this->valueClosure = $value; |
||
| 110 | } |
||
| 111 | |||
| 112 | 84 | if (($value === null || $value instanceof \Closure) && !$isChild) { |
|
| 113 | 75 | $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name)); |
|
| 114 | 78 | } elseif (!$isChild) { |
|
| 115 | 12 | $this->hasDefault = true; |
|
| 116 | 12 | } |
|
| 117 | 78 | } |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Get the template, can be config variable or view path |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | abstract protected function getTemplate(); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | 31 | protected function getViewTemplate() |
|
| 130 | { |
||
| 131 | 31 | return $this->parent->getTemplatePrefix() . $this->getOption('template', $this->template); |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param array $options |
||
| 136 | * @param bool $showLabel |
||
| 137 | * @param bool $showField |
||
| 138 | * @param bool $showError |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 31 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
|
| 142 | { |
||
| 143 | 31 | $this->prepareOptions($options); |
|
| 144 | 31 | $value = $this->getValue(); |
|
| 145 | 31 | $defaultValue = $this->getDefaultValue(); |
|
| 146 | |||
| 147 | 31 | if ($showField) { |
|
| 148 | 31 | $this->rendered = true; |
|
| 149 | 31 | } |
|
| 150 | |||
| 151 | // Override default value with value |
||
| 152 | 31 | if (!$this->isValidValue($value) && $this->isValidValue($defaultValue)) { |
|
| 153 | $this->setOption($this->valueProperty, $defaultValue); |
||
| 154 | } |
||
| 155 | |||
| 156 | 31 | if (!$this->needsLabel()) { |
|
| 157 | 8 | $showLabel = false; |
|
| 158 | 8 | } |
|
| 159 | |||
| 160 | 31 | if ($showError) { |
|
| 161 | 30 | $showError = $this->parent->haveErrorsEnabled(); |
|
| 162 | 30 | } |
|
| 163 | |||
| 164 | 31 | $data = $this->getRenderData(); |
|
| 165 | |||
| 166 | 31 | return $this->formHelper->getView()->make( |
|
| 167 | 31 | $this->getViewTemplate(), |
|
| 168 | $data + [ |
||
| 169 | 31 | 'name' => $this->name, |
|
| 170 | 31 | 'nameKey' => $this->getNameKey(), |
|
| 171 | 31 | 'type' => $this->type, |
|
| 172 | 31 | 'options' => $this->options, |
|
| 173 | 31 | 'showLabel' => $showLabel, |
|
| 174 | 31 | 'showField' => $showField, |
|
| 175 | 'showError' => $showError |
||
| 176 | 31 | ] |
|
| 177 | 31 | )->render(); |
|
| 178 | 1 | } |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Return the extra render data for this form field, passed into the field's template directly. |
||
| 182 | * |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | 31 | protected function getRenderData() { |
|
| 186 | 31 | return []; |
|
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the attribute value from the model by name |
||
| 191 | * |
||
| 192 | * @param mixed $model |
||
| 193 | * @param string $name |
||
| 194 | * @return mixed |
||
| 195 | */ |
||
| 196 | 77 | protected function getModelValueAttribute($model, $name) |
|
| 197 | { |
||
| 198 | 77 | $transformedName = $this->transformKey($name); |
|
| 199 | 77 | if (is_string($model)) { |
|
| 200 | return $model; |
||
| 201 | 77 | } elseif (is_object($model)) { |
|
| 202 | 2 | return object_get($model, $transformedName); |
|
| 203 | 77 | } elseif (is_array($model)) { |
|
| 204 | 76 | return array_get($model, $transformedName); |
|
| 205 | } |
||
| 206 | 5 | } |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Transform array like syntax to dot syntax |
||
| 210 | * |
||
| 211 | * @param $key |
||
| 212 | * @return mixed |
||
| 213 | */ |
||
| 214 | 84 | protected function transformKey($key) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Prepare options for rendering |
||
| 221 | * |
||
| 222 | * @param array $options |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | 84 | protected function prepareOptions(array $options = []) |
|
| 287 | 1 | ||
| 288 | /** |
||
| 289 | 84 | * Get name of the field |
|
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function getName() |
||
| 297 | 27 | ||
| 298 | /** |
||
| 299 | 27 | * Set name of the field |
|
| 300 | * |
||
| 301 | * @param string $name |
||
| 302 | * @return $this |
||
| 303 | */ |
||
| 304 | public function setName($name) |
||
| 310 | 11 | ||
| 311 | /** |
||
| 312 | 11 | * Get dot notation key for fields |
|
| 313 | * |
||
| 314 | * @return string |
||
| 315 | **/ |
||
| 316 | public function getNameKey() |
||
| 320 | 49 | ||
| 321 | /** |
||
| 322 | 49 | * Get field options |
|
| 323 | * |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | public function getOptions() |
||
| 330 | 12 | ||
| 331 | /** |
||
| 332 | 12 | * Get single option from options array. Can be used with dot notation ('attr.class') |
|
| 333 | * |
||
| 334 | * @param $option |
||
| 335 | * @param mixed $default |
||
| 336 | * |
||
| 337 | * @return mixed |
||
| 338 | */ |
||
| 339 | public function getOption($option, $default = null) |
||
| 343 | 84 | ||
| 344 | /** |
||
| 345 | 84 | * Set field options |
|
| 346 | * |
||
| 347 | * @param array $options |
||
| 348 | * @return $this |
||
| 349 | */ |
||
| 350 | public function setOptions($options) |
||
| 356 | 11 | ||
| 357 | /** |
||
| 358 | 11 | * Set single option on the field |
|
| 359 | * |
||
| 360 | * @param string $name |
||
| 361 | * @param mixed $value |
||
| 362 | * @return $this |
||
| 363 | */ |
||
| 364 | public function setOption($name, $value) |
||
| 370 | 84 | ||
| 371 | /** |
||
| 372 | 84 | * Get the type of the field |
|
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public function getType() |
||
| 380 | 51 | ||
| 381 | /** |
||
| 382 | 51 | * Set type of the field |
|
| 383 | * |
||
| 384 | * @param mixed $type |
||
| 385 | * @return $this |
||
| 386 | */ |
||
| 387 | public function setType($type) |
||
| 395 | 1 | ||
| 396 | /** |
||
| 397 | 1 | * @return Form |
|
| 398 | */ |
||
| 399 | public function getParent() |
||
| 403 | 84 | ||
| 404 | /** |
||
| 405 | 84 | * Check if the field is rendered |
|
| 406 | * |
||
| 407 | * @return bool |
||
| 408 | */ |
||
| 409 | public function isRendered() |
||
| 413 | 4 | ||
| 414 | /** |
||
| 415 | 4 | * Default options for field |
|
| 416 | * |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | protected function getDefaults() |
||
| 423 | 64 | ||
| 424 | /** |
||
| 425 | 64 | * Defaults used across all fields |
|
| 426 | * |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | private function allDefaults() |
||
| 448 | 84 | ||
| 449 | 84 | /** |
|
| 450 | 84 | * Get real name of the field without form namespace |
|
| 451 | * |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | public function getRealName() |
||
| 458 | 83 | ||
| 459 | /** |
||
| 460 | 83 | * @param $value |
|
| 461 | * @return $this |
||
| 462 | */ |
||
| 463 | public function setValue($value) |
||
| 483 | 78 | ||
| 484 | /** |
||
| 485 | 78 | * Set the template property on the object |
|
| 486 | */ |
||
| 487 | private function setTemplate() |
||
| 491 | 84 | ||
| 492 | /** |
||
| 493 | 84 | * Add error class to wrapper if validation errors exist |
|
| 494 | 84 | */ |
|
| 495 | protected function addErrorClass() |
||
| 509 | |||
| 510 | |||
| 511 | /** |
||
| 512 | 84 | * Merge all defaults with field specific defaults and set template if passed |
|
| 513 | * |
||
| 514 | * @param array $options |
||
| 515 | */ |
||
| 516 | protected function setDefaultOptions(array $options = []) |
||
| 526 | 84 | ||
| 527 | /** |
||
| 528 | 84 | * Creates default wrapper classes for the form element. |
|
| 529 | 84 | * |
|
| 530 | * @param array $options |
||
| 531 | * @return array |
||
| 532 | */ |
||
| 533 | protected function setDefaultClasses(array $options = []) |
||
| 551 | 1 | ||
| 552 | 1 | protected function setupLabel() |
|
| 566 | |||
| 567 | /** |
||
| 568 | 82 | * Check if fields needs label |
|
| 569 | 82 | * |
|
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | protected function needsLabel() |
||
| 583 | |||
| 584 | /** |
||
| 585 | 27 | * Disable field |
|
| 586 | * |
||
| 587 | * @return $this |
||
| 588 | */ |
||
| 589 | public function disable() |
||
| 595 | 1 | ||
| 596 | /** |
||
| 597 | 1 | * Enable field |
|
| 598 | * |
||
| 599 | * @return $this |
||
| 600 | */ |
||
| 601 | public function enable() |
||
| 607 | 1 | ||
| 608 | /** |
||
| 609 | 1 | * Get validation rules for a field if any with label for attributes |
|
| 610 | * |
||
| 611 | * @return array|null |
||
| 612 | */ |
||
| 613 | public function getValidationRules() |
||
| 639 | 7 | ||
| 640 | /** |
||
| 641 | 7 | * Get this field's attributes, probably just one. |
|
| 642 | * |
||
| 643 | * @return array |
||
| 644 | */ |
||
| 645 | public function getAllAttributes() |
||
| 649 | 3 | ||
| 650 | /** |
||
| 651 | 3 | * Get value property |
|
| 652 | * |
||
| 653 | * @param mixed|null $default |
||
| 654 | * @return mixed |
||
| 655 | */ |
||
| 656 | public function getValue($default = null) |
||
| 660 | 34 | ||
| 661 | /** |
||
| 662 | 34 | * Get default value property |
|
| 663 | * |
||
| 664 | * @param mixed|null $default |
||
| 665 | * @return mixed |
||
| 666 | */ |
||
| 667 | public function getDefaultValue($default = null) |
||
| 671 | 31 | ||
| 672 | /** |
||
| 673 | 31 | * Check if provided value is valid for this type |
|
| 674 | * |
||
| 675 | * @return bool |
||
| 676 | */ |
||
| 677 | protected function isValidValue($value) |
||
| 681 | } |
||
| 682 |
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.