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 | 71 | public function __construct($name, $type, Form $parent, array $options = []) |
|
| 93 | { |
||
| 94 | 71 | $this->name = $name; |
|
| 95 | 71 | $this->type = $type; |
|
| 96 | 71 | $this->parent = $parent; |
|
| 97 | 71 | $this->formHelper = $this->parent->getFormHelper(); |
|
| 98 | 71 | $this->setTemplate(); |
|
| 99 | 71 | $this->setDefaultOptions($options); |
|
| 100 | 71 | $this->setupValue(); |
|
| 101 | 66 | } |
|
| 102 | |||
| 103 | 71 | protected function setupValue() |
|
| 104 | { |
||
| 105 | 71 | $value = $this->getOption($this->valueProperty); |
|
| 106 | 71 | $isChild = $this->getOption('is_child'); |
|
| 107 | |||
| 108 | 71 | if ($value instanceof \Closure) { |
|
| 109 | $this->valueClosure = $value; |
||
| 110 | } |
||
| 111 | |||
| 112 | 71 | if (($value === null || $value instanceof \Closure) && !$isChild) { |
|
| 113 | 62 | $this->setValue($this->getModelValueAttribute($this->parent->getModel(), $this->name)); |
|
| 114 | 66 | } elseif (!$isChild) { |
|
| 115 | 12 | $this->hasDefault = true; |
|
| 116 | 12 | } |
|
| 117 | 66 | } |
|
| 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 | 27 | 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 | 27 | 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 | 64 | protected function getModelValueAttribute($model, $name) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Transform array like syntax to dot syntax |
||
| 199 | * |
||
| 200 | * @param $key |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | 71 | protected function transformKey($key) |
|
| 204 | { |
||
| 205 | 71 | return $this->formHelper->transformToDotSyntax($key); |
|
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Prepare options for rendering |
||
| 210 | * |
||
| 211 | * @param array $options |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | 71 | protected function prepareOptions(array $options = []) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Get name of the field |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | 23 | public function getName() |
|
| 272 | { |
||
| 273 | 23 | return $this->name; |
|
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set name of the field |
||
| 278 | * |
||
| 279 | * @param string $name |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | 11 | public function setName($name) |
|
| 283 | { |
||
| 284 | 11 | $this->name = $name; |
|
| 285 | |||
| 286 | 11 | return $this; |
|
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get dot notation key for fields |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | **/ |
||
| 294 | 39 | public function getNameKey() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Get field options |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | 10 | public function getOptions() |
|
| 305 | { |
||
| 306 | 10 | return $this->options; |
|
| 307 | } |
||
| 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 | 71 | public function getOption($option, $default = null) |
|
| 318 | { |
||
| 319 | 71 | return array_get($this->options, $option, $default); |
|
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set field options |
||
| 324 | * |
||
| 325 | * @param array $options |
||
| 326 | * @return $this |
||
| 327 | */ |
||
| 328 | 11 | public function setOptions($options) |
|
| 329 | { |
||
| 330 | 11 | $this->options = $this->prepareOptions($options); |
|
| 331 | |||
| 332 | 11 | return $this; |
|
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Set single option on the field |
||
| 337 | * |
||
| 338 | * @param string $name |
||
| 339 | * @param mixed $value |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | 71 | public function setOption($name, $value) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Get the type of the field |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | 41 | public function getType() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Set type of the field |
||
| 361 | * |
||
| 362 | * @param mixed $type |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | 1 | public function setType($type) |
|
| 366 | { |
||
| 367 | 1 | if ($this->formHelper->getFieldType($type)) { |
|
| 368 | 1 | $this->type = $type; |
|
| 369 | 1 | } |
|
| 370 | |||
| 371 | 1 | return $this; |
|
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @return Form |
||
| 376 | */ |
||
| 377 | 71 | public function getParent() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Check if the field is rendered |
||
| 384 | * |
||
| 385 | * @return bool |
||
| 386 | */ |
||
| 387 | 4 | public function isRendered() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Default options for field |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | 51 | protected function getDefaults() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Defaults used across all fields |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | 71 | private function allDefaults() |
|
| 408 | { |
||
| 409 | return [ |
||
| 410 | 71 | 'wrapper' => ['class' => $this->formHelper->getConfig('defaults.wrapper_class')], |
|
| 411 | 71 | 'attr' => ['class' => $this->formHelper->getConfig('defaults.field_class')], |
|
| 412 | 71 | 'help_block' => ['text' => null, 'tag' => 'p', 'attr' => [ |
|
| 413 | 71 | 'class' => $this->formHelper->getConfig('defaults.help_block_class') |
|
| 414 | 71 | ]], |
|
| 415 | 71 | 'value' => null, |
|
| 416 | 71 | 'default_value' => null, |
|
| 417 | 71 | 'label' => null, |
|
| 418 | 71 | 'label_show' => true, |
|
| 419 | 71 | 'is_child' => false, |
|
| 420 | 71 | 'label_attr' => ['class' => $this->formHelper->getConfig('defaults.label_class')], |
|
| 421 | 71 | 'errors' => ['class' => $this->formHelper->getConfig('defaults.error_class')], |
|
| 422 | 71 | 'rules' => [], |
|
| 423 | 71 | 'error_messages' => [] |
|
| 424 | 71 | ]; |
|
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Get real name of the field without form namespace |
||
| 429 | * |
||
| 430 | * @return string |
||
| 431 | */ |
||
| 432 | 70 | public function getRealName() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @param $value |
||
| 439 | * @return $this |
||
| 440 | */ |
||
| 441 | 65 | public function setValue($value) |
|
| 442 | { |
||
| 443 | 65 | if ($this->hasDefault) { |
|
| 444 | 1 | return $this; |
|
| 445 | } |
||
| 446 | |||
| 447 | 65 | $closure = $this->valueClosure; |
|
| 448 | |||
| 449 | 65 | if ($closure instanceof \Closure) { |
|
| 450 | $value = $closure($value ?: null); |
||
| 451 | } |
||
| 452 | |||
| 453 | 65 | if (!$this->isValidValue($value)) { |
|
| 454 | 63 | $value = $this->getOption($this->defaultValueProperty); |
|
| 455 | 63 | } |
|
| 456 | |||
| 457 | 65 | $this->options[$this->valueProperty] = $value; |
|
| 458 | |||
| 459 | 65 | return $this; |
|
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Set the template property on the object |
||
| 464 | */ |
||
| 465 | 71 | private function setTemplate() |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Add error class to wrapper if validation errors exist |
||
| 472 | */ |
||
| 473 | 71 | protected function addErrorClass() |
|
| 474 | { |
||
| 475 | 71 | $errors = $this->parent->getRequest()->session()->get('errors'); |
|
| 476 | |||
| 477 | 71 | if ($errors && $errors->has($this->getNameKey())) { |
|
| 478 | $errorClass = $this->formHelper->getConfig('defaults.wrapper_error_class'); |
||
| 479 | $wrapperClass = $this->getOption('wrapper.class'); |
||
| 480 | |||
| 481 | if ($this->getOption('wrapper') && !str_contains($wrapperClass, $errorClass)) { |
||
| 482 | $wrapperClass .= ' ' . $errorClass; |
||
| 483 | $this->setOption('wrapper.class', $wrapperClass); |
||
| 484 | } |
||
| 485 | } |
||
| 486 | 71 | } |
|
| 487 | |||
| 488 | |||
| 489 | /** |
||
| 490 | * Merge all defaults with field specific defaults and set template if passed |
||
| 491 | * |
||
| 492 | * @param array $options |
||
| 493 | */ |
||
| 494 | 71 | protected function setDefaultOptions(array $options = []) |
|
| 500 | |||
| 501 | 71 | protected function setupLabel() |
|
| 502 | { |
||
| 503 | 71 | if ($this->getOption('label') !== null) { |
|
| 504 | 18 | return; |
|
| 505 | } |
||
| 506 | |||
| 507 | 69 | if ($langName = $this->parent->getLanguageName()) { |
|
| 508 | 4 | $label = sprintf('%s.%s', $langName, $this->getRealName()); |
|
| 509 | 4 | } else { |
|
| 510 | 66 | $label = $this->getRealName(); |
|
| 511 | } |
||
| 512 | |||
| 513 | 69 | $this->setOption('label', $this->formHelper->formatLabel($label)); |
|
| 514 | 69 | } |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Check if fields needs label |
||
| 518 | * |
||
| 519 | * @return bool |
||
| 520 | */ |
||
| 521 | 27 | protected function needsLabel() |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Disable field |
||
| 535 | * |
||
| 536 | * @return $this |
||
| 537 | */ |
||
| 538 | 1 | public function disable() |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Enable field |
||
| 547 | * |
||
| 548 | * @return $this |
||
| 549 | */ |
||
| 550 | 1 | 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 | 4 | public function getValidationRules() |
|
| 563 | { |
||
| 564 | 4 | $rules = $this->getOption('rules', []); |
|
| 565 | 4 | $name = $this->getNameKey(); |
|
| 566 | 4 | $messages = $this->getOption('error_messages', []); |
|
| 567 | 4 | $formName = $this->parent->getName(); |
|
| 568 | |||
| 569 | 4 | if ($messages && $formName) { |
|
| 570 | 1 | $newMessages = []; |
|
| 571 | 1 | foreach ($messages as $messageKey => $message) { |
|
| 572 | 1 | $messageKey = sprintf('%s.%s', $formName, $messageKey); |
|
| 573 | 1 | $newMessages[$messageKey] = $message; |
|
| 574 | 1 | } |
|
| 575 | 1 | $messages = $newMessages; |
|
| 576 | 1 | } |
|
| 577 | |||
| 578 | 4 | if (!$rules) { |
|
| 579 | 1 | return []; |
|
| 580 | } |
||
| 581 | |||
| 582 | return [ |
||
| 583 | 4 | 'rules' => [$name => $rules], |
|
| 584 | 4 | 'attributes' => [$name => $this->getOption('label')], |
|
| 585 | 'error_messages' => $messages |
||
| 586 | 4 | ]; |
|
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Get value property |
||
| 591 | * |
||
| 592 | * @param mixed|null $default |
||
| 593 | * @return mixed |
||
| 594 | */ |
||
| 595 | 30 | public function getValue($default = null) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Get default value property |
||
| 602 | * |
||
| 603 | * @param mixed|null $default |
||
| 604 | * @return mixed |
||
| 605 | */ |
||
| 606 | 27 | public function getDefaultValue($default = null) |
|
| 610 | |||
| 611 | /** |
||
| 612 | * Check if provided value is valid for this type |
||
| 613 | * |
||
| 614 | * @return bool |
||
| 615 | */ |
||
| 616 | 66 | 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.