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 | 81 | public function __construct($name, $type, Form $parent, array $options = []) |
|
| 93 | { |
||
| 94 | 81 | $this->name = $name; |
|
| 95 | 81 | $this->type = $type; |
|
| 96 | 81 | $this->parent = $parent; |
|
| 97 | 81 | $this->formHelper = $this->parent->getFormHelper(); |
|
| 98 | 81 | $this->setTemplate(); |
|
| 99 | 81 | $this->setDefaultOptions($options); |
|
| 100 | 81 | $this->setupValue(); |
|
| 101 | 76 | } |
|
| 102 | |||
| 103 | 81 | protected function setupValue() |
|
| 104 | { |
||
| 105 | 81 | $value = $this->getOption($this->valueProperty); |
|
| 106 | 81 | $isChild = $this->getOption('is_child'); |
|
| 107 | |||
| 108 | 81 | if ($value instanceof \Closure) { |
|
| 109 | $this->valueClosure = $value; |
||
| 110 | } |
||
| 111 | |||
| 112 | 81 | if (($value === null || $value instanceof \Closure) && !$isChild) { |
|
| 113 | 72 | $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name)); |
|
| 114 | 18 | } elseif (!$isChild) { |
|
| 115 | 12 | $this->hasDefault = true; |
|
| 116 | } |
||
| 117 | 76 | } |
|
| 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() |
|
| 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) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Get the attribute value from the model by name |
||
| 182 | * |
||
| 183 | * @param mixed $model |
||
| 184 | * @param string $name |
||
| 185 | * @return mixed |
||
| 186 | */ |
||
| 187 | 74 | protected function getModelValueAttribute($model, $name) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Transform array like syntax to dot syntax |
||
| 201 | * |
||
| 202 | * @param $key |
||
| 203 | * @return mixed |
||
| 204 | */ |
||
| 205 | 81 | protected function transformKey($key) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Prepare options for rendering |
||
| 212 | * |
||
| 213 | * @param array $options |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | 81 | protected function prepareOptions(array $options = []) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Get name of the field |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | 24 | public function getName() |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Set name of the field |
||
| 295 | * |
||
| 296 | * @param string $name |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | 11 | public function setName($name) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Get dot notation key for fields |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | **/ |
||
| 311 | 47 | public function getNameKey() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Get field options |
||
| 318 | * |
||
| 319 | * @return array |
||
| 320 | */ |
||
| 321 | 12 | public function getOptions() |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Get single option from options array. Can be used with dot notation ('attr.class') |
||
| 328 | * |
||
| 329 | * @param $option |
||
| 330 | * @param mixed $default |
||
| 331 | * |
||
| 332 | * @return mixed |
||
| 333 | */ |
||
| 334 | 81 | public function getOption($option, $default = null) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Set field options |
||
| 341 | * |
||
| 342 | * @param array $options |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | 11 | public function setOptions($options) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Set single option on the field |
||
| 354 | * |
||
| 355 | * @param string $name |
||
| 356 | * @param mixed $value |
||
| 357 | * @return $this |
||
| 358 | */ |
||
| 359 | 81 | public function setOption($name, $value) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Get the type of the field |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | 48 | public function getType() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Set type of the field |
||
| 378 | * |
||
| 379 | * @param mixed $type |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | 1 | public function setType($type) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @return Form |
||
| 393 | */ |
||
| 394 | 81 | public function getParent() |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Check if the field is rendered |
||
| 401 | * |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | 4 | public function isRendered() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Default options for field |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | 61 | protected function getDefaults() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Defaults used across all fields |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | 81 | private function allDefaults() |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Get real name of the field without form namespace |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | 80 | public function getRealName() |
|
| 453 | |||
| 454 | /** |
||
| 455 | * @param $value |
||
| 456 | * @return $this |
||
| 457 | */ |
||
| 458 | 75 | public function setValue($value) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Set the template property on the object |
||
| 481 | */ |
||
| 482 | 81 | private function setTemplate() |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Add error class to wrapper if validation errors exist |
||
| 489 | */ |
||
| 490 | 81 | protected function addErrorClass() |
|
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * Merge all defaults with field specific defaults and set template if passed |
||
| 508 | * |
||
| 509 | * @param array $options |
||
| 510 | */ |
||
| 511 | 81 | protected function setDefaultOptions(array $options = []) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Creates default wrapper classes for the form element. |
||
| 524 | * |
||
| 525 | * @param array $options |
||
| 526 | * @return array |
||
| 527 | */ |
||
| 528 | 81 | protected function setDefaultClasses(array $options = []) |
|
| 546 | |||
| 547 | 81 | protected function setupLabel() |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Check if fields needs label |
||
| 564 | * |
||
| 565 | * @return bool |
||
| 566 | */ |
||
| 567 | 31 | protected function needsLabel() |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Disable field |
||
| 581 | * |
||
| 582 | * @return $this |
||
| 583 | */ |
||
| 584 | 1 | public function disable() |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Enable field |
||
| 593 | * |
||
| 594 | * @return $this |
||
| 595 | */ |
||
| 596 | 1 | public function enable() |
|
| 602 | |||
| 603 | /** |
||
| 604 | * Get validation rules for a field if any with label for attributes |
||
| 605 | * |
||
| 606 | * @return array|null |
||
| 607 | */ |
||
| 608 | 7 | public function getValidationRules() |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Get this field's attributes, probably just one. |
||
| 637 | * |
||
| 638 | * @return array |
||
| 639 | */ |
||
| 640 | 1 | public function getAllAttributes() |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Get value property |
||
| 647 | * |
||
| 648 | * @param mixed|null $default |
||
| 649 | * @return mixed |
||
| 650 | */ |
||
| 651 | 34 | public function getValue($default = null) |
|
| 655 | |||
| 656 | /** |
||
| 657 | * Get default value property |
||
| 658 | * |
||
| 659 | * @param mixed|null $default |
||
| 660 | * @return mixed |
||
| 661 | */ |
||
| 662 | 31 | public function getDefaultValue($default = null) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * Check if provided value is valid for this type |
||
| 669 | * |
||
| 670 | * @return bool |
||
| 671 | */ |
||
| 672 | 76 | protected function isValidValue($value) |
|
| 676 | } |
||
| 677 |
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.