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 SqlGenerator 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 SqlGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
163 | class SqlGenerator extends Fwolflib { |
||
|
|||
164 | |||
165 | /** |
||
166 | * From part user set, used in SELECT only |
||
167 | * @var mixed |
||
168 | */ |
||
169 | protected $mFrom = ''; |
||
170 | |||
171 | /** |
||
172 | * Group by part user set. |
||
173 | * @var mixed |
||
174 | */ |
||
175 | protected $mGroupby = ''; |
||
176 | |||
177 | /** |
||
178 | * Having part user set. |
||
179 | * @var mixed |
||
180 | */ |
||
181 | protected $mHaving = ''; |
||
182 | |||
183 | /** |
||
184 | * Limit part user set. |
||
185 | * @var mixed |
||
186 | */ |
||
187 | protected $mLimit = ''; |
||
188 | |||
189 | /** |
||
190 | * Order by part user set. |
||
191 | * @var mixed |
||
192 | */ |
||
193 | protected $mOrderby = ''; |
||
194 | |||
195 | /** |
||
196 | * Select (column list) part user set. |
||
197 | * @var mixed |
||
198 | */ |
||
199 | protected $mSelect = ''; |
||
200 | |||
201 | /** |
||
202 | * Set part user set, in UPDATE only. |
||
203 | * @var mixed |
||
204 | */ |
||
205 | protected $mSet = ''; |
||
206 | |||
207 | /** |
||
208 | * Values part user set. |
||
209 | * @var mixed |
||
210 | */ |
||
211 | protected $mValues = ''; |
||
212 | |||
213 | /** |
||
214 | * Where part user set. |
||
215 | * @var mixed |
||
216 | */ |
||
217 | protected $mWhere = ''; |
||
218 | |||
219 | /** |
||
220 | * Db object who call $this |
||
221 | * @var object |
||
222 | */ |
||
223 | protected $oDb; |
||
224 | |||
225 | /** |
||
226 | * Delete part(table name, 1 table only) user set. |
||
227 | * @var string |
||
228 | */ |
||
229 | protected $sDelete = ''; |
||
230 | |||
231 | /** |
||
232 | * Insert part(table name, 1 table only) user set. |
||
233 | * @var string |
||
234 | */ |
||
235 | protected $sInsert = ''; |
||
236 | |||
237 | /** |
||
238 | * Delete sql part generated |
||
239 | * @var string |
||
240 | */ |
||
241 | protected $sSqlDelect = ''; |
||
242 | |||
243 | /** |
||
244 | * From sql part generated |
||
245 | * @var string |
||
246 | */ |
||
247 | protected $sSqlFrom = ''; |
||
248 | |||
249 | /** |
||
250 | * Group by sql part generated |
||
251 | * @var string |
||
252 | */ |
||
253 | protected $sSqlGroupby = ''; |
||
254 | |||
255 | /** |
||
256 | * Having sql part generated |
||
257 | * @var string |
||
258 | */ |
||
259 | protected $sSqlHaving = ''; |
||
260 | |||
261 | /** |
||
262 | * Insert sql part generated |
||
263 | * @var string |
||
264 | */ |
||
265 | protected $sSqlInsert = ''; |
||
266 | |||
267 | /** |
||
268 | * Limit sql part generated |
||
269 | * @var string |
||
270 | */ |
||
271 | protected $sSqlLimit = ''; |
||
272 | |||
273 | /** |
||
274 | * Order by sql part generated |
||
275 | * @var string |
||
276 | */ |
||
277 | protected $sSqlOrderby = ''; |
||
278 | |||
279 | /** |
||
280 | * Select sql part generated |
||
281 | * @var string |
||
282 | */ |
||
283 | protected $sSqlSelect = ''; |
||
284 | |||
285 | /** |
||
286 | * Set sql part generated, for UPDATE only. |
||
287 | * @var string |
||
288 | */ |
||
289 | protected $sSqlSet = ''; |
||
290 | |||
291 | /** |
||
292 | * Update sql part generated |
||
293 | * @var string |
||
294 | */ |
||
295 | protected $sSqlUpdate = ''; |
||
296 | |||
297 | /** |
||
298 | * Values sql part generated, for INSERT only. |
||
299 | * @var string |
||
300 | */ |
||
301 | protected $sSqlValues = ''; |
||
302 | |||
303 | /** |
||
304 | * Where sql part generated |
||
305 | * @var string |
||
306 | */ |
||
307 | protected $sSqlWhere = ''; |
||
308 | |||
309 | /** |
||
310 | * Update part(table name, 1 table only) user set. |
||
311 | * @var string |
||
312 | */ |
||
313 | protected $sUpdate = ''; |
||
314 | |||
315 | |||
316 | /** |
||
317 | * Construct |
||
318 | * @param object &$db Db object |
||
319 | */ |
||
320 | public function __construct(&$db) |
||
325 | |||
326 | |||
327 | /** |
||
328 | * 重置已经设定的参数, or part of them |
||
329 | * |
||
330 | * @param string $part 重设哪一部分 |
||
331 | * @see gsql() |
||
332 | */ |
||
333 | public function Clear($part = '') |
||
372 | |||
373 | |||
374 | /** |
||
375 | * Generate an DELETE sql |
||
376 | * @param array $ar_config |
||
377 | * @return string |
||
378 | */ |
||
379 | protected function GenDelete($ar_config = array()) |
||
401 | |||
402 | |||
403 | /** |
||
404 | * Generate an INSERT sql |
||
405 | * @param array $ar_config |
||
406 | * @return string |
||
407 | */ |
||
408 | protected function GenInsert($ar_config = array()) |
||
430 | |||
431 | |||
432 | /** |
||
433 | * Generate an SELECT sql |
||
434 | * @param array $ar_config |
||
435 | * @return string |
||
436 | */ |
||
437 | View Code Duplication | protected function GenSelect($ar_config = array()) |
|
460 | |||
461 | |||
462 | /** |
||
463 | * Generate SQL part, which param is array and need to list out in plain format. |
||
464 | * |
||
465 | * @param mixed $param |
||
466 | * @param string $s_split String used between parts. |
||
467 | * @return string |
||
468 | */ |
||
469 | View Code Duplication | protected function GenSqlArray($param, $s_split = ', ') |
|
490 | |||
491 | |||
492 | /** |
||
493 | * Generate SQL part, which param is array and need use AS in it. |
||
494 | * @link http://dev.mysql.com/doc/refman/5.0/en/select.html |
||
495 | * @param mixed $param Items in SQL SELECT part, Array or string. |
||
496 | * Array($k=>$v) means '$k AS $v' in sql, |
||
497 | * but when $k is int, means '$v AS $v' in sql. |
||
498 | * @param boolean $use_as Sybase table alias can't use AS |
||
499 | * @param boolean $quote AS column alias, need to be quoted(true), |
||
500 | * AS table alias, need not to be quoted(false). |
||
501 | * @param boolean $tas True = reverse order, in table alias and select list, |
||
502 | * array($k=>$v) means 'FROM $v AS $k', |
||
503 | * set by $v => $k is because 1 table can have multi alias, |
||
504 | * and alias are unique, and this way is more goodlook when |
||
505 | * combile indexed and non-indexed item in list |
||
506 | * (non-indexed will use it's original name). |
||
507 | * @return string |
||
508 | */ |
||
509 | View Code Duplication | protected function GenSqlArrayAs($param, $use_as = true, $quote = false, $tas = true) |
|
539 | |||
540 | |||
541 | /** |
||
542 | * Generate SQL part, SET subparse of UPDATE |
||
543 | * @link http://dev.mysql.com/doc/refman/5.0/en/update.html |
||
544 | * @param array $param Items in SQL UPDATE part, |
||
545 | * Array only, string will return original value. |
||
546 | * Array($k=>$v) means 'SET $k = $v, ' in sql, |
||
547 | * @return string |
||
548 | */ |
||
549 | View Code Duplication | protected function GenSqlArraySet($param) |
|
570 | |||
571 | |||
572 | /** |
||
573 | * Generate SQL part, VALUES subparse of INSERT |
||
574 | * @link http://dev.mysql.com/doc/refman/5.0/en/insert.html |
||
575 | * @param array $param Items in SQL INSERT part, |
||
576 | * Array only, string will return original value. |
||
577 | * Array($k=>$v) means '($k) VALUES ($v)' in sql, |
||
578 | * @return string |
||
579 | */ |
||
580 | View Code Duplication | protected function GenSqlArrayValues($param) |
|
603 | |||
604 | |||
605 | /** |
||
606 | * Smarty quote string in sql, by check columns type |
||
607 | * @param string $table |
||
608 | * @param string $column |
||
609 | * @param mixed $val |
||
610 | * @return string |
||
611 | */ |
||
612 | protected function GenSqlQuote($table, $column, $val) |
||
642 | |||
643 | |||
644 | /** |
||
645 | * Generate an UPDATE sql |
||
646 | * @param array $ar_config |
||
647 | * @return string |
||
648 | */ |
||
649 | View Code Duplication | protected function GenUpdate($ar_config = array()) |
|
671 | |||
672 | |||
673 | /** |
||
674 | * Get DELETE sql only |
||
675 | * @param array $ar_config |
||
676 | * @return string |
||
677 | */ |
||
678 | public function GetDelete($ar_config = array()) |
||
682 | |||
683 | |||
684 | /** |
||
685 | * Get INSERT sql only |
||
686 | * @param array $ar_config |
||
687 | * @return string |
||
688 | */ |
||
689 | public function GetInsert($ar_config = array()) |
||
693 | |||
694 | |||
695 | /** |
||
696 | * Get SELECT sql only |
||
697 | * @param array $ar_config |
||
698 | * @return string |
||
699 | */ |
||
700 | public function GetSelect($ar_config = array()) |
||
704 | |||
705 | |||
706 | /** |
||
707 | * Get SQL statement |
||
708 | * |
||
709 | * If use SELECT, UPDATE, INSERT, DELETE simultaneously, |
||
710 | * System will select the first on occurs by this order. |
||
711 | * @param array $ar_config Array(SELECT=>..., FROM=>...) |
||
712 | * If obmit, use rememberd value. |
||
713 | * @param string $action SELECT/UPDATE ... etc |
||
714 | * @return string |
||
715 | */ |
||
716 | public function GetSql($ar_config = array(), $action = '') |
||
740 | |||
741 | |||
742 | /** |
||
743 | * Get SQL statement for Prepare usage |
||
744 | * |
||
745 | * value -> ? or :name, and quote chars removed |
||
746 | * |
||
747 | * Only simple treatment now. |
||
748 | * @param array $ar_config Same as GenSql() |
||
749 | * @return string |
||
750 | * @see GetSql() |
||
751 | */ |
||
752 | View Code Duplication | public function GetSqlPrepare($ar_config = array()) { |
|
777 | |||
778 | |||
779 | /** |
||
780 | * Get UPDATE sql only |
||
781 | * @param array $ar_config |
||
782 | * @return string |
||
783 | */ |
||
784 | public function GetUpdate($ar_config = array()) |
||
788 | |||
789 | |||
790 | /** |
||
791 | * Set value in array to property |
||
792 | * @param array &$ar_config |
||
793 | * @return string |
||
794 | */ |
||
795 | public function Set(&$ar_config) |
||
823 | |||
824 | |||
825 | /** |
||
826 | * Set Delete |
||
827 | * @param mixed $param |
||
828 | * @return string |
||
829 | */ |
||
830 | public function SetDelete($param) |
||
840 | |||
841 | |||
842 | /** |
||
843 | * Set From |
||
844 | * @param mixed $param |
||
845 | * @return string |
||
846 | */ |
||
847 | public function SetFrom($param) |
||
854 | |||
855 | |||
856 | /** |
||
857 | * Set Group by |
||
858 | * @param mixed $param |
||
859 | * @return string |
||
860 | */ |
||
861 | public function SetGroupby($param) |
||
867 | |||
868 | |||
869 | /** |
||
870 | * Set Having |
||
871 | * @param mixed $param |
||
872 | * @return string |
||
873 | */ |
||
874 | public function SetHaving($param) |
||
880 | |||
881 | |||
882 | /** |
||
883 | * Set Insert |
||
884 | * @param mixed $param |
||
885 | * @return string |
||
886 | */ |
||
887 | public function SetInsert($param) |
||
897 | |||
898 | |||
899 | /** |
||
900 | * Set Limit |
||
901 | * @param mixed $param |
||
902 | * @return string |
||
903 | */ |
||
904 | public function SetLimit($param) |
||
918 | |||
919 | |||
920 | /** |
||
921 | * Set Order by |
||
922 | * @param mixed $param |
||
923 | * @return string |
||
924 | */ |
||
925 | public function SetOrderby($param) |
||
931 | |||
932 | |||
933 | /** |
||
934 | * Set Select |
||
935 | * @param mixed $param |
||
936 | * @return string |
||
937 | */ |
||
938 | public function SetSelect($param) |
||
944 | |||
945 | |||
946 | /** |
||
947 | * Set Set |
||
948 | * @param mixed $param |
||
949 | * @return string |
||
950 | */ |
||
951 | public function SetSet($param) |
||
959 | |||
960 | |||
961 | /** |
||
962 | * Set Update |
||
963 | * @param mixed $param |
||
964 | * @return string |
||
965 | */ |
||
966 | public function SetUpdate($param) |
||
976 | |||
977 | |||
978 | /** |
||
979 | * Set Values |
||
980 | * @param mixed $param |
||
981 | * @return string |
||
982 | */ |
||
983 | public function SetValues($param) |
||
991 | |||
992 | |||
993 | /** |
||
994 | * Set Where |
||
995 | * @param mixed $param |
||
996 | * @return string |
||
997 | */ |
||
998 | public function SetWhere($param) |
||
1005 | |||
1006 | |||
1007 | } // end of class SqlGenerator |
||
1008 | ?> |
||
1009 |
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.