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 | 76 | public function __construct($name, $type, Form $parent, array $options = []) |
|
| 93 | { |
||
| 94 | 76 | $this->name = $name; |
|
| 95 | 76 | $this->type = $type; |
|
| 96 | 76 | $this->parent = $parent; |
|
| 97 | 76 | $this->formHelper = $this->parent->getFormHelper(); |
|
| 98 | 76 | $this->setTemplate(); |
|
| 99 | 76 | $this->setDefaultOptions($options); |
|
| 100 | 76 | $this->setupValue(); |
|
| 101 | 71 | } |
|
| 102 | |||
| 103 | 76 | protected function setupValue() |
|
| 104 | { |
||
| 105 | 76 | $value = $this->getOption($this->valueProperty); |
|
| 106 | 76 | $isChild = $this->getOption('is_child'); |
|
| 107 | |||
| 108 | 76 | if ($value instanceof \Closure) { |
|
| 109 | $this->valueClosure = $value; |
||
| 110 | } |
||
| 111 | |||
| 112 | 76 | if (($value === null || $value instanceof \Closure) && !$isChild) { |
|
| 113 | 67 | $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name)); |
|
| 114 | 18 | } elseif (!$isChild) { |
|
| 115 | 12 | $this->hasDefault = true; |
|
| 116 | } |
||
| 117 | 71 | } |
|
| 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 | 30 | protected function getViewTemplate() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param array $options |
||
| 136 | * @param bool $showLabel |
||
| 137 | * @param bool $showField |
||
| 138 | * @param bool $showError |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 30 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
|
| 142 | { |
||
| 143 | 30 | $this->prepareOptions($options); |
|
| 144 | 30 | $value = $this->getValue(); |
|
| 145 | 30 | $defaultValue = $this->getDefaultValue(); |
|
| 146 | |||
| 147 | 30 | if ($showField) { |
|
| 148 | 30 | $this->rendered = true; |
|
| 149 | } |
||
| 150 | |||
| 151 | // Override default value with value |
||
| 152 | 30 | if (!$this->isValidValue($value) && $this->isValidValue($defaultValue)) { |
|
| 153 | $this->setOption($this->valueProperty, $defaultValue); |
||
| 154 | } |
||
| 155 | |||
| 156 | 30 | if (!$this->needsLabel()) { |
|
| 157 | 8 | $showLabel = false; |
|
| 158 | } |
||
| 159 | |||
| 160 | 30 | if ($showError) { |
|
| 161 | 29 | $showError = $this->parent->haveErrorsEnabled(); |
|
| 162 | } |
||
| 163 | |||
| 164 | 30 | return $this->formHelper->getView()->make( |
|
| 165 | 30 | $this->getViewTemplate(), |
|
| 166 | [ |
||
| 167 | 30 | 'name' => $this->name, |
|
| 168 | 30 | 'nameKey' => $this->getNameKey(), |
|
| 169 | 30 | 'type' => $this->type, |
|
| 170 | 30 | 'options' => $this->options, |
|
| 171 | 30 | 'showLabel' => $showLabel, |
|
| 172 | 30 | 'showField' => $showField, |
|
| 173 | 30 | 'showError' => $showError |
|
| 174 | ] |
||
| 175 | 30 | )->render(); |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the attribute value from the model by name |
||
| 180 | * |
||
| 181 | * @param mixed $model |
||
| 182 | * @param string $name |
||
| 183 | * @return mixed |
||
| 184 | */ |
||
| 185 | 69 | protected function getModelValueAttribute($model, $name) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Transform array like syntax to dot syntax |
||
| 199 | * |
||
| 200 | * @param $key |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | 76 | protected function transformKey($key) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Prepare options for rendering |
||
| 210 | * |
||
| 211 | * @param array $options |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | 76 | protected function prepareOptions(array $options = []) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Get name of the field |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | 23 | public function getName() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Set name of the field |
||
| 291 | * |
||
| 292 | * @param string $name |
||
| 293 | * @return $this |
||
| 294 | */ |
||
| 295 | 11 | public function setName($name) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Get dot notation key for fields |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | **/ |
||
| 307 | 44 | public function getNameKey() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Get field options |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | 10 | public function getOptions() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get single option from options array. Can be used with dot notation ('attr.class') |
||
| 324 | * |
||
| 325 | * @param $option |
||
| 326 | * @param mixed $default |
||
| 327 | * |
||
| 328 | * @return mixed |
||
| 329 | */ |
||
| 330 | 76 | public function getOption($option, $default = null) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Set field options |
||
| 337 | * |
||
| 338 | * @param array $options |
||
| 339 | * @return $this |
||
| 340 | */ |
||
| 341 | 11 | public function setOptions($options) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Set single option on the field |
||
| 350 | * |
||
| 351 | * @param string $name |
||
| 352 | * @param mixed $value |
||
| 353 | * @return $this |
||
| 354 | */ |
||
| 355 | 76 | public function setOption($name, $value) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Get the type of the field |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | 43 | public function getType() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Set type of the field |
||
| 374 | * |
||
| 375 | * @param mixed $type |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | 1 | public function setType($type) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * @return Form |
||
| 389 | */ |
||
| 390 | 76 | public function getParent() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Check if the field is rendered |
||
| 397 | * |
||
| 398 | * @return bool |
||
| 399 | */ |
||
| 400 | 4 | public function isRendered() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Default options for field |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | 56 | protected function getDefaults() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Defaults used across all fields |
||
| 417 | * |
||
| 418 | * @return array |
||
| 419 | */ |
||
| 420 | 76 | private function allDefaults() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Get real name of the field without form namespace |
||
| 442 | * |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | 75 | public function getRealName() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * @param $value |
||
| 452 | * @return $this |
||
| 453 | */ |
||
| 454 | 70 | public function setValue($value) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Set the template property on the object |
||
| 477 | */ |
||
| 478 | 76 | private function setTemplate() |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Add error class to wrapper if validation errors exist |
||
| 485 | */ |
||
| 486 | 76 | protected function addErrorClass() |
|
| 500 | |||
| 501 | |||
| 502 | /** |
||
| 503 | * Merge all defaults with field specific defaults and set template if passed |
||
| 504 | * |
||
| 505 | * @param array $options |
||
| 506 | */ |
||
| 507 | 76 | protected function setDefaultOptions(array $options = []) |
|
| 513 | |||
| 514 | 76 | protected function setupLabel() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Check if fields needs label |
||
| 531 | * |
||
| 532 | * @return bool |
||
| 533 | */ |
||
| 534 | 30 | protected function needsLabel() |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Disable field |
||
| 548 | * |
||
| 549 | * @return $this |
||
| 550 | */ |
||
| 551 | 1 | public function disable() |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Enable field |
||
| 560 | * |
||
| 561 | * @return $this |
||
| 562 | */ |
||
| 563 | 1 | public function enable() |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Get validation rules for a field if any with label for attributes |
||
| 572 | * |
||
| 573 | * @return array|null |
||
| 574 | */ |
||
| 575 | 6 | public function getValidationRules() |
|
| 601 | |||
| 602 | /** |
||
| 603 | * Get this field's attributes, probably just one. |
||
| 604 | * |
||
| 605 | * @return array |
||
| 606 | */ |
||
| 607 | public function getAllAttributes() |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Get value property |
||
| 614 | * |
||
| 615 | * @param mixed|null $default |
||
| 616 | * @return mixed |
||
| 617 | */ |
||
| 618 | public function getValue($default = null) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Get default value property |
||
| 625 | * |
||
| 626 | * @param mixed|null $default |
||
| 627 | * @return mixed |
||
| 628 | */ |
||
| 629 | 71 | public function getDefaultValue($default = null) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Check if provided value is valid for this type |
||
| 636 | * |
||
| 637 | * @return bool |
||
| 638 | */ |
||
| 639 | protected function isValidValue($value) |
||
| 643 | } |
||
| 644 |
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.