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 string php_ext */ |
||
43 | protected $php_ext; |
||
44 | |||
45 | /** @var string ext_path */ |
||
46 | protected $ext_path; |
||
47 | |||
48 | /** @var string Custom form action */ |
||
49 | protected $u_action; |
||
50 | |||
51 | /** @var array Form validation errors */ |
||
52 | protected $errors = array(); |
||
53 | |||
54 | /** |
||
55 | * Constructor |
||
56 | * |
||
57 | * @param \phpbb\template\template $template Template object |
||
58 | * @param \phpbb\user $user User object |
||
59 | * @param \phpbb\request\request $request Request object |
||
60 | * @param \phpbb\ads\ad\manager $manager Advertisement manager object |
||
61 | * @param \phpbb\ads\location\manager $location_manager Template location manager object |
||
62 | * @param \phpbb\log\log $log The phpBB log system |
||
63 | * @param \phpbb\config\db_text $config_text Config text object |
||
64 | * @param string $php_ext PHP extension |
||
65 | * @param string $ext_path Path to this extension |
||
66 | */ |
||
67 | 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, $php_ext, $ext_path) |
|
79 | |||
80 | /** |
||
81 | * Process user request for manage mode |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | 6 | public function mode_manage() |
|
99 | |||
100 | /** |
||
101 | * Process user request for settings mode |
||
102 | * |
||
103 | * @return void |
||
104 | */ |
||
105 | 3 | public function mode_settings() |
|
144 | |||
145 | /** |
||
146 | * Set page url |
||
147 | * |
||
148 | * @param string $u_action Custom form action |
||
149 | * @return void |
||
150 | */ |
||
151 | 28 | public function set_page_url($u_action) |
|
155 | |||
156 | /** |
||
157 | * Get ACP page title for Ads module |
||
158 | * |
||
159 | * @return string Language string for Ads ACP module |
||
160 | */ |
||
161 | 1 | public function get_page_title() |
|
165 | |||
166 | /** |
||
167 | * Add an advertisement |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | 16 | public function action_add() |
|
210 | |||
211 | /** |
||
212 | * Edit an advertisement |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | 9 | public function action_edit() |
|
271 | |||
272 | /** |
||
273 | * Enable an advertisement |
||
274 | * |
||
275 | * @return void |
||
276 | */ |
||
277 | 3 | public function action_enable() |
|
281 | |||
282 | /** |
||
283 | * Disable an advertisement |
||
284 | * |
||
285 | * @return void |
||
286 | */ |
||
287 | 3 | public function action_disable() |
|
291 | |||
292 | /** |
||
293 | * Delete an advertisement |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | 3 | public function action_delete() |
|
337 | |||
338 | /** |
||
339 | * Display the ads |
||
340 | * |
||
341 | * @return void |
||
342 | */ |
||
343 | 1 | public function list_ads() |
|
370 | |||
371 | /** |
||
372 | * Perform general tasks |
||
373 | * |
||
374 | * @return void |
||
375 | */ |
||
376 | 9 | protected function setup() |
|
382 | |||
383 | /** |
||
384 | * Enable/disable an advertisement |
||
385 | * |
||
386 | * @param bool $enable Enable or disable the advertisement? |
||
387 | * @return void |
||
388 | */ |
||
389 | 4 | protected function ad_enable($enable) |
|
417 | |||
418 | /** |
||
419 | * Get admin form data. |
||
420 | * |
||
421 | * @param string $form_name The form name. |
||
422 | * @return array Form data |
||
423 | */ |
||
424 | 13 | protected function get_form_data($form_name) |
|
472 | |||
473 | /** |
||
474 | * Assign form data to the template. |
||
475 | * |
||
476 | * @param array $data The form data. |
||
477 | * @return void |
||
478 | */ |
||
479 | 11 | protected function assign_form_data($data) |
|
492 | /** |
||
493 | * Prepare end date for display |
||
494 | * |
||
495 | * @param mixed $end_date End date. |
||
496 | * @return string End date prepared for display. |
||
497 | */ |
||
498 | 11 | protected function prepare_end_date($end_date) |
|
512 | |||
513 | /** |
||
514 | * Assign template locations data to the template. |
||
515 | * |
||
516 | * @param mixed $data The form data or nothing. |
||
517 | * @return void |
||
518 | */ |
||
519 | 12 | protected function assign_locations($data = false) |
|
531 | |||
532 | /** |
||
533 | * Prepare advertisement preview |
||
534 | * |
||
535 | * @param string $code Ad code to preview |
||
536 | * @return void |
||
537 | */ |
||
538 | 2 | protected function ad_preview($code) |
|
542 | |||
543 | /** |
||
544 | * Print success message. |
||
545 | * |
||
546 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
547 | */ |
||
548 | 6 | protected function success() |
|
552 | |||
553 | /** |
||
554 | * Print error message. |
||
555 | * |
||
556 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
557 | */ |
||
558 | 5 | protected function error() |
|
562 | |||
563 | /** |
||
564 | * Log action |
||
565 | * |
||
566 | * @param string $action Performed action in uppercase |
||
567 | * @param string $ad_name Advertisement name |
||
568 | * @return void |
||
569 | */ |
||
570 | 3 | protected function log($action, $ad_name) |
|
574 | } |
||
575 |