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 |
||
13 | class TableManager extends \EE_Base |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @var TableAnalysis $table_analysis |
||
18 | */ |
||
19 | private $table_analysis; |
||
20 | |||
21 | |||
22 | /** |
||
23 | * TableManager constructor. |
||
24 | * |
||
25 | * @param TableAnalysis $TableAnalysis |
||
26 | */ |
||
27 | public function __construct(TableAnalysis $TableAnalysis) |
||
31 | |||
32 | |||
33 | /** |
||
34 | * Gets the injected table analyzer, or throws an exception |
||
35 | * |
||
36 | * @return TableAnalysis |
||
37 | * @throws \EE_Error |
||
38 | */ |
||
39 | View Code Duplication | protected function getTableAnalysis() |
|
52 | |||
53 | |||
54 | /** |
||
55 | * @param string $table_name which can optionally start with $wpdb->prefix or not |
||
56 | * @param string $column_name |
||
57 | * @param string $column_info |
||
58 | * @return bool|false|int |
||
59 | */ |
||
60 | public function addColumn($table_name, $column_name, $column_info = 'INT UNSIGNED NOT NULL') |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Gets the name of all columns on the table. $table_name can |
||
78 | * optionally start with $wpdb->prefix or not |
||
79 | * |
||
80 | * @global \wpdb $wpdb |
||
81 | * @param string $table_name |
||
82 | * @return array |
||
83 | */ |
||
84 | public function getTableColumns($table_name) |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Drops the specified table from the database. $table_name can |
||
103 | * optionally start with $wpdb->prefix or not |
||
104 | * |
||
105 | * @global \wpdb $wpdb |
||
106 | * @param string $table_name |
||
107 | * @return int |
||
108 | */ |
||
109 | public function dropTable($table_name) |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Drops all the tables mentioned in a single MYSQL query. Double-checks |
||
122 | * each table name provided has a wpdb prefix attached, and that it exists. |
||
123 | * Returns the list actually deleted |
||
124 | * |
||
125 | * @global WPDB $wpdb |
||
126 | * @param array $table_names |
||
127 | * @return array of table names which we deleted |
||
128 | */ |
||
129 | public function dropTables($table_names) |
||
146 | |||
147 | |||
148 | /** |
||
149 | * Drops the specified index from the specified table. $table_name can |
||
150 | * optionally start with $wpdb->prefix or not |
||
151 | * |
||
152 | * @global \wpdb $wpdb |
||
153 | * @param string $table_name |
||
154 | * @param string $index_name |
||
155 | * @return int the number of indexes dropped. False if there was a datbase error |
||
156 | */ |
||
157 | public function dropIndex($table_name, $index_name) |
||
173 | |||
174 | |||
175 | /** |
||
176 | * Just creates the requested table. $table_name can |
||
177 | * optionally start with $wpdb->prefix or not |
||
178 | * |
||
179 | * @param string $table_name |
||
180 | * @param string $create_sql defining the table's columns and indexes |
||
181 | * @param string $engine (no need to specify "ENGINE=", that's implied) |
||
182 | * @return void |
||
183 | * @throws \EE_Error |
||
184 | */ |
||
185 | public function createTable($table_name, $create_sql, $engine = 'MyISAM') |
||
220 | |||
221 | |||
222 | /** |
||
223 | * Drops the specified index if it's size differs from $desired_index_size. |
||
224 | * WordPress' dbdelta method doesn't automatically change index sizes, so this |
||
225 | * method can be used to only drop the index if needed, and afterwards dbdelta can be used as normal. |
||
226 | * If the table doesn't exist, or it exists but the index does not, or returns false |
||
227 | * |
||
228 | * @param string $table_name |
||
229 | * @param string $index_name |
||
230 | * @param string $column_name if none is provided, we assume the column name matches the index (often |
||
231 | * true in EE) |
||
232 | * @param string|int $desired_index_size defaults to TableAnalysis::index_col_size, the max for utf8mb4. |
||
233 | * @return bool whether an index was dropped or not |
||
234 | * @throws /EE_Error if table analysis object isn't defined |
||
235 | */ |
||
236 | public function dropIndexIfSizeNot( |
||
260 | } |
||
261 |