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:
| 1 | <?php |
||
| 16 | class admin_helper |
||
| 17 | { |
||
| 18 | /** @var \phpbb\user */ |
||
| 19 | protected $user; |
||
| 20 | |||
| 21 | /** @var \phpbb\user_loader */ |
||
| 22 | protected $user_loader; |
||
| 23 | |||
| 24 | /** @var \phpbb\language\language */ |
||
| 25 | protected $language; |
||
| 26 | |||
| 27 | /** @var \phpbb\template\template */ |
||
| 28 | protected $template; |
||
| 29 | |||
| 30 | /** @var \phpbb\log\log */ |
||
| 31 | protected $log; |
||
| 32 | |||
| 33 | /** @var \phpbb\ads\location\manager */ |
||
| 34 | protected $location_manager; |
||
| 35 | |||
| 36 | /** @var string root_path */ |
||
| 37 | protected $root_path; |
||
| 38 | |||
| 39 | /** @var string php_ext */ |
||
| 40 | protected $php_ext; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Constructor |
||
| 44 | * |
||
| 45 | * @param \phpbb\user $user User object |
||
| 46 | * @param \phpbb\user_loader $user_loader User loader object |
||
| 47 | * @param \phpbb\language\language $language Language object |
||
| 48 | * @param \phpbb\template\template $template Template object |
||
| 49 | * @param \phpbb\log\log $log The phpBB log system |
||
| 50 | * @param \phpbb\ads\location\manager $location_manager Template location manager object |
||
| 51 | * @param string $root_path phpBB root path |
||
| 52 | * @param string $php_ext PHP extension |
||
| 53 | */ |
||
| 54 | 16 | View Code Duplication | public function __construct(\phpbb\user $user, \phpbb\user_loader $user_loader, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\log\log $log, \phpbb\ads\location\manager $location_manager, $root_path, $php_ext) |
| 65 | |||
| 66 | /** |
||
| 67 | * Assign ad data for ACP form template. |
||
| 68 | * |
||
| 69 | * @param array $data Ad data |
||
| 70 | * @param array $errors Validation errors |
||
| 71 | */ |
||
| 72 | 5 | public function assign_data($data, $errors) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Assign template locations data to the template. |
||
| 95 | * |
||
| 96 | * @param mixed $ad_locations The form data or nothing. |
||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | 7 | public function assign_locations($ad_locations = false) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Log action |
||
| 114 | * |
||
| 115 | * @param string $action Performed action in uppercase |
||
| 116 | * @param string $ad_name Advertisement name |
||
| 117 | * @return void |
||
| 118 | */ |
||
| 119 | 1 | public function log($action, $ad_name) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Get "Find username" URL to easily look for ad owner. |
||
| 126 | * |
||
| 127 | * @return string Find username URL |
||
| 128 | */ |
||
| 129 | 1 | public function get_find_username_link() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Is an ad expired? |
||
| 136 | * |
||
| 137 | * @param array $row Advertisement data |
||
| 138 | * @return bool True if expired, false otherwise |
||
| 139 | */ |
||
| 140 | 7 | public function is_expired($row) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Prepare ad owner for display. Method takes user_id |
||
| 162 | * of the ad owner and returns username. |
||
| 163 | * |
||
| 164 | * @param int $user_id User ID |
||
| 165 | * @return string Username belonging to $user_id. |
||
| 166 | */ |
||
| 167 | 5 | protected function get_username($user_id) |
|
| 177 | } |
||
| 178 |