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 | const DEFAULT_PRIORITY = 5; |
||
21 | |||
22 | /** @var \phpbb\template\template */ |
||
23 | protected $template; |
||
24 | |||
25 | /** @var \phpbb\user */ |
||
26 | protected $user; |
||
27 | |||
28 | /** @var \phpbb\request\request */ |
||
29 | protected $request; |
||
30 | |||
31 | /** @var \phpbb\ads\ad\manager */ |
||
32 | protected $manager; |
||
33 | |||
34 | /** @var \phpbb\ads\location\manager */ |
||
35 | protected $location_manager; |
||
36 | |||
37 | /** @var \phpbb\log\log */ |
||
38 | protected $log; |
||
39 | |||
40 | /** @var \phpbb\config\db_text */ |
||
41 | protected $config_text; |
||
42 | |||
43 | /** @var \phpbb\config\config */ |
||
44 | protected $config; |
||
45 | |||
46 | /** @var string php_ext */ |
||
47 | protected $php_ext; |
||
48 | |||
49 | /** @var string ext_path */ |
||
50 | protected $ext_path; |
||
51 | |||
52 | /** @var string Custom form action */ |
||
53 | protected $u_action; |
||
54 | |||
55 | /** @var array Form validation errors */ |
||
56 | protected $errors = array(); |
||
57 | |||
58 | /** |
||
59 | * Constructor |
||
60 | * |
||
61 | * @param \phpbb\template\template $template Template object |
||
62 | * @param \phpbb\user $user User object |
||
63 | * @param \phpbb\request\request $request Request object |
||
64 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
65 | * @param \phpbb\ads\location\manager $location_manager Template location manager object |
||
66 | * @param \phpbb\log\log $log The phpBB log system |
||
67 | * @param \phpbb\config\db_text $config_text Config text object |
||
68 | * @param \phpbb\config\config $config Config object |
||
69 | * @param string $php_ext PHP extension |
||
70 | * @param string $ext_path Path to this extension |
||
71 | */ |
||
72 | 38 | 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, \phpbb\config\config $config, $php_ext, $ext_path) |
|
73 | { |
||
74 | 38 | $this->template = $template; |
|
75 | 38 | $this->user = $user; |
|
76 | 38 | $this->request = $request; |
|
77 | 38 | $this->manager = $manager; |
|
78 | 38 | $this->location_manager = $location_manager; |
|
79 | 38 | $this->log = $log; |
|
80 | 38 | $this->config_text = $config_text; |
|
81 | 38 | $this->config = $config; |
|
82 | 38 | $this->php_ext = $php_ext; |
|
83 | 38 | $this->ext_path = $ext_path; |
|
84 | 38 | } |
|
85 | |||
86 | /** |
||
87 | * Process user request for manage mode |
||
88 | * |
||
89 | * @return void |
||
90 | */ |
||
91 | 6 | public function mode_manage() |
|
92 | { |
||
93 | 6 | $this->setup(); |
|
94 | |||
95 | // Trigger specific action |
||
96 | 6 | $action = $this->request->variable('action', ''); |
|
97 | 6 | if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete'))) |
|
98 | 6 | { |
|
99 | 5 | $this->{'action_' . $action}(); |
|
100 | 5 | } |
|
101 | |||
102 | // Otherwise default to this |
||
103 | 6 | $this->list_ads(); |
|
104 | 6 | } |
|
105 | |||
106 | /** |
||
107 | * Process user request for settings mode |
||
108 | * |
||
109 | * @return void |
||
110 | */ |
||
111 | 3 | public function mode_settings() |
|
112 | { |
||
113 | 3 | $this->setup(); |
|
114 | |||
115 | 3 | add_form_key('phpbb/ads/settings'); |
|
116 | 3 | if ($this->request->is_set_post('submit')) |
|
117 | 3 | { |
|
118 | // Validate form key |
||
119 | 2 | if (!check_form_key('phpbb/ads/settings')) |
|
120 | 2 | { |
|
121 | 1 | $this->errors[] = $this->user->lang('FORM_INVALID'); |
|
122 | 1 | } |
|
123 | |||
124 | 2 | if (empty($this->errors)) |
|
125 | 2 | { |
|
126 | 1 | $this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0)); |
|
127 | 1 | $this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0)))); |
|
128 | |||
129 | 1 | $this->success('ACP_AD_SETTINGS_SAVED'); |
|
130 | } |
||
131 | |||
132 | 1 | $this->template->assign_vars(array( |
|
133 | 1 | 'S_ERROR' => (bool) count($this->errors), |
|
134 | 1 | 'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '', |
|
135 | 1 | )); |
|
136 | 1 | } |
|
137 | |||
138 | 2 | $hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true); |
|
139 | 2 | $groups = $this->manager->load_groups(); |
|
140 | 2 | foreach ($groups as $group) |
|
141 | { |
||
142 | 2 | $group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name']; |
|
143 | |||
144 | 2 | $this->template->assign_block_vars('groups', array( |
|
145 | 2 | 'ID' => $group['group_id'], |
|
146 | 2 | 'NAME' => $group_name, |
|
147 | 2 | 'S_SELECTED' => in_array($group['group_id'], $hide_groups), |
|
148 | 2 | )); |
|
149 | 2 | } |
|
150 | |||
151 | 2 | $this->template->assign_vars(array( |
|
152 | 2 | 'U_ACTION' => $this->u_action, |
|
153 | 2 | 'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'], |
|
154 | 2 | )); |
|
155 | 2 | } |
|
156 | |||
157 | /** |
||
158 | * Set page url |
||
159 | * |
||
160 | * @param string $u_action Custom form action |
||
161 | * @return void |
||
162 | */ |
||
163 | 32 | public function set_page_url($u_action) |
|
164 | { |
||
165 | 32 | $this->u_action = $u_action; |
|
166 | 32 | } |
|
167 | |||
168 | /** |
||
169 | * Get ACP page title for Ads module |
||
170 | * |
||
171 | * @return string Language string for Ads ACP module |
||
172 | */ |
||
173 | 1 | public function get_page_title() |
|
174 | { |
||
175 | 1 | return $this->user->lang('ACP_PHPBB_ADS_TITLE'); |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Add an advertisement |
||
180 | * |
||
181 | * @return void |
||
182 | */ |
||
183 | 12 | public function action_add() |
|
184 | { |
||
185 | 9 | $preview = $this->request->is_set_post('preview'); |
|
186 | 9 | $submit = $this->request->is_set_post('submit'); |
|
187 | |||
188 | 9 | add_form_key('phpbb/ads/add'); |
|
189 | 9 | if ($preview || $submit) |
|
190 | 9 | { |
|
191 | 8 | $data = $this->get_form_data('phpbb/ads/add'); |
|
192 | |||
193 | View Code Duplication | if ($preview) |
|
194 | 8 | { |
|
195 | 1 | $this->ad_preview($data['ad_code']); |
|
196 | 12 | } |
|
197 | else if (empty($this->errors)) |
||
198 | 7 | { |
|
199 | 1 | $ad_id = $this->manager->insert_ad($data); |
|
200 | 1 | $this->manager->insert_ad_locations($ad_id, $data['ad_locations']); |
|
201 | |||
202 | 1 | $this->log('ADD', $data['ad_name']); |
|
203 | |||
204 | 1 | $this->success('ACP_AD_ADD_SUCCESS'); |
|
205 | } |
||
206 | |||
207 | 7 | $this->assign_locations($data); |
|
208 | 7 | $this->assign_form_data($data); |
|
209 | 7 | } |
|
210 | else |
||
211 | { |
||
212 | 1 | $this->assign_locations(); |
|
213 | } |
||
214 | |||
215 | // Set output vars for display in the template |
||
216 | 8 | $this->template->assign_vars(array( |
|
217 | 8 | 'S_ADD_AD' => true, |
|
218 | 8 | 'U_BACK' => $this->u_action, |
|
219 | 8 | 'U_ACTION' => "{$this->u_action}&action=add", |
|
220 | 8 | 'PICKER_DATE_FORMAT' => self::DATE_FORMAT, |
|
221 | 8 | )); |
|
222 | 8 | } |
|
223 | |||
224 | /** |
||
225 | * Edit an advertisement |
||
226 | * |
||
227 | * @return void |
||
228 | */ |
||
229 | 11 | public function action_edit() |
|
285 | |||
286 | /** |
||
287 | * Enable an advertisement |
||
288 | * |
||
289 | * @return void |
||
290 | */ |
||
291 | 3 | public function action_enable() |
|
295 | |||
296 | /** |
||
297 | * Disable an advertisement |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | 3 | public function action_disable() |
|
305 | |||
306 | /** |
||
307 | * Delete an advertisement |
||
308 | * |
||
309 | * @return void |
||
310 | */ |
||
311 | 3 | public function action_delete() |
|
351 | |||
352 | /** |
||
353 | * Display the ads |
||
354 | * |
||
355 | * @return void |
||
356 | */ |
||
357 | 1 | public function list_ads() |
|
384 | |||
385 | /** |
||
386 | * Perform general tasks |
||
387 | * |
||
388 | * @return void |
||
389 | */ |
||
390 | 9 | protected function setup() |
|
396 | |||
397 | /** |
||
398 | * Enable/disable an advertisement |
||
399 | * |
||
400 | * @param bool $enable Enable or disable the advertisement? |
||
401 | * @return void |
||
402 | */ |
||
403 | 4 | protected function ad_enable($enable) |
|
431 | |||
432 | /** |
||
433 | * Get admin form data. |
||
434 | * |
||
435 | * @param string $form_name The form name. |
||
436 | * @return array Form data |
||
437 | */ |
||
438 | 17 | protected function get_form_data($form_name) |
|
493 | |||
494 | /** |
||
495 | * Assign form data to the template. |
||
496 | * |
||
497 | * @param array $data The form data. |
||
498 | * @return void |
||
499 | */ |
||
500 | 15 | protected function assign_form_data($data) |
|
514 | |||
515 | /** |
||
516 | * Prepare end date for display |
||
517 | * |
||
518 | * @param mixed $end_date End date. |
||
519 | * @return string End date prepared for display. |
||
520 | */ |
||
521 | 15 | protected function prepare_end_date($end_date) |
|
535 | |||
536 | /** |
||
537 | * Assign template locations data to the template. |
||
538 | * |
||
539 | * @param mixed $data The form data or nothing. |
||
540 | * @return void |
||
541 | */ |
||
542 | 16 | protected function assign_locations($data = false) |
|
554 | |||
555 | /** |
||
556 | * Prepare advertisement preview |
||
557 | * |
||
558 | * @param string $code Ad code to preview |
||
559 | * @return void |
||
560 | */ |
||
561 | 2 | protected function ad_preview($code) |
|
565 | |||
566 | /** |
||
567 | * Print success message. |
||
568 | * |
||
569 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
570 | */ |
||
571 | 6 | protected function success() |
|
575 | |||
576 | /** |
||
577 | * Print error message. |
||
578 | * |
||
579 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
580 | */ |
||
581 | 5 | protected function error() |
|
585 | |||
586 | /** |
||
587 | * Log action |
||
588 | * |
||
589 | * @param string $action Performed action in uppercase |
||
590 | * @param string $ad_name Advertisement name |
||
591 | * @return void |
||
592 | */ |
||
593 | 3 | protected function log($action, $ad_name) |
|
597 | } |
||
598 |