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() |
|
159 | |||
160 | /** |
||
161 | * Process user request for manage mode |
||
162 | * |
||
163 | * @return void |
||
164 | */ |
||
165 | 29 | public function mode_manage() |
|
179 | |||
180 | /** |
||
181 | * Add an advertisement |
||
182 | * |
||
183 | * @return void |
||
184 | */ |
||
185 | 12 | protected function action_add() |
|
210 | |||
211 | /** |
||
212 | * Edit an advertisement |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | 7 | protected function action_edit() |
|
250 | |||
251 | /** |
||
252 | * Enable an advertisement |
||
253 | * |
||
254 | * @return void |
||
255 | */ |
||
256 | 4 | protected function action_enable() |
|
260 | |||
261 | /** |
||
262 | * Disable an advertisement |
||
263 | * |
||
264 | * @return void |
||
265 | */ |
||
266 | 4 | protected function action_disable() |
|
270 | |||
271 | /** |
||
272 | * Delete an advertisement |
||
273 | * |
||
274 | * @return void |
||
275 | */ |
||
276 | 3 | protected function action_delete() |
|
316 | |||
317 | /** |
||
318 | * Display the list of all ads |
||
319 | * |
||
320 | * @return void |
||
321 | */ |
||
322 | 1 | protected function list_ads() |
|
358 | |||
359 | /** |
||
360 | * Get what action user wants to do with the form. |
||
361 | * Possible options are: |
||
362 | * - preview ad code |
||
363 | * - upload banner to display in an ad code |
||
364 | * - analyse ad code |
||
365 | * - submit form (either add or edit an ad) |
||
366 | * |
||
367 | * @return mixed Action name or false when no action was submitted |
||
368 | */ |
||
369 | 13 | protected function get_submitted_action() |
|
382 | |||
383 | /** |
||
384 | * Enable/disable an advertisement |
||
385 | * |
||
386 | * @param bool $enable Enable or disable the advertisement? |
||
387 | * @return void |
||
388 | */ |
||
389 | 6 | protected function ad_enable($enable) |
|
417 | |||
418 | /** |
||
419 | * Submit action "preview". |
||
420 | * Prepare advertisement preview. |
||
421 | * |
||
422 | * @return void |
||
423 | */ |
||
424 | 2 | protected function preview() |
|
428 | |||
429 | /** |
||
430 | * Submit action "upload_banner". |
||
431 | * Upload banner and append it to the ad code. |
||
432 | * |
||
433 | * @return void |
||
434 | */ |
||
435 | 1 | protected function upload_banner() |
|
439 | |||
440 | /** |
||
441 | * Submit action "analyse_ad_code". |
||
442 | * Upload banner and append it to the ad code. |
||
443 | * |
||
444 | * @return void |
||
445 | */ |
||
446 | 1 | protected function analyse_ad_code() |
|
450 | |||
451 | /** |
||
452 | * Submit action "submit_add". |
||
453 | * Add new ad. |
||
454 | * |
||
455 | * @return void |
||
456 | */ |
||
457 | 2 | protected function submit_add() |
|
469 | |||
470 | /** |
||
471 | * Submit action "submit_edit". |
||
472 | * Edit ad. |
||
473 | * |
||
474 | * @return void |
||
475 | */ |
||
476 | 4 | protected function submit_edit() |
|
496 | |||
497 | /** |
||
498 | * Print success message. |
||
499 | * |
||
500 | * @param string $msg Message lang key |
||
501 | */ |
||
502 | 6 | protected function success($msg) |
|
506 | |||
507 | /** |
||
508 | * Print error message. |
||
509 | * |
||
510 | * @param string $msg Message lang key |
||
511 | */ |
||
512 | 6 | protected function error($msg) |
|
516 | } |
||
517 |
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.