Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like admin_controller 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 admin_controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class admin_controller implements admin_interface |
||
17 | { |
||
18 | /** @var \phpbb\cache\driver\driver_interface */ |
||
19 | protected $cache; |
||
20 | |||
21 | /** @var \phpbb\db\driver\driver_interface */ |
||
22 | protected $db; |
||
23 | |||
24 | /** @var \phpbb\group\helper */ |
||
25 | protected $group_helper; |
||
26 | |||
27 | /** @var \phpbb\language\language */ |
||
28 | protected $language; |
||
29 | |||
30 | /** @var \phpbb\log\log */ |
||
31 | protected $log; |
||
32 | |||
33 | /** @var \phpbb\autogroups\conditions\manager */ |
||
34 | protected $manager; |
||
35 | |||
36 | /** @var \phpbb\request\request */ |
||
37 | protected $request; |
||
38 | |||
39 | /** @var \phpbb\template\template */ |
||
40 | protected $template; |
||
41 | |||
42 | /** @var \phpbb\user */ |
||
43 | protected $user; |
||
44 | |||
45 | /** @var string The database table the auto group rules are stored in */ |
||
46 | protected $autogroups_rules_table; |
||
47 | |||
48 | /** @var string The database table the auto group types are stored in */ |
||
49 | protected $autogroups_types_table; |
||
50 | |||
51 | /** @var string Custom form action */ |
||
52 | protected $u_action; |
||
53 | |||
54 | /** |
||
55 | * Constructor |
||
56 | * |
||
57 | * @param \phpbb\cache\driver\driver_interface $cache Cache driver interface |
||
58 | * @param \phpbb\db\driver\driver_interface $db Database object |
||
59 | * @param \phpbb\group\helper $group_helper Group helper object |
||
60 | * @param \phpbb\language\language $language Language object |
||
61 | * @param \phpbb\log\log $log The phpBB log system |
||
62 | * @param \phpbb\autogroups\conditions\manager $manager Auto groups condition manager object |
||
63 | * @param \phpbb\request\request $request Request object |
||
64 | * @param \phpbb\template\template $template Template object |
||
65 | * @param \phpbb\user $user User object |
||
66 | * @param string $autogroups_rules_table Name of the table used to store auto group rules data |
||
67 | * @param string $autogroups_types_table Name of the table used to store auto group types data |
||
68 | * @access public |
||
69 | */ |
||
70 | public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\group\helper $group_helper, \phpbb\language\language $language, \phpbb\log\log $log, \phpbb\autogroups\conditions\manager $manager, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $autogroups_rules_table, $autogroups_types_table) |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function display_autogroups() |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function save_autogroup_rule($autogroups_id = 0) |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function delete_autogroup_rule($autogroups_id) |
||
201 | |||
202 | /** |
||
203 | * {@inheritdoc} |
||
204 | */ |
||
205 | public function resync_autogroup_rule($autogroups_id) |
||
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | public function submit_autogroups_options() |
||
246 | |||
247 | /** |
||
248 | * Submit auto group rule form data |
||
249 | * |
||
250 | * @param int $autogroups_id An auto group identifier |
||
251 | * A value of 0 is new, otherwise we're updating |
||
252 | * @return void |
||
253 | * @access protected |
||
254 | */ |
||
255 | protected function submit_autogroup_rule($autogroups_id = 0) |
||
256 | { |
||
257 | $data = array( |
||
258 | 'autogroups_type_id' => $this->request->variable('autogroups_type_id', 0), |
||
259 | 'autogroups_min_value' => $this->request->variable('autogroups_min_value', 0), |
||
260 | 'autogroups_max_value' => $this->request->variable('autogroups_max_value', 0), |
||
261 | 'autogroups_group_id' => $this->request->variable('autogroups_group_id', 0), |
||
262 | 'autogroups_default' => $this->request->variable('autogroups_default', false), |
||
263 | 'autogroups_notify' => $this->request->variable('autogroups_notify', false), |
||
264 | 'autogroups_excluded_groups' => $this->request->variable('autogroups_excluded_groups', array(0)), |
||
265 | ); |
||
266 | |||
267 | // Prevent form submit when no user groups are available or selected |
||
268 | if (!$data['autogroups_group_id']) |
||
269 | { |
||
270 | trigger_error($this->language->lang('ACP_AUTOGROUPS_INVALID_GROUPS') . $this->get_back_link($autogroups_id), E_USER_WARNING); |
||
271 | } |
||
272 | |||
273 | // Prevent form submit when min and max values are identical |
||
274 | if ($data['autogroups_min_value'] == $data['autogroups_max_value']) |
||
275 | { |
||
276 | trigger_error($this->language->lang('ACP_AUTOGROUPS_INVALID_RANGE') . $this->get_back_link($autogroups_id), E_USER_WARNING); |
||
277 | } |
||
278 | |||
279 | // Prevent form submit when the target group is also in the excluded groups array |
||
280 | if (in_array($data['autogroups_group_id'], $data['autogroups_excluded_groups'])) |
||
281 | { |
||
282 | trigger_error($this->language->lang('ACP_AUTOGROUPS_INVALID_EXCLUDE_GROUPS') . $this->get_back_link($autogroups_id), E_USER_WARNING); |
||
283 | } |
||
284 | |||
285 | // Format autogroups_excluded_groups for storage in the db |
||
286 | $data['autogroups_excluded_groups'] = !empty($data['autogroups_excluded_groups']) ? json_encode($data['autogroups_excluded_groups']) : ''; |
||
287 | |||
288 | if ($autogroups_id != 0) // Update existing auto group data |
||
289 | { |
||
290 | $sql = 'UPDATE ' . $this->autogroups_rules_table . ' |
||
291 | SET ' . $this->db->sql_build_array('UPDATE', $data) . ' |
||
292 | WHERE autogroups_id = ' . (int) $autogroups_id; |
||
293 | $this->db->sql_query($sql); |
||
294 | } |
||
295 | else // Insert new auto group data |
||
296 | { |
||
297 | $sql = 'INSERT INTO ' . $this->autogroups_rules_table . ' ' . $this->db->sql_build_array('INSERT', $data); |
||
298 | $this->db->sql_query($sql); |
||
299 | $autogroups_id = (int) $this->db->sql_nextid(); |
||
300 | } |
||
301 | |||
302 | // Apply the auto group to all users |
||
303 | $this->manager->sync_autogroups($autogroups_id); |
||
304 | |||
305 | // Log the action |
||
306 | $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_AUTOGROUPS_SAVED_LOG', time()); |
||
307 | |||
308 | // Output message to user after submitting the form |
||
309 | trigger_error($this->language->lang('ACP_AUTOGROUPS_SUBMIT_SUCCESS') . $this->get_back_link()); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Get one auto group rule from the database |
||
314 | * |
||
315 | * @param int $id An auto group rule identifier |
||
316 | * @return array An auto group rule and it's associated data |
||
317 | * @access public |
||
318 | */ |
||
319 | protected function get_autogroup($id) |
||
330 | |||
331 | /** |
||
332 | * Get all auto group rules from the database |
||
333 | * |
||
334 | * @return array Array of auto group rules and their associated data |
||
335 | * @access public |
||
336 | */ |
||
337 | protected function get_all_autogroups() |
||
357 | |||
358 | /** |
||
359 | * Set the user groups marked as exempt from default switching. |
||
360 | * Sets the 'autogroup_default_exempt' field for all groups in |
||
361 | * $group_ids to true, while all other groups are set to false. |
||
362 | * |
||
363 | * @param array $group_ids An array of group ids |
||
364 | * @param bool $flag True or false |
||
365 | */ |
||
366 | protected function set_exempt_groups($group_ids, $flag = true) |
||
379 | |||
380 | /** |
||
381 | * Get an array of user groups marked as exempt from default switching |
||
382 | * |
||
383 | * @return array An array of exempted groups: array('group_id' => 'group_name') |
||
384 | * @access protected |
||
385 | */ |
||
386 | protected function get_exempt_groups() |
||
397 | |||
398 | /** |
||
399 | * Get an array of user groups marked as excluded from auto grouping |
||
400 | * |
||
401 | * @param string $excluded_groups A json encoded string of an array of group ids |
||
402 | * @return array An array of groups: array('group_id' => 'group_name') |
||
403 | * @access protected |
||
404 | */ |
||
405 | protected function get_excluded_groups($excluded_groups) |
||
424 | |||
425 | /** |
||
426 | * Build template vars for a select menu of user groups |
||
427 | * |
||
428 | * @param array $selected An array of identifiers for selected group(s) |
||
429 | * @param bool $exclude_predefined_groups Exclude GROUP_SPECIAL |
||
430 | * @param string $block Name of the template block vars array |
||
431 | * @return void |
||
432 | * @access protected |
||
433 | */ |
||
434 | protected function build_groups_menu($selected, $exclude_predefined_groups = false, $block = 'groups') |
||
450 | |||
451 | /** |
||
452 | * Build template vars for a select menu of auto group conditions |
||
453 | * |
||
454 | * @param int $selected An identifier for the selected group |
||
455 | * @return void |
||
456 | * @access protected |
||
457 | */ |
||
458 | protected function build_conditions_menu($selected) |
||
472 | |||
473 | /** |
||
474 | * Get group data, always excluding BOTS, Guests |
||
475 | * |
||
476 | * @param string $where_sql Optional additional SQL where conditions |
||
477 | * @return array An array of group data rows (group_id, group_name, group_type) |
||
478 | */ |
||
479 | protected function query_groups($where_sql = '') |
||
492 | |||
493 | /** |
||
494 | * Return the ACP action back link. For editing an auto group it will take you |
||
495 | * back to that auto group's edit page. Otherwise it will take you back to main |
||
496 | * auto groups ACP page. |
||
497 | * |
||
498 | * @param int $autogroups_id |
||
499 | * @return string |
||
500 | */ |
||
501 | protected function get_back_link($autogroups_id = 0) |
||
507 | |||
508 | /** |
||
509 | * {@inheritdoc} |
||
510 | */ |
||
511 | public function set_page_url($u_action) |
||
515 | } |
||
516 |