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  | 
            ||
| 15 | class attachments extends module_base  | 
            ||
| 16 | { | 
            ||
| 17 | /**  | 
            ||
| 18 | * Allowed columns: Just sum up your options (Exp: left + right = 10)  | 
            ||
| 19 | * top 1  | 
            ||
| 20 | * left 2  | 
            ||
| 21 | * center 4  | 
            ||
| 22 | * right 8  | 
            ||
| 23 | * bottom 16  | 
            ||
| 24 | */  | 
            ||
| 25 | public $columns = 31;  | 
            ||
| 26 | |||
| 27 | /**  | 
            ||
| 28 | * Default modulename  | 
            ||
| 29 | */  | 
            ||
| 30 | public $name = 'PORTAL_ATTACHMENTS';  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * Default module-image:  | 
            ||
| 34 | 	* file must be in "{T_THEME_PATH}/images/portal/" | 
            ||
| 35 | */  | 
            ||
| 36 | public $image_src = 'portal_attach.png';  | 
            ||
| 37 | |||
| 38 | /**  | 
            ||
| 39 | * module-language file  | 
            ||
| 40 | 	* file must be in "language/{$user->lang}/mods/portal/" | 
            ||
| 41 | */  | 
            ||
| 42 | public $language = 'portal_attachments_module';  | 
            ||
| 43 | |||
| 44 | /** @var \phpbb\auth\auth */  | 
            ||
| 45 | protected $auth;  | 
            ||
| 46 | |||
| 47 | /** @var \phpbb\config\config */  | 
            ||
| 48 | protected $config;  | 
            ||
| 49 | |||
| 50 | /** @var \board3\portal\includes\modules_helper */  | 
            ||
| 51 | protected $helper;  | 
            ||
| 52 | |||
| 53 | /** @var \phpbb\request\request */  | 
            ||
| 54 | protected $request;  | 
            ||
| 55 | |||
| 56 | /** @var \phpbb\template\template */  | 
            ||
| 57 | protected $template;  | 
            ||
| 58 | |||
| 59 | /** @var \phpbb\db\driver\driver_interface */  | 
            ||
| 60 | protected $db;  | 
            ||
| 61 | |||
| 62 | /** @var string PHP file extension */  | 
            ||
| 63 | protected $php_ext;  | 
            ||
| 64 | |||
| 65 | /** @var string phpBB root path */  | 
            ||
| 66 | protected $phpbb_root_path;  | 
            ||
| 67 | |||
| 68 | /** @var \phpbb\user */  | 
            ||
| 69 | protected $user;  | 
            ||
| 70 | |||
| 71 | /**  | 
            ||
| 72 | * Construct an attachments object  | 
            ||
| 73 | *  | 
            ||
| 74 | * @param \phpbb\auth\auth $auth phpBB auth service  | 
            ||
| 75 | * @param \phpbb\config\config $config phpBB config  | 
            ||
| 76 | * @param \board3\portal\includes\modules_helper $helper Modules helper  | 
            ||
| 77 | * @param \phpbb\template\template $template phpBB template  | 
            ||
| 78 | * @param \phpbb\db\driver\driver_interface $db Database driver  | 
            ||
| 79 | * @param \phpbb\request\request $request phpBB request  | 
            ||
| 80 | * @param string $phpEx php file extension  | 
            ||
| 81 | * @param string $phpbb_root_path phpBB root path  | 
            ||
| 82 | * @param \phpbb\user $user phpBB user object  | 
            ||
| 83 | */  | 
            ||
| 84 | View Code Duplication | public function __construct($auth, $config, $helper, $template, $db, $request, $phpEx, $phpbb_root_path, $user)  | 
            |
| 96 | |||
| 97 | /**  | 
            ||
| 98 | 	* {@inheritdoc} | 
            ||
| 99 | */  | 
            ||
| 100 | public function get_template_center($module_id)  | 
            ||
| 104 | |||
| 105 | /**  | 
            ||
| 106 | 	* {@inheritdoc} | 
            ||
| 107 | */  | 
            ||
| 108 | public function get_template_side($module_id)  | 
            ||
| 112 | |||
| 113 | /**  | 
            ||
| 114 | 	* {@inheritdoc} | 
            ||
| 115 | */  | 
            ||
| 116 | public function get_template_acp($module_id)  | 
            ||
| 131 | |||
| 132 | /**  | 
            ||
| 133 | 	* {@inheritdoc} | 
            ||
| 134 | */  | 
            ||
| 135 | public function install($module_id)  | 
            ||
| 145 | |||
| 146 | /**  | 
            ||
| 147 | 	* {@inheritdoc} | 
            ||
| 148 | */  | 
            ||
| 149 | View Code Duplication | public function uninstall($module_id, $db)  | 
            |
| 163 | |||
| 164 | /**  | 
            ||
| 165 | * Create select box for attachment filetype  | 
            ||
| 166 | *  | 
            ||
| 167 | * @param mixed $value Value of input  | 
            ||
| 168 | * @param string $key Key name  | 
            ||
| 169 | * @param int $module_id Module ID  | 
            ||
| 170 | *  | 
            ||
| 171 | * @return string Forum select box HTML  | 
            ||
| 172 | */  | 
            ||
| 173 | public function select_filetype($value, $key, $module_id)  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 174 | 	{ | 
            ||
| 175 | $extensions = array();  | 
            ||
| 176 | |||
| 177 | // Get extensions  | 
            ||
| 178 | $sql = 'SELECT *  | 
            ||
| 179 | FROM ' . EXTENSIONS_TABLE . '  | 
            ||
| 180 | ORDER BY extension ASC';  | 
            ||
| 181 | $result = $this->db->sql_query($sql);  | 
            ||
| 182 | |||
| 183 | while ($row = $this->db->sql_fetchrow($result))  | 
            ||
| 184 | 		{ | 
            ||
| 185 | $extensions[] = array(  | 
            ||
| 186 | 'value' => $row['extension'],  | 
            ||
| 187 | 'title' => $row['extension'],  | 
            ||
| 188 | );  | 
            ||
| 189 | }  | 
            ||
| 190 | $this->db->sql_freeresult($result);  | 
            ||
| 191 | |||
| 192 | $selected = $this->get_selected_filetypes($module_id);  | 
            ||
| 193 | |||
| 194 | return $this->helper->generate_select_box($key, $extensions, $selected, true);  | 
            ||
| 195 | }  | 
            ||
| 196 | |||
| 197 | /**  | 
            ||
| 198 | * Store selected filetypes  | 
            ||
| 199 | *  | 
            ||
| 200 | * @param string $key Key name  | 
            ||
| 201 | * @param int $module_id Module ID  | 
            ||
| 202 | *  | 
            ||
| 203 | * @return null  | 
            ||
| 204 | */  | 
            ||
| 205 | View Code Duplication | public function store_filetypes($key, $module_id)  | 
            |
| 215 | |||
| 216 | /**  | 
            ||
| 217 | * Parse template variables for module  | 
            ||
| 218 | *  | 
            ||
| 219 | * @param int $module_id Module ID  | 
            ||
| 220 | * @param string $type Module type (center or side)  | 
            ||
| 221 | *  | 
            ||
| 222 | * @return string Template file name or false if nothing should  | 
            ||
| 223 | * be displayed  | 
            ||
| 224 | */  | 
            ||
| 225 | protected function parse_template($module_id, $type)  | 
            ||
| 226 | 	{ | 
            ||
| 227 | $attach_forums = false;  | 
            ||
| 228 | $where = '';  | 
            ||
| 229 | |||
| 230 | // Get filetypes and put them into an array  | 
            ||
| 231 | $filetypes = $this->get_selected_filetypes($module_id);  | 
            ||
| 232 | |||
| 233 | if ($this->config['board3_attachments_forum_ids_' . $module_id] !== '')  | 
            ||
| 234 | 		{ | 
            ||
| 235 | 			$attach_forums_config = (strpos($this->config['board3_attachments_forum_ids_' . $module_id], ',') !== false) ? explode(',', $this->config['board3_attachments_forum_ids_' . $module_id]) : array($this->config['board3_attachments_forum_ids_' . $module_id]); | 
            ||
| 236 | 			$forum_list =  array_unique(array_keys($this->auth->acl_getf('f_read', true))); | 
            ||
| 237 | |||
| 238 | View Code Duplication | if ($this->config['board3_attachments_forum_exclude_' . $module_id])  | 
            |
| 239 | 			{ | 
            ||
| 240 | $forum_list = array_unique(array_diff($forum_list, $attach_forums_config));  | 
            ||
| 241 | }  | 
            ||
| 242 | else  | 
            ||
| 243 | 			{ | 
            ||
| 244 | $forum_list = array_unique(array_intersect($attach_forums_config, $forum_list));  | 
            ||
| 245 | }  | 
            ||
| 246 | }  | 
            ||
| 247 | else  | 
            ||
| 248 | 		{ | 
            ||
| 249 | 			$forum_list =  array_unique(array_keys($this->auth->acl_getf('f_read', true))); | 
            ||
| 250 | }  | 
            ||
| 251 | |||
| 252 | View Code Duplication | if (sizeof($forum_list))  | 
            |
| 253 | 		{ | 
            ||
| 254 | $attach_forums = true;  | 
            ||
| 255 | 			$where = 'AND ' . $this->db->sql_in_set('t.forum_id', $forum_list); | 
            ||
| 256 | }  | 
            ||
| 257 | |||
| 258 | if (sizeof($filetypes))  | 
            ||
| 259 | 		{ | 
            ||
| 260 | if ($this->config['board3_attachments_exclude_' . $module_id])  | 
            ||
| 261 | 			{ | 
            ||
| 262 | 				$where .= ' AND ' . $this->db->sql_in_set('a.extension', $filetypes, true); | 
            ||
| 263 | }  | 
            ||
| 264 | else  | 
            ||
| 265 | 			{ | 
            ||
| 266 | 				$where .= ' AND ' . $this->db->sql_in_set('a.extension', $filetypes); | 
            ||
| 267 | }  | 
            ||
| 268 | }  | 
            ||
| 269 | |||
| 270 | if ($attach_forums === true)  | 
            ||
| 271 | 		{ | 
            ||
| 272 | // Just grab all attachment info from database  | 
            ||
| 273 | $sql = 'SELECT  | 
            ||
| 274 | a.*,  | 
            ||
| 275 | t.forum_id  | 
            ||
| 276 | FROM  | 
            ||
| 277 | ' . ATTACHMENTS_TABLE . ' a,  | 
            ||
| 278 | ' . TOPICS_TABLE . ' t  | 
            ||
| 279 | WHERE  | 
            ||
| 280 | a.topic_id <> 0  | 
            ||
| 281 | AND a.topic_id = t.topic_id  | 
            ||
| 282 | ' . $where . '  | 
            ||
| 283 | ORDER BY  | 
            ||
| 284 | filetime ' . ((!$this->config['display_order']) ? 'DESC' : 'ASC') . ', post_msg_id ASC';  | 
            ||
| 285 | $result = $this->db->sql_query_limit($sql, $this->config['board3_attachments_number_' . $module_id], 0, 600);  | 
            ||
| 286 | |||
| 287 | while ($row = $this->db->sql_fetchrow($result))  | 
            ||
| 288 | 			{ | 
            ||
| 289 | $size_lang = ($row['filesize'] >= 1048576) ? $this->user->lang['MIB'] : (($row['filesize'] >= 1024) ? $this->user->lang['KIB'] : $this->user->lang['BYTES']);  | 
            ||
| 290 | $row['filesize'] = ($row['filesize'] >= 1048576) ? round((round($row['filesize'] / 1048576 * 100) / 100), 2) : (($row['filesize'] >= 1024) ? round((round($row['filesize'] / 1024 * 100) / 100), 2) : $row['filesize']);  | 
            ||
| 291 | |||
| 292 | $raw_filename = utf8_substr($row['real_filename'], 0, strrpos($row['real_filename'], '.'));  | 
            ||
| 293 | $replace = character_limit($raw_filename, $this->config['board3_attach_max_length_' . $module_id]);  | 
            ||
| 294 | |||
| 295 | 				$this->template->assign_block_vars('attach_' . $type, array( | 
            ||
| 296 | 'FILESIZE' => $row['filesize'] . ' ' . $size_lang,  | 
            ||
| 297 | 'FILETIME' => $this->user->format_date($row['filetime']),  | 
            ||
| 298 | 'DOWNLOAD_COUNT' => (int) $row['download_count'], // grab downloads count  | 
            ||
| 299 | 'FILENAME' => $replace,  | 
            ||
| 300 | 'REAL_FILENAME' => $row['real_filename'],  | 
            ||
| 301 | 'PHYSICAL_FILENAME' => basename($row['physical_filename']),  | 
            ||
| 302 | 'ATTACH_ID' => $row['attach_id'],  | 
            ||
| 303 | 'POST_IDS' => (!empty($post_ids[$row['attach_id']])) ? $post_ids[$row['attach_id']] : '',  | 
            ||
| 304 | 'POST_MSG_ID' => $row['post_msg_id'], // grab post ID to redirect to post  | 
            ||
| 305 | 'U_FILE' => append_sid($this->phpbb_root_path . 'download/file.' . $this->php_ext, 'id=' . $row['attach_id']),  | 
            ||
| 306 | 'U_TOPIC' => append_sid($this->phpbb_root_path . 'viewtopic.' . $this->php_ext, 'p='.$row['post_msg_id'].'#p'.$row['post_msg_id']),  | 
            ||
| 307 | ));  | 
            ||
| 308 | }  | 
            ||
| 309 | $this->db->sql_freeresult($result);  | 
            ||
| 310 | |||
| 311 | 			$this->template->assign_var('S_DISPLAY_ATTACHMENTS', true); | 
            ||
| 312 | }  | 
            ||
| 313 | else  | 
            ||
| 314 | 		{ | 
            ||
| 315 | 			$this->template->assign_var('S_DISPLAY_ATTACHMENTS', false); | 
            ||
| 316 | }  | 
            ||
| 317 | |||
| 318 | return 'attachments_' . $type . '.html';  | 
            ||
| 319 | }  | 
            ||
| 320 | |||
| 321 | /**  | 
            ||
| 322 | * Get the filetypes that were selected in the ACP  | 
            ||
| 323 | *  | 
            ||
| 324 | * @param int $module_id Module ID  | 
            ||
| 325 | *  | 
            ||
| 326 | * @return array An array with the selected filetypes  | 
            ||
| 327 | */  | 
            ||
| 328 | protected function get_selected_filetypes($module_id)  | 
            ||
| 338 | }  | 
            ||
| 339 | 
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.