Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FormElement 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 FormElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | abstract class FormElement implements \ArrayAccess |
||
| 9 | { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @var array $attributes settings to use to create element |
||
| 13 | * @var array $config default settings to use to create element |
||
| 14 | * @var array $characterEncoding setting for character encoding |
||
| 15 | */ |
||
| 16 | public $attributes; |
||
| 17 | public $config; |
||
| 18 | public $characterEncoding; |
||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | /** |
||
| 23 | * Constructor creating a form element. |
||
| 24 | * |
||
| 25 | * @param string $name of the element. |
||
| 26 | * @param array $attributes to set to the element. Default is an empty |
||
| 27 | * array. |
||
| 28 | */ |
||
| 29 | 26 | public function __construct($name, $attributes = []) |
|
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Set default values to use, merge incoming with existing. |
||
| 46 | * |
||
| 47 | * @param array $options key value array with settings to use. |
||
| 48 | * |
||
| 49 | * @return void |
||
| 50 | */ |
||
| 51 | 4 | public function setDefault($options) |
|
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Implementing ArrayAccess for this->attributes |
||
| 60 | * |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | 26 | public function offsetSet($offset, $value) |
|
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Implementing ArrayAccess for this->attributes |
||
| 76 | */ |
||
| 77 | 26 | public function offsetExists($offset) |
|
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Implementing ArrayAccess for this->attributes |
||
| 86 | */ |
||
| 87 | public function offsetUnset($offset) |
||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Implementing ArrayAccess for this->attributes |
||
| 96 | */ |
||
| 97 | 25 | public function offsetGet($offset) |
|
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * Get id of an element. |
||
| 106 | * |
||
| 107 | * @return HTML code for the element. |
||
| 108 | */ |
||
| 109 | 9 | public function getElementId() |
|
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Get alll validation messages. |
||
| 118 | * |
||
| 119 | * @return HTML code for the element. |
||
| 120 | */ |
||
| 121 | 9 | public function getValidationMessages() |
|
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | /** |
||
| 137 | * Get details for a HTML element, prepare for creating HTML code for it. |
||
| 138 | * |
||
| 139 | * @return HTML code for the element. |
||
| 140 | */ |
||
| 141 | 9 | public function getHTMLDetails() |
|
| 306 | |||
| 307 | |||
| 308 | |||
| 309 | /** |
||
| 310 | * Get HTML code for a element, must be implemented by each subclass. |
||
| 311 | * |
||
| 312 | * @return HTML code for the element. |
||
| 313 | */ |
||
| 314 | abstract public function getHTML(); |
||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Validate the form element value according a ruleset. |
||
| 320 | * |
||
| 321 | * @param array $rules validation rules. |
||
| 322 | * @param Form|null $form the parent form. |
||
| 323 | * |
||
| 324 | * @return boolean true if all rules pass, else false. |
||
| 325 | */ |
||
| 326 | 2 | public function validate($rules, $form = null) |
|
| 408 | |||
| 409 | |||
| 410 | |||
| 411 | /** |
||
| 412 | * Use the element name as label if label is not set. |
||
| 413 | * |
||
| 414 | * @param string $append a colon as default to the end of the label. |
||
| 415 | * |
||
| 416 | * @return void |
||
| 417 | */ |
||
| 418 | 19 | public function useNameAsDefaultLabel($append = ':') |
|
| 424 | |||
| 425 | |||
| 426 | |||
| 427 | /** |
||
| 428 | * Use the element name as value if value is not set. |
||
| 429 | * |
||
| 430 | * @return void |
||
| 431 | */ |
||
| 432 | 3 | public function useNameAsDefaultValue() |
|
| 438 | |||
| 439 | |||
| 440 | |||
| 441 | /** |
||
| 442 | * Get the value of the form element. |
||
| 443 | * |
||
| 444 | * @deprecated |
||
| 445 | * |
||
| 446 | * @return mixed the value of the form element. |
||
| 447 | */ |
||
| 448 | public function getValue() |
||
| 452 | |||
| 453 | |||
| 454 | |||
| 455 | /** |
||
| 456 | * Get the escaped value of the form element. |
||
| 457 | * |
||
| 458 | * @return mixed the value of the form element. |
||
| 459 | */ |
||
| 460 | 5 | public function getEscapedValue() |
|
| 464 | |||
| 465 | |||
| 466 | |||
| 467 | /** |
||
| 468 | * Get the unescaped value of the form element. |
||
| 469 | * |
||
| 470 | * @return mixed the value of the form element. |
||
| 471 | */ |
||
| 472 | 5 | public function getRawValue() |
|
| 476 | |||
| 477 | |||
| 478 | |||
| 479 | /** |
||
| 480 | * Get the value of the form element and respect configuration |
||
| 481 | * details whether it should be raw or escaped. |
||
| 482 | * |
||
| 483 | * @return mixed the value of the form element. |
||
| 484 | */ |
||
| 485 | public function value() |
||
| 495 | |||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * Get the escaped value of the form element and respect configuration |
||
| 500 | * details whether it should be raw or escaped. |
||
| 501 | * |
||
| 502 | * @return mixed the value of the form element. |
||
| 503 | */ |
||
| 504 | 1 | public function escValue() |
|
| 508 | |||
| 509 | |||
| 510 | |||
| 511 | /** |
||
| 512 | * Get the raw value of the form element. |
||
| 513 | * |
||
| 514 | * @return mixed the value of the form element. |
||
| 515 | */ |
||
| 516 | 1 | public function rawValue() |
|
| 520 | |||
| 521 | |||
| 522 | |||
| 523 | /** |
||
| 524 | * Set the value for the element. |
||
| 525 | * |
||
| 526 | * @param mixed $value set this to be the value of the formelement. |
||
| 527 | * |
||
| 528 | * @return mixed the value of the form element. |
||
| 529 | */ |
||
| 530 | 1 | public function setValue($value) |
|
| 534 | |||
| 535 | |||
| 536 | |||
| 537 | /** |
||
| 538 | * Get the status of the form element if it is checked or not. |
||
| 539 | * |
||
| 540 | * @return mixed the value of the form element. |
||
| 541 | */ |
||
| 542 | public function checked() |
||
| 546 | } |
||
| 547 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: