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 |
||
| 36 | class SqlGenerator |
||
| 37 | { |
||
| 38 | use UtilContainerAwareTrait; |
||
| 39 | |||
| 40 | |||
| 41 | /** |
||
| 42 | * Db connection |
||
| 43 | * |
||
| 44 | * @var object |
||
| 45 | */ |
||
| 46 | protected $db; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Param array index by SQL part |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $paramPart = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Generated SQL string part |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $sqlPart = []; |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Constructor |
||
| 65 | * |
||
| 66 | * @param Adodb &$db Db object |
||
| 67 | */ |
||
| 68 | public function __construct(&$db) |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * Clear all or some parts set param |
||
| 78 | * |
||
| 79 | * @param string $part |
||
| 80 | */ |
||
| 81 | public function clear($part = '') |
||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * Generate DELETE sql |
||
| 118 | * |
||
| 119 | * @param array $part |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public function genDelete($part = null) |
||
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Generate INSERT sql |
||
| 145 | * |
||
| 146 | * @param array $part |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | public function genInsert($part = []) |
||
| 168 | |||
| 169 | |||
| 170 | /** |
||
| 171 | * Generate SELECT sql |
||
| 172 | * |
||
| 173 | * @param array $part |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function genSelect($part = []) |
|
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Generate SQL part, convert param array to string with separator |
||
| 202 | * |
||
| 203 | * @param mixed $param |
||
| 204 | * @param string $separator Should have space included |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | View Code Duplication | protected function genSqlArray($param, $separator = ', ') |
|
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * Generate SQL part, which param is array and need use AS in it |
||
| 227 | * |
||
| 228 | * @param mixed $param Param content, array or string, |
||
| 229 | * {k: v} means 'k AS v', |
||
| 230 | * [v] means 'v AS v', |
||
| 231 | * remember to check $reverse param below. |
||
| 232 | * @param boolean $useAs Sybase table alias can't use AS |
||
| 233 | * @param boolean $quote AS column alias, need to be quoted(true), |
||
| 234 | * AS table alias, need not to be quoted(false). |
||
| 235 | * @param boolean $reverse Default true: {k: v} means 'v AS k'. |
||
| 236 | * Because array key must be unique, and a |
||
| 237 | * table can have many alias, so use unique |
||
| 238 | * alias as key in param array. Also this |
||
| 239 | * will make define code pretty and short, |
||
| 240 | * especially when mixed items with/without |
||
| 241 | * alias. Eg: {tbl1, a: tbl2} |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | View Code Duplication | protected function genSqlArrayAs( |
|
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * Generate SQL part, SET sub-parse of UPDATE |
||
| 286 | * |
||
| 287 | * @param array $param Array only, string will use original value. |
||
| 288 | * Array($k=>$v) means 'SET $k = $v, ' in sql, |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | View Code Duplication | protected function genSqlArraySet($param) |
|
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Generate SQL part, VALUES sub-parse of INSERT |
||
| 313 | * |
||
| 314 | * @param array $param Array only, string will use original value. |
||
| 315 | * Array($k=>$v) means '($k) VALUES ($v)' in sql. |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | View Code Duplication | protected function genSqlArrayValues($param) |
|
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * Smart quote value in sql, by check columns type |
||
| 342 | * |
||
| 343 | * @param string $tbl |
||
| 344 | * @param string $col |
||
| 345 | * @param mixed $val |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | protected function genSqlQuote($tbl, $col, $val) |
||
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * Generate UPDATE sql |
||
| 356 | * @param array $part |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | View Code Duplication | public function genUpdate($part = []) |
|
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * Get result SQL statement |
||
| 382 | * |
||
| 383 | * Notice: If $config include SELECT, UPDATE, INSERT, DELETE |
||
| 384 | * simultaneously, system will select the first occurs by raw order. |
||
| 385 | * |
||
| 386 | * @param array $config {SELECT: , FROM: ...} |
||
| 387 | * If omit, use $this->paramPart |
||
| 388 | * @param string $part SELECT/UPDATE ... etc |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | public function get($config = [], $part = '') |
||
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * Get DELETE sql only |
||
| 425 | * |
||
| 426 | * @param array $config |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getDelete($config = []) |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * Get INSERT sql only |
||
| 437 | * |
||
| 438 | * @param array $config |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function getInsert($config = []) |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * Get SQL statement for PREPARE usage |
||
| 449 | * |
||
| 450 | * Used need replace actual value with Adodb::param(col), to generate sql |
||
| 451 | * use placeholder (? or :name by db type), this method will auto remove |
||
| 452 | * quote. |
||
| 453 | * |
||
| 454 | * When execute a prepared SQL, db system will auto add quote, but it |
||
| 455 | * depends on type of value, NOT type of db column. |
||
| 456 | * |
||
| 457 | * Known replaceQuote list: |
||
| 458 | * mssql: double single-quote |
||
| 459 | * mysql: backslash-quote |
||
| 460 | * |
||
| 461 | * @param array $param |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | View Code Duplication | public function getPrepared($param = []) |
|
| 486 | |||
| 487 | |||
| 488 | /** |
||
| 489 | * Get SELECT sql only |
||
| 490 | * |
||
| 491 | * @param array $config |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | public function getSelect($config = []) |
||
| 498 | |||
| 499 | |||
| 500 | /** |
||
| 501 | * Get UPDATE sql only |
||
| 502 | * |
||
| 503 | * @param array $config |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | public function getUpdate($config = []) |
||
| 510 | |||
| 511 | |||
| 512 | /** |
||
| 513 | * Set param |
||
| 514 | * |
||
| 515 | * Un-recognized clause is ignored. |
||
| 516 | * |
||
| 517 | * @param array &$config |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | public function set(&$config) |
||
| 550 | |||
| 551 | |||
| 552 | /** |
||
| 553 | * Set and treat DELETE param |
||
| 554 | * |
||
| 555 | * @param mixed $param |
||
| 556 | * @return string |
||
| 557 | */ |
||
| 558 | View Code Duplication | public function setDelete($param) |
|
| 566 | |||
| 567 | |||
| 568 | /** |
||
| 569 | * Set and treat FROM param |
||
| 570 | * |
||
| 571 | * @param mixed $param |
||
| 572 | * @return string |
||
| 573 | */ |
||
| 574 | View Code Duplication | public function setFrom($param) |
|
| 585 | |||
| 586 | |||
| 587 | /** |
||
| 588 | * Set and treat GROUP BY param |
||
| 589 | * |
||
| 590 | * @param mixed $param |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | View Code Duplication | public function setGroupby($param) |
|
| 601 | |||
| 602 | |||
| 603 | /** |
||
| 604 | * Set and treat HAVING param |
||
| 605 | * |
||
| 606 | * @param mixed $param |
||
| 607 | * @return string |
||
| 608 | */ |
||
| 609 | View Code Duplication | public function setHaving($param) |
|
| 619 | |||
| 620 | |||
| 621 | /** |
||
| 622 | * Set and treat INSERT param |
||
| 623 | * |
||
| 624 | * @param mixed $param |
||
| 625 | * @return string |
||
| 626 | */ |
||
| 627 | View Code Duplication | public function setInsert($param) |
|
| 638 | |||
| 639 | |||
| 640 | /** |
||
| 641 | * Set and treat LIMIT param |
||
| 642 | * |
||
| 643 | * @param mixed $param |
||
| 644 | * @return string |
||
| 645 | */ |
||
| 646 | public function setLimit($param) |
||
| 662 | |||
| 663 | |||
| 664 | /** |
||
| 665 | * Set and treat ORDER BY param |
||
| 666 | * |
||
| 667 | * @param mixed $param |
||
| 668 | * @return string |
||
| 669 | */ |
||
| 670 | View Code Duplication | public function setOrderby($param) |
|
| 678 | |||
| 679 | |||
| 680 | /** |
||
| 681 | * Set and treat SELECT param |
||
| 682 | * |
||
| 683 | * @param mixed $param |
||
| 684 | * @return string |
||
| 685 | */ |
||
| 686 | View Code Duplication | public function setSelect($param) |
|
| 694 | |||
| 695 | |||
| 696 | /** |
||
| 697 | * Set and treat SET param |
||
| 698 | * |
||
| 699 | * @param mixed $param |
||
| 700 | * @return string |
||
| 701 | */ |
||
| 702 | View Code Duplication | public function setSet($param) |
|
| 712 | |||
| 713 | |||
| 714 | /** |
||
| 715 | * Set and treat UPDATE param |
||
| 716 | * |
||
| 717 | * @param mixed $param |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | View Code Duplication | public function setUpdate($param) |
|
| 731 | |||
| 732 | |||
| 733 | /** |
||
| 734 | * Set and treat VALUES param |
||
| 735 | * |
||
| 736 | * @param mixed $param |
||
| 737 | * @return string |
||
| 738 | */ |
||
| 739 | public function setValues($param) |
||
| 749 | |||
| 750 | |||
| 751 | /** |
||
| 752 | * Set and treat WHERE param |
||
| 753 | * |
||
| 754 | * @param mixed $param |
||
| 755 | * @return string |
||
| 756 | */ |
||
| 757 | View Code Duplication | public function setWhere($param) |
|
| 767 | } |
||
| 768 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.