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