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 | 79 | public function __construct($name, $type, Form $parent, array $options = []) |
|
| 93 | { |
||
| 94 | 79 | $this->name = $name; |
|
| 95 | 79 | $this->type = $type; |
|
| 96 | 79 | $this->parent = $parent; |
|
| 97 | 79 | $this->formHelper = $this->parent->getFormHelper(); |
|
| 98 | 79 | $this->setTemplate(); |
|
| 99 | 79 | $this->setDefaultOptions($options); |
|
| 100 | 79 | $this->setupValue(); |
|
| 101 | 74 | } |
|
| 102 | |||
| 103 | 79 | protected function setupValue() |
|
| 104 | { |
||
| 105 | 79 | $value = $this->getOption($this->valueProperty); |
|
| 106 | 79 | $isChild = $this->getOption('is_child'); |
|
| 107 | |||
| 108 | 79 | if ($value instanceof \Closure) { |
|
| 109 | $this->valueClosure = $value; |
||
| 110 | } |
||
| 111 | |||
| 112 | 79 | if (($value === null || $value instanceof \Closure) && !$isChild) { |
|
| 113 | 70 | $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name)); |
|
| 114 | 74 | } elseif (!$isChild) { |
|
| 115 | 12 | $this->hasDefault = true; |
|
| 116 | 12 | } |
|
| 117 | 74 | } |
|
| 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 | 30 | } |
|
| 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 | 8 | } |
|
| 159 | |||
| 160 | 30 | if ($showError) { |
|
| 161 | 29 | $showError = $this->parent->haveErrorsEnabled(); |
|
| 162 | 29 | } |
|
| 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 | 'showError' => $showError |
||
| 174 | 30 | ] |
|
| 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 | 72 | protected function getModelValueAttribute($model, $name) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Transform array like syntax to dot syntax |
||
| 199 | * |
||
| 200 | * @param $key |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | 79 | protected function transformKey($key) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Prepare options for rendering |
||
| 210 | * |
||
| 211 | * @param array $options |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | 79 | protected function prepareOptions(array $options = []) |
|
| 215 | { |
||
| 216 | 79 | $helper = $this->formHelper; |
|
| 217 | 79 | $rulesParser = new RulesParser($this); |
|
| 218 | 79 | $rules = $this->getOption('rules'); |
|
| 219 | 79 | $parsedRules = $rules ? $rulesParser->parse($rules) : []; |
|
| 220 | |||
| 221 | 79 | $this->options = $helper->mergeOptions($this->options, $options); |
|
| 222 | |||
| 223 | 79 | foreach (['attr', 'label_attr', 'wrapper'] as $appendable) { |
|
| 224 | // Append values to the 'class' attribute |
||
| 225 | 79 | 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 | 3 | $this->setOption("{$appendable}.class_append", null); |
|
| 233 | 3 | } |
|
| 234 | 79 | } |
|
| 235 | |||
| 236 | 79 | if ($this->getOption('attr.multiple') && !$this->getOption('tmp.multipleBracesSet')) { |
|
| 237 | 2 | $this->name = $this->name.'[]'; |
|
| 238 | 2 | $this->setOption('tmp.multipleBracesSet', true); |
|
| 239 | 2 | } |
|
| 240 | |||
| 241 | 79 | if ($this->parent->haveErrorsEnabled()) { |
|
| 242 | 79 | $this->addErrorClass(); |
|
| 243 | 79 | } |
|
| 244 | |||
| 245 | 79 | if ($this->parent->clientValidationEnabled()) { |
|
| 246 | 79 | if ($this->getOption('required') === true || isset($parsedRules['required'])) { |
|
| 247 | 3 | $lblClass = $this->getOption('label_attr.class', ''); |
|
| 248 | 3 | $requiredClass = $helper->getConfig('defaults.required_class', 'required'); |
|
| 249 | 3 | if (!str_contains($lblClass, $requiredClass)) { |
|
| 250 | 3 | $lblClass .= ' ' . $requiredClass; |
|
| 251 | 3 | $this->setOption('label_attr.class', $lblClass); |
|
| 252 | 3 | $this->setOption('attr.required', 'required'); |
|
| 253 | 3 | } |
|
| 254 | 3 | } |
|
| 255 | |||
| 256 | 79 | if ($parsedRules) { |
|
|
|
|||
| 257 | 1 | $attrs = $this->getOption('attr') + $parsedRules; |
|
| 258 | 1 | $this->setOption('attr', $attrs); |
|
| 259 | 1 | } |
|
| 260 | 79 | } |
|
| 261 | |||
| 262 | 79 | $this->setOption('wrapperAttrs', $helper->prepareAttributes($this->getOption('wrapper'))); |
|
| 263 | 79 | $this->setOption('errorAttrs', $helper->prepareAttributes($this->getOption('errors'))); |
|
| 264 | |||
| 265 | 79 | if ($this->getOption('is_child')) { |
|
| 266 | 16 | $this->setOption('labelAttrs', $helper->prepareAttributes($this->getOption('label_attr'))); |
|
| 267 | 16 | } |
|
| 268 | |||
| 269 | 79 | if ($this->getOption('help_block.text')) { |
|
| 270 | 1 | $this->setOption( |
|
| 271 | 1 | 'help_block.helpBlockAttrs', |
|
| 272 | 1 | $helper->prepareAttributes($this->getOption('help_block.attr')) |
|
| 273 | 1 | ); |
|
| 274 | 1 | } |
|
| 275 | |||
| 276 | 79 | return $this->options; |
|
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get name of the field |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | 24 | 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 | 45 | public function getNameKey() |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Get field options |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | 11 | 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 | 79 | 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 | 79 | public function setOption($name, $value) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Get the type of the field |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | 46 | 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) |
|
| 379 | { |
||
| 380 | 1 | if ($this->formHelper->getFieldType($type)) { |
|
| 381 | 1 | $this->type = $type; |
|
| 382 | 1 | } |
|
| 383 | |||
| 384 | 1 | return $this; |
|
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return Form |
||
| 389 | */ |
||
| 390 | 79 | 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 | 59 | protected function getDefaults() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Defaults used across all fields |
||
| 417 | * |
||
| 418 | * @return array |
||
| 419 | */ |
||
| 420 | 79 | private function allDefaults() |
|
| 421 | { |
||
| 422 | return [ |
||
| 423 | 79 | 'wrapper' => ['class' => $this->formHelper->getConfig('defaults.wrapper_class')], |
|
| 424 | 79 | 'attr' => ['class' => $this->formHelper->getConfig('defaults.field_class')], |
|
| 425 | 79 | 'help_block' => ['text' => null, 'tag' => 'p', 'attr' => [ |
|
| 426 | 79 | 'class' => $this->formHelper->getConfig('defaults.help_block_class') |
|
| 427 | 79 | ]], |
|
| 428 | 79 | 'value' => null, |
|
| 429 | 79 | 'default_value' => null, |
|
| 430 | 79 | 'label' => null, |
|
| 431 | 79 | 'label_show' => true, |
|
| 432 | 79 | 'is_child' => false, |
|
| 433 | 79 | 'label_attr' => ['class' => $this->formHelper->getConfig('defaults.label_class')], |
|
| 434 | 79 | 'errors' => ['class' => $this->formHelper->getConfig('defaults.error_class')], |
|
| 435 | 79 | 'rules' => [], |
|
| 436 | 79 | 'error_messages' => [] |
|
| 437 | 79 | ]; |
|
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Get real name of the field without form namespace |
||
| 442 | * |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | 78 | public function getRealName() |
|
| 446 | { |
||
| 447 | 78 | return $this->getOption('real_name', $this->name); |
|
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param $value |
||
| 452 | * @return $this |
||
| 453 | */ |
||
| 454 | 73 | public function setValue($value) |
|
| 455 | { |
||
| 456 | 73 | if ($this->hasDefault) { |
|
| 457 | 1 | return $this; |
|
| 458 | } |
||
| 459 | |||
| 460 | 73 | $closure = $this->valueClosure; |
|
| 461 | |||
| 462 | 73 | if ($closure instanceof \Closure) { |
|
| 463 | $value = $closure($value ?: null); |
||
| 464 | } |
||
| 465 | |||
| 466 | 73 | if (!$this->isValidValue($value)) { |
|
| 467 | 71 | $value = $this->getOption($this->defaultValueProperty); |
|
| 468 | 71 | } |
|
| 469 | |||
| 470 | 73 | $this->options[$this->valueProperty] = $value; |
|
| 471 | |||
| 472 | 73 | return $this; |
|
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Set the template property on the object |
||
| 477 | */ |
||
| 478 | 79 | private function setTemplate() |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Add error class to wrapper if validation errors exist |
||
| 485 | */ |
||
| 486 | 79 | protected function addErrorClass() |
|
| 487 | { |
||
| 488 | 79 | $errors = $this->parent->getRequest()->session()->get('errors'); |
|
| 489 | |||
| 490 | 79 | if ($errors && $errors->has($this->getNameKey())) { |
|
| 491 | $errorClass = $this->formHelper->getConfig('defaults.wrapper_error_class'); |
||
| 492 | $wrapperClass = $this->getOption('wrapper.class'); |
||
| 493 | |||
| 494 | if ($this->getOption('wrapper') && !str_contains($wrapperClass, $errorClass)) { |
||
| 495 | $wrapperClass .= ' ' . $errorClass; |
||
| 496 | $this->setOption('wrapper.class', $wrapperClass); |
||
| 497 | } |
||
| 498 | } |
||
| 499 | 79 | } |
|
| 500 | |||
| 501 | |||
| 502 | /** |
||
| 503 | * Merge all defaults with field specific defaults and set template if passed |
||
| 504 | * |
||
| 505 | * @param array $options |
||
| 506 | */ |
||
| 507 | 79 | protected function setDefaultOptions(array $options = []) |
|
| 508 | { |
||
| 509 | 79 | $this->options = $this->formHelper->mergeOptions($this->allDefaults(), $this->getDefaults()); |
|
| 510 | 79 | $this->options = $this->prepareOptions($options); |
|
| 511 | 79 | $this->setupLabel(); |
|
| 512 | 79 | } |
|
| 513 | |||
| 514 | 79 | protected function setupLabel() |
|
| 515 | { |
||
| 516 | 79 | if ($this->getOption('label') !== null) { |
|
| 517 | 19 | return; |
|
| 518 | } |
||
| 519 | |||
| 520 | 77 | if ($langName = $this->parent->getLanguageName()) { |
|
| 521 | 4 | $label = sprintf('%s.%s', $langName, $this->getRealName()); |
|
| 522 | 4 | } else { |
|
| 523 | 74 | $label = $this->getRealName(); |
|
| 524 | } |
||
| 525 | |||
| 526 | 77 | $this->setOption('label', $this->formHelper->formatLabel($label)); |
|
| 527 | 77 | } |
|
| 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 | 7 | public function getValidationRules() |
|
| 576 | { |
||
| 577 | 7 | $rules = $this->getOption('rules', []); |
|
| 578 | 7 | $name = $this->getNameKey(); |
|
| 579 | 7 | $messages = $this->getOption('error_messages', []); |
|
| 580 | 7 | $formName = $this->parent->getName(); |
|
| 581 | |||
| 582 | 7 | if ($messages && $formName) { |
|
| 583 | 1 | $newMessages = []; |
|
| 584 | 1 | foreach ($messages as $messageKey => $message) { |
|
| 585 | 1 | $messageKey = sprintf('%s.%s', $formName, $messageKey); |
|
| 586 | 1 | $newMessages[$messageKey] = $message; |
|
| 587 | 1 | } |
|
| 588 | 1 | $messages = $newMessages; |
|
| 589 | 1 | } |
|
| 590 | |||
| 591 | 7 | if (!$rules) { |
|
| 592 | 1 | return []; |
|
| 593 | } |
||
| 594 | |||
| 595 | return [ |
||
| 596 | 7 | 'rules' => [$name => $rules], |
|
| 597 | 7 | 'attributes' => [$name => $this->getOption('label')], |
|
| 598 | 'error_messages' => $messages |
||
| 599 | 7 | ]; |
|
| 600 | } |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Get value property |
||
| 604 | * |
||
| 605 | * @param mixed|null $default |
||
| 606 | * @return mixed |
||
| 607 | */ |
||
| 608 | 33 | public function getValue($default = null) |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Get default value property |
||
| 615 | * |
||
| 616 | * @param mixed|null $default |
||
| 617 | * @return mixed |
||
| 618 | */ |
||
| 619 | 30 | public function getDefaultValue($default = null) |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Check if provided value is valid for this type |
||
| 626 | * |
||
| 627 | * @return bool |
||
| 628 | */ |
||
| 629 | 74 | protected function isValidValue($value) |
|
| 633 | } |
||
| 634 |
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.