| Conditions | 13 |
| Paths | 36 |
| Total Lines | 161 |
| Code Lines | 98 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 16 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 94 | public function main($id, $mode) |
||
| 95 | { |
||
| 96 | $form_key = 'acp_similar_topics'; |
||
| 97 | add_form_key($form_key); |
||
| 98 | |||
| 99 | $action = $this->request->variable('action', ''); |
||
| 100 | |||
| 101 | switch ($action) |
||
| 102 | { |
||
| 103 | case 'advanced': |
||
| 104 | $forum_id = $this->request->variable('f', 0); |
||
| 105 | |||
| 106 | if ($this->request->is_set_post('submit')) |
||
| 107 | { |
||
| 108 | $this->check_form_key($form_key); |
||
| 109 | |||
| 110 | $similar_topic_forums = implode(',', $this->request->variable('similar_forums_id', array(0))); |
||
| 111 | $this->validate_config_length($similar_topic_forums); |
||
| 112 | |||
| 113 | $sql = 'UPDATE ' . FORUMS_TABLE . " |
||
| 114 | SET similar_topic_forums = '" . $this->db->sql_escape($similar_topic_forums) . "' |
||
| 115 | WHERE forum_id = $forum_id"; |
||
| 116 | $this->db->sql_query($sql); |
||
| 117 | |||
| 118 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG'); |
||
| 119 | |||
| 120 | $this->end('PST_SAVED'); |
||
| 121 | } |
||
| 122 | |||
| 123 | $forum_name = ''; |
||
| 124 | $selected = array(); |
||
| 125 | if ($forum_id > 0) |
||
| 126 | { |
||
| 127 | $sql = 'SELECT forum_name, similar_topic_forums |
||
| 128 | FROM ' . FORUMS_TABLE . " |
||
| 129 | WHERE forum_id = $forum_id"; |
||
| 130 | $result = $this->db->sql_query($sql); |
||
| 131 | while ($fid = $this->db->sql_fetchrow($result)) |
||
| 132 | { |
||
| 133 | $selected = explode(',', trim($fid['similar_topic_forums'])); |
||
| 134 | $forum_name = $fid['forum_name']; |
||
| 135 | } |
||
| 136 | $this->db->sql_freeresult($result); |
||
| 137 | } |
||
| 138 | |||
| 139 | $this->template->assign_vars(array( |
||
| 140 | 'S_ADVANCED_SETTINGS' => true, |
||
| 141 | 'SIMILAR_FORUMS_OPTIONS' => make_forum_select($selected, false, false, true), |
||
| 142 | 'PST_FORUM_NAME' => $forum_name, |
||
| 143 | 'PST_ADVANCED_EXP' => $this->user->lang('PST_ADVANCED_EXP', $forum_name), |
||
| 144 | 'U_ACTION' => $this->u_action . '&action=advanced&f=' . $forum_id, |
||
| 145 | 'U_BACK' => $this->u_action, |
||
| 146 | )); |
||
| 147 | break; |
||
| 148 | |||
| 149 | default: |
||
| 150 | if ($this->request->is_set_post('submit')) |
||
| 151 | { |
||
| 152 | $this->check_form_key($form_key); |
||
| 153 | |||
| 154 | // Get checkbox array form data and check string length |
||
| 155 | $mark_noshow_forum = implode(',', $this->request->variable('mark_noshow_forum', array(0), true)); |
||
| 156 | $mark_ignore_forum = implode(',', $this->request->variable('mark_ignore_forum', array(0), true)); |
||
| 157 | $this->validate_config_length($mark_noshow_forum, $mark_ignore_forum); |
||
| 158 | |||
| 159 | // Set basic config settings |
||
| 160 | $this->config->set('similar_topics', $this->request->variable('pst_enable', 0)); |
||
| 161 | $this->config->set('similar_topics_limit', abs($this->request->variable('pst_limit', 0))); // use abs for positive values only |
||
| 162 | $this->config->set('similar_topics_cache', abs($this->request->variable('pst_cache', 0))); // use abs for positive values only |
||
| 163 | $this->config->set('similar_topics_words', $this->request->variable('pst_words', '', true)); |
||
| 164 | $this->config->set('similar_topics_hide', $mark_noshow_forum); |
||
| 165 | $this->config->set('similar_topics_ignore', $mark_ignore_forum); |
||
| 166 | |||
| 167 | // Set date/time config settings |
||
| 168 | $pst_time = abs($this->request->variable('pst_time', 0)); // use abs for positive values only |
||
| 169 | $pst_time_type = $this->request->variable('pst_time_type', ''); |
||
| 170 | $this->config->set('similar_topics_type', $pst_time_type); |
||
| 171 | $this->config->set('similar_topics_time', $this->set_pst_time($pst_time, $pst_time_type)); |
||
| 172 | |||
| 173 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG'); |
||
| 174 | |||
| 175 | $this->end('PST_SAVED'); |
||
| 176 | } |
||
| 177 | |||
| 178 | // Allow option to update the database to enable FULLTEXT support |
||
| 179 | if ($this->request->is_set_post('fulltext')) |
||
| 180 | { |
||
| 181 | if (confirm_box(true)) |
||
| 182 | { |
||
| 183 | // If FULLTEXT is not supported, lets make it so |
||
| 184 | if (!$this->fulltext_support_enabled()) |
||
| 185 | { |
||
| 186 | // Alter the database to support FULLTEXT |
||
| 187 | $this->enable_fulltext_support(); |
||
| 188 | |||
| 189 | // Store the original database storage engine in a config var for recovery on uninstall |
||
| 190 | $this->config->set('similar_topics_fulltext', (string) $this->fulltext->get_engine()); |
||
| 191 | |||
| 192 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_FULLTEXT', time(), array(TOPICS_TABLE)); |
||
| 193 | |||
| 194 | $this->end('PST_SAVE_FULLTEXT'); |
||
| 195 | } |
||
| 196 | else |
||
| 197 | { |
||
| 198 | $this->end('PST_ERR_FULLTEXT', E_USER_WARNING); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | else |
||
| 202 | { |
||
| 203 | confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array( |
||
| 204 | 'fulltext' => 1, |
||
| 205 | ))); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | // Build the time options select menu |
||
| 210 | $time_options = array( |
||
| 211 | 'd' => $this->user->lang('PST_DAYS'), |
||
| 212 | 'w' => $this->user->lang('PST_WEEKS'), |
||
| 213 | 'm' => $this->user->lang('PST_MONTHS'), |
||
| 214 | 'y' => $this->user->lang('PST_YEARS') |
||
| 215 | ); |
||
| 216 | foreach ($time_options as $value => $label) |
||
| 217 | { |
||
| 218 | $this->template->assign_block_vars('similar_time_options', array( |
||
| 219 | 'VALUE' => $value, |
||
| 220 | 'LABEL' => $label, |
||
| 221 | 'S_SELECTED' => $value == $this->config['similar_topics_type'], |
||
| 222 | )); |
||
| 223 | } |
||
| 224 | |||
| 225 | $this->template->assign_vars(array( |
||
| 226 | 'S_PST_ENABLE' => $this->isset_or_default($this->config['similar_topics'], false), |
||
| 227 | 'PST_LIMIT' => $this->isset_or_default($this->config['similar_topics_limit'], ''), |
||
| 228 | 'PST_CACHE' => $this->isset_or_default($this->config['similar_topics_cache'], ''), |
||
| 229 | 'PST_WORDS' => $this->isset_or_default($this->config['similar_topics_words'], ''), |
||
| 230 | 'PST_TIME' => $this->get_pst_time($this->config['similar_topics_time'], $this->config['similar_topics_type']), |
||
| 231 | 'S_PST_NO_SUPPORT' => !$this->fulltext_support_enabled(), |
||
| 232 | 'S_PST_NO_MYSQL' => !$this->fulltext->is_mysql(), |
||
| 233 | 'U_ACTION' => $this->u_action, |
||
| 234 | )); |
||
| 235 | |||
| 236 | $ignore_forums = explode(',', trim($this->config['similar_topics_ignore'])); |
||
| 237 | $noshow_forums = explode(',', trim($this->config['similar_topics_hide'])); |
||
| 238 | |||
| 239 | $forum_list = $this->get_forum_list(); |
||
| 240 | foreach ($forum_list as $row) |
||
| 241 | { |
||
| 242 | $this->template->assign_block_vars('forums', array( |
||
| 243 | 'FORUM_NAME' => $row['forum_name'], |
||
| 244 | 'FORUM_ID' => $row['forum_id'], |
||
| 245 | 'CHECKED_IGNORE_FORUM' => (in_array($row['forum_id'], $ignore_forums)) ? 'checked="checked"' : '', |
||
| 246 | 'CHECKED_NOSHOW_FORUM' => (in_array($row['forum_id'], $noshow_forums)) ? 'checked="checked"' : '', |
||
| 247 | 'S_IS_ADVANCED' => (bool) $row['similar_topic_forums'], |
||
| 248 | 'U_ADVANCED' => "{$this->u_action}&action=advanced&f=" . $row['forum_id'], |
||
| 249 | 'U_FORUM' => append_sid("{$this->root_path}viewforum.{$this->php_ext}", 'f=' . $row['forum_id']), |
||
| 250 | )); |
||
| 251 | } |
||
| 252 | break; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | |||
| 405 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state