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 = ' ') { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Determine whether the given limit and offset will make a difference to |
||
| 53 | * a statement. |
||
| 54 | * |
||
| 55 | * Simply determines whether both are non-zero integers. |
||
| 56 | * |
||
| 57 | * @param int $limit |
||
| 58 | * @param int $offset |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | protected static function limitIsUseful($limit, $offset) { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Translate the given storage query into an SQL query. |
||
| 67 | * |
||
| 68 | * @param Storage\Query $storageQuery |
||
| 69 | * @return Database\Query |
||
| 70 | * @throws Exception |
||
| 71 | */ |
||
| 72 | public function translate(Storage\Query $storageQuery) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Translate a query that creates a record. |
||
| 88 | * |
||
| 89 | * @param Storage\Query $storageQuery |
||
| 90 | * @return Database\Query |
||
| 91 | */ |
||
| 92 | protected function translateCreate(Storage\Query $storageQuery) { |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Translate a query that reads records. |
||
| 108 | * |
||
| 109 | * @param Storage\Query $storageQuery |
||
| 110 | * @return Database\Query |
||
| 111 | */ |
||
| 112 | protected function translateRead(Storage\Query $storageQuery) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Translate a database storage query that reads records. |
||
| 132 | * |
||
| 133 | * @param Database\Storage\Query $storageQuery |
||
| 134 | */ |
||
| 135 | protected function translateDatabaseRead(Database\Storage\Query $storageQuery) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Translate a query that updates records. |
||
| 151 | * |
||
| 152 | * @param Storage\Query $storageQuery |
||
| 153 | * @return Database\Query |
||
| 154 | */ |
||
| 155 | protected function translateUpdate(Storage\Query $storageQuery) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Translate a query that deletes records. |
||
| 167 | * |
||
| 168 | * @param Storage\Query $storageQuery |
||
| 169 | * @return Database\Query |
||
| 170 | */ |
||
| 171 | protected function translateDelete(Storage\Query $storageQuery) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Resolve the given value as an identifier. |
||
| 183 | * |
||
| 184 | * @param mixed $identifier |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | abstract protected function resolveIdentifier($identifier); |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Prepare the given identifier. |
||
| 191 | * |
||
| 192 | * If the value is translatable, it is translated. |
||
| 193 | * |
||
| 194 | * If the value is an array, it is recursively prepared. |
||
| 195 | * |
||
| 196 | * @param mixed $identifier |
||
| 197 | * @return mixed |
||
| 198 | */ |
||
| 199 | View Code Duplication | protected function identifier($identifier) { |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Determine whether the given value is translatable. |
||
| 213 | * |
||
| 214 | * @param mixed $value |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | protected function translatable($value) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Translate the given value if it is a query or query builder. |
||
| 223 | * |
||
| 224 | * Returns the argument as is otherwise. |
||
| 225 | * |
||
| 226 | * @param mixed $value |
||
| 227 | * @return mixed |
||
| 228 | */ |
||
| 229 | View Code Duplication | protected function translateValue($value) { |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Prepare the given value for a prepared query. |
||
| 245 | * |
||
| 246 | * If the value translatable, it is translated. |
||
| 247 | * |
||
| 248 | * If the value is an array, it is recursively prepared. |
||
| 249 | * |
||
| 250 | * @param array|string $value |
||
| 251 | * @return array|string |
||
| 252 | */ |
||
| 253 | View Code Duplication | protected function value($value) { |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Resolve a placeholder or constant for the given parameter value. |
||
| 267 | * |
||
| 268 | * @param mixed $value |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | protected function resolveValue($value) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Determine whether the given value resolves a placeholder. |
||
| 285 | * |
||
| 286 | * @param mixed $value |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | protected function resolvesPlaceholder($value) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Prepare a set of column aliases. |
||
| 295 | * |
||
| 296 | * Uses the keys of the given array as identifiers and appends them to their |
||
| 297 | * values. |
||
| 298 | * |
||
| 299 | * @param array $columns |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | protected function prepareColumnAliases(array $columns) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Prepare the given columns as a string. |
||
| 315 | * |
||
| 316 | * @param array|string $columns |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | protected function prepareColumns($columns) { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Determine whether the given operator is valid. |
||
| 333 | * |
||
| 334 | * @param string $operator |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | protected function validOperator($operator) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Prepare the given conditional operator. |
||
| 345 | * |
||
| 346 | * Returns the equals operator if given value is not in the set of valid |
||
| 347 | * operators. |
||
| 348 | * |
||
| 349 | * @param string $operator |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | protected function prepareRawOperator($operator) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Prepare an appropriate conditional operator for the given value. |
||
| 360 | * |
||
| 361 | * @param string $operator |
||
| 362 | * @param mixed $value [optional] |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | protected function prepareOperator($operator, $value = null) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Prepare a join type. |
||
| 393 | * |
||
| 394 | * @param string $type |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | protected function prepareJoinType($type) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Prepare a join table. |
||
| 407 | * |
||
| 408 | * @param Join $join |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | protected function prepareJoinTable(Join $join) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Prepare a single join condition. |
||
| 420 | * |
||
| 421 | * TODO: Make this generic for WHERE or JOIN clauses. prepareCondition()? |
||
| 422 | * |
||
| 423 | * @param string $condition |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | protected function prepareJoinCondition($condition) { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Prepare a join's conditions. |
||
| 444 | * |
||
| 445 | * @param Join $join |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | protected function prepareJoinConditions(Join $join) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Prepare an individual table join. |
||
| 464 | * |
||
| 465 | * @param Join $join |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | protected function prepareJoin(Join $join) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Prepare table joins. |
||
| 485 | * |
||
| 486 | * @param array $joins |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | protected function prepareJoins(array $joins) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Prepare an individual filter condition. |
||
| 501 | * |
||
| 502 | * @param string $column |
||
| 503 | * @param mixed $given |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | protected function prepareFilterCondition($column, $given) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Prepare a filter as a set of query conditions. |
||
| 534 | * |
||
| 535 | * TODO: Could numeric keys be dealt with by prepareJoinCondition()? |
||
| 536 | * |
||
| 537 | * @param array $filter |
||
| 538 | * @return array |
||
| 539 | */ |
||
| 540 | protected function prepareFilter(array $filter) { |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Prepare a WHERE clause using the given filter and comparison operator. |
||
| 556 | * |
||
| 557 | * Example filter key-values and their SQL equivalents: |
||
| 558 | * 'id' => 1, // id = '1' |
||
| 559 | * 'name like' => 'Chris', // name LIKE 'Chris' |
||
| 560 | * 'count >' => 10, // count > '10' |
||
| 561 | * 'type in' => [1, 2], // type IN (1, 2) |
||
| 562 | * 'type' => [3, 4] // type IN (3, 4) |
||
| 563 | * |
||
| 564 | * Comparison operator between conditions defaults to 'AND'. |
||
| 565 | * |
||
| 566 | * @param array $filter |
||
| 567 | * @param string $comparison [optional] |
||
| 568 | * @param bool $excludeWhere [optional] |
||
| 569 | * @return string |
||
| 570 | */ |
||
| 571 | protected function prepareWhere(array $filter, $comparison = 'AND', $excludeWhere = false) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Prepare an individual order condition. |
||
| 585 | * |
||
| 586 | * @param string $column |
||
| 587 | * @param string $direction [optional] |
||
| 588 | * @return string |
||
| 589 | */ |
||
| 590 | protected function prepareOrder($column, $direction = null) { |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Prepare an ORDER BY clause using the given order. |
||
| 599 | * |
||
| 600 | * Example order key-values: |
||
| 601 | * 'column', |
||
| 602 | * 'other_column' => 'ASC', |
||
| 603 | * 'another_column' => 'DESC |
||
| 604 | * |
||
| 605 | * Ordered ascending by default. |
||
| 606 | * |
||
| 607 | * @param array|string $order |
||
| 608 | * @return string |
||
| 609 | */ |
||
| 610 | protected function prepareOrderBy($order) { |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Prepare a LIMIT clause using the given limit and offset. |
||
| 626 | * |
||
| 627 | * @param int $limit [optional] |
||
| 628 | * @param int $offset [optional] |
||
| 629 | * @return string |
||
| 630 | */ |
||
| 631 | abstract protected function prepareLimit($limit = 0, $offset = 0); |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Prepare a SELECT statement using the given columns, table, clauses and |
||
| 635 | * options. |
||
| 636 | * |
||
| 637 | * TODO: Simplify this so that prepareSelect only actually prepares the |
||
| 638 | * SELECT and FROM clauses. The rest could be concatenated by |
||
| 639 | * translateRead(). |
||
| 640 | * |
||
| 641 | * @param string $table |
||
| 642 | * @param array|string $columns |
||
| 643 | * @param string $joins [optional] |
||
| 644 | * @param string $where [optional] |
||
| 645 | * @param string $order [optional] |
||
| 646 | * @param string $limit [optional] |
||
| 647 | * @param bool $distinct [optional] |
||
| 648 | * @return string |
||
| 649 | */ |
||
| 650 | abstract protected function prepareSelect($table, $columns, $joins = null, $where = null, $order = null, $limit = null, $distinct = false); |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Prepare an INSERT INTO statement using the given table and data. |
||
| 654 | * |
||
| 655 | * @param string $table |
||
| 656 | * @param array $data |
||
| 657 | * @return string |
||
| 658 | */ |
||
| 659 | protected function prepareInsert($table, array $data) { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Prepare an INSERT SELECT statement using the given table and |
||
| 673 | * subquery. |
||
| 674 | * |
||
| 675 | * @param string $table |
||
| 676 | * @param array $columns |
||
| 677 | * @param Storage\Query $subquery |
||
| 678 | * @return string |
||
| 679 | */ |
||
| 680 | public function prepareInsertSelect($table, array $columns, Storage\Query $subquery) { |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Prepare an UPDATE statement with the given table, data and clauses. |
||
| 693 | * |
||
| 694 | * @param string $table |
||
| 695 | * @param array $data |
||
| 696 | * @param string $where [optional] |
||
| 697 | * @param string $limit [optional] |
||
| 698 | * @return string |
||
| 699 | */ |
||
| 700 | abstract protected function prepareUpdate($table, $data, $where = null, $limit = null); |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Prepare a DELETE statement with the given table and clauses. |
||
| 704 | * |
||
| 705 | * @param string $table |
||
| 706 | * @param string $where [optional] |
||
| 707 | * @param string $limit [optional] |
||
| 708 | * @return string |
||
| 709 | */ |
||
| 710 | abstract protected function prepareDelete($table, $where = null, $limit = null); |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Prepare a set of query parameters from the given set of columns. |
||
| 714 | * |
||
| 715 | * @param array $columns |
||
| 716 | * @return array |
||
| 717 | */ |
||
| 718 | View Code Duplication | protected function columnParameters($columns) { |
|
| 733 | |||
| 734 | /** |
||
| 735 | * Prepare a set of query parameters from the given data. |
||
| 736 | * |
||
| 737 | * @param array $data |
||
| 738 | * @return array |
||
| 739 | */ |
||
| 740 | protected function dataParameters($data) { |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Prepare a set of query parameters from the given set of joins. |
||
| 754 | * |
||
| 755 | * @param Storage\Query\Join[] $joins |
||
| 756 | * @return array |
||
| 757 | */ |
||
| 758 | protected function joinParameters($joins) { |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Prepare a set of query parameters from the given filter. |
||
| 770 | * |
||
| 771 | * @param array $filter |
||
| 772 | * @return array |
||
| 773 | */ |
||
| 774 | protected function filterParameters($filter) { |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Retrieve an array of parameters from the given query for executing a |
||
| 812 | * prepared query. |
||
| 813 | * |
||
| 814 | * @param Storage\Query $storageQuery |
||
| 815 | * @return array |
||
| 816 | */ |
||
| 817 | public function parameters(Storage\Query $storageQuery) { |
||
| 837 | |||
| 838 | } |
||
| 839 |
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.