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 | |||
20 | /** @var \phpbb\template\template */ |
||
21 | protected $template; |
||
22 | |||
23 | /** @var \phpbb\user */ |
||
24 | protected $user; |
||
25 | |||
26 | /** @var \phpbb\request\request */ |
||
27 | protected $request; |
||
28 | |||
29 | /** @var \phpbb\admanagement\ad\manager */ |
||
30 | protected $manager; |
||
31 | |||
32 | /** @var \phpbb\admanagement\location\manager */ |
||
33 | protected $location_manager; |
||
34 | |||
35 | /** @var \phpbb\log\log */ |
||
36 | protected $log; |
||
37 | |||
38 | /** @var string php_ext */ |
||
39 | protected $php_ext; |
||
40 | |||
41 | /** @var string ext_path */ |
||
42 | protected $ext_path; |
||
43 | |||
44 | /** @var string Custom form action */ |
||
45 | protected $u_action; |
||
46 | |||
47 | /** @var array Form validation errors */ |
||
48 | protected $errors = array(); |
||
49 | |||
50 | /** |
||
51 | * Constructor |
||
52 | * |
||
53 | * @param \phpbb\template\template $template Template object |
||
54 | * @param \phpbb\user $user User object |
||
55 | * @param \phpbb\request\request $request Request object |
||
56 | * @param \phpbb\admanagement\ad\manager $manager Advertisement manager object |
||
57 | * @param \phpbb\admanagement\location\manager $location_manager Template location manager object |
||
58 | * @param \phpbb\log\log $log The phpBB log system |
||
59 | * @param string $php_ext PHP extension |
||
60 | * @param string $ext_path Path to this extension |
||
61 | */ |
||
62 | 29 | public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\admanagement\ad\manager $manager, \phpbb\admanagement\location\manager $location_manager, \phpbb\log\log $log, $php_ext, $ext_path) |
|
73 | |||
74 | /** |
||
75 | * Process user request |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | 6 | public function main() |
|
93 | |||
94 | /** |
||
95 | * Set page url |
||
96 | * |
||
97 | * @param string $u_action Custom form action |
||
98 | * @return void |
||
99 | */ |
||
100 | 23 | public function set_page_url($u_action) |
|
104 | |||
105 | /** |
||
106 | * Get ACP page title for Ads module |
||
107 | * |
||
108 | * @return string Language string for Ads ACP module |
||
109 | */ |
||
110 | 1 | public function get_page_title() |
|
114 | |||
115 | /** |
||
116 | * Add an advertisement |
||
117 | * |
||
118 | * @return void |
||
119 | */ |
||
120 | 6 | public function action_add() |
|
121 | { |
||
122 | 6 | $preview = $this->request->is_set_post('preview'); |
|
123 | 6 | $submit = $this->request->is_set_post('submit'); |
|
124 | |||
125 | 6 | add_form_key('phpbb/admanagement/add'); |
|
126 | 6 | if ($preview || $submit) |
|
127 | 6 | { |
|
128 | 5 | $data = $this->get_form_data(); |
|
129 | |||
130 | 5 | $this->validate($data, 'phpbb/admanagement/add'); |
|
131 | |||
132 | View Code Duplication | if ($preview) |
|
1 ignored issue
–
show
|
|||
133 | 5 | { |
|
134 | 1 | $this->ad_preview($data['ad_code']); |
|
135 | 1 | } |
|
136 | else if (empty($this->errors)) |
||
137 | 4 | { |
|
138 | 1 | $ad_id = $this->manager->insert_ad($data); |
|
139 | 1 | $this->manager->insert_ad_locations($ad_id, $data['ad_locations']); |
|
140 | |||
141 | 1 | $this->log('ADD', $data['ad_name']); |
|
142 | |||
143 | 1 | $this->success('ACP_AD_ADD_SUCCESS'); |
|
144 | } |
||
145 | |||
146 | 4 | $this->assign_locations($data); |
|
147 | 4 | $this->assign_form_data($data); |
|
148 | 4 | } |
|
149 | else |
||
150 | { |
||
151 | 1 | $this->assign_locations(); |
|
152 | } |
||
153 | |||
154 | // Set output vars for display in the template |
||
155 | 5 | $this->template->assign_vars(array( |
|
156 | 5 | 'S_ADD_AD' => true, |
|
157 | 5 | 'U_BACK' => $this->u_action, |
|
158 | 5 | )); |
|
159 | 5 | } |
|
160 | |||
161 | /** |
||
162 | * Edit an advertisement |
||
163 | * |
||
164 | * @return void |
||
165 | */ |
||
166 | 10 | public function action_edit() |
|
167 | { |
||
168 | 8 | $ad_id = $this->request->variable('id', 0); |
|
169 | 8 | $preview = $this->request->is_set_post('preview'); |
|
170 | 8 | $submit = $this->request->is_set_post('submit'); |
|
171 | |||
172 | 8 | add_form_key('phpbb/admanagement/edit/' . $ad_id); |
|
173 | 8 | if ($preview || $submit) |
|
174 | 8 | { |
|
175 | 6 | $data = $this->get_form_data(); |
|
176 | |||
177 | 6 | $this->validate($data, 'phpbb/admanagement/edit/' . $ad_id); |
|
178 | |||
179 | if ($preview) |
||
180 | 6 | { |
|
181 | 1 | $this->ad_preview($data['ad_code']); |
|
182 | 1 | } |
|
183 | View Code Duplication | else if (empty($this->errors)) |
|
1 ignored issue
–
show
|
|||
184 | 5 | { |
|
185 | 2 | $success = $this->manager->update_ad($ad_id, $data); |
|
186 | |||
187 | if ($success) |
||
188 | 2 | { |
|
189 | // Only insert new ad locations to DB when ad exists |
||
190 | 1 | $this->manager->delete_ad_locations($ad_id); |
|
191 | 1 | $this->manager->insert_ad_locations($ad_id, $data['ad_locations']); |
|
192 | |||
193 | 1 | $this->log('EDIT', $data['ad_name']); |
|
194 | |||
195 | 1 | $this->success('ACP_AD_EDIT_SUCCESS'); |
|
196 | 10 | } |
|
197 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
198 | } |
||
199 | 4 | } |
|
200 | else |
||
201 | { |
||
202 | // Load ad data |
||
203 | 2 | $data = $this->manager->get_ad($ad_id); |
|
204 | 2 | if (empty($data)) |
|
205 | 2 | { |
|
206 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
207 | } |
||
208 | |||
209 | // Load ad template locations |
||
210 | 1 | $data['ad_locations'] = $this->manager->get_ad_locations($ad_id); |
|
211 | } |
||
212 | |||
213 | // Set output vars for display in the template |
||
214 | 5 | $this->template->assign_vars(array( |
|
215 | 5 | 'S_EDIT_AD' => true, |
|
216 | 5 | 'EDIT_ID' => $ad_id, |
|
217 | 5 | 'U_BACK' => $this->u_action, |
|
218 | 5 | )); |
|
219 | 5 | $this->assign_locations($data); |
|
220 | 5 | $this->assign_form_data($data); |
|
221 | 5 | } |
|
222 | |||
223 | /** |
||
224 | * Enable an advertisement |
||
225 | * |
||
226 | * @return void |
||
227 | */ |
||
228 | 3 | public function action_enable() |
|
232 | |||
233 | /** |
||
234 | * Disable an advertisement |
||
235 | * |
||
236 | * @return void |
||
237 | */ |
||
238 | 3 | public function action_disable() |
|
242 | |||
243 | /** |
||
244 | * Delete an advertisement |
||
245 | * |
||
246 | * @return void |
||
247 | */ |
||
248 | 3 | public function action_delete() |
|
288 | |||
289 | /** |
||
290 | * Display the ads |
||
291 | * |
||
292 | * @return void |
||
293 | */ |
||
294 | 1 | public function list_ads() |
|
314 | |||
315 | /** |
||
316 | * Enable/disable an advertisement |
||
317 | * |
||
318 | * @param bool $enable Enable or disable the advertisement? |
||
319 | * @return void |
||
320 | */ |
||
321 | 4 | protected function ad_enable($enable) |
|
349 | |||
350 | /** |
||
351 | * Get admin form data. |
||
352 | * |
||
353 | * @return array Form data |
||
354 | */ |
||
355 | 11 | protected function get_form_data() |
|
365 | |||
366 | /** |
||
367 | * Validate form data. |
||
368 | * |
||
369 | * @param array $data The form data. |
||
370 | * @param string $form_name The form name. |
||
371 | * @return void |
||
372 | */ |
||
373 | 11 | protected function validate($data, $form_name) |
|
389 | |||
390 | /** |
||
391 | * Assign form data to the template. |
||
392 | * |
||
393 | * @param array $data The form data. |
||
394 | * @return void |
||
395 | */ |
||
396 | 9 | protected function assign_form_data($data) |
|
408 | |||
409 | /** |
||
410 | * Assign template locations data to the template. |
||
411 | * |
||
412 | * @param mixed $data The form data or nothing. |
||
413 | * @return void |
||
414 | */ |
||
415 | 10 | protected function assign_locations($data = false) |
|
427 | |||
428 | /** |
||
429 | * Prepare advertisement preview |
||
430 | * |
||
431 | * @param string $code Ad code to preview |
||
432 | * @return void |
||
433 | */ |
||
434 | 2 | protected function ad_preview($code) |
|
435 | { |
||
436 | 2 | $this->template->assign_var('PREVIEW', htmlspecialchars_decode($code)); |
|
437 | 2 | } |
|
438 | |||
439 | /** |
||
440 | * Print success message. |
||
441 | * |
||
442 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
443 | */ |
||
444 | 5 | protected function success() |
|
448 | |||
449 | /** |
||
450 | * Print error message. |
||
451 | * |
||
452 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
453 | */ |
||
454 | 5 | protected function error() |
|
458 | |||
459 | /** |
||
460 | * Log action |
||
461 | * |
||
462 | * @param string $action Performed action in uppercase |
||
463 | * @param string $ad_name Advertisement name |
||
464 | * @return void |
||
465 | */ |
||
466 | 3 | protected function log($action, $ad_name) |
|
470 | } |
||
471 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.