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 | View Code Duplication | class Give_DB_Comment_Meta extends Give_DB_Meta { |
|
|
|||
25 | /** |
||
26 | * Meta supports. |
||
27 | * |
||
28 | * @since 2.3.0 |
||
29 | * @access protected |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $supports = array(); |
||
33 | |||
34 | /** |
||
35 | * Meta type |
||
36 | * |
||
37 | * @since 2.3.0 |
||
38 | * @access protected |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $meta_type = 'give_comment'; |
||
42 | |||
43 | /** |
||
44 | * Give_DB_Comment_Meta constructor. |
||
45 | * |
||
46 | * @access public |
||
47 | * @since 2.3.0 |
||
48 | */ |
||
49 | public function __construct() { |
||
59 | |||
60 | /** |
||
61 | * Get table columns and data types. |
||
62 | * |
||
63 | * @access public |
||
64 | * @since 2.3.0 |
||
65 | * |
||
66 | * @return array Columns and formats. |
||
67 | */ |
||
68 | public function get_columns() { |
||
76 | |||
77 | /** |
||
78 | * Delete all comment meta |
||
79 | * |
||
80 | * @since 2.3.0 |
||
81 | * @access public |
||
82 | * |
||
83 | * @param int $comment_id |
||
84 | * |
||
85 | * @return bool |
||
86 | */ |
||
87 | public function delete_row( $comment_id = 0 ) { |
||
104 | |||
105 | /** |
||
106 | * Check if current id is valid |
||
107 | * |
||
108 | * @since 2.3.0 |
||
109 | * @access protected |
||
110 | * |
||
111 | * @param $ID |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | protected function is_valid_post_type( $ID ) { |
||
118 | } |
||
119 |
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.