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 | 21 | public function __construct($name, $attributes = []) |
|
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Set default values to use, merge incoming with existing. |
||
| 45 | * |
||
| 46 | * @param array $options key value array with settings to use. |
||
| 47 | * |
||
| 48 | * @return void |
||
| 49 | */ |
||
| 50 | public function setDefault($options) |
||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Implementing ArrayAccess for this->attributes |
||
| 59 | * |
||
| 60 | * @return void |
||
| 61 | */ |
||
| 62 | 21 | public function offsetSet($offset, $value) |
|
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Implementing ArrayAccess for this->attributes |
||
| 75 | */ |
||
| 76 | 21 | public function offsetExists($offset) |
|
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * Implementing ArrayAccess for this->attributes |
||
| 85 | */ |
||
| 86 | public function offsetUnset($offset) |
||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | /** |
||
| 94 | * Implementing ArrayAccess for this->attributes |
||
| 95 | */ |
||
| 96 | 20 | public function offsetGet($offset) |
|
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Get id of an element. |
||
| 105 | * |
||
| 106 | * @return HTML code for the element. |
||
| 107 | */ |
||
| 108 | 9 | public function getElementId() |
|
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Get alll validation messages. |
||
| 117 | * |
||
| 118 | * @return HTML code for the element. |
||
| 119 | */ |
||
| 120 | 9 | public function getValidationMessages() |
|
| 132 | |||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * Get details for a HTML element, prepare for creating HTML code for it. |
||
| 137 | * |
||
| 138 | * @return HTML code for the element. |
||
| 139 | */ |
||
| 140 | 9 | public function getHTMLDetails() |
|
| 305 | |||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * Get HTML code for a element, must be implemented by each subclass. |
||
| 310 | * |
||
| 311 | * @return HTML code for the element. |
||
| 312 | */ |
||
| 313 | abstract public function getHTML(); |
||
| 314 | |||
| 315 | |||
| 316 | |||
| 317 | /** |
||
| 318 | * Validate the form element value according a ruleset. |
||
| 319 | * |
||
| 320 | * @param array $rules validation rules. |
||
| 321 | * @param Form|null $form the parent form. |
||
| 322 | * |
||
| 323 | * @return boolean true if all rules pass, else false. |
||
| 324 | */ |
||
| 325 | 2 | public function validate($rules, $form = null) |
|
| 407 | |||
| 408 | |||
| 409 | |||
| 410 | /** |
||
| 411 | * Use the element name as label if label is not set. |
||
| 412 | * |
||
| 413 | * @param string $append a colon as default to the end of the label. |
||
| 414 | * |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | 14 | public function useNameAsDefaultLabel($append = ':') |
|
| 423 | |||
| 424 | |||
| 425 | |||
| 426 | /** |
||
| 427 | * Use the element name as value if value is not set. |
||
| 428 | * |
||
| 429 | * @return void |
||
| 430 | */ |
||
| 431 | 3 | public function useNameAsDefaultValue() |
|
| 437 | |||
| 438 | |||
| 439 | |||
| 440 | /** |
||
| 441 | * Get the value of the form element. |
||
| 442 | * |
||
| 443 | * @return mixed the value of the form element. |
||
| 444 | */ |
||
| 445 | 6 | public function getValue() |
|
| 449 | |||
| 450 | |||
| 451 | |||
| 452 | /** |
||
| 453 | * Get the value of the form element. |
||
| 454 | * |
||
| 455 | * @return mixed the value of the form element. |
||
| 456 | */ |
||
| 457 | public function value() |
||
| 461 | |||
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * Get the status of the form element if it is checked or not. |
||
| 466 | * |
||
| 467 | * @return mixed the value of the form element. |
||
| 468 | */ |
||
| 469 | public function checked() |
||
| 473 | } |
||
| 474 |
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: