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) { |
||
| 52 | $this->settings = $settings; |
||
| 53 | } |
||
| 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 general warning message. |
||
| 142 | * |
||
| 143 | * @param string $message |
||
| 144 | * |
||
| 145 | * @return Object |
||
| 146 | */ |
||
| 147 | public function render_warning_message($message, $type = 'warning') { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Validate if general section will be shown. |
||
| 160 | * |
||
| 161 | * @return boolean |
||
| 162 | */ |
||
| 163 | public static function section_general_shown() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Validate if record meeting section will be shown. |
||
| 171 | * |
||
| 172 | * @return boolean |
||
| 173 | */ |
||
| 174 | public static function section_record_meeting_shown() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Validate if import recording section will be shown. |
||
| 183 | * |
||
| 184 | * @return boolean |
||
| 185 | */ |
||
| 186 | public static function section_import_recordings_shown() { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Validate if show recording section will be shown. |
||
| 194 | * |
||
| 195 | * @return boolean |
||
| 196 | */ |
||
| 197 | public static function section_show_recordings_shown() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Validate if wait moderator section will be shown. |
||
| 209 | * |
||
| 210 | * @return boolean |
||
| 211 | */ |
||
| 212 | public static function section_wait_moderator_shown() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Validate if static voice bridge section will be shown. |
||
| 222 | * |
||
| 223 | * @return boolean |
||
| 224 | */ |
||
| 225 | public static function section_static_voice_bridge_shown() { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Validate if preupload presentation section will be shown. |
||
| 232 | * |
||
| 233 | * @return boolean |
||
| 234 | */ |
||
| 235 | public static function section_preupload_presentation_shown() { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Validate if user limit section will be shown. |
||
| 242 | * |
||
| 243 | * @return boolean |
||
| 244 | */ |
||
| 245 | public static function section_user_limit_shown() { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Validate if scheduled duration section will be shown. |
||
| 253 | * |
||
| 254 | * @return boolean |
||
| 255 | */ |
||
| 256 | public static function section_scheduled_duration_shown() { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Validate if moderator default section will be shown. |
||
| 263 | * |
||
| 264 | * @return boolean |
||
| 265 | */ |
||
| 266 | public static function section_moderator_default_shown() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Validate if send notification section will be shown. |
||
| 273 | * |
||
| 274 | * @return boolean |
||
| 275 | */ |
||
| 276 | public static function section_send_notifications_shown() { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Validate if settings extended section will be shown. |
||
| 283 | * |
||
| 284 | * @return boolean |
||
| 285 | */ |
||
| 286 | public static function section_settings_extended_shown() { |
||
| 291 | } |
||
| 292 |
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.