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) |
50 | { |
||
51 | $sql = 'SELECT * |
||
52 | 13 | FROM ' . $this->ads_table . ' |
|
53 | 13 | WHERE ad_id = ' . (int) $ad_id; |
|
54 | 13 | $result = $this->db->sql_query($sql); |
|
55 | 13 | $data = $this->db->sql_fetchrow($result); |
|
56 | 13 | $this->db->sql_freeresult($result); |
|
57 | |||
58 | 13 | return $data !== false ? $data : array(); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Get one ad per every location |
||
63 | * |
||
64 | * @param array $ad_locations List of ad locations to fetch ads for |
||
65 | * @return array List of ad codes for each location |
||
66 | */ |
||
67 | 6 | public function get_ads($ad_locations) |
|
100 | |||
101 | /** |
||
102 | * Get all advertisements. |
||
103 | * |
||
104 | * @return array List of all ads |
||
105 | */ |
||
106 | 2 | View Code Duplication | public function get_all_ads() |
116 | |||
117 | /** |
||
118 | * Get all owner's ads |
||
119 | * |
||
120 | * @param int $user_id Ad owner |
||
121 | * @return array List of owner's ads |
||
122 | */ |
||
123 | 5 | View Code Duplication | public function get_ads_by_owner($user_id) |
124 | { |
||
125 | $sql = 'SELECT ad_name, ad_views, ad_clicks |
||
126 | 5 | FROM ' . $this->ads_table . ' |
|
127 | 5 | WHERE ad_owner = ' . (int) $user_id; |
|
128 | 5 | $result = $this->db->sql_query($sql); |
|
129 | 5 | $data = $this->db->sql_fetchrowset($result); |
|
130 | 5 | $this->db->sql_freeresult($result); |
|
131 | |||
132 | 5 | return $data; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Increment views for specified ads |
||
137 | * |
||
138 | * Note, that views are incremented only by one even when |
||
139 | * an ad is displayed multiple times on the same page. |
||
140 | * |
||
141 | * @param array $ad_ids IDs of ads to increment views |
||
142 | * @return void |
||
143 | */ |
||
144 | 2 | View Code Duplication | public function increment_ads_views($ad_ids) |
145 | { |
||
146 | 2 | if (!empty($ad_ids)) |
|
147 | 2 | { |
|
148 | 2 | $sql = 'UPDATE ' . $this->ads_table . ' |
|
149 | SET ad_views = ad_views + 1 |
||
150 | 2 | WHERE ' . $this->db->sql_in_set('ad_id', $ad_ids); |
|
151 | 2 | $this->db->sql_query($sql); |
|
152 | 2 | } |
|
153 | 2 | } |
|
154 | |||
155 | /** |
||
156 | * Increment clicks for specified ad |
||
157 | * |
||
158 | * @param int $ad_id ID of an ad to increment clicks |
||
159 | * @return void |
||
160 | */ |
||
161 | 2 | public function increment_ad_clicks($ad_id) |
|
168 | |||
169 | /** |
||
170 | * Insert new advertisement to the database |
||
171 | * |
||
172 | * @param array $data New ad data |
||
173 | * @return int New advertisement ID |
||
174 | */ |
||
175 | 2 | View Code Duplication | public function insert_ad($data) |
184 | |||
185 | /** |
||
186 | * Update advertisement |
||
187 | * |
||
188 | * @param int $ad_id Advertisement ID |
||
189 | * @param array $data List of data to update in the database |
||
190 | * @return int Number of affected rows. Can be used to determine if any ad has been updated. |
||
191 | */ |
||
192 | 4 | View Code Duplication | public function update_ad($ad_id, $data) |
203 | |||
204 | /** |
||
205 | * Delete advertisement |
||
206 | * |
||
207 | * @param int $ad_id Advertisement ID |
||
208 | * @return int Number of affected rows. Can be used to determine if any ad has been deleted. |
||
209 | */ |
||
210 | 1 | public function delete_ad($ad_id) |
|
218 | |||
219 | /** |
||
220 | * Remove ad owner |
||
221 | * |
||
222 | * @param array $user_ids User IDs |
||
223 | * @return void |
||
224 | */ |
||
225 | 3 | View Code Duplication | public function remove_ad_owner(array $user_ids) |
237 | |||
238 | /** |
||
239 | * Get all locations for specified advertisement |
||
240 | * |
||
241 | * @param int $ad_id Advertisement ID |
||
242 | * @return array List of template locations for specified ad |
||
243 | */ |
||
244 | 7 | View Code Duplication | public function get_ad_locations($ad_id) |
260 | |||
261 | /** |
||
262 | * Insert advertisement locations |
||
263 | * |
||
264 | * @param int $ad_id Advertisement ID |
||
265 | * @param array $ad_locations List of template locations for this ad |
||
266 | * @return void |
||
267 | */ |
||
268 | 2 | public function insert_ad_locations($ad_id, $ad_locations) |
|
280 | |||
281 | /** |
||
282 | * Delete advertisement locations |
||
283 | * |
||
284 | * @param int $ad_id Advertisement ID |
||
285 | * @return void |
||
286 | */ |
||
287 | 3 | public function delete_ad_locations($ad_id) |
|
293 | |||
294 | /** |
||
295 | * Load memberships of the user |
||
296 | * |
||
297 | * @param int $user_id User ID to load memberships |
||
298 | * @return array List of group IDs user is member of |
||
299 | */ |
||
300 | 5 | View Code Duplication | public function load_memberships($user_id) |
315 | |||
316 | /** |
||
317 | * Load all board groups |
||
318 | * |
||
319 | * @return array List of groups |
||
320 | */ |
||
321 | 1 | View Code Duplication | public function load_groups() |
333 | |||
334 | /** |
||
335 | * Make sure only necessary data make their way to SQL query |
||
336 | * |
||
337 | * @param array $data List of data to query the database |
||
338 | * @return array Cleaned data that contain only valid keys |
||
339 | */ |
||
340 | 6 | protected function intersect_ad_data($data) |
|
354 | |||
355 | /** |
||
356 | * Get the random statement for this database layer |
||
357 | * Random function should generate a float value between 0 and 1 |
||
358 | * |
||
359 | * @return string Random statement for current database layer |
||
360 | */ |
||
361 | 6 | protected function sql_random() |
|
380 | } |
||
381 |