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 |
||
| 13 | class manager |
||
| 14 | { |
||
| 15 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 16 | protected $db; |
||
| 17 | |||
| 18 | /** @var \phpbb\config\config */ |
||
| 19 | protected $config; |
||
| 20 | |||
| 21 | /** @var string */ |
||
| 22 | protected $ads_table; |
||
| 23 | |||
| 24 | /** @var string */ |
||
| 25 | protected $ad_locations_table; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Constructor |
||
| 29 | * |
||
| 30 | * @param \phpbb\db\driver\driver_interface $db DB driver interface |
||
| 31 | * @param \phpbb\config\config $config Config object |
||
| 32 | * @param string $ads_table Ads table |
||
| 33 | * @param string $ad_locations_table Ad locations table |
||
| 34 | */ |
||
| 35 | 53 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, $ads_table, $ad_locations_table) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Get specific ad |
||
| 45 | * |
||
| 46 | * @param int $ad_id Advertisement ID |
||
| 47 | * @return array Array with advertisement data |
||
| 48 | */ |
||
| 49 | 13 | View Code Duplication | public function get_ad($ad_id) |
| 65 | |||
| 66 | /** |
||
| 67 | * Get one ad per every location |
||
| 68 | * |
||
| 69 | * @param array $ad_locations List of ad locations to fetch ads for |
||
| 70 | * @return array List of ad codes for each location |
||
| 71 | */ |
||
| 72 | 6 | public function get_ads($ad_locations) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Get all advertisements. |
||
| 108 | * |
||
| 109 | * @return array List of all ads |
||
| 110 | */ |
||
| 111 | 2 | View Code Duplication | public function get_all_ads() |
| 121 | |||
| 122 | /** |
||
| 123 | * Get all owner's ads |
||
| 124 | * |
||
| 125 | * @param int $user_id Ad owner |
||
| 126 | * @return array List of owner's ads |
||
| 127 | */ |
||
| 128 | 5 | View Code Duplication | public function get_ads_by_owner($user_id) |
| 139 | |||
| 140 | /** |
||
| 141 | * Increment views for specified ads |
||
| 142 | * |
||
| 143 | * Note, that views are incremented only by one even when |
||
| 144 | * an ad is displayed multiple times on the same page. |
||
| 145 | * |
||
| 146 | * @param array $ad_ids IDs of ads to increment views |
||
| 147 | * @return void |
||
| 148 | */ |
||
| 149 | 2 | View Code Duplication | public function increment_ads_views($ad_ids) |
| 159 | |||
| 160 | /** |
||
| 161 | * Increment clicks for specified ad |
||
| 162 | * |
||
| 163 | * @param int $ad_id ID of an ad to increment clicks |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | 2 | public function increment_ad_clicks($ad_id) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Insert new advertisement to the database |
||
| 176 | * |
||
| 177 | * @param array $data New ad data |
||
| 178 | * @return int New advertisement ID |
||
| 179 | */ |
||
| 180 | 2 | View Code Duplication | public function insert_ad($data) |
| 189 | |||
| 190 | /** |
||
| 191 | * Update advertisement |
||
| 192 | * |
||
| 193 | * @param int $ad_id Advertisement ID |
||
| 194 | * @param array $data List of data to update in the database |
||
| 195 | * @return int Number of affected rows. Can be used to determine if any ad has been updated. |
||
| 196 | */ |
||
| 197 | 4 | View Code Duplication | public function update_ad($ad_id, $data) |
| 208 | |||
| 209 | /** |
||
| 210 | * Delete advertisement |
||
| 211 | * |
||
| 212 | * @param int $ad_id Advertisement ID |
||
| 213 | * @return int Number of affected rows. Can be used to determine if any ad has been deleted. |
||
| 214 | */ |
||
| 215 | 1 | public function delete_ad($ad_id) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Remove ad owner |
||
| 226 | * |
||
| 227 | * @param array $user_ids User IDs |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | 3 | View Code Duplication | public function remove_ad_owner(array $user_ids) |
| 242 | |||
| 243 | /** |
||
| 244 | * Get all locations for specified advertisement |
||
| 245 | * |
||
| 246 | * @param int $ad_id Advertisement ID |
||
| 247 | * @return array List of template locations for specified ad |
||
| 248 | */ |
||
| 249 | 7 | View Code Duplication | public function get_ad_locations($ad_id) |
| 265 | |||
| 266 | /** |
||
| 267 | * Insert advertisement locations |
||
| 268 | * |
||
| 269 | * @param int $ad_id Advertisement ID |
||
| 270 | * @param array $ad_locations List of template locations for this ad |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | 2 | public function insert_ad_locations($ad_id, $ad_locations) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Delete advertisement locations |
||
| 288 | * |
||
| 289 | * @param int $ad_id Advertisement ID |
||
| 290 | * @return void |
||
| 291 | */ |
||
| 292 | 3 | public function delete_ad_locations($ad_id) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Load memberships of the user |
||
| 301 | * |
||
| 302 | * @param int $user_id User ID to load memberships |
||
| 303 | * @return array List of group IDs user is member of |
||
| 304 | */ |
||
| 305 | 5 | View Code Duplication | public function load_memberships($user_id) |
| 320 | |||
| 321 | /** |
||
| 322 | * Load all board groups |
||
| 323 | * |
||
| 324 | * @return array List of groups |
||
| 325 | */ |
||
| 326 | 1 | View Code Duplication | public function load_groups() |
| 338 | |||
| 339 | /** |
||
| 340 | * Make sure only necessary data make their way to SQL query |
||
| 341 | * |
||
| 342 | * @param array $data List of data to query the database |
||
| 343 | * @return array Cleaned data that contain only valid keys |
||
| 344 | */ |
||
| 345 | 6 | protected function intersect_ad_data($data) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Get the random statement for this database layer |
||
| 362 | * Random function should generate a float value between 0 and 1 |
||
| 363 | * |
||
| 364 | * @return string Random statement for current database layer |
||
| 365 | */ |
||
| 366 | 6 | protected function sql_random() |
|
| 385 | } |
||
| 386 |