Total Complexity | 40 |
Total Lines | 435 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like similar_topics_admin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use similar_topics_admin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class similar_topics_admin |
||
26 | { |
||
27 | /** @var cache */ |
||
28 | protected $cache; |
||
29 | |||
30 | /** @var config */ |
||
31 | protected $config; |
||
32 | |||
33 | /** @var db_text */ |
||
34 | protected $config_text; |
||
35 | |||
36 | /** @var dbal */ |
||
37 | protected $db; |
||
38 | |||
39 | /** @var language */ |
||
40 | protected $language; |
||
41 | |||
42 | /** @var log */ |
||
43 | protected $log; |
||
44 | |||
45 | /** @var request */ |
||
46 | protected $request; |
||
47 | |||
48 | /** @var similartopics */ |
||
49 | protected $similartopics; |
||
50 | |||
51 | /** @var template */ |
||
52 | protected $template; |
||
53 | |||
54 | /** @var user */ |
||
55 | protected $user; |
||
56 | |||
57 | /** @var string */ |
||
58 | protected $root_path; |
||
59 | |||
60 | /** @var string */ |
||
61 | protected $php_ext; |
||
62 | |||
63 | /** @var array */ |
||
64 | protected $times; |
||
65 | |||
66 | /** @var string */ |
||
67 | protected $form_key; |
||
68 | |||
69 | /** @var string */ |
||
70 | public $u_action; |
||
71 | |||
72 | /** |
||
73 | * Admin controller constructor |
||
74 | * |
||
75 | * @access public |
||
76 | * @param cache $cache |
||
77 | * @param config $config |
||
78 | * @param db_text $config_text |
||
79 | * @param dbal $db |
||
80 | * @param manager $similartopics |
||
81 | * @param language $language |
||
82 | * @param log $log |
||
83 | * @param request $request |
||
84 | * @param template $template |
||
85 | * @param user $user |
||
86 | * @param string $root_path |
||
87 | * @param string $php_ext |
||
88 | */ |
||
89 | public function __construct(cache $cache, config $config, db_text $config_text, dbal $db, manager $similartopics, language $language, log $log, request $request, template $template, user $user, $root_path, $php_ext) |
||
90 | { |
||
91 | $this->cache = $cache; |
||
92 | $this->config = $config; |
||
93 | $this->config_text = $config_text; |
||
94 | $this->db = $db; |
||
95 | $this->similartopics = $similartopics->get_driver($this->db->get_sql_layer()); |
||
96 | $this->language = $language; |
||
97 | $this->log = $log; |
||
98 | $this->request = $request; |
||
99 | $this->template = $template; |
||
100 | $this->user = $user; |
||
101 | $this->root_path = $root_path; |
||
102 | $this->php_ext = $php_ext; |
||
103 | $this->form_key = 'acp_similar_topics'; |
||
104 | $this->times = [ |
||
105 | 'd' => 86400, // one day |
||
106 | 'w' => 604800, // one week |
||
107 | 'm' => 2626560, // one month |
||
108 | 'y' => 31536000, // one year |
||
109 | ]; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Set the u_action variable from the form/module |
||
114 | * |
||
115 | * @access public |
||
116 | * @param string $u_action |
||
117 | * |
||
118 | * @return similar_topics_admin $this |
||
119 | */ |
||
120 | public function set_u_action($u_action) |
||
121 | { |
||
122 | $this->u_action = $u_action; |
||
123 | return $this; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Controller handler. Call this method from the ACP module. |
||
128 | * |
||
129 | * @access public |
||
130 | */ |
||
131 | public function handle() |
||
144 | } |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Display/Save default settings |
||
149 | * |
||
150 | * @access protected |
||
151 | */ |
||
152 | protected function default_settings() |
||
153 | { |
||
154 | if ($this->request->is_set_post('submit')) |
||
155 | { |
||
156 | $this->check_form_key($this->form_key); |
||
157 | |||
158 | // Set basic config settings |
||
159 | $this->config->set('similar_topics', $this->request->variable('pst_enable', 0)); |
||
160 | $this->config->set('similar_topics_limit', abs($this->request->variable('pst_limit', 0))); // use abs for positive values only |
||
161 | $this->config->set('similar_topics_cache', abs($this->request->variable('pst_cache', 0))); // use abs for positive values only |
||
162 | $this->config_text_set('similar_topics_words', $this->request->variable('pst_words', '', true)); |
||
163 | |||
164 | // Set sensitivity |
||
165 | $pst_sense = min(abs($this->request->variable('pst_sense', 5)), 10); // use abs for positive values only |
||
166 | $this->config->set('similar_topics_sense', $pst_sense); |
||
167 | |||
168 | // Set date/time config settings |
||
169 | $pst_time = abs($this->request->variable('pst_time', 0)); // use abs for positive values only |
||
170 | $pst_time_type = $this->request->variable('pst_time_type', ''); |
||
171 | $this->config->set('similar_topics_type', $pst_time_type); |
||
172 | $this->config->set('similar_topics_time', $this->set_pst_time($pst_time, $pst_time_type)); |
||
173 | |||
174 | // Set checkbox array form data |
||
175 | $this->update_forum('similar_topics_hide', $this->request->variable('mark_noshow_forum', array(0), true)); |
||
176 | $this->update_forum('similar_topics_ignore', $this->request->variable('mark_ignore_forum', array(0), true)); |
||
177 | |||
178 | // Set PostgreSQL TS Name |
||
179 | if ($this->similartopics && $this->similartopics->get_type() === 'postgres') |
||
180 | { |
||
181 | $ts_name = $this->request->variable('pst_postgres_ts_name', ($this->config['pst_postgres_ts_name'] ?: 'simple')); |
||
182 | $this->config->set('pst_postgres_ts_name', $ts_name); |
||
183 | $this->similartopics->create_fulltext_index('topic_title'); |
||
184 | } |
||
185 | |||
186 | $this->cache->destroy('sql', TOPICS_TABLE); |
||
187 | |||
188 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG'); |
||
189 | |||
190 | $this->end('PST_SAVED'); |
||
191 | } |
||
192 | |||
193 | // Build the time options select menu |
||
194 | $time_options = array( |
||
195 | 'd' => 'PST_DAYS', |
||
196 | 'w' => 'PST_WEEKS', |
||
197 | 'm' => 'PST_MONTHS', |
||
198 | 'y' => 'PST_YEARS', |
||
199 | ); |
||
200 | foreach ($time_options as $value => $label) |
||
201 | { |
||
202 | $this->template->assign_block_vars('similar_time_options', array( |
||
203 | 'VALUE' => $value, |
||
204 | 'LABEL' => $label, |
||
205 | 'S_SELECTED' => $value === $this->config['similar_topics_type'], |
||
206 | )); |
||
207 | } |
||
208 | |||
209 | $this->template->assign_vars(array( |
||
210 | 'S_PST_ENABLE' => $this->isset_or_default($this->config['similar_topics'], false), |
||
211 | 'PST_LIMIT' => $this->isset_or_default($this->config['similar_topics_limit'], ''), |
||
212 | 'PST_CACHE' => $this->isset_or_default($this->config['similar_topics_cache'], ''), |
||
213 | 'PST_SENSE' => $this->isset_or_default($this->config['similar_topics_sense'], ''), |
||
214 | 'PST_WORDS' => $this->isset_or_default($this->config_text_get('similar_topics_words'), ''), |
||
215 | 'PST_TIME' => $this->get_pst_time($this->config['similar_topics_time'], $this->config['similar_topics_type']), |
||
216 | 'PST_SENSITIVITY' => $this->similartopics && $this->similartopics->get_engine() === 'innodb' ? 1 : 5, |
||
217 | 'S_PST_NO_COMPAT' => $this->similartopics === null || !$this->similartopics->is_fulltext('topic_title'), |
||
218 | 'U_ACTION' => $this->u_action, |
||
219 | )); |
||
220 | |||
221 | // If postgresql, we need to make an options list of text search names |
||
222 | if ($this->similartopics && $this->similartopics->get_type() === 'postgres') |
||
223 | { |
||
224 | $this->language->add_lang('acp/search'); |
||
225 | foreach ($this->get_cfgname_list() as $row) |
||
226 | { |
||
227 | $this->template->assign_block_vars('postgres_ts_names', array( |
||
228 | 'NAME' => $row['ts_name'], |
||
229 | 'S_SELECTED' => $row['ts_name'] === $this->config['pst_postgres_ts_name'], |
||
230 | )); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | $forum_list = $this->get_forum_list(); |
||
235 | foreach ($forum_list as $row) |
||
236 | { |
||
237 | $this->template->assign_block_vars('forums', array( |
||
238 | 'FORUM_NAME' => $row['forum_name'], |
||
239 | 'FORUM_ID' => $row['forum_id'], |
||
240 | 'CHECKED_IGNORE_FORUM' => $row['similar_topics_ignore'] ? 'checked="checked"' : '', |
||
241 | 'CHECKED_NOSHOW_FORUM' => $row['similar_topics_hide'] ? 'checked="checked"' : '', |
||
242 | 'S_IS_ADVANCED' => (bool) $row['similar_topic_forums'], |
||
243 | 'U_ADVANCED' => "$this->u_action&action=advanced&f=" . $row['forum_id'], |
||
244 | 'U_FORUM' => append_sid("{$this->root_path}viewforum.$this->php_ext", 'f=' . $row['forum_id']), |
||
245 | )); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Display/Save advanced settings |
||
251 | * |
||
252 | * @access protected |
||
253 | */ |
||
254 | protected function advanced_settings() |
||
255 | { |
||
256 | $forum_id = $this->request->variable('f', 0); |
||
257 | |||
258 | if ($this->request->is_set_post('submit')) |
||
259 | { |
||
260 | $this->check_form_key($this->form_key); |
||
261 | |||
262 | $similar_topic_forums = $this->request->variable('similar_forums_id', array(0)); |
||
263 | $similar_topic_forums = !empty($similar_topic_forums) ? json_encode($similar_topic_forums) : ''; |
||
264 | |||
265 | $sql = 'UPDATE ' . FORUMS_TABLE . " |
||
266 | SET similar_topic_forums = '" . $this->db->sql_escape($similar_topic_forums) . "' |
||
267 | WHERE forum_id = $forum_id"; |
||
268 | $this->db->sql_query($sql); |
||
269 | |||
270 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'PST_LOG_MSG'); |
||
271 | |||
272 | $this->end('PST_SAVED'); |
||
273 | } |
||
274 | |||
275 | $forum_name = ''; |
||
276 | $selected = array(); |
||
277 | if ($forum_id > 0) |
||
278 | { |
||
279 | $sql = 'SELECT forum_name, similar_topic_forums |
||
280 | FROM ' . FORUMS_TABLE . " |
||
281 | WHERE forum_id = $forum_id"; |
||
282 | $result = $this->db->sql_query($sql); |
||
283 | while ($fid = $this->db->sql_fetchrow($result)) |
||
284 | { |
||
285 | $selected = json_decode($fid['similar_topic_forums'], true); |
||
286 | $forum_name = $fid['forum_name']; |
||
287 | } |
||
288 | $this->db->sql_freeresult($result); |
||
289 | } |
||
290 | |||
291 | $this->template->assign_vars(array( |
||
292 | 'S_ADVANCED_SETTINGS' => true, |
||
293 | 'SIMILAR_FORUMS_OPTIONS' => make_forum_select($selected, false, false, true), |
||
294 | 'PST_FORUM_NAME' => $forum_name, |
||
295 | 'U_ACTION' => $this->u_action . '&action=advanced&f=' . $forum_id, |
||
296 | 'U_BACK' => $this->u_action, |
||
297 | )); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * Check form key, trigger error if invalid |
||
302 | * |
||
303 | * @access protected |
||
304 | * @param string $form_key The form key value |
||
305 | */ |
||
306 | protected function check_form_key($form_key) |
||
307 | { |
||
308 | if (!check_form_key($form_key)) |
||
309 | { |
||
310 | $this->end('FORM_INVALID', E_USER_WARNING); |
||
311 | } |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Get list of PostgreSQL text search names |
||
316 | * |
||
317 | * @access protected |
||
318 | * @return array array of text search names |
||
319 | */ |
||
320 | protected function get_cfgname_list() |
||
321 | { |
||
322 | $sql = 'SELECT cfgname AS ts_name FROM pg_ts_config'; |
||
323 | $result = $this->db->sql_query($sql); |
||
324 | $ts_options = $this->db->sql_fetchrowset($result); |
||
325 | $this->db->sql_freeresult($result); |
||
326 | |||
327 | return $ts_options; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Get forums list |
||
332 | * |
||
333 | * @access protected |
||
334 | * @return array forum data rows |
||
335 | */ |
||
336 | protected function get_forum_list() |
||
337 | { |
||
338 | $sql = 'SELECT forum_id, forum_name, similar_topic_forums, similar_topics_hide, similar_topics_ignore |
||
339 | FROM ' . FORUMS_TABLE . ' |
||
340 | WHERE forum_type = ' . FORUM_POST . ' |
||
341 | ORDER BY left_id ASC'; |
||
342 | $result = $this->db->sql_query($sql); |
||
343 | $forum_list = $this->db->sql_fetchrowset($result); |
||
344 | $this->db->sql_freeresult($result); |
||
345 | |||
346 | return $forum_list; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Update the similar topics columns in the forums table |
||
351 | * |
||
352 | * @access protected |
||
353 | * @param string $column The name of the column to update |
||
354 | * @param array $forum_ids An array of forum_ids |
||
355 | */ |
||
356 | protected function update_forum($column, $forum_ids) |
||
357 | { |
||
358 | $this->db->sql_transaction('begin'); |
||
359 | |||
360 | // Set marked forums (in set) to 1 |
||
361 | $sql = 'UPDATE ' . FORUMS_TABLE . " |
||
362 | SET $column = 1 |
||
363 | WHERE " . $this->db->sql_in_set('forum_id', $forum_ids, false, true); |
||
364 | $this->db->sql_query($sql); |
||
365 | |||
366 | // Set unmarked forums (not in set) to 0 |
||
367 | $sql = 'UPDATE ' . FORUMS_TABLE . " |
||
368 | SET $column = 0 |
||
369 | WHERE " . $this->db->sql_in_set('forum_id', $forum_ids, true, true); |
||
370 | $this->db->sql_query($sql); |
||
371 | |||
372 | $this->db->sql_transaction('commit'); |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * Store a config_text item in the database. |
||
377 | * |
||
378 | * @access protected |
||
379 | * @param string $name Name of a config_text item |
||
380 | * @param string $value Value of a config_text item |
||
381 | */ |
||
382 | protected function config_text_set($name, $value) |
||
383 | { |
||
384 | $this->config_text->set($name, $value); |
||
385 | $this->cache->put("_$name", $value); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * Get a config_text value from the cache if it is cached, otherwise |
||
390 | * get it directly from the database. |
||
391 | * |
||
392 | * @access protected |
||
393 | * @param string $name Name of a config_text item |
||
394 | * @return string|null Value of a config_text item, either cached or from db |
||
395 | */ |
||
396 | protected function config_text_get($name) |
||
397 | { |
||
398 | if (($value = $this->cache->get("_$name")) === false) |
||
399 | { |
||
400 | $value = $this->config_text->get($name); |
||
401 | |||
402 | $this->cache->put("_$name", $value); |
||
403 | } |
||
404 | |||
405 | return !empty($value) ? $value : null; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Calculate the time in seconds based on requested time period length |
||
410 | * |
||
411 | * @access protected |
||
412 | * @param int $length user entered value |
||
413 | * @param string $type years, months, weeks, days (y|m|w|d) |
||
414 | * @return int time in seconds |
||
415 | */ |
||
416 | protected function set_pst_time($length, $type = 'y') |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Get the correct time period length value for the form |
||
425 | * |
||
426 | * @access protected |
||
427 | * @param int $time as a timestamp |
||
428 | * @param string $type years, months, weeks, days (y|m|w|d) |
||
429 | * @return int time converted to the given $type |
||
430 | */ |
||
431 | protected function get_pst_time($time, $type = '') |
||
432 | { |
||
433 | return isset($this->times[$type]) ? (int) round($time / $this->times[$type]) : 0; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Return a variable if it is set, otherwise default |
||
438 | * |
||
439 | * @access protected |
||
440 | * @param mixed $var The variable to test |
||
441 | * @param mixed $default The default value to use |
||
442 | * @return mixed The value of the variable if set, otherwise default value |
||
443 | */ |
||
444 | protected function isset_or_default($var, $default) |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * End script execution with a trigger_error message |
||
451 | * |
||
452 | * @access protected |
||
453 | * @param string $message Language key string |
||
454 | * @param int $code E_USER_NOTICE|E_USER_WARNING |
||
455 | * @return void |
||
456 | */ |
||
457 | protected function end($message, $code = E_USER_NOTICE) |
||
462 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths