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) |
|
64 | { |
||
65 | 26 | View Code Duplication | if (is_null($offset)) { |
66 | $this->attributes[] = $value; |
||
67 | } else { |
||
68 | 26 | $this->attributes[$offset] = $value; |
|
69 | } |
||
70 | 26 | } |
|
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() |
|
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 | 2 | 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 | 19 | 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 | 3 | 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 | 5 | 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 | 5 | 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 | 1 | 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 | 1 | 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 | 1 | 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: