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 |
||
39 | class renderer { |
||
40 | |||
41 | /** |
||
42 | * @var $settings stores the settings as they come from settings.php |
||
43 | */ |
||
44 | private $settings; |
||
45 | |||
46 | /** |
||
47 | * Constructor. |
||
48 | * |
||
49 | * @param object $settings |
||
50 | */ |
||
51 | public function __construct(&$settings) { |
||
54 | |||
55 | /** |
||
56 | * Render the header for a group. |
||
57 | * |
||
58 | * @param string $name |
||
59 | * @param string $itemname |
||
60 | * @param string $itemdescription |
||
61 | * |
||
62 | * @return void |
||
63 | */ |
||
64 | public function render_group_header($name, $itemname = null, $itemdescription = null) { |
||
74 | |||
75 | /** |
||
76 | * Render an element in a group. |
||
77 | * |
||
78 | * @param string $name |
||
79 | * @param object $item |
||
80 | * |
||
81 | * @return void |
||
82 | */ |
||
83 | public function render_group_element($name, $item) { |
||
89 | |||
90 | /** |
||
91 | * Render a text element in a group. |
||
92 | * |
||
93 | * @param string $name |
||
94 | * @param object $default |
||
95 | * @param string $type |
||
96 | * |
||
97 | * @return Object |
||
98 | */ |
||
99 | View Code Duplication | public function render_group_element_text($name, $default = null, $type = PARAM_RAW) { |
|
106 | |||
107 | /** |
||
108 | * Render a html editor element in a group. |
||
109 | * |
||
110 | * @param string $name |
||
111 | * @param object $default |
||
112 | * @param string $type |
||
113 | * |
||
114 | * @return Object |
||
115 | */ |
||
116 | View Code Duplication | public function render_group_element_textarea($name, $default = null, $type = PARAM_RAW) { |
|
123 | |||
124 | /** |
||
125 | * Render a checkbox element in a group. |
||
126 | * |
||
127 | * @param string $name |
||
128 | * @param object $default |
||
129 | * |
||
130 | * @return Object |
||
131 | */ |
||
132 | View Code Duplication | public function render_group_element_checkbox($name, $default = null) { |
|
139 | |||
140 | /** |
||
141 | * Render a multiselect element in a group. |
||
142 | * |
||
143 | * @param string $name |
||
144 | * @param object $defaultsetting |
||
145 | * @param object $choices |
||
146 | * |
||
147 | * @return Object |
||
148 | */ |
||
149 | View Code Duplication | public function render_group_element_configmultiselect($name, $defaultsetting, $choices) { |
|
156 | |||
157 | /** |
||
158 | * Render a select element in a group. |
||
159 | * |
||
160 | * @param string $name |
||
161 | * @param object $defaultsetting |
||
162 | * @param object $choices |
||
163 | * |
||
164 | * @return Object |
||
165 | */ |
||
166 | View Code Duplication | public function render_group_element_configselect($name, $defaultsetting, $choices) { |
|
173 | |||
174 | /** |
||
175 | * Render a general warning message. |
||
176 | * |
||
177 | * @param string $name |
||
178 | * @param string $message |
||
179 | * @param string $type |
||
180 | * @param boolean $closable |
||
181 | * |
||
182 | * @return Object |
||
183 | */ |
||
184 | public function render_warning_message($name, $message, $type = 'warning', $closable = true) { |
||
196 | |||
197 | /** |
||
198 | * Render a general manage file for use as default presentation. |
||
199 | * |
||
200 | * @param string $name |
||
201 | * |
||
202 | * @return Object |
||
203 | */ |
||
204 | public function render_filemanager_default_file_presentation($name) { |
||
223 | } |
||
224 |
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.