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 |
||
17 | class TableManager extends \EE_Base { |
||
18 | |||
19 | /** |
||
20 | * @var TableAnalysis $table_analysis |
||
21 | */ |
||
22 | private $table_analysis; |
||
23 | |||
24 | |||
25 | |||
26 | /** |
||
27 | * TableManager constructor. |
||
28 | * |
||
29 | * @param TableAnalysis $TableAnalysis |
||
30 | */ |
||
31 | public function __construct( TableAnalysis $TableAnalysis ) { |
||
34 | |||
35 | /** |
||
36 | * Gets the injected table analyzer, or throws an exception |
||
37 | * @return TableAnalysis |
||
38 | * @throws \EE_Error |
||
39 | */ |
||
40 | View Code Duplication | protected function getTableAnalysis() { |
|
52 | |||
53 | |||
54 | |||
55 | /** |
||
56 | * @param string $table_name which can optionally start with $wpdb->prefix or not |
||
57 | * @param string $column_name |
||
58 | * @param string $column_info |
||
59 | * @return bool|false|int |
||
60 | */ |
||
61 | public function addColumn( $table_name, $column_name, $column_info='INT UNSIGNED NOT NULL' ) |
||
75 | |||
76 | /** |
||
77 | * Gets the name of all columns on the table. $table_name can |
||
78 | * optionally start with $wpdb->prefix or not |
||
79 | * @global \wpdb $wpdb |
||
80 | * @param string $table_name |
||
81 | * @return array |
||
82 | */ |
||
83 | public function getTableColumns( $table_name ) |
||
98 | |||
99 | /** |
||
100 | * Drops the specified table from the database. $table_name can |
||
101 | * optionally start with $wpdb->prefix or not |
||
102 | * |
||
103 | * @global \wpdb $wpdb |
||
104 | * @param string $table_name |
||
105 | * @return int |
||
106 | */ |
||
107 | public function dropTable( $table_name ) |
||
116 | |||
117 | /** |
||
118 | * Drops all the tables mentioned in a single MYSQL query. Double-checks |
||
119 | * each table name provided has a wpdb prefix attached, and that it exists. |
||
120 | * Returns the list actually deleted |
||
121 | * @global WPDB $wpdb |
||
122 | * @param array $table_names |
||
123 | * @return array of table names which we deleted |
||
124 | */ |
||
125 | public function dropTables( $table_names ) |
||
138 | |||
139 | /** |
||
140 | * Drops the specified index from the specified table. $table_name can |
||
141 | * optionally start with $wpdb->prefix or not |
||
142 | * |
||
143 | * @global \wpdb $wpdb |
||
144 | * @param string $table_name |
||
145 | * @param string $indexName |
||
146 | * @return int |
||
147 | */ |
||
148 | public function dropIndex( $table_name, $indexName ) |
||
164 | |||
165 | /** |
||
166 | * Just creates the requested table. $table_name can |
||
167 | * optionally start with $wpdb->prefix or not |
||
168 | * @param string $table_name |
||
169 | * @param string $createSql defining the table's columns and indexes |
||
170 | * @param string $engine (no need to specify "ENGINE=", that's implied) |
||
171 | * @return void |
||
172 | * @throws \EE_Error |
||
173 | */ |
||
174 | public function createTable( $table_name, $createSql, $engine = 'MyISAM' ) |
||
205 | |||
206 | } |
||
207 |
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.