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 |
||
19 | abstract class SqlDBStore extends Store { |
||
20 | |||
21 | // There are two types of stores - for event ids and status |
||
22 | const BAT_EVENT = 'event'; |
||
23 | const BAT_STATE = 'state'; |
||
24 | |||
25 | /** |
||
26 | * The table that holds day data. |
||
27 | * @var |
||
28 | */ |
||
29 | public $day_table; |
||
30 | |||
31 | /** |
||
32 | * The table that holds hour data. |
||
33 | * @var |
||
34 | */ |
||
35 | public $hour_table; |
||
36 | |||
37 | /** |
||
38 | * The table that holds minute data. |
||
39 | * @var |
||
40 | */ |
||
41 | public $minute_table; |
||
42 | |||
43 | /** |
||
44 | * The table that holds day data without prefix. |
||
45 | * @var |
||
46 | */ |
||
47 | public $day_table_no_prefix; |
||
48 | |||
49 | /** |
||
50 | * The table that holds hour data without prefix. |
||
51 | * @var |
||
52 | */ |
||
53 | public $hour_table_no_prefix; |
||
54 | |||
55 | /** |
||
56 | * The table that holds minute data without prefix. |
||
57 | * @var |
||
58 | */ |
||
59 | public $minute_table_no_prefix; |
||
60 | |||
61 | /** |
||
62 | * The event type we are dealing with. |
||
63 | * @var |
||
64 | */ |
||
65 | public $event_type; |
||
66 | |||
67 | /** |
||
68 | * SqlDBStore constructor. |
||
69 | * |
||
70 | * Provided with the event type it will determine the appropriate table names to |
||
71 | * store data in. This assumes standard behaviour from Bat_Event |
||
72 | * @param $event_type |
||
73 | * @param string $event_data |
||
74 | */ |
||
75 | public function __construct($event_type, $event_data = 'state', $prefix = '') { |
||
100 | |||
101 | /** |
||
102 | * @param \DateTime $start_date |
||
103 | * @param \DateTime $end_date |
||
104 | * @param $unit_ids |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function buildQueries(\DateTime $start_date, \DateTime $end_date, $unit_ids) { |
||
152 | |||
153 | } |
||
154 |
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.