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 Module 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 Module, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | abstract class Module extends Fwolflib { |
||
|
|||
25 | |||
26 | /** |
||
27 | * Cache object |
||
28 | * @var object |
||
29 | */ |
||
30 | public $oCache = NULL; |
||
31 | |||
32 | /** |
||
33 | * Use cache or not |
||
34 | * @var boolean |
||
35 | */ |
||
36 | public $bCacheOn = FALSE; |
||
37 | |||
38 | /** |
||
39 | * Database object |
||
40 | * @var object |
||
41 | */ |
||
42 | public $oDb = NULL; |
||
43 | |||
44 | /** |
||
45 | * Number of items in list |
||
46 | * |
||
47 | * In simple idea, this should set in view, |
||
48 | * but pagesize is impleted as limit in select, |
||
49 | * so when generate sql you need to use it. |
||
50 | * @var int |
||
51 | */ |
||
52 | public $iPageSize = 10; |
||
53 | |||
54 | /** |
||
55 | * Caller: view object |
||
56 | * @var object |
||
57 | */ |
||
58 | public $oView = NULL; |
||
59 | |||
60 | |||
61 | // Get db connection, because unknown db & dblib, |
||
62 | // implete it in application module class. |
||
63 | // Also can extend db connect class easily. |
||
64 | abstract protected function DbConn ($dbprofile); |
||
65 | |||
66 | |||
67 | /** |
||
68 | * construct |
||
69 | * @param object $view Caller view object |
||
70 | */ |
||
71 | public function __construct ($view = null) { |
||
85 | |||
86 | |||
87 | /** |
||
88 | * Overload __call |
||
89 | * Auto call CacheXxx func. |
||
90 | * |
||
91 | * @param string $name Method name |
||
92 | * @param array $arg Method argument |
||
93 | * @return mixed |
||
94 | */ |
||
95 | public function __call ($name, $arg) { |
||
130 | |||
131 | |||
132 | /** |
||
133 | * Get cache lifetime |
||
134 | * Subclass should rewrite this method, |
||
135 | * to get lifetime by assigned key. |
||
136 | * |
||
137 | * @param string $key Key, or key prefix |
||
138 | * @return int Lifetime in seconds |
||
139 | */ |
||
140 | public function CacheLifetime ($key) { |
||
144 | |||
145 | |||
146 | /** |
||
147 | * Compare new data with data from db, get diff array |
||
148 | * |
||
149 | * New/old array are all assoc, index by table column. |
||
150 | * PK column must exists in new array, value can be NULL. |
||
151 | * |
||
152 | * In new array, empty PK means INSERT. |
||
153 | * In old array, empty or not exists PK means DELETE. |
||
154 | * |
||
155 | * Multi table and row supported, new/old array must match. |
||
156 | * |
||
157 | * After modify db according diff, 'flag' should be set, |
||
158 | * 'code, msg' can also used to store db op message. |
||
159 | * When db query success, 'code' is count for rows changed, |
||
160 | * when db query fail, 'code' is error no and 'msg' is error msg. |
||
161 | * |
||
162 | * New/old array structure: |
||
163 | * array( |
||
164 | * [tbl] => array( |
||
165 | * array( // Single row can admit this level |
||
166 | * // DbDiffRow() use this as param |
||
167 | * [col] => [val], |
||
168 | * ... |
||
169 | * ), |
||
170 | * ... |
||
171 | * ), |
||
172 | * ... |
||
173 | * ) |
||
174 | * |
||
175 | * Result is array, structure: |
||
176 | * array( |
||
177 | * code: >= 0 means success, < 0 means error |
||
178 | * msg: Error msg |
||
179 | * flag: 0=default, 100=committed, -100=rollbacked |
||
180 | * diff = array( |
||
181 | * [tbl] = array( |
||
182 | * array( // DbDiffRow() return this array |
||
183 | * mode: INSERT | DELETE | UPDATE |
||
184 | * // PK new/old is same in UPDATE mode |
||
185 | * pk: array( |
||
186 | * [pk1] = array( |
||
187 | * old => [val], |
||
188 | * new => [val], |
||
189 | * ), |
||
190 | * ... |
||
191 | * ), |
||
192 | * // Other cols change |
||
193 | * col: array( |
||
194 | * [col] = array( |
||
195 | * old => [val], |
||
196 | * new => [val], |
||
197 | * ), |
||
198 | * ... |
||
199 | * ), |
||
200 | * ), |
||
201 | * ... |
||
202 | * ), |
||
203 | * ... |
||
204 | * ) |
||
205 | * ) |
||
206 | * @param array $ar_new New data array |
||
207 | * @param Adodb $db Db object, need to be connected, |
||
208 | * NULL to use $this->oDb. |
||
209 | * @param array $ar_old Old data array, NULL to read from db |
||
210 | * @return array |
||
211 | */ |
||
212 | public function DbDiff (array $ar_new, Adodb $db = NULL, array $ar_old = NULL) { |
||
280 | |||
281 | |||
282 | /** |
||
283 | * Execute DbDiff()'s result to modify db |
||
284 | * |
||
285 | * @param array $ar_diff Same with DbDiff()'s result |
||
286 | * @param Adodb $db |
||
287 | * @return int Rows modified, < 0 when error. |
||
288 | * @see DbDiff() |
||
289 | */ |
||
290 | View Code Duplication | public function DbDiffCommit (array &$ar_diff, Adodb $db = NULL) { |
|
377 | |||
378 | |||
379 | /** |
||
380 | * Do DbDiff() and commit diff result |
||
381 | * |
||
382 | * Param and result same with DbDiff() |
||
383 | * |
||
384 | * @param array $ar_new |
||
385 | * @param Adodb $db |
||
386 | * @param array $ar_old |
||
387 | * @return array |
||
388 | * @see DbDiff() |
||
389 | */ |
||
390 | public function DbDiffExec (array $ar_new, Adodb $db = NULL, array $ar_old = NULL) { |
||
400 | |||
401 | |||
402 | /** |
||
403 | * Rollback committed DbDiff() result |
||
404 | * |
||
405 | * @param array $ar_diff Same with DbDiff()'s result |
||
406 | * @param Adodb $db |
||
407 | * @return int Rows modified, < 0 when error. |
||
408 | * @see DbDiff() |
||
409 | */ |
||
410 | View Code Duplication | public function DbDiffRollback (array &$ar_diff, Adodb $db = NULL) { |
|
497 | |||
498 | |||
499 | /** |
||
500 | * Compare a row's for DbDiff() |
||
501 | * |
||
502 | * Param and result structure, see DbDiff() |
||
503 | * |
||
504 | * $ar_new MUST contain all PK columns. |
||
505 | * |
||
506 | * @param array $ar_new |
||
507 | * @param array $ar_old |
||
508 | * @param array $ar_pk NULL to use first col in $ar_new |
||
509 | * @return array |
||
510 | * @see DbDiff() |
||
511 | */ |
||
512 | public function DbDiffRow (array $ar_new, array $ar_old = NULL |
||
607 | |||
608 | |||
609 | /** |
||
610 | * Define id relation between db and form - action name |
||
611 | * |
||
612 | * Key is id from db, value id id from form. |
||
613 | * So we can easily turn data between from/post and db. |
||
614 | * |
||
615 | * If one side is not directly assign from another side, |
||
616 | * do not define it here, |
||
617 | * they should be specially treated in other method |
||
618 | * after use this to treat all other easy ones. |
||
619 | * |
||
620 | * This is only an example func. |
||
621 | * @return array |
||
622 | */ |
||
623 | /* |
||
624 | protected function FormActionNameDef () { |
||
625 | $ar = array(); |
||
626 | $this->FormDefSameId($ar, 'field_same_id'); |
||
627 | $ar['id_db'] = 'id_form'; |
||
628 | |||
629 | return $ar; |
||
630 | } // end of func FormActionNameDef |
||
631 | */ |
||
632 | |||
633 | |||
634 | /** |
||
635 | * Define id relation between db and form, the same id ones |
||
636 | * |
||
637 | * For detail note, see example func FormActionNameDef(). |
||
638 | * @param array &$ar Config array |
||
639 | * @param string $id Field id |
||
640 | */ |
||
641 | protected function FormDefSameId (&$ar, $id) { |
||
644 | |||
645 | |||
646 | /** |
||
647 | * Get data from form, according setting in FormActionNameDef() |
||
648 | * |
||
649 | * Data source is $_POST. |
||
650 | * @param string $form Form name |
||
651 | * @return array |
||
652 | */ |
||
653 | View Code Duplication | public function FormGet ($form) { |
|
675 | |||
676 | |||
677 | /** |
||
678 | * Prepare data from db for form display |
||
679 | * |
||
680 | * According setting in FormActionNameDef() |
||
681 | * @param string $form Form name |
||
682 | * @return array Can use in Form::AddElementValue() |
||
683 | * @see Form::AddElementValue() |
||
684 | */ |
||
685 | View Code Duplication | public function FormSet ($form) { |
|
706 | |||
707 | |||
708 | /** |
||
709 | * New Cache instance |
||
710 | * Shoud be overwrited by sub class if use cache. |
||
711 | * |
||
712 | * @return object |
||
713 | */ |
||
714 | protected function NewObjCache () { |
||
717 | |||
718 | |||
719 | /** |
||
720 | * New db object |
||
721 | * |
||
722 | * @return object |
||
723 | */ |
||
724 | protected function NewObjDb () { |
||
727 | |||
728 | |||
729 | /** |
||
730 | * Set default config. |
||
731 | * |
||
732 | * @return object |
||
733 | */ |
||
734 | protected function SetCfgDefault () { |
||
749 | |||
750 | |||
751 | } // end of class Module |
||
752 | ?> |
||
753 |
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.