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