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 |
||
24 | class Give_DB_Logs extends Give_DB { |
||
25 | |||
26 | /** |
||
27 | * Give_DB_Logs constructor. |
||
28 | * |
||
29 | * Set up the Give DB Donor class. |
||
30 | * |
||
31 | * @since 2.0 |
||
32 | * @access public |
||
33 | */ |
||
34 | View Code Duplication | public function __construct() { |
|
|
|||
35 | /* @var WPDB $wpdb */ |
||
36 | global $wpdb; |
||
37 | |||
38 | $this->table_name = $wpdb->prefix . 'give_logs'; |
||
39 | $this->primary_key = 'ID'; |
||
40 | $this->version = '1.0'; |
||
41 | |||
42 | // Install table. |
||
43 | $this->register_table(); |
||
44 | |||
45 | parent::__construct(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Get columns and formats |
||
50 | * |
||
51 | * @since 2.0 |
||
52 | * @access public |
||
53 | * |
||
54 | * @return array Columns and formats. |
||
55 | */ |
||
56 | public function get_columns() { |
||
67 | |||
68 | /** |
||
69 | * Get default column values |
||
70 | * |
||
71 | * @since 2.0 |
||
72 | * @access public |
||
73 | * |
||
74 | * @return array Default column values. |
||
75 | */ |
||
76 | public function get_column_defaults() { |
||
90 | |||
91 | /** |
||
92 | * Add a log |
||
93 | * |
||
94 | * @since 2.0 |
||
95 | * @access public |
||
96 | * |
||
97 | * @param array $data |
||
98 | * |
||
99 | * @return bool|int |
||
100 | */ |
||
101 | public function add( $data = array() ) { |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Retrieves a single log from the database |
||
142 | * |
||
143 | * @since 2.0 |
||
144 | * @access public |
||
145 | * |
||
146 | * @param int $log_id |
||
147 | * @param string $by |
||
148 | * |
||
149 | * @return bool|null|array |
||
150 | */ |
||
151 | public function get_log_by( $log_id = 0, $by = 'id' ) { |
||
181 | |||
182 | /** |
||
183 | * Retrieve logs from the database. |
||
184 | * |
||
185 | * @since 2.0 |
||
186 | * @access public |
||
187 | * |
||
188 | * @param array $args |
||
189 | * |
||
190 | * @return mixed |
||
191 | */ |
||
192 | public function get_logs( $args = array() ) { |
||
204 | |||
205 | |||
206 | /** |
||
207 | * Count the total number of logs in the database |
||
208 | * |
||
209 | * @since 2.0 |
||
210 | * @access public |
||
211 | * |
||
212 | * @param array $args |
||
213 | * |
||
214 | * @return int |
||
215 | */ |
||
216 | public function count( $args = array() ) { |
||
232 | |||
233 | /** |
||
234 | * Create the table |
||
235 | * |
||
236 | * @since 2.0 |
||
237 | * @access public |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | View Code Duplication | public function create_table() { |
|
242 | global $wpdb; |
||
243 | $charset_collate = $wpdb->get_charset_collate(); |
||
244 | |||
245 | $sql = "CREATE TABLE {$this->table_name} ( |
||
246 | ID bigint(20) NOT NULL AUTO_INCREMENT, |
||
247 | log_title longtext NOT NULL, |
||
248 | log_content longtext NOT NULL, |
||
249 | log_parent bigint(20) NOT NULL, |
||
250 | log_type mediumtext NOT NULL, |
||
251 | log_date datetime NOT NULL, |
||
252 | log_date_gmt datetime NOT NULL, |
||
253 | PRIMARY KEY (ID) |
||
254 | ) {$charset_collate};"; |
||
255 | |||
256 | require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
||
257 | dbDelta( $sql ); |
||
258 | |||
259 | update_option( $this->table_name . '_db_version', $this->version, false ); |
||
260 | } |
||
261 | |||
262 | |||
263 | /** |
||
264 | * Get sql query from quaried array. |
||
265 | * |
||
266 | * @since 2.0 |
||
267 | * @access public |
||
268 | * |
||
269 | * @param array $args |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | public function get_sql( $args = array() ) { |
||
374 | |||
375 | |||
376 | /** |
||
377 | * Validate query params. |
||
378 | * |
||
379 | * @since 2.0 |
||
380 | * @access private |
||
381 | * |
||
382 | * @param $args |
||
383 | * |
||
384 | * @return mixed |
||
385 | */ |
||
386 | private function validate_params( &$args ) { |
||
395 | } |
||
396 |
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.