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 random_member 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 = 'PORTAL_RANDOM_MEMBER'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Default module-image: |
||
| 34 | * file must be in "{T_THEME_PATH}/images/portal/" |
||
| 35 | */ |
||
| 36 | public $image_src = 'portal_random_member.png'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * module-language file |
||
| 40 | * file must be in "language/{$user->lang}/mods/portal/" |
||
| 41 | */ |
||
| 42 | public $language = 'portal_random_member_module'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * custom acp template |
||
| 46 | * file must be in "adm/style/portal/" |
||
| 47 | */ |
||
| 48 | public $custom_acp_tpl = ''; |
||
| 49 | |||
| 50 | /** @var \phpbb\db\driver */ |
||
| 51 | protected $db; |
||
| 52 | |||
| 53 | /** @var \phpbb\template */ |
||
| 54 | protected $template; |
||
| 55 | |||
| 56 | /** @var \phpbb\user */ |
||
| 57 | protected $user; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Construct a random member object |
||
| 61 | * |
||
| 62 | * @param \phpbb\db\driver $db phpBB database system |
||
| 63 | * @param \phpbb\template $template phpBB template |
||
| 64 | * @param \phpbb\user $user phpBB user object |
||
| 65 | */ |
||
| 66 | public function __construct($db, $template, $user) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | public function get_template_side($module_id) |
||
| 77 | { |
||
| 78 | switch ($this->db->get_sql_layer()) |
||
| 79 | { |
||
| 80 | View Code Duplication | case 'postgres': |
|
| 81 | $sql = 'SELECT * |
||
| 82 | FROM ' . USERS_TABLE . ' |
||
| 83 | WHERE user_type <> ' . USER_IGNORE . ' |
||
| 84 | AND user_type <> ' . USER_INACTIVE . ' |
||
| 85 | ORDER BY RANDOM()'; |
||
| 86 | break; |
||
| 87 | |||
| 88 | case 'mssql': |
||
| 89 | View Code Duplication | case 'mssql_odbc': |
|
| 90 | $sql = 'SELECT * |
||
| 91 | FROM ' . USERS_TABLE . ' |
||
| 92 | WHERE user_type <> ' . USER_IGNORE . ' |
||
| 93 | AND user_type <> ' . USER_INACTIVE . ' |
||
| 94 | ORDER BY NEWID()'; |
||
| 95 | break; |
||
| 96 | |||
| 97 | View Code Duplication | default: |
|
| 98 | $sql = 'SELECT * |
||
| 99 | FROM ' . USERS_TABLE . ' |
||
| 100 | WHERE user_type <> ' . USER_IGNORE . ' |
||
| 101 | AND user_type <> ' . USER_INACTIVE . ' |
||
| 102 | ORDER BY RAND()'; |
||
| 103 | break; |
||
| 104 | } |
||
| 105 | |||
| 106 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 107 | $row = $this->db->sql_fetchrow($result); |
||
| 108 | |||
| 109 | $avatar_img = phpbb_get_avatar(\phpbb\avatar\manager::clean_row($row, 'user'), 'USER_AVATAR'); |
||
| 110 | |||
| 111 | $rank_data = phpbb_get_user_rank($row, $row['user_posts']); |
||
| 112 | |||
| 113 | $username = $row['username']; |
||
| 114 | $user_id = (int) $row['user_id']; |
||
| 115 | $colour = $row['user_colour']; |
||
| 116 | |||
| 117 | $this->template->assign_block_vars('random_member', array( |
||
| 118 | 'USERNAME_FULL' => get_username_string('full', $user_id, $username, $colour), |
||
| 119 | 'USERNAME' => get_username_string('username', $user_id, $username, $colour), |
||
| 120 | 'USER_COLOR' => get_username_string('colour', $user_id, $username, $colour), |
||
| 121 | 'U_VIEW_PROFILE' => get_username_string('profile', $user_id, $username, $colour), |
||
| 122 | |||
| 123 | 'RANK_TITLE' => $rank_data['title'], |
||
| 124 | 'RANK_IMG' => $rank_data['img'], |
||
| 125 | 'RANK_IMG_SRC' => $rank_data['img_src'], |
||
| 126 | |||
| 127 | 'USER_POSTS' => (int) $row['user_posts'], |
||
| 128 | 'AVATAR_IMG' => $avatar_img, |
||
| 129 | 'JOINED' => $this->user->format_date($row['user_regdate']), |
||
| 130 | // 'USER_OCC' => censor_text($row['user_occ']), |
||
| 131 | // 'USER_FROM' => censor_text($row['user_from']), |
||
| 132 | // 'U_WWW' => censor_text($row['user_website']), |
||
| 133 | )); |
||
| 134 | $this->db->sql_freeresult($result); |
||
| 135 | |||
| 136 | return 'random_member_side.html'; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | public function get_template_acp($module_id) |
||
| 149 | } |
||
| 150 |