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 DbBak2Sql 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 DbBak2Sql, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class DbBak2Sql extends Fwolflib { |
||
|
|||
26 | /** |
||
27 | * Charset of database |
||
28 | * If charset of db diff from os, do convert when execute sql. |
||
29 | * @var string |
||
30 | * @access public |
||
31 | */ |
||
32 | var $mCharsetDb = ''; |
||
33 | |||
34 | /** |
||
35 | * Charset of operation system |
||
36 | * @var string |
||
37 | * @access public |
||
38 | * @see $mCharsetDb |
||
39 | */ |
||
40 | var $mCharsetOs = ''; |
||
41 | |||
42 | /** |
||
43 | * Db connection object |
||
44 | * @var object |
||
45 | * @access private |
||
46 | */ |
||
47 | var $mDb; |
||
48 | |||
49 | /** |
||
50 | * Ignore columns |
||
51 | * These columns will not be trans to sql when processed. |
||
52 | * @var array |
||
53 | * @access public |
||
54 | */ |
||
55 | var $mIgnoreColumn = array('lasttime'); |
||
56 | |||
57 | /** |
||
58 | * Log file |
||
59 | * Log file to write, this is only filename, path is $this->mTargetPath |
||
60 | * @var string |
||
61 | * @access public |
||
62 | * @see $mTargetPath |
||
63 | */ |
||
64 | var $mLogFile = 'dbbak2sql.log'; |
||
65 | |||
66 | /** |
||
67 | * Db server information array |
||
68 | * Array item: host, user, pass, name, type. |
||
69 | * @var array |
||
70 | * @access private |
||
71 | */ |
||
72 | var $mServer = array(); |
||
73 | |||
74 | /** |
||
75 | * Summary text |
||
76 | * @var string |
||
77 | * @access public |
||
78 | */ |
||
79 | var $mSummary = ''; |
||
80 | |||
81 | /** |
||
82 | * Tables will be backup |
||
83 | * Include needed, exclude no needed, this is result. |
||
84 | * @var array |
||
85 | * @access private |
||
86 | */ |
||
87 | var $mTable = array(); |
||
88 | |||
89 | /** |
||
90 | * Tables to be exclude from backup task |
||
91 | * @var array |
||
92 | * @access private |
||
93 | */ |
||
94 | var $mTableExclude = array(); |
||
95 | |||
96 | /** |
||
97 | * Table need to be group by some cols when backup |
||
98 | * Usually used when table contains too much rows |
||
99 | * @var array |
||
100 | * @access private |
||
101 | */ |
||
102 | var $mTableGroupby = array(); |
||
103 | |||
104 | /** |
||
105 | * Tables to be include in backup task |
||
106 | * If not empty, will only backup tables in this list. |
||
107 | * @var array |
||
108 | * @access private |
||
109 | */ |
||
110 | var $mTableInclude = array(); |
||
111 | |||
112 | /** |
||
113 | * Where to save exported sql files. |
||
114 | * @var string |
||
115 | * @access private |
||
116 | */ |
||
117 | var $mTargetPath = '/tmp/dbbak2sql'; |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Construct function |
||
122 | * @access public |
||
123 | * @param array $server Db server information |
||
124 | */ |
||
125 | function __construct($server=array()) |
||
130 | |||
131 | |||
132 | /** |
||
133 | * Do backup to database |
||
134 | * @access public |
||
135 | */ |
||
136 | public function Bak() |
||
144 | |||
145 | |||
146 | /** |
||
147 | * Do backup to a single table |
||
148 | * @access private |
||
149 | * @param string $tbl Table name |
||
150 | */ |
||
151 | private function BakTable($tbl) |
||
245 | |||
246 | |||
247 | /** |
||
248 | * 获得数据库连接 |
||
249 | * @access private |
||
250 | * @param array $server |
||
251 | * @return object |
||
252 | */ |
||
253 | private function &DbConn($server) |
||
281 | |||
282 | |||
283 | /** |
||
284 | * Retrieve table list from db |
||
285 | * @access private |
||
286 | */ |
||
287 | private function GetTableList() |
||
307 | |||
308 | |||
309 | /** |
||
310 | * Get fields of a table, ignore prefered fields |
||
311 | * @access private |
||
312 | * @param string $tbl |
||
313 | * @return array |
||
314 | */ |
||
315 | private function GetTblFields($tbl) |
||
329 | |||
330 | |||
331 | /** |
||
332 | * Convert groupby rules to where sql clauses |
||
333 | * Retrieve data from db by groupby rules, and convert to where sql. |
||
334 | * Used when backup, where clauses can used directly in select sql |
||
335 | * @access private |
||
336 | * @param string $tbl |
||
337 | * @return array |
||
338 | */ |
||
339 | private function GroupbyRule2WhereSql($tbl) |
||
365 | |||
366 | |||
367 | /** |
||
368 | * Save log |
||
369 | * Both log file and summary text is saved to. |
||
370 | * @access private |
||
371 | * @param string $log |
||
372 | */ |
||
373 | public function Log ($log) { |
||
379 | |||
380 | |||
381 | /** |
||
382 | * Determin if current db driver need set identity_insert tbl on/off |
||
383 | * @access private |
||
384 | * @return boolean |
||
385 | */ |
||
386 | private function NeedIdentityInsert() |
||
394 | |||
395 | |||
396 | /** |
||
397 | * Parse sql text used in sql value field |
||
398 | * @access private |
||
399 | * @param string $val |
||
400 | * @param string $type |
||
401 | * @return string |
||
402 | */ |
||
403 | private function ParseSqlData($val, $type) |
||
427 | |||
428 | |||
429 | /** |
||
430 | * Convert ADOdb recordset to sql text |
||
431 | * @access private |
||
432 | * @param object $rs |
||
433 | * @param string $tbl |
||
434 | * @param array $cols |
||
435 | * @return string |
||
436 | */ |
||
437 | private function Rs2Sql(&$rs, $tbl, $cols=array()) |
||
470 | |||
471 | |||
472 | /** |
||
473 | * Accept database information from outside class |
||
474 | * Didnot validate data send in. |
||
475 | * And connect to db after store infomation. |
||
476 | * @access public |
||
477 | * @var array $server array items: host, user, pass, name, type |
||
478 | */ |
||
479 | View Code Duplication | public function SetDatabase($server) |
|
487 | |||
488 | |||
489 | /** |
||
490 | * Set tables will not be backup |
||
491 | * @access public |
||
492 | * @var array $ar |
||
493 | */ |
||
494 | public function SetTableExclude($ar) |
||
501 | |||
502 | |||
503 | /** |
||
504 | * Set table group by rules when backup-select |
||
505 | * If given cols is empty, it will remove tbl from list need-to-be groupby. |
||
506 | * Multi cols can be assigned split by ','. |
||
507 | * @access public |
||
508 | * @var string $tbl |
||
509 | * @var string $cols |
||
510 | */ |
||
511 | public function SetTableGroupby($tbl, $cols) |
||
518 | |||
519 | |||
520 | /** |
||
521 | * Set tables will only be backup |
||
522 | * @access public |
||
523 | * @var array $ar |
||
524 | */ |
||
525 | public function SetTableInclude($ar) |
||
532 | |||
533 | |||
534 | /** |
||
535 | * Set where to save sql files exported. |
||
536 | * If directory doesn't exists, create it. |
||
537 | * @access public |
||
538 | * @var string $path |
||
539 | */ |
||
540 | public function SetTargetPath($path) |
||
549 | |||
550 | /** |
||
551 | * Print or write summary text of the whole backup process |
||
552 | * @access private |
||
553 | */ |
||
554 | private function Summary() |
||
558 | |||
559 | |||
560 | } // end of class DbBak2Sql |
||
561 | /* |
||
589 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.