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:
Complex classes like AdminerPlugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AdminerPlugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class AdminerPlugin extends Adminer { |
||
10 | /** @access protected */ |
||
11 | var $plugins; |
||
12 | |||
13 | function _findRootClass($class) { // is_subclass_of(string, string) is available since PHP 5.0.3 |
||
19 | |||
20 | /** Register plugins |
||
21 | * @param array object instances or null to register all classes starting by 'Adminer' |
||
22 | */ |
||
23 | function __construct($plugins) { |
||
35 | |||
36 | function _callParent($function, $args) { |
||
39 | |||
40 | function _applyPlugin($function, $args) { |
||
60 | |||
61 | function _appendPlugin($function, $args) { |
||
70 | |||
71 | // appendPlugin |
||
72 | |||
73 | function dumpFormat() { |
||
77 | |||
78 | function dumpOutput() { |
||
82 | |||
83 | function editFunctions($field) { |
||
87 | |||
88 | // applyPlugin |
||
89 | |||
90 | function name() { |
||
94 | |||
95 | function credentials() { |
||
99 | |||
100 | function permanentLogin($create = false) { |
||
104 | |||
105 | function database() { |
||
109 | |||
110 | function schemas() { |
||
114 | |||
115 | function databases($flush = true) { |
||
119 | |||
120 | function queryTimeout() { |
||
124 | |||
125 | function headers() { |
||
129 | |||
130 | function head() { |
||
134 | |||
135 | function loginForm() { |
||
139 | |||
140 | function login($login, $password) { |
||
144 | |||
145 | function tableName($tableStatus) { |
||
149 | |||
150 | function fieldName($field, $order = 0) { |
||
154 | |||
155 | function selectLinks($tableStatus, $set = '') { |
||
159 | |||
160 | function foreignKeys($table) { |
||
164 | |||
165 | function backwardKeys($table, $tableName) { |
||
169 | |||
170 | function backwardKeysPrint($backwardKeys, $row) { |
||
174 | |||
175 | function selectQuery($query, $time) { |
||
179 | |||
180 | function rowDescription($table) { |
||
184 | |||
185 | function rowDescriptions($rows, $foreignKeys) { |
||
189 | |||
190 | function selectLink($val, $field) { |
||
194 | |||
195 | function selectVal($val, $link, $field, $original) { |
||
199 | |||
200 | function editVal($val, $field) { |
||
204 | |||
205 | function selectColumnsPrint($select, $columns) { |
||
209 | |||
210 | function selectSearchPrint($where, $columns, $indexes) { |
||
214 | |||
215 | function selectOrderPrint($order, $columns, $indexes) { |
||
219 | |||
220 | function selectLimitPrint($limit) { |
||
224 | |||
225 | function selectLengthPrint($text_length) { |
||
229 | |||
230 | function selectActionPrint($indexes) { |
||
234 | |||
235 | function selectCommandPrint() { |
||
239 | |||
240 | function selectImportPrint() { |
||
244 | |||
245 | function selectEmailPrint($emailFields, $columns) { |
||
249 | |||
250 | function selectColumnsProcess($columns, $indexes) { |
||
254 | |||
255 | function selectSearchProcess($fields, $indexes) { |
||
259 | |||
260 | function selectOrderProcess($fields, $indexes) { |
||
264 | |||
265 | function selectLimitProcess() { |
||
269 | |||
270 | function selectLengthProcess() { |
||
274 | |||
275 | function selectEmailProcess($where, $foreignKeys) { |
||
279 | |||
280 | function selectQueryBuild($select, $where, $group, $order, $limit, $page) { |
||
284 | |||
285 | function messageQuery($query, $time) { |
||
289 | |||
290 | function editInput($table, $field, $attrs, $value) { |
||
294 | |||
295 | function processInput($field, $value, $function = '') { |
||
299 | |||
300 | function dumpDatabase($db) { |
||
304 | |||
305 | function dumpTable($table, $style, $is_view = 0) { |
||
309 | |||
310 | function dumpData($table, $style, $query) { |
||
314 | |||
315 | function dumpFilename($identifier) { |
||
319 | |||
320 | function dumpHeaders($identifier, $multi_table = false) { |
||
324 | |||
325 | function homepage() { |
||
329 | |||
330 | function navigation($missing) { |
||
334 | |||
335 | function databasesPrint($missing) { |
||
339 | |||
340 | function tablesPrint($tables) { |
||
344 | |||
345 | } |
||
346 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.