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 ListTable 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 ListTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class ListTable extends Fwolflib { |
||
|
|||
50 | /** |
||
51 | * Configuration |
||
52 | * |
||
53 | * <code> |
||
54 | * color_bg_[th/tr_even/tr_odd]: |
||
55 | * Colors of rows. |
||
56 | * color_bg_tr_hover: |
||
57 | * Change to this color when mouseover of row.(Not implement) |
||
58 | * fit_data_title: 0=data fit title, cut data items who's index not |
||
59 | * in title |
||
60 | * 1=title fit data. |
||
61 | * 2=fit to fewest, only items both have allowed. |
||
62 | * 3=fit to mostest, all items in title or data allowed. |
||
63 | * fit_empty: If an value in data is empty, set to this value. |
||
64 | * Title will always set to field name in same situation. |
||
65 | * code_prefix: Prefix auto add before $sId, use to generate html. |
||
66 | * page_cur: Current page no, ONLY USED TO DISP PAGER. |
||
67 | * page_param: Param name of page number in URL, |
||
68 | * Will change if $sId changed. |
||
69 | * page_size: Rows per page, ONLY USED TO DISP PAGER, havn't any |
||
70 | * effect to list data. |
||
71 | * rows_total: Total rows, ONLY USED TO DISP PAGER. |
||
72 | * tpl: Smarty template file to use. |
||
73 | * </code> |
||
74 | * @var array |
||
75 | */ |
||
76 | public $aCfg = array( |
||
77 | // 浅蓝色配色方案 |
||
78 | // fwolflib-list-table = fl_lt |
||
79 | 'code_prefix' => 'fl_lt', // Used in id/class in html and css. |
||
80 | 'color_bg_th' => '#d0dcff', // 表头(第0行) |
||
81 | 'color_bg_tr_even' => '#fff', // 偶数行 |
||
82 | 'color_bg_tr_hover' => '#e3e3de', // 鼠标指向时变色 |
||
83 | 'color_bg_tr_odd' => '#eef2ff', // 奇数行,tbody后从0开始算 |
||
84 | 'fit_data_title' => 0, |
||
85 | 'fit_empty' => ' ', |
||
86 | 'orderby' => 0, // 0=off, 1=on |
||
87 | 'orderby_dir' => 'asc', |
||
88 | 'orderby_idx' => '', // Idx of th ar |
||
89 | 'orderby_param' => 'o', |
||
90 | 'orderby_text' => '', |
||
91 | // ← = ← ↑ = ↑ → = → ↓ = ↓ |
||
92 | // ∆ = ∆ ∇ = ∇ |
||
93 | 'orderby_text_asc' => '↑', |
||
94 | 'orderby_text_desc' => '↓', |
||
95 | 'page_cur' => 1, |
||
96 | 'page_param' => 'p', |
||
97 | 'page_size' => 10, |
||
98 | 'pager' => false, // Is or not use pager |
||
99 | 'pager_bottom' => true, // Is or not use pager bottom, used when pager=true |
||
100 | // This is a message template |
||
101 | // When display, use key append by '_value' |
||
102 | 'pager_text_cur' => '共{rows_total}条记录,每页显示{page_size}条,当前为第{page_cur}/{page_max}页', |
||
103 | |||
104 | 'pager_text_first' => '首页', |
||
105 | 'pager_text_goto1' => '转到第', |
||
106 | 'pager_text_goto2' => '页', |
||
107 | 'pager_text_goto3' => '转', |
||
108 | 'pager_text_last' => '尾页', |
||
109 | 'pager_text_next' => '下一页', |
||
110 | 'pager_text_prev' => '上一页', |
||
111 | 'pager_text_spacer' => ' | ', // To be between below texts. |
||
112 | 'pager_top' => true, // Is or not use pager top, used when pager=true |
||
113 | // 'param' => 'p', // Used in url to set page no. |
||
114 | 'rows_total' => 0, |
||
115 | 'tpl' => 'list-table.tpl', |
||
116 | |||
117 | // Add custom string in td/th/tr tag, eg: nowrap="nowrap" |
||
118 | // td/th can use index same with data array index, |
||
119 | // tr can use int index which's value is string too. |
||
120 | // For tr of th row, use th instead. |
||
121 | 'td_add' => array(), |
||
122 | 'th_add' => array(), |
||
123 | 'tr_add' => array(), |
||
124 | ); |
||
125 | |||
126 | /** |
||
127 | * 数组变量,指向要显示数据存放的数组,其格式见类说明 |
||
128 | * @var array |
||
129 | */ |
||
130 | protected $aData = array(); |
||
131 | |||
132 | /** |
||
133 | * Page url param array. |
||
134 | * @var array |
||
135 | */ |
||
136 | protected $aParam = array(); |
||
137 | |||
138 | /** |
||
139 | * Title of data, used as table title. |
||
140 | * @var array |
||
141 | */ |
||
142 | protected $aTitle = array(); |
||
143 | |||
144 | /** |
||
145 | * Array of url, for links to display in tpl |
||
146 | * <code> |
||
147 | * array( |
||
148 | * base => Original page url |
||
149 | * o_cur => Cur orderby link(modified) |
||
150 | * o_other => Other orderby link(modified) |
||
151 | * p_first => First page link |
||
152 | * p_last => Last page link |
||
153 | * p_next => Next page link |
||
154 | * p_prev => Prev page link |
||
155 | * ) |
||
156 | * </code> |
||
157 | * @var array |
||
158 | */ |
||
159 | protected $aUrl = array( |
||
160 | 'base' => '', |
||
161 | 'o_cur' => '', |
||
162 | 'o_other' => '', |
||
163 | 'p_first' => '', |
||
164 | 'p_last' => '', |
||
165 | 'p_next' => '', |
||
166 | 'p_prev' => '', |
||
167 | ); |
||
168 | |||
169 | /** |
||
170 | * 模板变量,指向在构造函数中传入的全局模板变量 |
||
171 | * @var object |
||
172 | */ |
||
173 | protected $oTpl = null; |
||
174 | |||
175 | /** |
||
176 | * Class of this list in html, used with {@see $sId} |
||
177 | * |
||
178 | * Diff between $sClass and $sId: |
||
179 | * $sClass has no prefix, while $sId has. |
||
180 | * $sClass can be applyed css in project css file, |
||
181 | * while $sId can be applyed css inline in tpl file. |
||
182 | * @var string |
||
183 | */ |
||
184 | protected $sClass = 'fl_lt'; |
||
185 | |||
186 | /** |
||
187 | * Identify of this list, |
||
188 | * Also used in html, as div id property. |
||
189 | * @var string |
||
190 | */ |
||
191 | protected $sId = 'fl_lt'; |
||
192 | |||
193 | |||
194 | /** |
||
195 | * Construct |
||
196 | * |
||
197 | * $ard, $art can't use referenct because title and data value maybe |
||
198 | * changed in treatment. |
||
199 | * @param object &$tpl Smarty object, will save as {@link $oTpl}. |
||
200 | * @param array $ard Data array, will save as {@link $aData}. |
||
201 | * @param array $art Title of list. |
||
202 | * @param string $id Identify of this list, while multi list |
||
203 | * in page, this is needed. |
||
204 | * Note: will be applyed prefix automatic |
||
205 | * when write to $sId. |
||
206 | * @param array &$conf Configuration. |
||
207 | */ |
||
208 | public function __construct (&$tpl, $ard = array(), $art = array(), |
||
219 | |||
220 | |||
221 | /** |
||
222 | * Fit data and title when their items count diff |
||
223 | * |
||
224 | * <code> |
||
225 | * fit_data_title: 0=data fit title, cut data items who's index not |
||
226 | * in title |
||
227 | * 1=title fit data. |
||
228 | * 2=fit to fewest, only items both have allowed. |
||
229 | * 3=fit to mostest, all items in title or data allowed. |
||
230 | * </code> |
||
231 | * Notice: data have multi row(2 dim), title have only 1 row(1 dim). |
||
232 | * @see $aCfg['fit_data_title'] |
||
233 | */ |
||
234 | protected function FitDataTitle () { |
||
363 | |||
364 | |||
365 | /** |
||
366 | * Get full output html |
||
367 | * @return string |
||
368 | */ |
||
369 | public function GetHtml () { |
||
372 | |||
373 | |||
374 | /** |
||
375 | * Get http GET param. |
||
376 | * @return array |
||
377 | */ |
||
378 | public function GetParam() { |
||
405 | |||
406 | |||
407 | /** |
||
408 | * Get info about some part of query sql |
||
409 | * what can directly use in SqlGenerator, eg: limit, orderby |
||
410 | * |
||
411 | * @return array |
||
412 | * @see SqlGenerator |
||
413 | */ |
||
414 | public function GetSqlInfo () { |
||
428 | |||
429 | |||
430 | /** |
||
431 | * Get info about some part of query sql from url $_REQUEST |
||
432 | * what can directly use in SqlGenerator, eg: limit, orderby |
||
433 | * |
||
434 | * @return array |
||
435 | * @see SqlGenerator |
||
436 | */ |
||
437 | public function GetSqlInfoFromUrl () { |
||
456 | |||
457 | |||
458 | /** |
||
459 | * Parse & compute page_cur param |
||
460 | * |
||
461 | * @param int $p Page num param come from outer |
||
462 | * @return int |
||
463 | */ |
||
464 | protected function ParsePageCur ($p = 0) { |
||
498 | |||
499 | |||
500 | /** |
||
501 | * Set table data and title to display. |
||
502 | * @param array &$ard Data, will save as {@link $aData}. |
||
503 | * @param array &$art Title of list. |
||
504 | */ |
||
505 | public function SetData (&$ard = array(), &$art = array()) { |
||
534 | |||
535 | |||
536 | /** |
||
537 | * Set identify and class of this list <div> in html |
||
538 | * @param string $id |
||
539 | * @param string $class |
||
540 | * @return string |
||
541 | */ |
||
542 | public function SetId ($id, $class = '') { |
||
567 | |||
568 | |||
569 | /** |
||
570 | * Set orderby info |
||
571 | * |
||
572 | * Didn't validate $idx to th array. |
||
573 | * @param mixed $idx Idx of th array |
||
574 | * @param string $dir asc/desc, lower letter only |
||
575 | */ |
||
576 | public function SetOrderby ($idx, $dir = 'asc') { |
||
610 | |||
611 | |||
612 | /** |
||
613 | * Set pager info |
||
614 | * |
||
615 | * Config data will also write to $aCfg, the difference with direct set config |
||
616 | * is this will add more treatment about pager. |
||
617 | * And use after SetCfg() |
||
618 | * @param int $rows_total Total row/record number |
||
619 | * @param int $page_cur Current displayed page, default is get from GET param |
||
620 | * if fail, set to 1. |
||
621 | * @see $aCfg |
||
622 | */ |
||
623 | public function SetPager ($rows_total= 0, $page_cur = 0) { |
||
702 | |||
703 | |||
704 | /** |
||
705 | * Set url param, get the url |
||
706 | * |
||
707 | * If $k is string, then $v is string too and means $k=$v. |
||
708 | * if $k is array, then $v is array to, |
||
709 | * and values in $k/$v is added/removed from url param. |
||
710 | * Always 'save' setting and return result url. |
||
711 | * |
||
712 | * @param mixed $k |
||
713 | * @param mixed $v |
||
714 | * @return string |
||
715 | * @see func/request.php::GetParam() |
||
716 | */ |
||
717 | public function SetParam ($k, $v = '') { |
||
741 | |||
742 | |||
743 | } // end class ListTable |
||
744 | ?> |
||
745 |
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.