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 |
||
18 | class admin_controller |
||
19 | { |
||
20 | /** @var array|bool Form data */ |
||
21 | protected $data = false; |
||
22 | |||
23 | /** @var \phpbb\template\template */ |
||
24 | protected $template; |
||
25 | |||
26 | /** @var \phpbb\language\language */ |
||
27 | protected $language; |
||
28 | |||
29 | /** @var \phpbb\request\request */ |
||
30 | protected $request; |
||
31 | |||
32 | /** @var \phpbb\ads\ad\manager */ |
||
33 | protected $manager; |
||
34 | |||
35 | /** @var \phpbb\config\db_text */ |
||
36 | protected $config_text; |
||
37 | |||
38 | /** @var \phpbb\config\config */ |
||
39 | protected $config; |
||
40 | |||
41 | /** @var \phpbb\group\helper */ |
||
42 | protected $group_helper; |
||
43 | |||
44 | /** @var \phpbb\ads\controller\admin_input */ |
||
45 | protected $input; |
||
46 | |||
47 | /** @var \phpbb\ads\controller\admin_helper */ |
||
48 | protected $helper; |
||
49 | |||
50 | /** @var \phpbb\ads\analyser\manager */ |
||
51 | protected $analyser; |
||
52 | |||
53 | /** @var string Custom form action */ |
||
54 | protected $u_action; |
||
55 | |||
56 | /** |
||
57 | * Constructor |
||
58 | * |
||
59 | * @param \phpbb\template\template $template Template object |
||
60 | * @param \phpbb\language\language $language Language object |
||
61 | * @param \phpbb\request\request $request Request object |
||
62 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
63 | * @param \phpbb\config\db_text $config_text Config text object |
||
64 | * @param \phpbb\config\config $config Config object |
||
65 | * @param \phpbb\group\helper $group_helper Group helper object |
||
66 | * @param \phpbb\ads\controller\admin_input $input Admin input object |
||
67 | * @param \phpbb\ads\controller\admin_helper $helper Admin helper object |
||
68 | * @param \phpbb\ads\analyser\manager $analyser Ad code analyser object |
||
69 | * @param string $root_path phpBB root path |
||
70 | * @param string $php_ext PHP extension |
||
71 | */ |
||
72 | 33 | public function __construct(\phpbb\template\template $template, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\group\helper $group_helper, \phpbb\ads\controller\admin_input $input, \phpbb\ads\controller\admin_helper $helper, \phpbb\ads\analyser\manager $analyser, $root_path, $php_ext) |
|
95 | |||
96 | /** |
||
97 | * Set page url |
||
98 | * |
||
99 | * @param string $u_action Custom form action |
||
100 | * @return void |
||
101 | */ |
||
102 | 27 | public function set_page_url($u_action) |
|
106 | |||
107 | /** |
||
108 | * Get ACP page title for Ads module |
||
109 | * |
||
110 | * @return string Language string for Ads ACP module |
||
111 | */ |
||
112 | 1 | public function get_page_title() |
|
116 | |||
117 | /** |
||
118 | * Process user request for settings mode |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | 3 | public function mode_settings() |
|
158 | |||
159 | /** |
||
160 | * Process user request for manage mode |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | 29 | public function mode_manage() |
|
178 | |||
179 | /** |
||
180 | * Add an advertisement |
||
181 | * |
||
182 | * @return void |
||
183 | */ |
||
184 | 12 | protected function action_add() |
|
207 | |||
208 | /** |
||
209 | * Edit an advertisement |
||
210 | * |
||
211 | * @return void |
||
212 | */ |
||
213 | 7 | protected function action_edit() |
|
244 | |||
245 | /** |
||
246 | * Enable an advertisement |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | 4 | protected function action_enable() |
|
254 | |||
255 | /** |
||
256 | * Disable an advertisement |
||
257 | * |
||
258 | * @return void |
||
259 | */ |
||
260 | 4 | protected function action_disable() |
|
264 | |||
265 | /** |
||
266 | * Delete an advertisement |
||
267 | * |
||
268 | * @return void |
||
269 | */ |
||
270 | 3 | protected function action_delete() |
|
310 | |||
311 | /** |
||
312 | * Display the list of all ads |
||
313 | * |
||
314 | * @return void |
||
315 | */ |
||
316 | 1 | protected function list_ads() |
|
352 | |||
353 | /** |
||
354 | * Get what action user wants to do with the form. |
||
355 | * Possible options are: |
||
356 | * - preview ad code |
||
357 | * - upload banner to display in an ad code |
||
358 | * - analyse ad code |
||
359 | * - submit form (either add or edit an ad) |
||
360 | * |
||
361 | * @return mixed Action name or false when no action was submitted |
||
362 | */ |
||
363 | 13 | protected function get_submitted_action() |
|
376 | |||
377 | /** |
||
378 | * Enable/disable an advertisement |
||
379 | * |
||
380 | * @param bool $enable Enable or disable the advertisement? |
||
381 | * @return void |
||
382 | */ |
||
383 | 6 | protected function ad_enable($enable) |
|
411 | |||
412 | /** |
||
413 | * Submit action "preview". |
||
414 | * Prepare advertisement preview. |
||
415 | * |
||
416 | * @return void |
||
417 | */ |
||
418 | 2 | protected function preview() |
|
422 | |||
423 | /** |
||
424 | * Submit action "upload_banner". |
||
425 | * Upload banner and append it to the ad code. |
||
426 | * |
||
427 | * @return void |
||
428 | */ |
||
429 | 1 | protected function upload_banner() |
|
433 | |||
434 | /** |
||
435 | * Submit action "analyse_ad_code". |
||
436 | * Upload banner and append it to the ad code. |
||
437 | * |
||
438 | * @return void |
||
439 | */ |
||
440 | 1 | protected function analyse_ad_code() |
|
444 | |||
445 | /** |
||
446 | * Submit action "submit_add". |
||
447 | * Add new ad. |
||
448 | * |
||
449 | * @return void |
||
450 | */ |
||
451 | 2 | protected function submit_add() |
|
463 | |||
464 | /** |
||
465 | * Submit action "submit_edit". |
||
466 | * Edit ad. |
||
467 | * |
||
468 | * @return void |
||
469 | */ |
||
470 | 4 | protected function submit_edit() |
|
490 | |||
491 | /** |
||
492 | * Print success message. |
||
493 | * |
||
494 | * @param string $msg Message lang key |
||
495 | */ |
||
496 | 6 | protected function success($msg) |
|
500 | |||
501 | /** |
||
502 | * Print error message. |
||
503 | * |
||
504 | * @param string $msg Message lang key |
||
505 | */ |
||
506 | 6 | protected function error($msg) |
|
510 | } |
||
511 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.