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 |
||
17 | { |
||
18 | const MAX_NAME_LENGTH = 255; |
||
19 | const DATE_FORMAT = 'Y-m-d'; |
||
20 | |||
21 | /** @var \phpbb\template\template */ |
||
22 | protected $template; |
||
23 | |||
24 | /** @var \phpbb\user */ |
||
25 | protected $user; |
||
26 | |||
27 | /** @var \phpbb\request\request */ |
||
28 | protected $request; |
||
29 | |||
30 | /** @var \phpbb\ads\ad\manager */ |
||
31 | protected $manager; |
||
32 | |||
33 | /** @var \phpbb\ads\location\manager */ |
||
34 | protected $location_manager; |
||
35 | |||
36 | /** @var \phpbb\log\log */ |
||
37 | protected $log; |
||
38 | |||
39 | /** @var \phpbb\config\db_text */ |
||
40 | protected $config_text; |
||
41 | |||
42 | /** @var string php_ext */ |
||
43 | protected $php_ext; |
||
44 | |||
45 | /** @var string ext_path */ |
||
46 | protected $ext_path; |
||
47 | |||
48 | /** @var string Custom form action */ |
||
49 | protected $u_action; |
||
50 | |||
51 | /** @var array Form validation errors */ |
||
52 | protected $errors = array(); |
||
53 | |||
54 | /** |
||
55 | * Constructor |
||
56 | * |
||
57 | * @param \phpbb\template\template $template Template object |
||
58 | * @param \phpbb\user $user User object |
||
59 | * @param \phpbb\request\request $request Request object |
||
60 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
61 | * @param \phpbb\ads\location\manager $location_manager Template location manager object |
||
62 | * @param \phpbb\log\log $log The phpBB log system |
||
63 | * @param \phpbb\config\db_text $config_text Config text object |
||
64 | * @param string $php_ext PHP extension |
||
65 | * @param string $ext_path Path to this extension |
||
66 | */ |
||
67 | 33 | public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\log\log $log, \phpbb\config\db_text $config_text, $php_ext, $ext_path) |
|
79 | |||
80 | /** |
||
81 | * Process user request for manage mode |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | 6 | public function mode_manage() |
|
99 | |||
100 | /** |
||
101 | * Process user request for settings mode |
||
102 | * |
||
103 | * @return void |
||
104 | */ |
||
105 | 2 | public function mode_settings() |
|
106 | { |
||
107 | 2 | $this->setup(); |
|
108 | |||
109 | 2 | add_form_key('phpbb/ads/settings'); |
|
110 | 2 | if ($this->request->is_set_post('submit')) |
|
111 | 2 | { |
|
112 | // Validate form key |
||
113 | 1 | if (!check_form_key('phpbb/ads/settings')) |
|
114 | 1 | { |
|
115 | $this->errors[] = $this->user->lang('FORM_INVALID'); |
||
116 | } |
||
117 | |||
118 | 1 | if (empty($this->errors)) |
|
119 | 1 | { |
|
120 | 1 | $this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0)))); |
|
121 | |||
122 | 1 | $this->success('ACP_AD_SETTINGS_SAVED'); |
|
123 | } |
||
124 | |||
125 | $this->template->assign_vars(array( |
||
126 | 'S_ERROR' => (bool) count($this->errors), |
||
127 | 'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '', |
||
128 | )); |
||
129 | } |
||
130 | |||
131 | 1 | $hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true); |
|
132 | 1 | $groups = $this->manager->load_groups(); |
|
133 | 1 | foreach ($groups as $group) |
|
134 | { |
||
135 | 1 | $group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name']; |
|
136 | |||
137 | 1 | $this->template->assign_block_vars('groups', array( |
|
138 | 1 | 'ID' => $group['group_id'], |
|
139 | 1 | 'NAME' => $group_name, |
|
140 | 1 | 'S_SELECTED' => in_array($group['group_id'], $hide_groups), |
|
141 | 1 | )); |
|
142 | 1 | } |
|
143 | 1 | } |
|
144 | |||
145 | /** |
||
146 | * Set page url |
||
147 | * |
||
148 | * @param string $u_action Custom form action |
||
149 | * @return void |
||
150 | */ |
||
151 | 27 | public function set_page_url($u_action) |
|
155 | |||
156 | /** |
||
157 | * Get ACP page title for Ads module |
||
158 | * |
||
159 | * @return string Language string for Ads ACP module |
||
160 | */ |
||
161 | 1 | public function get_page_title() |
|
165 | |||
166 | /** |
||
167 | * Add an advertisement |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | 16 | public function action_add() |
|
210 | |||
211 | /** |
||
212 | * Edit an advertisement |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | 9 | public function action_edit() |
|
271 | |||
272 | /** |
||
273 | * Enable an advertisement |
||
274 | * |
||
275 | * @return void |
||
276 | */ |
||
277 | 3 | public function action_enable() |
|
281 | |||
282 | /** |
||
283 | * Disable an advertisement |
||
284 | * |
||
285 | * @return void |
||
286 | */ |
||
287 | 3 | public function action_disable() |
|
291 | |||
292 | /** |
||
293 | * Delete an advertisement |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | 3 | public function action_delete() |
|
337 | |||
338 | /** |
||
339 | * Display the ads |
||
340 | * |
||
341 | * @return void |
||
342 | */ |
||
343 | 1 | public function list_ads() |
|
370 | |||
371 | /** |
||
372 | * Perform general tasks |
||
373 | * |
||
374 | * @return void |
||
375 | */ |
||
376 | 8 | protected function setup() |
|
377 | { |
||
378 | 8 | $this->user->add_lang_ext('phpbb/ads', 'acp'); |
|
379 | |||
380 | 8 | $this->template->assign_vars(array( |
|
381 | 8 | 'S_PHPBB_ADS' => true, |
|
382 | 8 | 'U_ACTION' => $this->u_action, |
|
383 | 8 | )); |
|
384 | 8 | } |
|
385 | |||
386 | /** |
||
387 | * Enable/disable an advertisement |
||
388 | * |
||
389 | * @param bool $enable Enable or disable the advertisement? |
||
390 | * @return void |
||
391 | */ |
||
392 | 4 | protected function ad_enable($enable) |
|
420 | |||
421 | /** |
||
422 | * Get admin form data. |
||
423 | * |
||
424 | * @param string $form_name The form name. |
||
425 | * @return array Form data |
||
426 | */ |
||
427 | 13 | protected function get_form_data($form_name) |
|
475 | |||
476 | /** |
||
477 | * Assign form data to the template. |
||
478 | * |
||
479 | * @param array $data The form data. |
||
480 | * @return void |
||
481 | */ |
||
482 | 11 | protected function assign_form_data($data) |
|
495 | |||
496 | /** |
||
497 | * Prepare end date for display |
||
498 | * |
||
499 | * @param mixed $end_date End date. |
||
500 | * @return string End date prepared for display. |
||
501 | */ |
||
502 | 11 | protected function prepare_end_date($end_date) |
|
516 | |||
517 | /** |
||
518 | * Assign template locations data to the template. |
||
519 | * |
||
520 | * @param mixed $data The form data or nothing. |
||
521 | * @return void |
||
522 | */ |
||
523 | 12 | protected function assign_locations($data = false) |
|
535 | |||
536 | /** |
||
537 | * Prepare advertisement preview |
||
538 | * |
||
539 | * @param string $code Ad code to preview |
||
540 | * @return void |
||
541 | */ |
||
542 | 2 | protected function ad_preview($code) |
|
546 | |||
547 | /** |
||
548 | * Print success message. |
||
549 | * |
||
550 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
551 | */ |
||
552 | 6 | protected function success() |
|
556 | |||
557 | /** |
||
558 | * Print error message. |
||
559 | * |
||
560 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
561 | */ |
||
562 | 5 | protected function error() |
|
566 | |||
567 | /** |
||
568 | * Log action |
||
569 | * |
||
570 | * @param string $action Performed action in uppercase |
||
571 | * @param string $ad_name Advertisement name |
||
572 | * @return void |
||
573 | */ |
||
574 | 3 | protected function log($action, $ad_name) |
|
578 | } |
||
579 |