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\admanagement\ad\manager */ |
||
31 | protected $manager; |
||
32 | |||
33 | /** @var \phpbb\admanagement\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\admanagement\ad\manager $manager Advertisement manager object |
||
61 | * @param \phpbb\admanagement\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\admanagement\ad\manager $manager, \phpbb\admanagement\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) |
|
511 | |||
512 | /** |
||
513 | * Assign template locations data to the template. |
||
514 | * |
||
515 | * @param mixed $data The form data or nothing. |
||
516 | * @return void |
||
517 | */ |
||
518 | 12 | protected function assign_locations($data = false) |
|
530 | |||
531 | /** |
||
532 | * Prepare advertisement preview |
||
533 | * |
||
534 | * @param string $code Ad code to preview |
||
535 | * @return void |
||
536 | */ |
||
537 | 2 | protected function ad_preview($code) |
|
541 | |||
542 | /** |
||
543 | * Print success message. |
||
544 | * |
||
545 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
546 | */ |
||
547 | 6 | protected function success() |
|
551 | |||
552 | /** |
||
553 | * Print error message. |
||
554 | * |
||
555 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
556 | */ |
||
557 | 5 | protected function error() |
|
561 | |||
562 | /** |
||
563 | * Log action |
||
564 | * |
||
565 | * @param string $action Performed action in uppercase |
||
566 | * @param string $ad_name Advertisement name |
||
567 | * @return void |
||
568 | */ |
||
569 | 3 | protected function log($action, $ad_name) |
|
573 | } |
||
574 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.