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 AbstractSqlTranslator 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 AbstractSqlTranslator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class AbstractSqlTranslator implements Translator |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Filter comparison operators. |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected $operators = array('>=', '<=', '>', '<', '=', '!=', '<>', 'in', 'not in', 'is', 'is not', 'like', 'not like'); |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Placeholder for values in prepared queries. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $placeholder = '?'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Concatenates the given set of strings that aren't empty. |
||
| 34 | * |
||
| 35 | * Runs implode() after filtering out empty elements. |
||
| 36 | * |
||
| 37 | * Delimiter defaults to a single whitespace character. |
||
| 38 | * |
||
| 39 | * @param array $strings |
||
| 40 | * @param string $delimiter [optional] |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | protected static function concatenate($strings, $delimiter = ' ') |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Determine whether the given limit and offset will make a difference to |
||
| 54 | * a statement. |
||
| 55 | * |
||
| 56 | * Simply determines whether both are non-zero integers. |
||
| 57 | * |
||
| 58 | * @param int $limit |
||
| 59 | * @param int $offset |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | protected static function limitIsUseful($limit, $offset) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Translate the given storage query into an SQL query. |
||
| 69 | * |
||
| 70 | * @param Storage\Query $storageQuery |
||
| 71 | * @return Database\Query |
||
| 72 | * @throws InvalidArgumentException |
||
| 73 | */ |
||
| 74 | public function translate(Storage\Query $storageQuery) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Translate a query that creates a record. |
||
| 91 | * |
||
| 92 | * @param Storage\Query $storageQuery |
||
| 93 | * @return Database\Query |
||
| 94 | */ |
||
| 95 | protected function translateCreate(Storage\Query $storageQuery) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Translate a query that reads records. |
||
| 112 | * |
||
| 113 | * @param Storage\Query $storageQuery |
||
| 114 | * @return Database\Query |
||
| 115 | */ |
||
| 116 | protected function translateRead(Storage\Query $storageQuery) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Translate a database storage query that reads records. |
||
| 140 | * |
||
| 141 | * @param Database\Storage\Query $storageQuery |
||
| 142 | */ |
||
| 143 | protected function translateDatabaseRead(Database\Storage\Query $storageQuery) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Translate a query that updates records. |
||
| 162 | * |
||
| 163 | * @param Storage\Query $storageQuery |
||
| 164 | * @return Database\Query |
||
| 165 | */ |
||
| 166 | protected function translateUpdate(Storage\Query $storageQuery) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Translate a query that deletes records. |
||
| 179 | * |
||
| 180 | * @param Storage\Query $storageQuery |
||
| 181 | * @return Database\Query |
||
| 182 | */ |
||
| 183 | protected function translateDelete(Storage\Query $storageQuery) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Resolve the given value as an identifier. |
||
| 196 | * |
||
| 197 | * @param mixed $identifier |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | abstract protected function resolveIdentifier($identifier); |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Prepare the given identifier. |
||
| 204 | * |
||
| 205 | * If the value is translatable, it is translated. |
||
| 206 | * |
||
| 207 | * If the value is an array, it is recursively prepared. |
||
| 208 | * |
||
| 209 | * @param mixed $identifier |
||
| 210 | * @return mixed |
||
| 211 | */ |
||
| 212 | protected function identifier($identifier) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Determine whether the given value is translatable. |
||
| 227 | * |
||
| 228 | * @param mixed $value |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | protected function translatable($value) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Translate the given translatable query. |
||
| 238 | * |
||
| 239 | * Helper for handling the translation of query objects from query builders. |
||
| 240 | * |
||
| 241 | * @param mixed $query |
||
| 242 | * @return Database\Query |
||
| 243 | */ |
||
| 244 | protected function translateTranslatable($query) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Translate the given value if it is a query or query builder. |
||
| 261 | * |
||
| 262 | * Returns the argument as is otherwise. |
||
| 263 | * |
||
| 264 | * @param mixed $value |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | protected function translateValue($value) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Prepare the given value for a prepared query. |
||
| 276 | * |
||
| 277 | * If the value translatable, it is translated. |
||
| 278 | * |
||
| 279 | * If the value is an array, it is recursively prepared. |
||
| 280 | * |
||
| 281 | * @param mixed $value |
||
| 282 | * @return array|string |
||
| 283 | */ |
||
| 284 | protected function value($value) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Resolve a placeholder or constant for the given parameter value. |
||
| 299 | * |
||
| 300 | * @param mixed $value |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | protected function resolveValue($value) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Determine whether the given value resolves a placeholder. |
||
| 318 | * |
||
| 319 | * @param mixed $value |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | protected function resolvesPlaceholder($value) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Prepare a set of column aliases. |
||
| 329 | * |
||
| 330 | * Uses the keys of the given array as identifiers and appends them to their |
||
| 331 | * values. |
||
| 332 | * |
||
| 333 | * @param array $columns |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | protected function prepareColumnAliases(array $columns) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Prepare the given columns as a string. |
||
| 350 | * |
||
| 351 | * @param array|string $columns |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | protected function prepareColumns($columns) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Determine whether the given operator is valid. |
||
| 369 | * |
||
| 370 | * @param string $operator |
||
| 371 | * @return bool |
||
| 372 | */ |
||
| 373 | protected function validOperator($operator) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Prepare the given conditional operator. |
||
| 382 | * |
||
| 383 | * Returns the equals operator if given value is not in the set of valid |
||
| 384 | * operators. |
||
| 385 | * |
||
| 386 | * @param string $operator |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | protected function prepareRawOperator($operator) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Prepare an appropriate conditional operator for the given value. |
||
| 398 | * |
||
| 399 | * @param string $operator |
||
| 400 | * @param mixed $value [optional] |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | protected function prepareOperator($operator, $value = null) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Prepare a join type. |
||
| 432 | * |
||
| 433 | * @param string $type |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | protected function prepareJoinType($type) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Prepare a join table. |
||
| 447 | * |
||
| 448 | * @param Join $join |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | protected function prepareJoinTable(Join $join) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Prepare a single join condition. |
||
| 461 | * |
||
| 462 | * TODO: Make this generic for WHERE or JOIN clauses. prepareCondition()? |
||
| 463 | * |
||
| 464 | * @param string $condition |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | protected function prepareJoinCondition($condition) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Prepare a join's conditions. |
||
| 486 | * |
||
| 487 | * @param Join $join |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | protected function prepareJoinConditions(Join $join) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Prepare an individual table join. |
||
| 505 | * |
||
| 506 | * @param Join $join |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | protected function prepareJoin(Join $join) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Prepare table joins. |
||
| 527 | * |
||
| 528 | * @param array $joins |
||
| 529 | * @return string |
||
| 530 | */ |
||
| 531 | protected function prepareJoins(array $joins) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Prepare an individual filter condition. |
||
| 544 | * |
||
| 545 | * @param string $column |
||
| 546 | * @param mixed $given |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | protected function prepareFilterCondition($column, $given) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Prepare a filter as a set of query conditions. |
||
| 578 | * |
||
| 579 | * TODO: Could numeric keys be dealt with by prepareJoinCondition()? |
||
| 580 | * |
||
| 581 | * @param array $filter |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | protected function prepareFilter(array $filter) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Prepare a WHERE clause using the given filter and comparison operator. |
||
| 601 | * |
||
| 602 | * Example filter key-values and their SQL equivalents: |
||
| 603 | * 'id' => 1, // id = '1' |
||
| 604 | * 'name like' => 'Chris', // name LIKE 'Chris' |
||
| 605 | * 'count >' => 10, // count > '10' |
||
| 606 | * 'type in' => [1, 2], // type IN (1, 2) |
||
| 607 | * 'type' => [3, 4] // type IN (3, 4) |
||
| 608 | * |
||
| 609 | * Comparison operator between conditions defaults to 'AND'. |
||
| 610 | * |
||
| 611 | * @param array $filter |
||
| 612 | * @param string $comparison [optional] |
||
| 613 | * @param bool $excludeWhere [optional] |
||
| 614 | * @return string |
||
| 615 | */ |
||
| 616 | protected function prepareWhere(array $filter, $comparison = 'AND', $excludeWhere = false) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Prepare an individual order condition. |
||
| 631 | * |
||
| 632 | * @param string $column |
||
| 633 | * @param string $direction [optional] |
||
| 634 | * @return string |
||
| 635 | */ |
||
| 636 | protected function prepareOrder($column, $direction = null) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Prepare an ORDER BY clause using the given order. |
||
| 646 | * |
||
| 647 | * Example order key-values: |
||
| 648 | * 'column', |
||
| 649 | * 'other_column' => 'ASC', |
||
| 650 | * 'another_column' => 'DESC |
||
| 651 | * |
||
| 652 | * Ordered ascending by default. |
||
| 653 | * |
||
| 654 | * @param array|string $order |
||
| 655 | * @return string |
||
| 656 | */ |
||
| 657 | protected function prepareOrderBy($order) |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Prepare a LIMIT clause using the given limit and offset. |
||
| 674 | * |
||
| 675 | * @param int $limit [optional] |
||
| 676 | * @param int $offset [optional] |
||
| 677 | * @return string |
||
| 678 | */ |
||
| 679 | abstract protected function prepareLimit($limit = 0, $offset = 0); |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Prepare a GROUP BY clause using the given groupings. |
||
| 683 | * |
||
| 684 | * @param string[] $groupings |
||
| 685 | * @return string |
||
| 686 | */ |
||
| 687 | protected function prepareGroupBy(array $groupings) |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Prepare a HAVING clause using the given filter. |
||
| 694 | * |
||
| 695 | * @param array $filter |
||
| 696 | * @return string |
||
| 697 | */ |
||
| 698 | protected function prepareHaving(array $filter) |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Prepare a SELECT statement using the given columns, table, clauses and |
||
| 711 | * options. |
||
| 712 | * |
||
| 713 | * TODO: Simplify this so that prepareSelect only actually prepares the |
||
| 714 | * SELECT and FROM clauses. The rest could be concatenated by |
||
| 715 | * translateRead(). |
||
| 716 | * |
||
| 717 | * @param string $table |
||
| 718 | * @param array|string $columns |
||
| 719 | * @param string $joins [optional] |
||
| 720 | * @param string $where [optional] |
||
| 721 | * @param string $order [optional] |
||
| 722 | * @param string $limit [optional] |
||
| 723 | * @param string $groupings [optional] |
||
| 724 | * @param string $having [optional] |
||
| 725 | * @param bool $distinct [optional] |
||
| 726 | * @return string |
||
| 727 | */ |
||
| 728 | abstract protected function prepareSelect( |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Prepare an INSERT INTO statement using the given table and data. |
||
| 742 | * |
||
| 743 | * @param string $table |
||
| 744 | * @param array $data |
||
| 745 | * @return string |
||
| 746 | */ |
||
| 747 | protected function prepareInsert($table, array $data) |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Prepare an INSERT SELECT statement using the given table and |
||
| 762 | * subquery. |
||
| 763 | * |
||
| 764 | * @param string $table |
||
| 765 | * @param array $columns |
||
| 766 | * @param Storage\Query $subquery |
||
| 767 | * @return string |
||
| 768 | */ |
||
| 769 | public function prepareInsertSelect($table, array $columns, Storage\Query $subquery) |
||
| 782 | |||
| 783 | /** |
||
| 784 | * Prepare an UPDATE statement with the given table, data and clauses. |
||
| 785 | * |
||
| 786 | * @param string $table |
||
| 787 | * @param array $data |
||
| 788 | * @param string $where [optional] |
||
| 789 | * @param string $limit [optional] |
||
| 790 | * @return string |
||
| 791 | */ |
||
| 792 | abstract protected function prepareUpdate($table, $data, $where = null, $limit = null); |
||
| 793 | |||
| 794 | /** |
||
| 795 | * Prepare a DELETE statement with the given table and clauses. |
||
| 796 | * |
||
| 797 | * @param string $table |
||
| 798 | * @param string $where [optional] |
||
| 799 | * @param string $limit [optional] |
||
| 800 | * @return string |
||
| 801 | */ |
||
| 802 | abstract protected function prepareDelete($table, $where = null, $limit = null); |
||
| 803 | |||
| 804 | /** |
||
| 805 | * Prepare a set of query parameters from the given set of columns. |
||
| 806 | * |
||
| 807 | * @param array $columns |
||
| 808 | * @return array |
||
| 809 | */ |
||
| 810 | protected function columnParameters($columns) |
||
| 826 | |||
| 827 | /** |
||
| 828 | * Prepare a set of query parameters from the given data. |
||
| 829 | * |
||
| 830 | * @param array $data |
||
| 831 | * @return array |
||
| 832 | */ |
||
| 833 | protected function dataParameters($data) |
||
| 845 | |||
| 846 | /** |
||
| 847 | * Prepare a set of query parameters from the given set of joins. |
||
| 848 | * |
||
| 849 | * @param Join[] $joins |
||
| 850 | * @return array |
||
| 851 | */ |
||
| 852 | protected function joinParameters($joins) |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Prepare a set of query parameters from the given filter. |
||
| 865 | * |
||
| 866 | * @param array $filter |
||
| 867 | * @return array |
||
| 868 | */ |
||
| 869 | protected function filterParameters($filter) |
||
| 905 | |||
| 906 | /** |
||
| 907 | * Retrieve an array of parameters from the given query for executing a |
||
| 908 | * prepared query. |
||
| 909 | * |
||
| 910 | * @param Storage\Query $storageQuery |
||
| 911 | * @return array |
||
| 912 | */ |
||
| 913 | public function parameters(Storage\Query $storageQuery) |
||
| 938 | } |
||
| 939 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.