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 DbUpdater 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 DbUpdater, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class DbUpdater extends Fwolflib { |
||
|
|||
40 | /** |
||
41 | * Db server information array |
||
42 | * Array item: type, host, user, pass, name. |
||
43 | * @var array |
||
44 | */ |
||
45 | private $aServer = array(); |
||
46 | |||
47 | /** |
||
48 | * Last update id |
||
49 | * @var int |
||
50 | */ |
||
51 | public $iLastId = 0; |
||
52 | |||
53 | /** |
||
54 | * Last done update id |
||
55 | * @var int |
||
56 | */ |
||
57 | public $iLastDoneId = 0; |
||
58 | |||
59 | /** |
||
60 | * Charset of database |
||
61 | * If charset of db diff from os, do convert when execute sql. |
||
62 | * @var string |
||
63 | */ |
||
64 | public $sCharsetDb = ''; |
||
65 | |||
66 | /** |
||
67 | * Charset of operation system |
||
68 | * @var string |
||
69 | * @see $sCharsetDb |
||
70 | */ |
||
71 | public $sCharsetOs = ''; |
||
72 | |||
73 | /** |
||
74 | * Summary text |
||
75 | * @var string |
||
76 | */ |
||
77 | public $sSummary = ''; |
||
78 | |||
79 | /** |
||
80 | * Table to save modifications and logs |
||
81 | * |
||
82 | * If u want to change log table name in use(had log some sql already) |
||
83 | * remember to rename table in database also. |
||
84 | * @var string |
||
85 | */ |
||
86 | public $sTblLog = 'log_dbupdater'; |
||
87 | |||
88 | |||
89 | /** |
||
90 | * Db connection object |
||
91 | * @var object |
||
92 | */ |
||
93 | public $oDb; |
||
94 | |||
95 | /** |
||
96 | * Construct function |
||
97 | * @access public |
||
98 | * @param array $server Db server information |
||
99 | */ |
||
100 | function __construct($dbserver=array()) |
||
108 | |||
109 | |||
110 | /** |
||
111 | * Check and install log table if not exists |
||
112 | * @access public |
||
113 | */ |
||
114 | public function CheckLogTbl() |
||
189 | |||
190 | |||
191 | /** |
||
192 | * 获得数据库连接 |
||
193 | * @param array $server |
||
194 | * @return object |
||
195 | */ |
||
196 | private function &DbConn($server) |
||
202 | |||
203 | |||
204 | /** |
||
205 | * Del error record when last done. |
||
206 | * |
||
207 | * So it can rewrite/update these record in db. |
||
208 | * Only del failed sql is not enough, |
||
209 | * you need del all sql start from the failed ONE. |
||
210 | */ |
||
211 | public function DelErrorSql() { |
||
224 | |||
225 | |||
226 | /** |
||
227 | * Do updates according to update-records in log table |
||
228 | */ |
||
229 | public function DoUpdate() |
||
274 | |||
275 | |||
276 | /** |
||
277 | * Get last update id, whether done or not |
||
278 | * @access public |
||
279 | * @return int |
||
280 | */ |
||
281 | View Code Duplication | public function GetLastId() |
|
292 | |||
293 | |||
294 | /** |
||
295 | * Get last done update id |
||
296 | * @access public |
||
297 | * @return int |
||
298 | */ |
||
299 | View Code Duplication | public function GetLastDoneId() |
|
314 | |||
315 | |||
316 | /** |
||
317 | * Return if an update is already done |
||
318 | * Use cached $iLastDoneId if it's non-zero, doesn't retrieve id from db. |
||
319 | * @access public |
||
320 | * @param int $id |
||
321 | * @return boolean |
||
322 | * @see $iLastDoneId |
||
323 | * @see GetLastDoneId() |
||
324 | */ |
||
325 | public function IfDone($id) |
||
331 | |||
332 | |||
333 | /* |
||
334 | * Save log |
||
335 | * @access private |
||
336 | * @param string $log |
||
337 | */ |
||
338 | public function Log($log) |
||
342 | |||
343 | |||
344 | /** |
||
345 | * Accept database information from outside class |
||
346 | * Didnot validate data send in. |
||
347 | * And connect to db after store infomation. |
||
348 | * @access public |
||
349 | * @var array $server array items: type, host, user, pass, name |
||
350 | */ |
||
351 | View Code Duplication | public function SetDatabase($server) |
|
359 | |||
360 | |||
361 | /** |
||
362 | * Set a update step, but doesn't execute it |
||
363 | * Use $iLastId if it's non-zero. |
||
364 | * @access public |
||
365 | * @param int $id |
||
366 | * @param string $comment |
||
367 | * @param string $sqltext |
||
368 | * @see $iLastId |
||
369 | */ |
||
370 | public function SetUpdate($id, $comment, $sqltext) |
||
399 | |||
400 | |||
401 | /** |
||
402 | * Set a update record's status done or not or failed |
||
403 | * Didn't validate $id or $status. |
||
404 | * @param int $id Update id |
||
405 | * @param int $status Update id's status(0/1/-1) |
||
406 | */ |
||
407 | private function SetUpdateDone($id, $status) |
||
419 | |||
420 | |||
421 | /** |
||
422 | * Return summary text of the whole backup process |
||
423 | * @param boolean print |
||
424 | * @return string |
||
425 | */ |
||
426 | public function Summary($print = false) |
||
438 | |||
439 | |||
440 | } // end of class DbUpdater |
||
441 | ?> |
||
442 |
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.