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_Donor_Meta extends Give_DB_Meta { |
||
25 | |||
26 | /** |
||
27 | * Meta supports. |
||
28 | * |
||
29 | * @since 2.0 |
||
30 | * @access protected |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $supports = array(); |
||
34 | |||
35 | /** |
||
36 | * Meta type |
||
37 | * |
||
38 | * @since 2.0 |
||
39 | * @access public |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | public $meta_type = 'donor'; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Give_DB_Donor_Meta constructor. |
||
48 | * |
||
49 | * @access public |
||
50 | * @since 1.6 |
||
51 | */ |
||
52 | View Code Duplication | public function __construct() { |
|
|
|||
53 | /* @var WPDB $wpdb */ |
||
54 | global $wpdb; |
||
55 | |||
56 | $wpdb->donormeta = $this->table_name = $wpdb->prefix . 'give_donormeta'; |
||
57 | $this->primary_key = 'meta_id'; |
||
58 | $this->version = '1.0'; |
||
59 | |||
60 | parent::__construct(); |
||
61 | |||
62 | $this->bc_200_params(); |
||
63 | $this->register_table(); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Get table columns and data types. |
||
68 | * |
||
69 | * @access public |
||
70 | * @since 1.6 |
||
71 | * |
||
72 | * @return array Columns and formats. |
||
73 | */ |
||
74 | public function get_columns() { |
||
82 | |||
83 | /** |
||
84 | * Add backward compatibility for old table name |
||
85 | * |
||
86 | * @since 2.0 |
||
87 | * @access private |
||
88 | * @global wpdb $wpdb |
||
89 | */ |
||
90 | private function bc_200_params() { |
||
104 | |||
105 | /** |
||
106 | * Check if current id is valid |
||
107 | * |
||
108 | * @since 2.0 |
||
109 | * @access protected |
||
110 | * |
||
111 | * @param $ID |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | protected function is_valid_post_type( $ID ) { |
||
118 | |||
119 | } |
||
120 |
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.