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 |
||
25 | class listener implements EventSubscriberInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var session_helper_interface |
||
29 | */ |
||
30 | private $session_helper; |
||
31 | |||
32 | /** |
||
33 | * @var user |
||
34 | */ |
||
35 | private $user; |
||
36 | |||
37 | /** |
||
38 | * @var request_interface |
||
39 | */ |
||
40 | private $request; |
||
41 | |||
42 | /** |
||
43 | * @var driver_interface |
||
44 | */ |
||
45 | private $db; |
||
46 | |||
47 | /** |
||
48 | * @var template |
||
49 | */ |
||
50 | private $template; |
||
51 | |||
52 | /** |
||
53 | * @var config |
||
54 | */ |
||
55 | private $config; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $php_ext; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $root_path; |
||
66 | |||
67 | /** |
||
68 | * Constructor |
||
69 | * |
||
70 | * @access public |
||
71 | * |
||
72 | * @param session_helper_interface $session_helper |
||
73 | * @param user $user |
||
74 | * @param request_interface $request |
||
75 | * @param driver_interface $db |
||
76 | * @param template $template |
||
77 | * @param config $config |
||
78 | * @param string $php_ext |
||
79 | * @param string $root_path |
||
80 | */ |
||
81 | View Code Duplication | public function __construct(session_helper_interface $session_helper, user $user, request_interface $request, driver_interface $db, template $template, config $config, $php_ext, $root_path) |
|
|
|||
82 | { |
||
83 | $this->session_helper = $session_helper; |
||
84 | $this->user = $user; |
||
85 | $this->request = $request; |
||
86 | $this->config = $config; |
||
87 | $this->db = $db; |
||
88 | $this->template = $template; |
||
89 | $this->php_ext = $php_ext; |
||
90 | $this->root_path = $root_path; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Assign functions defined in this class to event listeners in the core |
||
95 | * |
||
96 | * @return array |
||
97 | * @static |
||
98 | * @access public |
||
99 | */ |
||
100 | public static function getSubscribedEvents() |
||
101 | { |
||
102 | return array( |
||
103 | 'core.auth_login_session_create_before' => 'auth_login_session_create_before', |
||
104 | 'core.user_setup_after' => 'user_setup_after', |
||
105 | 'core.permissions' => 'add_permission', |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param data $event |
||
111 | */ |
||
112 | public function add_permission($event) |
||
113 | { |
||
114 | $permissions = $event['permissions']; |
||
115 | $permissions['a_tfa'] = array('lang' => 'ACL_A_TFA', 'cat' => 'misc'); |
||
116 | $event['permissions'] = $permissions; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param data $event |
||
121 | */ |
||
122 | public function user_setup_after($event) |
||
123 | { |
||
124 | $this->user->add_lang_ext('paul999/tfa', 'common'); |
||
125 | if (strpos($this->user->page['page'], 'paul999/tfa/save') !== false) |
||
126 | { |
||
127 | // We are at our controller. Don't do anything. In all cases. |
||
128 | @define('SKIP_CHECK_DISABLED', true); |
||
129 | return; |
||
130 | } |
||
131 | |||
132 | // We skip this when tfa is disabled or we are at a page related to login (This includes logout :)) |
||
133 | if ($this->config['tfa_mode'] == session_helper_interface::MODE_DISABLED || defined('IN_LOGIN')) |
||
134 | { |
||
135 | return; |
||
136 | } |
||
137 | |||
138 | if ($this->user->data['is_bot'] == false && $this->user->data['user_id'] != ANONYMOUS && $this->session_helper->is_tfa_required($this->user->data['user_id'], false, $this->user->data) && !$this->session_helper->is_tfa_registered($this->user->data['user_id'])) |
||
139 | { |
||
140 | @define('SKIP_CHECK_DISABLED', true); |
||
141 | if ($this->user->page['page_name'] === 'memberlist.' . $this->php_ext && $this->request->variable('mode', '') == 'contactadmin') |
||
142 | { |
||
143 | // We are at the contact admin page. We will allow this in all cases. |
||
144 | return; |
||
145 | } |
||
146 | |||
147 | $this->user->set_cookie('rn', $this->user->data['session_id'], time() + 3600 * 24, true); |
||
148 | |||
149 | $msg_title = $this->user->lang['INFORMATION']; |
||
150 | if ($this->session_helper->is_tfa_key_registred($this->user->data['user_id'])) |
||
151 | { |
||
152 | // the user has keys registered, but they are not usable (Might be due to browser requirements, or others) |
||
153 | // We will not allow them to register a new key. They will need to contact the admin instead unfortunately. |
||
154 | $url = phpbb_get_board_contact_link($this->config, $this->root_path, $this->php_ext); |
||
155 | $msg_text = $this->user->lang('TFA_REQUIRED_KEY_AVAILABLE_BUT_UNUSABLE', '<a href="' . $url . '">', '</a>'); |
||
156 | $this->user->session_kill(); |
||
157 | $this->generate_fatal_error($msg_title, $msg_text); |
||
158 | } |
||
159 | |||
160 | $sql = 'SELECT module_id FROM ' . MODULES_TABLE . " WHERE module_langname = 'UCP_TFA' OR module_langname = 'UCP_TFA_MANAGE'"; |
||
161 | $result = $this->db->sql_query($sql, 3600); |
||
162 | $allowed_i = array(); |
||
163 | |||
164 | while ($row = $this->db->sql_fetchrow($result)) |
||
165 | { |
||
166 | $allowed_i[] = $row['module_id']; |
||
167 | } |
||
168 | $this->db->sql_freeresult($result); |
||
169 | $ucp_mode = '-paul999-tfa-ucp-tfa_module'; |
||
170 | $allowed_i[] = $ucp_mode; |
||
171 | |||
172 | if ($this->user->page['page_name'] === 'ucp.' . $this->php_ext && in_array($this->request->variable('i', ''), $allowed_i)) |
||
173 | { |
||
174 | return; // We are at our UCP page, so skip any other checks. This page is always available |
||
175 | } |
||
176 | $url = append_sid("{$this->root_path}ucp.{$this->php_ext}", "i={$ucp_mode}"); |
||
177 | $msg_text = $this->user->lang('TFA_REQUIRED_KEY_MISSING', '<a href="' . $url . '">', '</a>'); |
||
178 | |||
179 | $this->generate_fatal_error($msg_title, $msg_text); |
||
180 | } |
||
181 | |||
182 | // If the user had no key when logged in, but now has a key, we will force him to use the key. |
||
183 | if ($this->user->data['is_bot'] == false && $this->user->data['user_id'] != ANONYMOUS && $this->request->variable($this->config['cookie_name'] . '_rn', '', false, request_interface::COOKIE) !== '' && $this->session_helper->is_tfa_required($this->user->data['user_id'], false, $this->user->data)) |
||
184 | { |
||
185 | $this->session_helper->generate_page($this->user->data['user_id'], false, $this->user->data['session_autologin'], $this->user->data['session_viewonline'], $this->user->page['page'], true); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param data $event |
||
191 | * |
||
192 | * @return data $event|null |
||
193 | * @throw http_exception |
||
194 | */ |
||
195 | public function auth_login_session_create_before($event) |
||
226 | |||
227 | /** |
||
228 | * Generate a fatal error. This method will always exit. |
||
229 | * |
||
230 | * @param $msg_title string Error title |
||
231 | * @param $msg_text string Error message |
||
232 | */ |
||
233 | private function generate_fatal_error($msg_title, $msg_text) |
||
253 | } |
||
254 |
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.