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 | public function __construct($name, $type, Form $parent, array $options = []) |
||
| 93 | { |
||
| 94 | $this->name = $name; |
||
| 95 | $this->type = $type; |
||
| 96 | $this->parent = $parent; |
||
| 97 | $this->formHelper = $this->parent->getFormHelper(); |
||
| 98 | $this->setTemplate(); |
||
| 99 | $this->setDefaultOptions($options); |
||
| 100 | $this->setupValue(); |
||
| 101 | } |
||
| 102 | |||
| 103 | protected function setupValue() |
||
| 104 | { |
||
| 105 | $value = $this->getOption($this->valueProperty); |
||
| 106 | $isChild = $this->getOption('is_child'); |
||
| 107 | |||
| 108 | if ($value instanceof \Closure) { |
||
| 109 | $this->valueClosure = $value; |
||
| 110 | } |
||
| 111 | |||
| 112 | if (($value === null || $value instanceof \Closure) && !$isChild) { |
||
| 113 | $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name)); |
||
| 114 | } elseif (!$isChild) { |
||
| 115 | $this->hasDefault = true; |
||
| 116 | } |
||
| 117 | } |
||
| 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 | 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 | public function render(array $options = [], $showLabel = true, $showField = true, $showError = true) |
||
| 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 | protected function getModelValueAttribute($model, $name) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Transform array like syntax to dot syntax |
||
| 199 | * |
||
| 200 | * @param $key |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | protected function transformKey($key) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Prepare options for rendering |
||
| 210 | * |
||
| 211 | * @param array $options |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | protected function prepareOptions(array $options = []) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get name of the field |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function getName() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set name of the field |
||
| 278 | * |
||
| 279 | * @param string $name |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | public function setName($name) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get dot notation key for fields |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | **/ |
||
| 294 | public function getNameKey() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get field options |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function getOptions() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get single option from options array. Can be used with dot notation ('attr.class') |
||
| 311 | * |
||
| 312 | * @param $option |
||
| 313 | * @param mixed $default |
||
| 314 | * |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | public function getOption($option, $default = null) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set field options |
||
| 324 | * |
||
| 325 | * @param array $options |
||
| 326 | * @return $this |
||
| 327 | */ |
||
| 328 | public function setOptions($options) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Set single option on the field |
||
| 337 | * |
||
| 338 | * @param string $name |
||
| 339 | * @param mixed $value |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | public function setOption($name, $value) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get the type of the field |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function getType() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set type of the field |
||
| 361 | * |
||
| 362 | * @param mixed $type |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | public function setType($type) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @return Form |
||
| 376 | */ |
||
| 377 | public function getParent() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Check if the field is rendered |
||
| 384 | * |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | public function isRendered() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Default options for field |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | protected function getDefaults() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Defaults used across all fields |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | private function allDefaults() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get real name of the field without form namespace |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | public function getRealName() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @param $value |
||
| 439 | * @return $this |
||
| 440 | */ |
||
| 441 | public function setValue($value) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Set the template property on the object |
||
| 464 | */ |
||
| 465 | private function setTemplate() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Add error class to wrapper if validation errors exist |
||
| 472 | */ |
||
| 473 | protected function addErrorClass() |
||
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * Merge all defaults with field specific defaults and set template if passed |
||
| 491 | * |
||
| 492 | * @param array $options |
||
| 493 | */ |
||
| 494 | protected function setDefaultOptions(array $options = []) |
||
| 500 | |||
| 501 | protected function setupLabel() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Check if fields needs label |
||
| 518 | * |
||
| 519 | * @return bool |
||
| 520 | */ |
||
| 521 | protected function needsLabel() |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Disable field |
||
| 535 | * |
||
| 536 | * @return $this |
||
| 537 | */ |
||
| 538 | public function disable() |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Enable field |
||
| 547 | * |
||
| 548 | * @return $this |
||
| 549 | */ |
||
| 550 | public function enable() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Get validation rules for a field if any with label for attributes |
||
| 559 | * |
||
| 560 | * @return array|null |
||
| 561 | */ |
||
| 562 | public function getValidationRules() |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Get value property |
||
| 591 | * |
||
| 592 | * @param mixed|null $default |
||
| 593 | * @return mixed |
||
| 594 | */ |
||
| 595 | public function getValue($default = null) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Get default value property |
||
| 602 | * |
||
| 603 | * @param mixed|null $default |
||
| 604 | * @return mixed |
||
| 605 | */ |
||
| 606 | public function getDefaultValue($default = null) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Check if provided value is valid for this type |
||
| 613 | * |
||
| 614 | * @return bool |
||
| 615 | */ |
||
| 616 | protected function isValidValue($value) |
||
| 620 | } |
||
| 621 |
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.