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 | public function __construct($name, $attributes = []) |
||
| 30 | { |
||
| 31 | $this->attributes = $attributes; |
||
| 32 | $this['name'] = $name; |
||
| 33 | //$this['key'] = $name; |
||
| 34 | //$this['name'] = isset($this['name']) ? $this['name'] : $name; |
||
| 35 | |||
| 36 | $this->characterEncoding = 'UTF-8'; |
||
| 37 | $this->default["wrapper-element"] = "p"; |
||
|
|
|||
| 38 | $this->default["br-after-label"] = true; |
||
| 39 | $this->default["escape-values"] = true; |
||
| 40 | } |
||
| 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 | public function setDefault($options) |
||
| 52 | { |
||
| 53 | $this->default = array_merge($this->default, $options); |
||
| 54 | } |
||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Implementing ArrayAccess for this->attributes |
||
| 60 | * |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | public function offsetSet($offset, $value) |
||
| 64 | { |
||
| 65 | if (is_null($offset)) { |
||
| 66 | $this->attributes[] = $value; |
||
| 67 | } else { |
||
| 68 | $this->attributes[$offset] = $value; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Implementing ArrayAccess for this->attributes |
||
| 76 | */ |
||
| 77 | public function offsetExists($offset) |
||
| 78 | { |
||
| 79 | return isset($this->attributes[$offset]); |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Implementing ArrayAccess for this->attributes |
||
| 86 | */ |
||
| 87 | public function offsetUnset($offset) |
||
| 88 | { |
||
| 89 | unset($this->attributes[$offset]); |
||
| 90 | } |
||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Implementing ArrayAccess for this->attributes |
||
| 96 | */ |
||
| 97 | public function offsetGet($offset) |
||
| 98 | { |
||
| 99 | return isset($this->attributes[$offset]) ? $this->attributes[$offset] : null; |
||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * Get id of an element. |
||
| 106 | * |
||
| 107 | * @return HTML code for the element. |
||
| 108 | */ |
||
| 109 | public function getElementId() |
||
| 110 | { |
||
| 111 | return ($this['id'] = isset($this['id']) ? $this['id'] : 'form-element-' . $this['name']); |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Get alll validation messages. |
||
| 118 | * |
||
| 119 | * @return HTML code for the element. |
||
| 120 | */ |
||
| 121 | public function getValidationMessages() |
||
| 122 | { |
||
| 123 | $messages = null; |
||
| 124 | if (isset($this['validation-messages'])) { |
||
| 125 | $message = null; |
||
| 126 | foreach ($this['validation-messages'] as $val) { |
||
| 127 | $message .= "<li>{$val}</li>\n"; |
||
| 128 | } |
||
| 129 | $messages = "<ul class='validation-message'>\n{$message}</ul>\n"; |
||
| 130 | } |
||
| 131 | return $messages; |
||
| 132 | } |
||
| 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 | public function getHTMLDetails() |
||
| 142 | { |
||
| 143 | // Add disabled to be able to disable a form element |
||
| 144 | // Add maxlength |
||
| 145 | $id = $this->getElementId(); |
||
| 146 | |||
| 147 | $class = isset($this['class']) |
||
| 148 | ? "{$this['class']}" |
||
| 149 | : null; |
||
| 150 | |||
| 151 | $validates = (isset($this['validation-pass']) && $this['validation-pass'] === false) |
||
| 152 | ? ' validation-failed' |
||
| 153 | : null; |
||
| 154 | |||
| 155 | $class = (isset($class) || isset($validates)) |
||
| 156 | ? " class='{$class}{$validates}'" |
||
| 157 | : null; |
||
| 158 | |||
| 159 | $wrapperElement = isset($this['wrapper-element']) |
||
| 160 | ? $this['wrapper-element'] |
||
| 161 | : $this->default["wrapper-element"]; |
||
| 162 | |||
| 163 | $wrapperClass = isset($this['wrapper-class']) |
||
| 164 | ? " class=\"{$this['wrapper-class']}\"" |
||
| 165 | : null; |
||
| 166 | |||
| 167 | $brAfterLabel = isset($this['br-after-label']) |
||
| 168 | ? $this['br-after-label'] |
||
| 169 | : $this->default["br-after-label"]; |
||
| 170 | |||
| 171 | $brAfterLabel = $brAfterLabel |
||
| 172 | ? "<br>" |
||
| 173 | : null; |
||
| 174 | |||
| 175 | $name = " name='{$this['name']}'"; |
||
| 176 | |||
| 177 | $label = isset($this['label']) |
||
| 178 | ? ($this['label'] . (isset($this['required']) && $this['required'] |
||
| 179 | ? "<span class='form-element-required'>*</span>" |
||
| 180 | : null)) |
||
| 181 | : null; |
||
| 182 | |||
| 183 | $autofocus = isset($this['autofocus']) && $this['autofocus'] |
||
| 184 | ? " autofocus='autofocus'" |
||
| 185 | : null; |
||
| 186 | |||
| 187 | $required = isset($this['required']) && $this['required'] |
||
| 188 | ? " required='required'" |
||
| 189 | : null; |
||
| 190 | |||
| 191 | $readonly = isset($this['readonly']) && $this['readonly'] |
||
| 192 | ? " readonly='readonly'" |
||
| 193 | : null; |
||
| 194 | |||
| 195 | $placeholder = isset($this['placeholder']) && $this['placeholder'] |
||
| 196 | ? " placeholder='{$this['placeholder']}'" |
||
| 197 | : null; |
||
| 198 | |||
| 199 | $multiple = isset($this['multiple']) && $this['multiple'] |
||
| 200 | ? " multiple" |
||
| 201 | : null; |
||
| 202 | |||
| 203 | $max = isset($this['max']) |
||
| 204 | ? " max='{$this['max']}'" |
||
| 205 | : null; |
||
| 206 | |||
| 207 | $min = isset($this['min']) |
||
| 208 | ? " min='{$this['min']}'" |
||
| 209 | : null; |
||
| 210 | |||
| 211 | $low = isset($this['low']) |
||
| 212 | ? " low='{$this['low']}'" |
||
| 213 | : null; |
||
| 214 | |||
| 215 | $high = isset($this['high']) |
||
| 216 | ? " high='{$this['high']}'" |
||
| 217 | : null; |
||
| 218 | |||
| 219 | $optimum = isset($this['optimum']) |
||
| 220 | ? " optimum='{$this['optimum']}'" |
||
| 221 | : null; |
||
| 222 | |||
| 223 | $step = isset($this['step']) |
||
| 224 | ? " step='{$this['step']}'" |
||
| 225 | : null; |
||
| 226 | |||
| 227 | $size = isset($this['size']) |
||
| 228 | ? " size='{$this['size']}'" |
||
| 229 | : null; |
||
| 230 | |||
| 231 | $text = isset($this['text']) |
||
| 232 | ? htmlentities($this['text'], ENT_QUOTES, $this->characterEncoding) |
||
| 233 | : null; |
||
| 234 | |||
| 235 | $checked = isset($this['checked']) && $this['checked'] |
||
| 236 | ? " checked='checked'" |
||
| 237 | : null; |
||
| 238 | |||
| 239 | $type = isset($this['type']) |
||
| 240 | ? " type='{$this['type']}'" |
||
| 241 | : null; |
||
| 242 | |||
| 243 | $title = isset($this['title']) |
||
| 244 | ? " title='{$this['title']}'" |
||
| 245 | : null; |
||
| 246 | |||
| 247 | $pattern = isset($this['pattern']) |
||
| 248 | ? " pattern='{$this['pattern']}'" |
||
| 249 | : null; |
||
| 250 | |||
| 251 | $description = isset($this['description']) |
||
| 252 | ? $this['description'] |
||
| 253 | : null; |
||
| 254 | |||
| 255 | $novalidate = isset($this['formnovalidate']) |
||
| 256 | ? " formnovalidate='formnovalidate'" |
||
| 257 | : null; |
||
| 258 | |||
| 259 | $onlyValue = isset($this['value']) |
||
| 260 | ? htmlentities($this['value'], ENT_QUOTES, $this->characterEncoding) |
||
| 261 | : null; |
||
| 262 | |||
| 263 | $value = isset($this['value']) |
||
| 264 | ? " value='{$onlyValue}'" |
||
| 265 | : null; |
||
| 266 | |||
| 267 | $onclick = isset($this['onclick']) |
||
| 268 | ? " onclick=\"{$this['onclick']}\"" |
||
| 269 | : null; |
||
| 270 | |||
| 271 | $maxlength = isset($this['maxlength']) |
||
| 272 | ? " maxlength='{$this['maxlength']}'" |
||
| 273 | : null; |
||
| 274 | |||
| 275 | $messages = $this->getValidationMessages(); |
||
| 276 | |||
| 277 | return [ |
||
| 278 | 'id' => $id, |
||
| 279 | 'class' => $class, |
||
| 280 | 'wrapperElement' => $wrapperElement, |
||
| 281 | 'wrapperClass' => $wrapperClass, |
||
| 282 | 'brAfterLabel' => $brAfterLabel, |
||
| 283 | 'name' => $name, |
||
| 284 | 'label' => $label, |
||
| 285 | 'autofocus' => $autofocus, |
||
| 286 | 'required' => $required, |
||
| 287 | 'readonly' => $readonly, |
||
| 288 | 'placeholder' => $placeholder, |
||
| 289 | 'multiple' => $multiple, |
||
| 290 | 'min' => $min, |
||
| 291 | 'max' => $max, |
||
| 292 | 'maxlength' => $maxlength, |
||
| 293 | 'low' => $low, |
||
| 294 | 'high' => $high, |
||
| 295 | 'step' => $step, |
||
| 296 | 'optimum' => $optimum, |
||
| 297 | 'size' => $size, |
||
| 298 | 'text' => $text, |
||
| 299 | 'checked' => $checked, |
||
| 300 | 'type' => $type, |
||
| 301 | 'title' => $title, |
||
| 302 | 'pattern' => $pattern, |
||
| 303 | 'description' => $description, |
||
| 304 | 'novalidate' => $novalidate, |
||
| 305 | 'onlyValue' => $onlyValue, |
||
| 306 | 'value' => $value, |
||
| 307 | 'onclick' => $onclick, |
||
| 308 | 'messages' => $messages, |
||
| 309 | ]; |
||
| 310 | } |
||
| 311 | |||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * Get HTML code for a element, must be implemented by each subclass. |
||
| 316 | * |
||
| 317 | * @return HTML code for the element. |
||
| 318 | */ |
||
| 319 | abstract public function getHTML(); |
||
| 320 | |||
| 321 | |||
| 322 | |||
| 323 | /** |
||
| 324 | * Validate the form element value according a ruleset. |
||
| 325 | * |
||
| 326 | * @param array $rules validation rules. |
||
| 327 | * @param Form|null $form the parent form. |
||
| 328 | * |
||
| 329 | * @return boolean true if all rules pass, else false. |
||
| 330 | */ |
||
| 331 | public function validate($rules, $form = null) |
||
| 413 | |||
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * Use the element name as label if label is not set. |
||
| 418 | * |
||
| 419 | * @param string $append a colon as default to the end of the label. |
||
| 420 | * |
||
| 421 | * @return void |
||
| 422 | */ |
||
| 423 | public function useNameAsDefaultLabel($append = ':') |
||
| 429 | |||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Use the element name as value if value is not set. |
||
| 434 | * |
||
| 435 | * @return void |
||
| 436 | */ |
||
| 437 | public function useNameAsDefaultValue() |
||
| 443 | |||
| 444 | |||
| 445 | |||
| 446 | /** |
||
| 447 | * Get the value of the form element. |
||
| 448 | * |
||
| 449 | * @deprecated |
||
| 450 | * |
||
| 451 | * @return mixed the value of the form element. |
||
| 452 | */ |
||
| 453 | public function getValue() |
||
| 457 | |||
| 458 | |||
| 459 | |||
| 460 | /** |
||
| 461 | * Get the escaped value of the form element. |
||
| 462 | * |
||
| 463 | * @return mixed the value of the form element. |
||
| 464 | */ |
||
| 465 | public function getEscapedValue() |
||
| 469 | |||
| 470 | |||
| 471 | |||
| 472 | /** |
||
| 473 | * Get the unescaped value of the form element. |
||
| 474 | * |
||
| 475 | * @return mixed the value of the form element. |
||
| 476 | */ |
||
| 477 | public function getRawValue() |
||
| 481 | |||
| 482 | |||
| 483 | |||
| 484 | /** |
||
| 485 | * Get the value of the form element and respect configuration |
||
| 486 | * details whether it should be raw or escaped. |
||
| 487 | * |
||
| 488 | * @return mixed the value of the form element. |
||
| 489 | */ |
||
| 490 | public function value() |
||
| 500 | |||
| 501 | |||
| 502 | |||
| 503 | /** |
||
| 504 | * Get the escaped value of the form element and respect configuration |
||
| 505 | * details whether it should be raw or escaped. |
||
| 506 | * |
||
| 507 | * @return mixed the value of the form element. |
||
| 508 | */ |
||
| 509 | public function escValue() |
||
| 513 | |||
| 514 | |||
| 515 | |||
| 516 | /** |
||
| 517 | * Get the raw value of the form element. |
||
| 518 | * |
||
| 519 | * @return mixed the value of the form element. |
||
| 520 | */ |
||
| 521 | public function rawValue() |
||
| 525 | |||
| 526 | |||
| 527 | |||
| 528 | /** |
||
| 529 | * Set the value for the element. |
||
| 530 | * |
||
| 531 | * @param mixed $value set this to be the value of the formelement. |
||
| 532 | * |
||
| 533 | * @return mixed the value of the form element. |
||
| 534 | */ |
||
| 535 | public function setValue($value) |
||
| 539 | |||
| 540 | |||
| 541 | |||
| 542 | /** |
||
| 543 | * Get the status of the form element if it is checked or not. |
||
| 544 | * |
||
| 545 | * @return mixed the value of the form element. |
||
| 546 | */ |
||
| 547 | public function checked() |
||
| 551 | |||
| 552 | |||
| 553 | |||
| 554 | /** |
||
| 555 | * Check if the element is a button. |
||
| 556 | * |
||
| 557 | * @return boolean true or false. |
||
| 558 | */ |
||
| 559 | public function isButton() |
||
| 563 | } |
||
| 564 |
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: