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()  | 
            ||
| 257 | |||
| 258 | /**  | 
            ||
| 259 | * Submit auto group rule form data  | 
            ||
| 260 | *  | 
            ||
| 261 | * @param int $autogroups_id An auto group identifier  | 
            ||
| 262 | * A value of 0 is new, otherwise we're updating  | 
            ||
| 263 | * @return void  | 
            ||
| 264 | * @access protected  | 
            ||
| 265 | */  | 
            ||
| 266 | protected function submit_autogroup_rule($autogroups_id = 0)  | 
            ||
| 322 | |||
| 323 | /**  | 
            ||
| 324 | * Get one auto group rule from the database  | 
            ||
| 325 | *  | 
            ||
| 326 | * @param int $id An auto group rule identifier  | 
            ||
| 327 | * @return array An auto group rule and it's associated data  | 
            ||
| 328 | * @access public  | 
            ||
| 329 | */  | 
            ||
| 330 | protected function get_autogroup($id)  | 
            ||
| 341 | |||
| 342 | /**  | 
            ||
| 343 | * Get all auto group rules from the database  | 
            ||
| 344 | *  | 
            ||
| 345 | * @return array Array of auto group rules and their associated data  | 
            ||
| 346 | * @access public  | 
            ||
| 347 | */  | 
            ||
| 348 | protected function get_all_autogroups()  | 
            ||
| 368 | |||
| 369 | /**  | 
            ||
| 370 | * Get an array of user groups marked as exempt from default switching  | 
            ||
| 371 | *  | 
            ||
| 372 | 	 * @return array An array of exempted groups: array('group_id' => 'group_name') | 
            ||
| 373 | * @access protected  | 
            ||
| 374 | */  | 
            ||
| 375 | protected function get_exempt_groups()  | 
            ||
| 376 | 	{ | 
            ||
| 377 | $groups = array();  | 
            ||
| 378 | |||
| 379 | View Code Duplication | 		foreach ($this->query_groups('autogroup_default_exempt = 1') as $row) | 
            |
| 380 | 		{ | 
            ||
| 381 | 			$groups[$row['group_id']] = $this->group_helper->get_name_string('full', $row['group_id'], $row['group_name'], $row['group_colour']); | 
            ||
| 382 | }  | 
            ||
| 383 | |||
| 384 | return $groups;  | 
            ||
| 385 | }  | 
            ||
| 386 | |||
| 387 | /**  | 
            ||
| 388 | * Get an array of user groups marked as excluded from auto grouping  | 
            ||
| 389 | *  | 
            ||
| 390 | * @param string $excluded_groups A json encoded string of an array of group ids  | 
            ||
| 391 | 	 * @return array An array of groups: array('group_id' => 'group_name') | 
            ||
| 392 | * @access protected  | 
            ||
| 393 | */  | 
            ||
| 394 | protected function get_excluded_groups($excluded_groups)  | 
            ||
| 395 | 	{ | 
            ||
| 396 | $groups = array();  | 
            ||
| 397 | |||
| 398 | if (!empty($excluded_groups))  | 
            ||
| 399 | 		{ | 
            ||
| 400 | $excluded_groups = json_decode($excluded_groups, true);  | 
            ||
| 401 | |||
| 402 | View Code Duplication | foreach ($this->query_groups() as $row)  | 
            |
| 403 | 			{ | 
            ||
| 404 | if (in_array($row['group_id'], $excluded_groups))  | 
            ||
| 405 | 				{ | 
            ||
| 406 | 					$groups[$row['group_id']] = $this->group_helper->get_name_string('full', $row['group_id'], $row['group_name'], $row['group_colour']); | 
            ||
| 407 | }  | 
            ||
| 408 | }  | 
            ||
| 409 | }  | 
            ||
| 410 | |||
| 411 | return $groups;  | 
            ||
| 412 | }  | 
            ||
| 413 | |||
| 414 | /**  | 
            ||
| 415 | * Build template vars for a select menu of user groups  | 
            ||
| 416 | *  | 
            ||
| 417 | * @param array $selected An array of identifiers for selected group(s)  | 
            ||
| 418 | * @param bool $exclude_predefined_groups Exclude GROUP_SPECIAL  | 
            ||
| 419 | * @param string $block Name of the template block vars array  | 
            ||
| 420 | * @return void  | 
            ||
| 421 | * @access protected  | 
            ||
| 422 | */  | 
            ||
| 423 | protected function build_groups_menu($selected, $exclude_predefined_groups = false, $block = 'groups')  | 
            ||
| 439 | |||
| 440 | /**  | 
            ||
| 441 | * Build template vars for a select menu of auto group conditions  | 
            ||
| 442 | *  | 
            ||
| 443 | * @param int $selected An identifier for the selected group  | 
            ||
| 444 | * @return void  | 
            ||
| 445 | * @access protected  | 
            ||
| 446 | */  | 
            ||
| 447 | protected function build_conditions_menu($selected)  | 
            ||
| 461 | |||
| 462 | /**  | 
            ||
| 463 | * Get group data, always excluding BOTS, Guests  | 
            ||
| 464 | *  | 
            ||
| 465 | * @param string $where_sql Optional additional SQL where conditions  | 
            ||
| 466 | * @return array An array of group data rows (group_id, group_name, group_type)  | 
            ||
| 467 | */  | 
            ||
| 468 | protected function query_groups($where_sql = '')  | 
            ||
| 481 | |||
| 482 | /**  | 
            ||
| 483 | 	 * {@inheritdoc} | 
            ||
| 484 | */  | 
            ||
| 485 | public function set_page_url($u_action)  | 
            ||
| 489 | }  | 
            ||
| 490 |