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 | /** @var string */ |
||
28 | protected $ad_group_table; |
||
29 | |||
30 | /** |
||
31 | * Constructor |
||
32 | * |
||
33 | * @param \phpbb\db\driver\driver_interface $db DB driver interface |
||
34 | * @param \phpbb\config\config $config Config object |
||
35 | * @param string $ads_table Ads table |
||
36 | * @param string $ad_locations_table Ad locations table |
||
37 | * @param string $ad_group_table Ad group table |
||
38 | */ |
||
39 | 56 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, $ads_table, $ad_locations_table, $ad_group_table) |
|
47 | |||
48 | /** |
||
49 | * Get specific ad |
||
50 | * |
||
51 | * @param int $ad_id Advertisement ID |
||
52 | * @return array Array with advertisement data |
||
53 | */ |
||
54 | 13 | 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 | * @param array $user_groups List of user groups |
||
71 | * @param bool $non_content_page Is current page non-content oriented (e.g.: login, UCP, MCP)? Default is false. |
||
72 | * @return array List of ad codes for each location |
||
73 | */ |
||
74 | 8 | public function get_ads($ad_locations, $user_groups, $non_content_page = false) |
|
107 | |||
108 | /** |
||
109 | * Get all advertisements. |
||
110 | * |
||
111 | * @return array List of all ads |
||
112 | */ |
||
113 | 2 | View Code Duplication | public function get_all_ads() |
123 | |||
124 | /** |
||
125 | * Get all owner's ads |
||
126 | * |
||
127 | * @param int $user_id Ad owner |
||
128 | * @return array List of owner's ads |
||
129 | */ |
||
130 | 5 | View Code Duplication | public function get_ads_by_owner($user_id) |
141 | |||
142 | /** |
||
143 | * Increment views for specified ads |
||
144 | * |
||
145 | * Note, that views are incremented only by one even when |
||
146 | * an ad is displayed multiple times on the same page. |
||
147 | * |
||
148 | * @param array $ad_ids IDs of ads to increment views |
||
149 | * @return void |
||
150 | */ |
||
151 | 2 | View Code Duplication | public function increment_ads_views($ad_ids) |
161 | |||
162 | /** |
||
163 | * Increment clicks for specified ad |
||
164 | * |
||
165 | * @param int $ad_id ID of an ad to increment clicks |
||
166 | * @return void |
||
167 | */ |
||
168 | 2 | public function increment_ad_clicks($ad_id) |
|
175 | |||
176 | /** |
||
177 | * Insert new advertisement to the database |
||
178 | * |
||
179 | * @param array $data New ad data |
||
180 | * @return int New advertisement ID |
||
181 | */ |
||
182 | 2 | public function insert_ad($data) |
|
197 | |||
198 | /** |
||
199 | * Update advertisement |
||
200 | * |
||
201 | * @param int $ad_id Advertisement ID |
||
202 | * @param array $data List of data to update in the database |
||
203 | * @return int Number of affected rows. Can be used to determine if any ad has been updated. |
||
204 | */ |
||
205 | 4 | public function update_ad($ad_id, $data) |
|
222 | |||
223 | /** |
||
224 | * Delete advertisement |
||
225 | * |
||
226 | * @param int $ad_id Advertisement ID |
||
227 | * @return int Number of affected rows. Can be used to determine if any ad has been deleted. |
||
228 | */ |
||
229 | 1 | public function delete_ad($ad_id) |
|
237 | |||
238 | /** |
||
239 | * Remove ad owner |
||
240 | * |
||
241 | * @param array $user_ids User IDs |
||
242 | * @return void |
||
243 | */ |
||
244 | 3 | View Code Duplication | public function remove_ad_owner(array $user_ids) |
256 | |||
257 | /** |
||
258 | * Get all locations for specified advertisement |
||
259 | * |
||
260 | * @param int $ad_id Advertisement ID |
||
261 | * @return array List of template locations for specified ad |
||
262 | */ |
||
263 | 7 | View Code Duplication | public function get_ad_locations($ad_id) |
279 | |||
280 | /** |
||
281 | * Insert advertisement locations |
||
282 | * |
||
283 | * @param int $ad_id Advertisement ID |
||
284 | * @param array $ad_locations List of template locations for this ad |
||
285 | * @return void |
||
286 | */ |
||
287 | 2 | View Code Duplication | public function insert_ad_locations($ad_id, $ad_locations) |
299 | |||
300 | /** |
||
301 | * Delete advertisement locations |
||
302 | * |
||
303 | * @param int $ad_id Advertisement ID |
||
304 | * @return void |
||
305 | */ |
||
306 | 3 | public function delete_ad_locations($ad_id) |
|
312 | |||
313 | /** |
||
314 | * Load memberships of the user |
||
315 | * |
||
316 | * @param int $user_id User ID to load memberships |
||
317 | * @return array List of group IDs user is member of |
||
318 | */ |
||
319 | 2 | View Code Duplication | public function load_memberships($user_id) |
334 | |||
335 | /** |
||
336 | * Load all board groups |
||
337 | * |
||
338 | * @param int $ad_id Advertisement ID |
||
339 | * @return array List of groups |
||
340 | */ |
||
341 | 1 | public function load_groups($ad_id) |
|
358 | |||
359 | /** |
||
360 | * Make sure only necessary data make their way to SQL query |
||
361 | * |
||
362 | * @param array $data List of data to query the database |
||
363 | * @return array Cleaned data that contain only valid keys |
||
364 | */ |
||
365 | 6 | protected function intersect_ad_data($data) |
|
380 | |||
381 | /** |
||
382 | * Get the random statement for this database layer |
||
383 | * Random function should generate a float value between 0 and 1 |
||
384 | * |
||
385 | * @return string Random statement for current database layer |
||
386 | */ |
||
387 | 8 | protected function sql_random() |
|
406 | |||
407 | /** |
||
408 | * Add rows to ad_group table. |
||
409 | * |
||
410 | * @param int $ad_id Advertisement ID |
||
411 | * @param array $ad_groups List of groups that should not see this ad |
||
412 | * @return void |
||
413 | */ |
||
414 | 6 | View Code Duplication | protected function insert_ad_group_data($ad_id, $ad_groups) |
426 | |||
427 | /** |
||
428 | * Remove all rows of specified ad in ad_group table |
||
429 | * |
||
430 | * @param int $ad_id Advertisement ID |
||
431 | * @return void |
||
432 | */ |
||
433 | 4 | protected function remove_ad_group_data($ad_id) |
|
439 | } |
||
440 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.