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 whois_online 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_WHOIS_ONLINE'; |
||
31 | |||
32 | /** |
||
33 | * Default module-image: |
||
34 | * file must be in "{T_THEME_PATH}/images/portal/" |
||
35 | */ |
||
36 | public $image_src = 'portal_friends.png'; |
||
37 | |||
38 | /** |
||
39 | * module-language file |
||
40 | * file must be in "language/{$user->lang}/mods/portal/" |
||
41 | */ |
||
42 | public $language = 'portal_whois_online_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\auth\auth */ |
||
51 | protected $auth; |
||
52 | |||
53 | /** @var \phpbb\config\config */ |
||
54 | protected $config; |
||
55 | |||
56 | /** @var \phpbb\db\driver\driver_interface */ |
||
57 | protected $db; |
||
58 | |||
59 | /** @var \phpbb\template\template */ |
||
60 | protected $template; |
||
61 | |||
62 | /** @var \phpbb\user */ |
||
63 | protected $user; |
||
64 | |||
65 | /** @var string phpBB root path */ |
||
66 | protected $phpbb_root_path; |
||
67 | |||
68 | /** @var string PHP file extension */ |
||
69 | protected $php_ext; |
||
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\db\driver\driver_interface $db phpBB db driver |
||
77 | * @param \phpbb\template\template $template phpBB template |
||
78 | * @param \phpbb\user $user phpBB user |
||
79 | * @param string $phpbb_root_path phpBB root path |
||
80 | * @param string $phpEx php file extension |
||
81 | */ |
||
82 | View Code Duplication | public function __construct($auth, $config, $db, $template, $user, $phpbb_root_path, $phpEx) |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function get_template_center($module_id) |
||
97 | { |
||
98 | $order_legend = ($this->config['legend_sort_groupname']) ? 'group_name' : 'group_legend'; |
||
99 | |||
100 | // Grab group details for legend display |
||
101 | View Code Duplication | if ($this->auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel')) |
|
102 | { |
||
103 | $sql = 'SELECT group_id, group_name, group_colour, group_type |
||
104 | FROM ' . GROUPS_TABLE . ' |
||
105 | WHERE group_legend > 0 |
||
106 | ORDER BY ' . $order_legend . ' ASC'; |
||
107 | } |
||
108 | else |
||
109 | { |
||
110 | $sql = 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type |
||
111 | FROM ' . GROUPS_TABLE . ' g |
||
112 | LEFT JOIN ' . USER_GROUP_TABLE . ' ug |
||
113 | ON ( |
||
114 | g.group_id = ug.group_id |
||
115 | AND ug.user_id = ' . (int) $this->user->data['user_id'] . ' |
||
116 | AND ug.user_pending = 0 |
||
117 | ) |
||
118 | WHERE g.group_legend > 0 |
||
119 | AND (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . (int) $this->user->data['user_id'] . ') |
||
120 | ORDER BY g.' . $order_legend . ' ASC'; |
||
121 | } |
||
122 | $result = $this->db->sql_query($sql); |
||
123 | |||
124 | $legend = array(); |
||
125 | while ($row = $this->db->sql_fetchrow($result)) |
||
126 | { |
||
127 | $colour_text = ($row['group_colour']) ? ' style="color:#' . $row['group_colour'] . '"' : ''; |
||
128 | $group_name = ($row['group_type'] == GROUP_SPECIAL) ? $this->user->lang['G_' . $row['group_name']] : $row['group_name']; |
||
129 | |||
130 | if ($row['group_name'] == 'BOTS' || ($this->user->data['user_id'] != ANONYMOUS && !$this->auth->acl_get('u_viewprofile'))) |
||
131 | { |
||
132 | $legend[] = '<span' . $colour_text . '>' . $group_name . '</span>'; |
||
133 | } |
||
134 | else |
||
135 | { |
||
136 | $legend[] = '<a' . $colour_text . ' href="' . append_sid("{$this->phpbb_root_path}memberlist.{$this->php_ext}", 'mode=group&g=' . $row['group_id']) . '">' . $group_name . '</a>'; |
||
137 | } |
||
138 | } |
||
139 | $this->db->sql_freeresult($result); |
||
140 | |||
141 | $legend = implode(', ', $legend); |
||
142 | |||
143 | $this->template->assign_var('PORTAL_LEGEND', $legend); |
||
144 | |||
145 | return 'whois_online_center.html'; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | public function get_template_side($module_id) |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | public function get_template_acp($module_id) |
||
166 | } |
||
167 |