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 |
||
20 | class Give_DB_Sessions extends Give_DB { |
||
21 | /** |
||
22 | * Cache group name |
||
23 | * |
||
24 | * @since 2.2.0 |
||
25 | * @access private |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | private $cache_group = 'give_sessions'; |
||
30 | |||
31 | /** |
||
32 | * Cache incrementer name |
||
33 | * |
||
34 | * @since 2.2.0 |
||
35 | * @access private |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $incrementer_name = 'give_sessions'; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Class Constructor |
||
44 | * |
||
45 | * @since 2.2.0 |
||
46 | * @access public |
||
47 | */ |
||
48 | public function __construct() { |
||
65 | |||
66 | |||
67 | /** |
||
68 | * Whitelist of columns |
||
69 | * |
||
70 | * @since 2.2.0 |
||
71 | * @access public |
||
72 | * |
||
73 | * @return array Columns and formats. |
||
74 | */ |
||
75 | public function get_columns() { |
||
83 | |||
84 | /** |
||
85 | * Create Meta Tables. |
||
86 | * |
||
87 | * @since 2.2.0 |
||
88 | * @access public |
||
89 | */ |
||
90 | View Code Duplication | public function create_table() { |
|
109 | |||
110 | |||
111 | /** |
||
112 | * Returns the session. |
||
113 | * |
||
114 | * @todo: add cache logic |
||
115 | * |
||
116 | * @param string $donor_id Donor ID. |
||
117 | * @param mixed $default Default session value. |
||
118 | * |
||
119 | * @return mixed |
||
120 | */ |
||
121 | public function get_session( $donor_id, $default = false ) { |
||
152 | |||
153 | /** |
||
154 | * Update the session expiry timestamp. |
||
155 | * |
||
156 | * @param string $donor_id Donor ID. |
||
157 | * @param int $timestamp Timestamp to expire the cookie. |
||
158 | */ |
||
159 | public function update_session_timestamp( $donor_id, $timestamp ) { |
||
177 | |||
178 | /** |
||
179 | * Delete the session from the cache and database. |
||
180 | * |
||
181 | * @since 2.2.0 |
||
182 | * @access public |
||
183 | * |
||
184 | * @param int $donor_id Customer ID. |
||
185 | */ |
||
186 | public function delete_session( $donor_id ) { |
||
200 | |||
201 | |||
202 | /** |
||
203 | * Cleanup session data from the database and clear caches. |
||
204 | * Note: for internal logic only. |
||
205 | * |
||
206 | * @since 2.2.0 |
||
207 | * @access public |
||
208 | */ |
||
209 | public function delete_expired_sessions() { |
||
223 | |||
224 | /** |
||
225 | * Replace table data |
||
226 | * Note: only for internal use |
||
227 | * |
||
228 | * @since 2.2.0 |
||
229 | * @access public |
||
230 | * |
||
231 | * @param string $table_name Table name. |
||
232 | * @param array $data Data. |
||
233 | * @param array $format Array for data format of each key:value in data. |
||
234 | */ |
||
235 | public function __replace( $table_name, $data, $format = null ) { |
||
249 | } |
||
250 |
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.