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 \phpbb\config\config */ |
||
43 | protected $config; |
||
44 | |||
45 | /** @var string php_ext */ |
||
46 | protected $php_ext; |
||
47 | |||
48 | /** @var string ext_path */ |
||
49 | protected $ext_path; |
||
50 | |||
51 | /** @var string Custom form action */ |
||
52 | protected $u_action; |
||
53 | |||
54 | /** @var array Form validation errors */ |
||
55 | protected $errors = array(); |
||
56 | |||
57 | /** |
||
58 | * Constructor |
||
59 | * |
||
60 | * @param \phpbb\template\template $template Template object |
||
61 | * @param \phpbb\user $user User object |
||
62 | * @param \phpbb\request\request $request Request object |
||
63 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
64 | * @param \phpbb\ads\location\manager $location_manager Template location manager object |
||
65 | * @param \phpbb\log\log $log The phpBB log system |
||
66 | * @param \phpbb\config\db_text $config_text Config text object |
||
67 | * @param \phpbb\config\config $config Config object |
||
68 | * @param string $php_ext PHP extension |
||
69 | * @param string $ext_path Path to this extension |
||
70 | */ |
||
71 | 34 | 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) |
|
84 | |||
85 | /** |
||
86 | * Process user request for manage mode |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | 6 | public function mode_manage() |
|
104 | |||
105 | /** |
||
106 | * Process user request for settings mode |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | 3 | public function mode_settings() |
|
155 | |||
156 | /** |
||
157 | * Set page url |
||
158 | * |
||
159 | * @param string $u_action Custom form action |
||
160 | * @return void |
||
161 | */ |
||
162 | 28 | public function set_page_url($u_action) |
|
166 | |||
167 | /** |
||
168 | * Get ACP page title for Ads module |
||
169 | * |
||
170 | * @return string Language string for Ads ACP module |
||
171 | */ |
||
172 | 1 | public function get_page_title() |
|
176 | |||
177 | /** |
||
178 | * Add an advertisement |
||
179 | * |
||
180 | * @return void |
||
181 | */ |
||
182 | 11 | public function action_add() |
|
222 | |||
223 | /** |
||
224 | * Edit an advertisement |
||
225 | * |
||
226 | * @return void |
||
227 | */ |
||
228 | 9 | public function action_edit() |
|
284 | |||
285 | /** |
||
286 | * Enable an advertisement |
||
287 | * |
||
288 | * @return void |
||
289 | */ |
||
290 | 3 | public function action_enable() |
|
294 | |||
295 | /** |
||
296 | * Disable an advertisement |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | 3 | public function action_disable() |
|
304 | |||
305 | /** |
||
306 | * Delete an advertisement |
||
307 | * |
||
308 | * @return void |
||
309 | */ |
||
310 | 3 | public function action_delete() |
|
350 | |||
351 | /** |
||
352 | * Display the ads |
||
353 | * |
||
354 | * @return void |
||
355 | */ |
||
356 | 1 | public function list_ads() |
|
383 | |||
384 | /** |
||
385 | * Perform general tasks |
||
386 | * |
||
387 | * @return void |
||
388 | */ |
||
389 | 9 | protected function setup() |
|
395 | |||
396 | /** |
||
397 | * Enable/disable an advertisement |
||
398 | * |
||
399 | * @param bool $enable Enable or disable the advertisement? |
||
400 | * @return void |
||
401 | */ |
||
402 | 4 | protected function ad_enable($enable) |
|
430 | |||
431 | /** |
||
432 | * Get admin form data. |
||
433 | * |
||
434 | * @param string $form_name The form name. |
||
435 | * @return array Form data |
||
436 | */ |
||
437 | 13 | protected function get_form_data($form_name) |
|
485 | |||
486 | /** |
||
487 | * Assign form data to the template. |
||
488 | * |
||
489 | * @param array $data The form data. |
||
490 | * @return void |
||
491 | */ |
||
492 | 11 | protected function assign_form_data($data) |
|
505 | |||
506 | /** |
||
507 | * Prepare end date for display |
||
508 | * |
||
509 | * @param mixed $end_date End date. |
||
510 | * @return string End date prepared for display. |
||
511 | */ |
||
512 | 11 | protected function prepare_end_date($end_date) |
|
526 | |||
527 | /** |
||
528 | * Assign template locations data to the template. |
||
529 | * |
||
530 | * @param mixed $data The form data or nothing. |
||
531 | * @return void |
||
532 | */ |
||
533 | 12 | protected function assign_locations($data = false) |
|
545 | |||
546 | /** |
||
547 | * Prepare advertisement preview |
||
548 | * |
||
549 | * @param string $code Ad code to preview |
||
550 | * @return void |
||
551 | */ |
||
552 | 2 | protected function ad_preview($code) |
|
556 | |||
557 | /** |
||
558 | * Print success message. |
||
559 | * |
||
560 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
561 | */ |
||
562 | 6 | protected function success() |
|
566 | |||
567 | /** |
||
568 | * Print error message. |
||
569 | * |
||
570 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
571 | */ |
||
572 | 5 | protected function error() |
|
576 | |||
577 | /** |
||
578 | * Log action |
||
579 | * |
||
580 | * @param string $action Performed action in uppercase |
||
581 | * @param string $ad_name Advertisement name |
||
582 | * @return void |
||
583 | */ |
||
584 | 3 | protected function log($action, $ad_name) |
|
588 | } |
||
589 |