Conditions | 15 |
Paths | 73 |
Total Lines | 104 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
101 | public function submit($user_id, $admin, $auto_login, $viewonline, $class) |
||
102 | { |
||
103 | $this->user->add_lang_ext('paul999/tfa', 'common'); |
||
104 | |||
105 | if (!check_form_key('tfa_login_page')) |
||
106 | { |
||
107 | throw new http_exception(403, 'FORM_INVALID'); |
||
108 | } |
||
109 | |||
110 | if (empty($this->user->data['tfa_random']) || $user_id != $this->user->data['tfa_uid']) |
||
111 | { |
||
112 | throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG'); |
||
113 | } |
||
114 | $random = $this->request->variable('random', ''); |
||
115 | |||
116 | if ($this->user->data['tfa_random'] !== $random || strlen($random) !== 40) |
||
117 | { |
||
118 | throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG'); |
||
119 | } |
||
120 | $sql_ary = array( |
||
121 | 'tfa_random' => '', |
||
122 | 'tfa_uid' => 0, |
||
123 | ); |
||
124 | $sql = 'UPDATE ' . SESSIONS_TABLE . ' SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . " |
||
125 | WHERE |
||
126 | session_id = '" . $this->db->sql_escape($this->user->data['session_id']) . "' AND |
||
127 | session_user_id = " . (int) $this->user->data['user_id']; |
||
128 | $this->db->sql_query($sql); |
||
129 | |||
130 | if (empty($class)) |
||
131 | { |
||
132 | throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG'); |
||
133 | } |
||
134 | |||
135 | $module = $this->session_helper->find_module($class); |
||
136 | |||
137 | if ($module == null) |
||
138 | { |
||
139 | throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG'); |
||
140 | } |
||
141 | |||
142 | $redirect = $this->request->variable('redirect', "{$this->root_path}/index.{$this->php_ext}"); |
||
143 | try |
||
144 | { |
||
145 | if (!$module->login($user_id)) |
||
146 | { |
||
147 | $this->log->add('critical', $this->user->data['user_id'], $this->user->ip, 'LOG_TFA_EXCEPTION',false, ['TFA_INCORRECT_KEY']); |
||
148 | $this->template->assign_var('S_ERROR', $this->user->lang('TFA_INCORRECT_KEY')); |
||
149 | $this->session_helper->generate_page($user_id, $admin, $auto_login, $viewonline, $redirect); |
||
150 | } |
||
151 | } |
||
152 | catch (http_exception $ex) // @TODO: Replace exception with own exception |
||
153 | { |
||
154 | |||
155 | $this->log->add('critical', $this->user->data['user_id'], $this->user->ip, 'LOG_TFA_EXCEPTION', false, [$ex->getMessage()]); |
||
156 | |||
157 | if ($admin) |
||
158 | { |
||
159 | // Also log it to admin log just to be sure. |
||
160 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_TFA_EXCEPTION', false, [$ex->getMessage()]); |
||
161 | } |
||
162 | if ($ex->getStatusCode() == 400) |
||
163 | { |
||
164 | $this->template->assign_var('S_ERROR', $this->user->lang($ex->getMessage())); |
||
165 | $this->session_helper->generate_page($user_id, $admin, $auto_login, $viewonline, $redirect); |
||
166 | } |
||
167 | else |
||
168 | { |
||
169 | throw $ex; |
||
170 | } |
||
171 | } |
||
172 | |||
173 | $old_session_id = $this->user->session_id; |
||
174 | if ($admin) |
||
175 | { |
||
176 | $cookie_expire = time() - 31536000; |
||
177 | $this->user->set_cookie('u', '', $cookie_expire); |
||
178 | $this->user->set_cookie('sid', '', $cookie_expire); |
||
179 | } |
||
180 | $result = $this->user->session_create($user_id, $admin, $auto_login, $viewonline); |
||
181 | |||
182 | // Successful session creation |
||
183 | if ($result === true) |
||
184 | { |
||
185 | // Remove our cookie that causes filling in a key. |
||
186 | $this->user->set_cookie('rn', '', time() + 3600 * 24, true); |
||
187 | // If admin re-authentication we remove the old session entry because a new one has been created... |
||
188 | if ($admin) |
||
189 | { |
||
190 | // the login array is used because the user ids do not differ for re-authentication |
||
191 | $sql = 'DELETE FROM ' . SESSIONS_TABLE . " |
||
192 | WHERE session_id = '" . $this->db->sql_escape($old_session_id) . "' |
||
193 | AND session_user_id = " . (int) $user_id; |
||
194 | $this->db->sql_query($sql); |
||
195 | |||
196 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_ADMIN_AUTH_SUCCESS'); |
||
197 | |||
198 | redirect(append_sid("{$this->root_path}adm/index.{$this->php_ext}", false, true, $this->user->data['session_id'])); |
||
199 | } |
||
200 | |||
201 | redirect(append_sid($redirect, false, true, $this->user->data['session_id'])); |
||
202 | } |
||
203 | throw new http_exception(400, 'TFA_SOMETHING_WENT_WRONG'); |
||
204 | } |
||
205 | } |
||
206 |
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.