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) |
|
| 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 | } |
||
| 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 | 9 | $showLabel = false; |
|
| 158 | } |
||
| 159 | |||
| 160 | 31 | if ($showError) { |
|
| 161 | 30 | $showError = $this->parent->haveErrorsEnabled(); |
|
| 162 | } |
||
| 163 | |||
| 164 | 31 | return $this->formHelper->getView()->make( |
|
| 165 | 31 | $this->getViewTemplate(), |
|
| 166 | [ |
||
| 167 | 31 | 'name' => $this->name, |
|
| 168 | 31 | 'nameKey' => $this->getNameKey(), |
|
| 169 | 31 | 'type' => $this->type, |
|
| 170 | 31 | 'options' => $this->options, |
|
| 171 | 31 | 'showLabel' => $showLabel, |
|
| 172 | 31 | 'showField' => $showField, |
|
| 173 | 31 | 'showError' => $showError |
|
| 174 | ] |
||
| 175 | 31 | )->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 | 74 | protected function getModelValueAttribute($model, $name) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Transform array like syntax to dot syntax |
||
| 199 | * |
||
| 200 | * @param $key |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | 81 | protected function transformKey($key) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Prepare options for rendering |
||
| 210 | * |
||
| 211 | * @param array $options |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | 81 | protected function prepareOptions(array $options = []) |
|
| 215 | { |
||
| 216 | 81 | $helper = $this->formHelper; |
|
| 217 | 81 | $rulesParser = new RulesParser($this); |
|
| 218 | 81 | $rules = $this->getOption('rules'); |
|
| 219 | 81 | $parsedRules = $rules ? $rulesParser->parse($rules) : []; |
|
| 220 | |||
| 221 | 81 | $this->options = $helper->mergeOptions($this->options, $options); |
|
| 222 | |||
| 223 | 81 | foreach (['attr', 'label_attr', 'wrapper'] as $appendable) { |
|
| 224 | // Append values to the 'class' attribute |
||
| 225 | 81 | if ($this->getOption("{$appendable}.class_append")) { |
|
| 226 | // Combine the current class attribute with the appends |
||
| 227 | 3 | $append = $this->getOption("{$appendable}.class_append"); |
|
| 228 | 3 | $classAttribute = $this->getOption("{$appendable}.class", '').' '.$append; |
|
| 229 | 3 | $this->setOption("{$appendable}.class", $classAttribute); |
|
| 230 | |||
| 231 | // Then remove the class_append option to prevent it from showing up as an attribute in the HTML |
||
| 232 | 81 | $this->setOption("{$appendable}.class_append", null); |
|
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | 81 | if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) { |
|
| 237 | 2 | $this->name = $this->name.'[]'; |
|
| 238 | 2 | $this->setOption('tmp.multipleBracesSet', true); |
|
| 239 | } |
||
| 240 | |||
| 241 | 81 | if ($this->parent->haveErrorsEnabled()) { |
|
| 242 | 81 | $this->addErrorClass(); |
|
| 243 | } |
||
| 244 | |||
| 245 | 81 | if ($this->getOption('required') === true || isset($parsedRules['required'])) { |
|
| 246 | 4 | $lblClass = $this->getOption('label_attr.class', ''); |
|
| 247 | 4 | $requiredClass = $helper->getConfig('defaults.required_class', 'required'); |
|
| 248 | |||
| 249 | 4 | if (! str_contains($lblClass, $requiredClass)) { |
|
| 250 | 4 | $lblClass .= ' '.$requiredClass; |
|
| 251 | 4 | $this->setOption('label_attr.class', $lblClass); |
|
| 252 | } |
||
| 253 | |||
| 254 | 4 | if ($this->parent->clientValidationEnabled()) { |
|
| 255 | 3 | $this->setOption('attr.required', 'required'); |
|
| 256 | |||
| 257 | 3 | if ($parsedRules) { |
|
|
|
|||
| 258 | 1 | $attrs = $this->getOption('attr') + $parsedRules; |
|
| 259 | 1 | $this->setOption('attr', $attrs); |
|
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | 81 | $this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper'))); |
|
| 265 | 81 | $this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors'))); |
|
| 266 | |||
| 267 | 81 | if ($this->getOption('is_child')) { |
|
| 268 | 16 | $this->setOption('labelAttrs', $helper->prepareAttributes($this->getOption('label_attr'))); |
|
| 269 | } |
||
| 270 | |||
| 271 | 81 | if ($this->getOption('help_block.text')) { |
|
| 272 | 1 | $this->setOption( |
|
| 273 | 1 | 'help_block.helpBlockAttrs', |
|
| 274 | 1 | $helper->prepareAttributes($this->getOption('help_block.attr')) |
|
| 275 | ); |
||
| 276 | } |
||
| 277 | |||
| 278 | 81 | return $this->options; |
|
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get name of the field |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | 24 | public function getName() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Set name of the field |
||
| 293 | * |
||
| 294 | * @param string $name |
||
| 295 | * @return $this |
||
| 296 | */ |
||
| 297 | 11 | public function setName($name) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Get dot notation key for fields |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | **/ |
||
| 309 | 47 | public function getNameKey() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Get field options |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | 12 | public function getOptions() |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Get single option from options array. Can be used with dot notation ('attr.class') |
||
| 326 | * |
||
| 327 | * @param $option |
||
| 328 | * @param mixed $default |
||
| 329 | * |
||
| 330 | * @return mixed |
||
| 331 | */ |
||
| 332 | 81 | public function getOption($option, $default = null) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Set field options |
||
| 339 | * |
||
| 340 | * @param array $options |
||
| 341 | * @return $this |
||
| 342 | */ |
||
| 343 | 11 | public function setOptions($options) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Set single option on the field |
||
| 352 | * |
||
| 353 | * @param string $name |
||
| 354 | * @param mixed $value |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | 81 | public function setOption($name, $value) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Get the type of the field |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | 48 | public function getType() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Set type of the field |
||
| 376 | * |
||
| 377 | * @param mixed $type |
||
| 378 | * @return $this |
||
| 379 | */ |
||
| 380 | 1 | public function setType($type) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @return Form |
||
| 391 | */ |
||
| 392 | 81 | public function getParent() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Check if the field is rendered |
||
| 399 | * |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | 4 | public function isRendered() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Default options for field |
||
| 409 | * |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | 61 | protected function getDefaults() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Defaults used across all fields |
||
| 419 | * |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | 81 | private function allDefaults() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Get real name of the field without form namespace |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | 80 | public function getRealName() |
|
| 451 | |||
| 452 | /** |
||
| 453 | * @param $value |
||
| 454 | * @return $this |
||
| 455 | */ |
||
| 456 | 75 | public function setValue($value) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Set the template property on the object |
||
| 479 | */ |
||
| 480 | 81 | private function setTemplate() |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Add error class to wrapper if validation errors exist |
||
| 487 | */ |
||
| 488 | 81 | protected function addErrorClass() |
|
| 502 | |||
| 503 | |||
| 504 | /** |
||
| 505 | * Merge all defaults with field specific defaults and set template if passed |
||
| 506 | * |
||
| 507 | * @param array $options |
||
| 508 | */ |
||
| 509 | 81 | protected function setDefaultOptions(array $options = []) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Creates default wrapper classes for the form element. |
||
| 522 | * |
||
| 523 | * @param array $options |
||
| 524 | * @return array |
||
| 525 | */ |
||
| 526 | 81 | protected function setDefaultClasses(array $options = []) |
|
| 544 | |||
| 545 | 81 | protected function setupLabel() |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Check if fields needs label |
||
| 562 | * |
||
| 563 | * @return bool |
||
| 564 | */ |
||
| 565 | 31 | protected function needsLabel() |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Disable field |
||
| 579 | * |
||
| 580 | * @return $this |
||
| 581 | */ |
||
| 582 | 1 | public function disable() |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Enable field |
||
| 591 | * |
||
| 592 | * @return $this |
||
| 593 | */ |
||
| 594 | 1 | public function enable() |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Get validation rules for a field if any with label for attributes |
||
| 603 | * |
||
| 604 | * @return array|null |
||
| 605 | */ |
||
| 606 | 7 | public function getValidationRules() |
|
| 632 | |||
| 633 | /** |
||
| 634 | * Get this field's attributes, probably just one. |
||
| 635 | * |
||
| 636 | * @return array |
||
| 637 | */ |
||
| 638 | 1 | public function getAllAttributes() |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Get value property |
||
| 645 | * |
||
| 646 | * @param mixed|null $default |
||
| 647 | * @return mixed |
||
| 648 | */ |
||
| 649 | 34 | public function getValue($default = null) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Get default value property |
||
| 656 | * |
||
| 657 | * @param mixed|null $default |
||
| 658 | * @return mixed |
||
| 659 | */ |
||
| 660 | 31 | public function getDefaultValue($default = null) |
|
| 664 | |||
| 665 | /** |
||
| 666 | * Check if provided value is valid for this type |
||
| 667 | * |
||
| 668 | * @return bool |
||
| 669 | */ |
||
| 670 | 76 | protected function isValidValue($value) |
|
| 674 | } |
||
| 675 |
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.