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 user_menu 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 = 10; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Default modulename |
||
| 29 | */ |
||
| 30 | public $name = 'USER_MENU'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Default module-image: |
||
| 34 | * file must be in "{T_THEME_PATH}/images/portal/" |
||
| 35 | */ |
||
| 36 | public $image_src = 'portal_user.png'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * module-language file |
||
| 40 | * file must be in "language/{$user->lang}/mods/portal/" |
||
| 41 | */ |
||
| 42 | public $language = 'portal_user_menu_module'; |
||
| 43 | |||
| 44 | /** @var \phpbb\auth\auth */ |
||
| 45 | protected $auth; |
||
| 46 | |||
| 47 | /** @var \phpbb\config\config */ |
||
| 48 | protected $config; |
||
| 49 | |||
| 50 | /** @var \phpbb\controller\helper */ |
||
| 51 | protected $controller_helper; |
||
| 52 | |||
| 53 | /** @var \phpbb\db\driver */ |
||
| 54 | protected $db; |
||
| 55 | |||
| 56 | /** @var \phpbb\path_helper */ |
||
| 57 | protected $path_helper; |
||
| 58 | |||
| 59 | /** @var \phpbb\template */ |
||
| 60 | protected $template; |
||
| 61 | |||
| 62 | /** @var \phpbb\user */ |
||
| 63 | protected $user; |
||
| 64 | |||
| 65 | /** @var string PHP file extension */ |
||
| 66 | protected $php_ext; |
||
| 67 | |||
| 68 | /** @var string phpBB root path */ |
||
| 69 | protected $phpbb_root_path; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Construct a user menu object |
||
| 73 | * |
||
| 74 | * @param \phpbb\auth\auth $auth phpBB auth |
||
| 75 | * @param \phpbb\config\config $config phpBB config |
||
| 76 | * @param \phpbb\controller\helper $controller_helper Controller helper |
||
| 77 | * @param \phpbb\db\driver $db phpBB db driver |
||
| 78 | * @param \phpbb\path_helper $path_helper phpBB path helper |
||
| 79 | * @param \phpbb\template $template phpBB template |
||
| 80 | * @param \phpbb\user $user phpBB user |
||
| 81 | * @param string $phpbb_root_path phpBB root path |
||
| 82 | * @param string $phpEx php file extension |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function __construct($auth, $config, $controller_helper, $db, $path_helper, $template, $user, $phpbb_root_path, $phpEx) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * {@inheritdoc} |
||
| 99 | */ |
||
| 100 | public function get_template_side($module_id) |
||
| 101 | { |
||
| 102 | if (!function_exists('phpbb_get_user_rank')) |
||
| 103 | { |
||
| 104 | include($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext); |
||
| 105 | } |
||
| 106 | |||
| 107 | if ($this->user->data['is_registered']) |
||
| 108 | { |
||
| 109 | // |
||
| 110 | // + new posts since last visit & you post number |
||
| 111 | // |
||
| 112 | $ex_fid_ary = array_unique(array_merge(array_keys($this->auth->acl_getf('!f_read', true)), array_keys($this->auth->acl_getf('!f_search', true)))); |
||
| 113 | |||
| 114 | if ($this->auth->acl_get('m_approve')) |
||
| 115 | { |
||
| 116 | $m_approve_fid_sql = ''; |
||
| 117 | } |
||
| 118 | else if ($this->auth->acl_getf_global('m_approve')) |
||
| 119 | { |
||
| 120 | $m_approve_fid_ary = array_diff(array_keys($this->auth->acl_getf('!m_approve', true)), $ex_fid_ary); |
||
| 121 | $m_approve_fid_sql = ' AND (p.post_visibility = 1' . ((sizeof($m_approve_fid_ary)) ? ' OR ' . $this->db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) : '') . ')'; |
||
| 122 | } |
||
| 123 | else |
||
| 124 | { |
||
| 125 | $m_approve_fid_sql = ' AND p.post_visibility = 1'; |
||
| 126 | } |
||
| 127 | |||
| 128 | $sql = 'SELECT COUNT(DISTINCT t.topic_id) as total |
||
| 129 | FROM ' . TOPICS_TABLE . ' t |
||
| 130 | WHERE t.topic_last_post_time > ' . (int) $this->user->data['user_lastvisit'] . ' |
||
| 131 | AND t.topic_moved_id = 0 |
||
| 132 | ' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . ' |
||
| 133 | ' . ((sizeof($ex_fid_ary)) ? 'AND ' . $this->db->sql_in_set('t.forum_id', $ex_fid_ary, true) : ''); |
||
| 134 | $result = $this->db->sql_query($sql, 600); |
||
| 135 | $new_posts_count = (int) $this->db->sql_fetchfield('total'); |
||
| 136 | $this->db->sql_freeresult($result); |
||
| 137 | |||
| 138 | // unread posts |
||
| 139 | $sql_where = 'AND t.topic_moved_id = 0 |
||
| 140 | ' . str_replace(array('p.', 'post_'), array('t.', 'topic_'), $m_approve_fid_sql) . ' |
||
| 141 | ' . ((sizeof($ex_fid_ary)) ? 'AND ' . $this->db->sql_in_set('t.forum_id', $ex_fid_ary, true) : ''); |
||
| 142 | $unread_list = get_unread_topics($this->user->data['user_id'], $sql_where, 'ORDER BY t.topic_id DESC'); |
||
| 143 | $unread_posts_count = sizeof($unread_list); |
||
| 144 | |||
| 145 | // Get user avatar and rank |
||
| 146 | $user_id = $this->user->data['user_id']; |
||
| 147 | $username = $this->user->data['username']; |
||
| 148 | $colour = $this->user->data['user_colour']; |
||
| 149 | $avatar_img = phpbb_get_avatar(\phpbb\avatar\manager::clean_row($this->user->data, 'user'), 'USER_AVATAR'); |
||
| 150 | $rank_data = phpbb_get_user_rank($this->user->data, $this->user->data['user_posts']); |
||
| 151 | |||
| 152 | // Assign specific vars |
||
| 153 | $this->template->assign_vars(array( |
||
| 154 | 'L_NEW_POSTS' => $this->user->lang['SEARCH_NEW'] . ' (' . $new_posts_count . ')', |
||
| 155 | 'L_SELF_POSTS' => $this->user->lang['SEARCH_SELF'] . ' (' . $this->user->data['user_posts'] . ')', |
||
| 156 | 'L_UNREAD_POSTS'=> $this->user->lang['SEARCH_UNREAD'] . ' (' . $unread_posts_count . ')', |
||
| 157 | |||
| 158 | 'B3P_AVATAR_IMG' => $avatar_img, |
||
| 159 | 'B3P_RANK_TITLE' => $rank_data['title'], |
||
| 160 | 'B3P_RANK_IMG' => $rank_data['img'], |
||
| 161 | 'RANK_IMG_SRC' => $rank_data['img_src'], |
||
| 162 | |||
| 163 | 'USERNAME_FULL' => get_username_string('full', $user_id, $username, $colour), |
||
| 164 | 'U_VIEW_PROFILE' => get_username_string('profile', $user_id, $username, $colour), |
||
| 165 | |||
| 166 | 'U_NEW_POSTS' => append_sid("{$this->phpbb_root_path}search.{$this->php_ext}", 'search_id=newposts'), |
||
| 167 | 'U_SELF_POSTS' => append_sid("{$this->phpbb_root_path}search.{$this->php_ext}", 'search_id=egosearch'), |
||
| 168 | 'U_UNREAD_POSTS' => append_sid("{$this->phpbb_root_path}search.{$this->php_ext}", 'search_id=unreadposts'), |
||
| 169 | 'U_UM_BOOKMARKS' => ($this->config['allow_bookmarks']) ? append_sid("{$this->phpbb_root_path}ucp.{$this->php_ext}", 'i=main&mode=bookmarks') : '', |
||
| 170 | 'U_UM_MAIN_SUBSCRIBED' => append_sid("{$this->phpbb_root_path}ucp.{$this->php_ext}", 'i=main&mode=subscribed'), |
||
| 171 | 'U_UM_MCP' => ($this->auth->acl_get('m_') || $this->auth->acl_getf_global('m_')) ? append_sid("{$this->phpbb_root_path}mcp.{$this->php_ext}", 'i=main&mode=front', true, $this->user->session_id) : '', |
||
| 172 | 'S_DISPLAY_SUBSCRIPTIONS' => ($this->config['allow_topic_notify'] || $this->config['allow_forum_notify']) ? true : false, |
||
| 173 | )); |
||
| 174 | |||
| 175 | return 'user_menu_side.html'; |
||
| 176 | } |
||
| 177 | else |
||
| 178 | { |
||
| 179 | /* |
||
| 180 | * Assign specific vars |
||
| 181 | * Need to remove web root path as ucp.php will do the |
||
| 182 | * redirect |
||
| 183 | */ |
||
| 184 | $this->template->assign_vars(array( |
||
| 185 | 'U_PORTAL_REDIRECT' => $this->path_helper->remove_web_root_path($this->controller_helper->route('board3_portal_controller')), |
||
| 186 | 'S_DISPLAY_FULL_LOGIN' => true, |
||
| 187 | 'S_AUTOLOGIN_ENABLED' => ($this->config['allow_autologin']) ? true : false, |
||
| 188 | 'S_LOGIN_ACTION' => append_sid("{$this->phpbb_root_path}ucp.{$this->php_ext}", 'mode=login'), |
||
| 189 | 'S_SHOW_REGISTER' => ($this->config['board3_user_menu_register_' . $module_id]) ? true : false, |
||
| 190 | )); |
||
| 191 | |||
| 192 | return 'login_box_side.html'; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | public function get_template_acp($module_id) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritdoc} |
||
| 212 | */ |
||
| 213 | public function install($module_id) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | View Code Duplication | public function uninstall($module_id, $db) |
|
| 232 | } |
||
| 233 |