Total Complexity | 67 |
Total Lines | 596 |
Duplicated Lines | 53.02 % |
Changes | 0 |
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 contest_model 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.
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 contest_model, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class contest_model extends Model { |
||
4 | |||
5 | function __construct() { |
||
|
|||
6 | $this->load_library("db_lib"); |
||
7 | } |
||
8 | |||
9 | private function is_registered($table_name, $user_nick) { |
||
10 | $stmt = $this->db_lib->prepared_execute( |
||
11 | $this->DB->contest, |
||
12 | "SELECT * |
||
13 | FROM `$table_name` |
||
14 | WHERE `nick` = ?", |
||
15 | "s", |
||
16 | [$user_nick] |
||
17 | ); |
||
18 | if (!$stmt) { |
||
19 | return false; |
||
20 | } |
||
21 | $user_details = $stmt->get_result()->fetch_assoc(); |
||
22 | if ($user_details) { |
||
23 | return $user_details; |
||
24 | } |
||
25 | return false; |
||
26 | } |
||
27 | |||
28 | /* |
||
29 | |--------------------------------------------------------------------------- |
||
30 | | TTT-Workshop |
||
31 | |--------------------------------------------------------------------------- |
||
32 | */ |
||
33 | |||
34 | public function is_registered_for_ttt($user_nick) { |
||
36 | } |
||
37 | |||
38 | View Code Duplication | public function register_for_ttt($user_nick, $contact_number) { |
|
39 | $stmt = $this->db_lib->prepared_execute( |
||
40 | $this->DB->contest, |
||
41 | "INSERT INTO `ttt_registrations` |
||
42 | (`nick`, `contact_number`) VALUES (?, ?)", |
||
43 | "ss", |
||
44 | [$user_nick, $contact_number] |
||
45 | ); |
||
46 | if (!$stmt) { |
||
47 | return false; |
||
48 | } |
||
49 | else { |
||
50 | return true; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | View Code Duplication | public function ttt_payment_success($payment_id, $nick, $status, $payment_data) { |
|
55 | $stmt = $this->db_lib->prepared_execute( |
||
56 | $this->DB->contest, |
||
57 | "UPDATE `ttt_registrations` |
||
58 | SET `payment_id` = ?, `payment_status` = ?, `payment_data` = ? |
||
59 | WHERE `nick` = ?", |
||
60 | "ssss", |
||
61 | [$payment_id, $status, $payment_data, $nick] |
||
62 | ); |
||
63 | $this->ttt_dump_data($nick, 'callback', $payment_data); |
||
64 | if (!$stmt) { |
||
65 | return false; |
||
66 | } |
||
67 | else { |
||
68 | return true; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | public function ttt_dump_data($nick, $type, $response) { |
||
73 | $this->db_lib->prepared_execute( |
||
74 | $this->DB->contest, |
||
75 | "INSERT INTO `ttt_payment_dump` |
||
76 | (`nick`, `type`, `response`) |
||
77 | VALUES (?, ?, ?)", |
||
78 | "sss", |
||
79 | [$nick, $type, $response] |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | /* |
||
84 | |--------------------------------------------------------------------------- |
||
85 | | Futsal |
||
86 | |--------------------------------------------------------------------------- |
||
87 | */ |
||
88 | |||
89 | private function get_futsal_team($team_id) { |
||
90 | $stmt = $this->db_lib->prepared_execute( |
||
91 | $this->DB->contest, |
||
92 | "SELECT * |
||
93 | FROM `futsal_teams` |
||
94 | WHERE `id` = ?", |
||
95 | "i", |
||
96 | [$team_id] |
||
97 | ); |
||
98 | if (!$stmt) { |
||
99 | return false; |
||
100 | } |
||
101 | $team_info = $stmt->get_result()->fetch_assoc(); |
||
102 | if ($team_info) { |
||
103 | return $team_info; |
||
104 | } |
||
105 | return false; |
||
106 | } |
||
107 | |||
108 | public function is_registered_for_futsal($user_nick) { |
||
109 | $user_details = $this->is_registered('futsal_participants', $user_nick); |
||
110 | if ($user_details) { |
||
111 | $team_info = $this->get_futsal_team($user_details['team_id']); |
||
112 | return $team_info; |
||
113 | } |
||
114 | return false; |
||
115 | } |
||
116 | |||
117 | View Code Duplication | public function check_futsal_team_exists($team_name) { |
|
118 | $stmt = $this->db_lib->prepared_execute( |
||
119 | $this->DB->contest, |
||
120 | "SELECT * |
||
121 | FROM `futsal_teams` |
||
122 | WHERE `team_name` = ?", |
||
123 | "s", |
||
124 | [$team_name] |
||
125 | ); |
||
126 | if (!$stmt) { |
||
127 | return false; |
||
128 | } |
||
129 | $stmt->store_result(); |
||
130 | if ($stmt->num_rows > 0) { |
||
131 | return true; |
||
132 | } |
||
133 | return false; |
||
134 | } |
||
135 | |||
136 | View Code Duplication | public function check_futsal_participant_exists($user_nick) { |
|
137 | $stmt = $this->db_lib->prepared_execute( |
||
138 | $this->DB->contest, |
||
139 | "SELECT * |
||
140 | FROM `futsal_participants` |
||
141 | WHERE `nick` = ?", |
||
142 | "s", |
||
143 | [$user_nick] |
||
144 | ); |
||
145 | if (!$stmt) { |
||
146 | return false; |
||
147 | } |
||
148 | $stmt->store_result(); |
||
149 | if ($stmt->num_rows > 0) { |
||
150 | return true; |
||
151 | } |
||
152 | return false; |
||
153 | } |
||
154 | |||
155 | View Code Duplication | public function register_for_futsal($team_name, $contact_number, $team) { |
|
156 | $this->DB->contest->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); |
||
157 | $stmt = $this->db_lib->prepared_execute( |
||
158 | $this->DB->contest, |
||
159 | "INSERT INTO `futsal_teams` |
||
160 | (`team_name`, `contact_number`) VALUES (?, ?)", |
||
161 | "ss", |
||
162 | [$team_name, $contact_number] |
||
163 | ); |
||
164 | if (!$stmt) { |
||
165 | return false; |
||
166 | } |
||
167 | $team_id = $this->DB->contest->insert_id; |
||
168 | foreach ($team as $nick) { |
||
169 | $stmt = $this->db_lib->prepared_execute( |
||
170 | $this->DB->contest, |
||
171 | "INSERT INTO `futsal_participants` |
||
172 | (`team_id`, `nick`) VALUES (?, ?)", |
||
173 | "is", |
||
174 | [$team_id, $nick] |
||
175 | ); |
||
176 | if (!$stmt) { |
||
177 | return false; |
||
178 | } |
||
179 | } |
||
180 | return $this->DB->contest->commit(); |
||
181 | } |
||
182 | |||
183 | /* |
||
184 | |--------------------------------------------------------------------------- |
||
185 | | Paper Presentation |
||
186 | |--------------------------------------------------------------------------- |
||
187 | */ |
||
188 | |||
189 | public function is_registered_for_paper_presentation($user_nick) { |
||
190 | return $this->is_registered('paper_presentation', $user_nick); |
||
191 | } |
||
192 | |||
193 | View Code Duplication | public function register_for_paper_presentation($info) { |
|
194 | return $this->db_lib->prepared_execute( |
||
195 | $this->DB->contest, |
||
196 | "INSERT INTO `paper_presentation` |
||
197 | (`nick`, `contact_number`, `paper_link`) |
||
198 | VALUES (?, ?, ?)", |
||
199 | "sss", |
||
200 | [ |
||
201 | $info['nick'], |
||
202 | $info['contact_number'], |
||
203 | $info['paper_link'], |
||
204 | ], |
||
205 | false |
||
206 | ); |
||
207 | } |
||
208 | |||
209 | /* |
||
210 | |--------------------------------------------------------------------------- |
||
211 | | Visualize it |
||
212 | |--------------------------------------------------------------------------- |
||
213 | */ |
||
214 | |||
215 | public function is_registered_for_visualizeit($user_nick) { |
||
216 | return $this->is_registered('visualizeit', $user_nick); |
||
217 | } |
||
218 | |||
219 | View Code Duplication | public function register_for_visualizeit($info) { |
|
220 | return $this->db_lib->prepared_execute( |
||
221 | $this->DB->contest, |
||
222 | "INSERT INTO `visualizeit` |
||
223 | (`nick`, `paper_link`) |
||
224 | VALUES (?, ?)", |
||
225 | "ss", |
||
226 | [ |
||
227 | $info['nick'], |
||
228 | $info['paper_link'], |
||
229 | ], |
||
230 | false |
||
231 | ); |
||
232 | } |
||
233 | |||
234 | /* |
||
235 | |--------------------------------------------------------------------------- |
||
236 | | Web Development Workshop |
||
237 | |--------------------------------------------------------------------------- |
||
238 | */ |
||
239 | |||
240 | public function is_registered_for_webdev($user_nick) { |
||
241 | return $this->is_registered('webdev_registrations', $user_nick); |
||
242 | } |
||
243 | |||
244 | View Code Duplication | public function register_for_webdev($info) { |
|
245 | return $this->db_lib->prepared_execute( |
||
246 | $this->DB->contest, |
||
247 | "INSERT INTO `webdev_registrations` |
||
248 | (`nick`, `contact_number`, `stream`, `year`, `experience`, `why_join`) |
||
249 | VALUES (?, ?, ?, ?, ?, ?)", |
||
250 | "ssssss", |
||
251 | [ |
||
252 | $info['nick'], |
||
253 | $info['contact_number'], |
||
254 | $info['stream'], |
||
255 | $info['year'], |
||
256 | $info['experience'], |
||
257 | $info['why_join'], |
||
258 | ], |
||
259 | false |
||
260 | ); |
||
261 | } |
||
262 | |||
263 | View Code Duplication | public function webdev_payment_success($payment_id, $nick, $status, $payment_data) { |
|
264 | $stmt = $this->db_lib->prepared_execute( |
||
265 | $this->DB->contest, |
||
266 | "UPDATE `webdev_registrations` |
||
267 | SET `payment_id` = ?, `payment_status` = ?, `payment_data` = ? |
||
268 | WHERE `nick` = ?", |
||
269 | "ssss", |
||
270 | [$payment_id, $status, $payment_data, $nick] |
||
271 | ); |
||
272 | $this->webdev_dump_data($nick, 'callback', $payment_data); |
||
273 | if (!$stmt) { |
||
274 | return false; |
||
275 | } |
||
276 | else { |
||
277 | return true; |
||
278 | } |
||
279 | } |
||
280 | |||
281 | public function webdev_dump_data($nick, $type, $response) { |
||
282 | $this->db_lib->prepared_execute( |
||
283 | $this->DB->contest, |
||
284 | "INSERT INTO `webdev_payment_dump` |
||
285 | (`nick`, `type`, `response`) |
||
286 | VALUES (?, ?, ?)", |
||
287 | "sss", |
||
288 | [$nick, $type, $response] |
||
289 | ); |
||
290 | } |
||
291 | |||
292 | /* |
||
293 | |--------------------------------------------------------------------------- |
||
294 | | Riders on the storm |
||
295 | |--------------------------------------------------------------------------- |
||
296 | */ |
||
297 | |||
298 | public function is_registered_for_riderofstorms($user_nick) { |
||
299 | return $this->is_registered('riderofstorms_registrations', $user_nick); |
||
300 | } |
||
301 | |||
302 | View Code Duplication | public function register_for_riderofstorms($info) { |
|
303 | /*$user_details = [ |
||
304 | 'nick' => $info['nick'], |
||
305 | 'contact_number' => $info['contact_number'], |
||
306 | 'name' => $info['name'], |
||
307 | 'leader' => $info['leader'], |
||
308 | 'members' => $info['members'], |
||
309 | 'link' => $info['link'], |
||
310 | 'tell' => $info['tell'] |
||
311 | ];*/ |
||
312 | |||
313 | return $this->db_lib->prepared_execute( |
||
314 | $this->DB->contest, |
||
315 | "INSERT INTO `riderofstorms_registrations` |
||
316 | (`nick`, `name`, `contact_number`, `leader`, `link`, `members`, `tell`) |
||
317 | VALUES (?, ?, ?, ?, ?, ?, ?)", |
||
318 | "sssssss", |
||
319 | [ |
||
320 | $info['nick'], |
||
321 | $info['name'], |
||
322 | $info['contact_number'], |
||
323 | $info['leader'], |
||
324 | $info['link'], |
||
325 | $info['members'], |
||
326 | $info['tell'] |
||
327 | ], |
||
328 | false |
||
329 | ); |
||
330 | } |
||
331 | |||
332 | View Code Duplication | public function riderofstorms_payment_success($payment_id, $nick, $status, $payment_data) { |
|
333 | $stmt = $this->db_lib->prepared_execute( |
||
334 | $this->DB->contest, |
||
335 | "UPDATE `riderofstorms_registrations` |
||
336 | SET `payment_id` = ?, `payment_status` = ?, `payment_data` = ? |
||
337 | WHERE `nick` = ?", |
||
338 | "ssss", |
||
339 | [$payment_id, $status, $payment_data, $nick] |
||
340 | ); |
||
341 | $this->riderofstorms_dump_data($nick, 'callback', $payment_data); |
||
342 | if (!$stmt) { |
||
343 | return false; |
||
344 | } |
||
345 | else { |
||
346 | return true; |
||
347 | } |
||
348 | } |
||
349 | |||
350 | public function riderofstorms_dump_data($nick, $type, $response) { |
||
351 | $this->db_lib->prepared_execute( |
||
352 | $this->DB->contest, |
||
353 | "INSERT INTO `riderofstorms_payment_dump` |
||
354 | (`nick`, `type`, `response`) |
||
355 | VALUES (?, ?, ?)", |
||
356 | "sss", |
||
357 | [$nick, $type, $response] |
||
358 | ); |
||
359 | } |
||
360 | |||
361 | |||
362 | /* |
||
363 | |--------------------------------------------------------------------------- |
||
364 | | ARVR Workshop |
||
365 | |--------------------------------------------------------------------------- |
||
366 | */ |
||
367 | |||
368 | public function is_registered_for_arvr($user_nick) { |
||
369 | return $this->is_registered('arvr_registrations', $user_nick); |
||
370 | } |
||
371 | |||
372 | View Code Duplication | public function register_for_arvr($info) { |
|
373 | return $this->db_lib->prepared_execute( |
||
374 | $this->DB->contest, |
||
375 | "INSERT INTO `arvr_registrations` |
||
376 | (`nick`, `contact_number`) |
||
377 | VALUES (?, ?)", |
||
378 | "ss", |
||
379 | [ |
||
380 | $info['nick'], |
||
381 | $info['contact_number'], |
||
382 | ], |
||
383 | false |
||
384 | ); |
||
385 | } |
||
386 | |||
387 | View Code Duplication | public function arvr_payment_success($payment_id, $nick, $status, $payment_data) { |
|
388 | $stmt = $this->db_lib->prepared_execute( |
||
389 | $this->DB->contest, |
||
390 | "UPDATE `arvr_registrations` |
||
391 | SET `payment_id` = ?, `payment_status` = ?, `payment_data` = ? |
||
392 | WHERE `nick` = ?", |
||
393 | "ssss", |
||
394 | [$payment_id, $status, $payment_data, $nick] |
||
395 | ); |
||
396 | $this->arvr_dump_data($nick, 'callback', $payment_data); |
||
397 | if (!$stmt) { |
||
398 | return false; |
||
399 | } |
||
400 | else { |
||
401 | return true; |
||
402 | } |
||
403 | } |
||
404 | |||
405 | public function arvr_dump_data($nick, $type, $response) { |
||
406 | $this->db_lib->prepared_execute( |
||
407 | $this->DB->contest, |
||
408 | "INSERT INTO `arvr_payment_dump` |
||
409 | (`nick`, `type`, `response`) |
||
410 | VALUES (?, ?, ?)", |
||
411 | "sss", |
||
412 | [$nick, $type, $response] |
||
413 | ); |
||
414 | } |
||
415 | |||
416 | /* |
||
417 | |--------------------------------------------------------------------------- |
||
418 | | Artuino Workshop |
||
419 | |--------------------------------------------------------------------------- |
||
420 | */ |
||
421 | |||
422 | private function get_artuino_team($team_id) { |
||
423 | $stmt = $this->db_lib->prepared_execute( |
||
424 | $this->DB->contest, |
||
425 | "SELECT * |
||
426 | FROM `artuino_teams` |
||
427 | WHERE `id` = ?", |
||
428 | "i", |
||
429 | [$team_id] |
||
430 | ); |
||
431 | if (!$stmt) { |
||
432 | return false; |
||
433 | } |
||
434 | $team_info = $stmt->get_result()->fetch_assoc(); |
||
435 | if ($team_info) { |
||
436 | return $team_info; |
||
437 | } |
||
438 | return false; |
||
439 | } |
||
440 | |||
441 | public function is_registered_for_artuino($user_nick) { |
||
442 | $user_details = $this->is_registered('artuino_participants', $user_nick); |
||
443 | if ($user_details) { |
||
444 | $team_info = $this->get_artuino_team($user_details['team_id']); |
||
445 | return $team_info; |
||
446 | } |
||
447 | return false; |
||
448 | } |
||
449 | |||
450 | View Code Duplication | public function check_artuino_team_exists($team_name) { |
|
467 | } |
||
468 | |||
469 | View Code Duplication | public function check_artuino_participant_exists($user_nick) { |
|
486 | } |
||
487 | |||
488 | View Code Duplication | public function register_for_artuino($team_name, $contact_number, $team) { |
|
489 | $this->DB->contest->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); |
||
490 | $stmt = $this->db_lib->prepared_execute( |
||
491 | $this->DB->contest, |
||
492 | "INSERT INTO `artuino_teams` |
||
493 | (`team_name`, `contact_number`) VALUES (?, ?)", |
||
494 | "ss", |
||
495 | [$team_name, $contact_number] |
||
496 | ); |
||
497 | if (!$stmt) { |
||
498 | return false; |
||
499 | } |
||
500 | $team_id = $this->DB->contest->insert_id; |
||
501 | foreach ($team as $nick) { |
||
502 | $stmt = $this->db_lib->prepared_execute( |
||
503 | $this->DB->contest, |
||
504 | "INSERT INTO `artuino_participants` |
||
505 | (`team_id`, `nick`) VALUES (?, ?)", |
||
506 | "is", |
||
507 | [$team_id, $nick] |
||
508 | ); |
||
509 | if (!$stmt) { |
||
510 | return false; |
||
511 | } |
||
512 | } |
||
513 | return $this->DB->contest->commit(); |
||
514 | } |
||
515 | |||
516 | View Code Duplication | public function artuino_payment_success($payment_id, $team_id, $status, $payment_data) { |
|
531 | } |
||
532 | } |
||
533 | |||
534 | public function artuino_dump_data($team_id, $type, $response) { |
||
535 | $this->db_lib->prepared_execute( |
||
542 | ); |
||
543 | } |
||
544 | |||
545 | /* |
||
546 | |--------------------------------------------------------------------------- |
||
547 | | MUN |
||
548 | |--------------------------------------------------------------------------- |
||
549 | */ |
||
550 | |||
551 | public function is_registered_for_mun($user_nick) { |
||
552 | return $this->is_registered('mun_registrations', $user_nick); |
||
553 | } |
||
554 | |||
555 | public function register_for_mun($info) { |
||
556 | return $this->db_lib->prepared_execute( |
||
557 | $this->DB->contest, |
||
558 | "INSERT INTO `mun_registrations` |
||
559 | (`nick`, `contact_number`, `institute`, `team_size`, `needs_accomodation`) |
||
560 | VALUES (?, ?, ?, ?, ?)", |
||
561 | "sssii", |
||
562 | [ |
||
563 | $info['nick'], |
||
564 | $info['contact_number'], |
||
565 | $info['institute'], |
||
570 | ); |
||
571 | } |
||
572 | |||
573 | public function mun_payment_success($payment_id, $nick, $status, $payment_data) { |
||
574 | $stmt = $this->db_lib->prepared_execute( |
||
575 | $this->DB->contest, |
||
576 | "UPDATE `mun_registrations` |
||
577 | SET `payment_id` = ?, `payment_status` = ?, `payment_data` = ? |
||
578 | WHERE `nick` = ?", |
||
579 | "ssss", |
||
580 | [$payment_id, $status, $payment_data, $nick] |
||
581 | ); |
||
582 | $this->mun_dump_data($nick, 'callback', $payment_data); |
||
583 | if (!$stmt) { |
||
584 | return false; |
||
585 | } |
||
586 | else { |
||
587 | return true; |
||
588 | } |
||
589 | } |
||
590 | |||
591 | public function mun_dump_data($nick, $type, $response) { |
||
599 | ); |
||
600 | } |
||
603 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.