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:
1 | <?php |
||
13 | class StickyForms { |
||
14 | |||
15 | /** |
||
16 | * Save form submission data (all GET and POST vars) into a session cache |
||
17 | * |
||
18 | * Call this from an action when you want all your submitted variables |
||
19 | * available if the submission fails validation and is sent back to the form |
||
20 | * |
||
21 | * @param string $form_name Name of the sticky form |
||
22 | * |
||
23 | * @return void |
||
24 | */ |
||
25 | public function makeStickyForm($form_name) { |
||
48 | |||
49 | /** |
||
50 | * Remove form submission data from the session |
||
51 | * |
||
52 | * Call this if validation is successful in the action handler or |
||
53 | * when they sticky values have been used to repopulate the form |
||
54 | * after a validation error. |
||
55 | * |
||
56 | * @param string $form_name Form namespace |
||
57 | * |
||
58 | * @return void |
||
59 | */ |
||
60 | View Code Duplication | function clearStickyForm($form_name) { |
|
66 | |||
67 | /** |
||
68 | * Does form submission data exist for this form? |
||
69 | * |
||
70 | * @param string $form_name Form namespace |
||
71 | * |
||
72 | * @return boolean |
||
73 | */ |
||
74 | function isStickyForm($form_name) { |
||
79 | |||
80 | /** |
||
81 | * Get a specific value from cached form submission data |
||
82 | * |
||
83 | * @param string $form_name The name of the form |
||
84 | * @param string $variable The name of the variable |
||
85 | * @param mixed $default Default value if the variable does not exist in sticky cache |
||
86 | * @param boolean $filter_result Filter for bad input if true |
||
87 | * |
||
88 | * @return mixed |
||
89 | * |
||
90 | * @todo should this filter the default value? |
||
91 | */ |
||
92 | View Code Duplication | function getStickyValue($form_name, $variable = '', $default = null, $filter_result = true) { |
|
105 | |||
106 | /** |
||
107 | * Get all submission data cached for a form |
||
108 | * |
||
109 | * @param string $form_name The name of the form |
||
110 | * @param bool $filter_result Filter for bad input if true |
||
111 | * |
||
112 | * @return array |
||
113 | */ |
||
114 | View Code Duplication | function getStickyValues($form_name, $filter_result = true) { |
|
130 | |||
131 | /** |
||
132 | * Remove one value of form submission data from the session |
||
133 | * |
||
134 | * @param string $form_name The name of the form |
||
135 | * @param string $variable The name of the variable to clear |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | View Code Duplication | function clearStickyValue($form_name, $variable) { |
|
145 | |||
146 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.