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 renderer 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 renderer, and based on these observations, apply Extract Interface, too.
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 checkbox element in a group. |
||
109 | * |
||
110 | * @param string $name |
||
111 | * @param object $default |
||
112 | * |
||
113 | * @return Object |
||
114 | */ |
||
115 | View Code Duplication | public function render_group_element_checkbox($name, $default = null) { |
|
122 | |||
123 | /** |
||
124 | * Render a multiselect element in a group. |
||
125 | * |
||
126 | * @param string $name |
||
127 | * @param object $defaultsetting |
||
128 | * @param object $choices |
||
129 | * |
||
130 | * @return Object |
||
131 | */ |
||
132 | View Code Duplication | public function render_group_element_configmultiselect($name, $defaultsetting, $choices) { |
|
139 | |||
140 | /** |
||
141 | * Render a select 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_configselect($name, $defaultsetting, $choices) { |
|
156 | |||
157 | /** |
||
158 | * Render a general warning message. |
||
159 | * |
||
160 | * @param string $name |
||
161 | * @param string $message |
||
162 | * @param string $type |
||
163 | * @param boolean $closable |
||
164 | * |
||
165 | * @return Object |
||
166 | */ |
||
167 | public function render_warning_message($name, $message, $type = 'warning', $closable = true) { |
||
180 | |||
181 | /** |
||
182 | * Validate if general section will be shown. |
||
183 | * |
||
184 | * @return boolean |
||
185 | */ |
||
186 | public static function section_general_shown() { |
||
191 | |||
192 | /** |
||
193 | * Validate if record meeting section will be shown. |
||
194 | * |
||
195 | * @return boolean |
||
196 | */ |
||
197 | public static function section_record_meeting_shown() { |
||
203 | |||
204 | /** |
||
205 | * Validate if import recording section will be shown. |
||
206 | * |
||
207 | * @return boolean |
||
208 | */ |
||
209 | public static function section_import_recordings_shown() { |
||
214 | |||
215 | /** |
||
216 | * Validate if show recording section will be shown. |
||
217 | * |
||
218 | * @return boolean |
||
219 | */ |
||
220 | public static function section_show_recordings_shown() { |
||
232 | |||
233 | /** |
||
234 | * Validate if wait moderator section will be shown. |
||
235 | * |
||
236 | * @return boolean |
||
237 | */ |
||
238 | public static function section_wait_moderator_shown() { |
||
245 | |||
246 | /** |
||
247 | * Validate if static voice bridge section will be shown. |
||
248 | * |
||
249 | * @return boolean |
||
250 | */ |
||
251 | public static function section_static_voice_bridge_shown() { |
||
255 | |||
256 | /** |
||
257 | * Validate if preupload presentation section will be shown. |
||
258 | * |
||
259 | * @return boolean |
||
260 | */ |
||
261 | public static function section_preupload_presentation_shown() { |
||
265 | |||
266 | /** |
||
267 | * Validate if user limit section will be shown. |
||
268 | * |
||
269 | * @return boolean |
||
270 | */ |
||
271 | public static function section_user_limit_shown() { |
||
276 | |||
277 | /** |
||
278 | * Validate if scheduled duration section will be shown. |
||
279 | * |
||
280 | * @return boolean |
||
281 | */ |
||
282 | public static function section_scheduled_duration_shown() { |
||
286 | |||
287 | /** |
||
288 | * Validate if moderator default section will be shown. |
||
289 | * |
||
290 | * @return boolean |
||
291 | */ |
||
292 | public static function section_moderator_default_shown() { |
||
296 | |||
297 | /** |
||
298 | * Validate if send notification section will be shown. |
||
299 | * |
||
300 | * @return boolean |
||
301 | */ |
||
302 | public static function section_send_notifications_shown() { |
||
306 | |||
307 | /** |
||
308 | * Validate if clienttype section will be shown. |
||
309 | * |
||
310 | * @return boolean |
||
311 | */ |
||
312 | public static function section_clienttype_shown() { |
||
324 | |||
325 | /** |
||
326 | * Validate if settings extended section will be shown. |
||
327 | * |
||
328 | * @return boolean |
||
329 | */ |
||
330 | public static function section_settings_extended_shown() { |
||
335 | } |
||
336 |
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.