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 InvalidArgumentException |
||
| 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 translatable query. |
||
| 223 | * |
||
| 224 | * Helper for handling the translation of query objects from query builders. |
||
| 225 | * |
||
| 226 | * @param mixed $value |
||
| 227 | * @return Database\Query |
||
| 228 | */ |
||
| 229 | protected function translateTranslatable($query) { |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Translate the given value if it is a query or query builder. |
||
| 245 | * |
||
| 246 | * Returns the argument as is otherwise. |
||
| 247 | * |
||
| 248 | * @param mixed $value |
||
| 249 | * @return mixed |
||
| 250 | */ |
||
| 251 | protected function translateValue($value) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Prepare the given value for a prepared query. |
||
| 259 | * |
||
| 260 | * If the value translatable, it is translated. |
||
| 261 | * |
||
| 262 | * If the value is an array, it is recursively prepared. |
||
| 263 | * |
||
| 264 | * @param mixed $value |
||
| 265 | * @return array|string |
||
| 266 | */ |
||
| 267 | View Code Duplication | protected function value($value) { |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Resolve a placeholder or constant for the given parameter value. |
||
| 281 | * |
||
| 282 | * @param mixed $value |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected function resolveValue($value) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Determine whether the given value resolves a placeholder. |
||
| 299 | * |
||
| 300 | * @param mixed $value |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | protected function resolvesPlaceholder($value) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Prepare a set of column aliases. |
||
| 309 | * |
||
| 310 | * Uses the keys of the given array as identifiers and appends them to their |
||
| 311 | * values. |
||
| 312 | * |
||
| 313 | * @param array $columns |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | protected function prepareColumnAliases(array $columns) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Prepare the given columns as a string. |
||
| 329 | * |
||
| 330 | * @param array|string $columns |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | protected function prepareColumns($columns) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Determine whether the given operator is valid. |
||
| 347 | * |
||
| 348 | * @param string $operator |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | protected function validOperator($operator) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Prepare the given conditional operator. |
||
| 359 | * |
||
| 360 | * Returns the equals operator if given value is not in the set of valid |
||
| 361 | * operators. |
||
| 362 | * |
||
| 363 | * @param string $operator |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | protected function prepareRawOperator($operator) { |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Prepare an appropriate conditional operator for the given value. |
||
| 374 | * |
||
| 375 | * @param string $operator |
||
| 376 | * @param mixed $value [optional] |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | protected function prepareOperator($operator, $value = null) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Prepare a join type. |
||
| 407 | * |
||
| 408 | * @param string $type |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | protected function prepareJoinType($type) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Prepare a join table. |
||
| 421 | * |
||
| 422 | * @param Join $join |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | protected function prepareJoinTable(Join $join) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Prepare a single join condition. |
||
| 434 | * |
||
| 435 | * TODO: Make this generic for WHERE or JOIN clauses. prepareCondition()? |
||
| 436 | * |
||
| 437 | * @param string $condition |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | protected function prepareJoinCondition($condition) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Prepare a join's conditions. |
||
| 458 | * |
||
| 459 | * @param Join $join |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | protected function prepareJoinConditions(Join $join) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Prepare an individual table join. |
||
| 476 | * |
||
| 477 | * @param Join $join |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | protected function prepareJoin(Join $join) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Prepare table joins. |
||
| 497 | * |
||
| 498 | * @param array $joins |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | protected function prepareJoins(array $joins) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Prepare an individual filter condition. |
||
| 513 | * |
||
| 514 | * @param string $column |
||
| 515 | * @param mixed $given |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | protected function prepareFilterCondition($column, $given) { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Prepare a filter as a set of query conditions. |
||
| 546 | * |
||
| 547 | * TODO: Could numeric keys be dealt with by prepareJoinCondition()? |
||
| 548 | * |
||
| 549 | * @param array $filter |
||
| 550 | * @return array |
||
| 551 | */ |
||
| 552 | protected function prepareFilter(array $filter) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Prepare a WHERE clause using the given filter and comparison operator. |
||
| 568 | * |
||
| 569 | * Example filter key-values and their SQL equivalents: |
||
| 570 | * 'id' => 1, // id = '1' |
||
| 571 | * 'name like' => 'Chris', // name LIKE 'Chris' |
||
| 572 | * 'count >' => 10, // count > '10' |
||
| 573 | * 'type in' => [1, 2], // type IN (1, 2) |
||
| 574 | * 'type' => [3, 4] // type IN (3, 4) |
||
| 575 | * |
||
| 576 | * Comparison operator between conditions defaults to 'AND'. |
||
| 577 | * |
||
| 578 | * @param array $filter |
||
| 579 | * @param string $comparison [optional] |
||
| 580 | * @param bool $excludeWhere [optional] |
||
| 581 | * @return string |
||
| 582 | */ |
||
| 583 | protected function prepareWhere(array $filter, $comparison = 'AND', $excludeWhere = false) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Prepare an individual order condition. |
||
| 597 | * |
||
| 598 | * @param string $column |
||
| 599 | * @param string $direction [optional] |
||
| 600 | * @return string |
||
| 601 | */ |
||
| 602 | protected function prepareOrder($column, $direction = null) { |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Prepare an ORDER BY clause using the given order. |
||
| 611 | * |
||
| 612 | * Example order key-values: |
||
| 613 | * 'column', |
||
| 614 | * 'other_column' => 'ASC', |
||
| 615 | * 'another_column' => 'DESC |
||
| 616 | * |
||
| 617 | * Ordered ascending by default. |
||
| 618 | * |
||
| 619 | * @param array|string $order |
||
| 620 | * @return string |
||
| 621 | */ |
||
| 622 | protected function prepareOrderBy($order) { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Prepare a LIMIT clause using the given limit and offset. |
||
| 638 | * |
||
| 639 | * @param int $limit [optional] |
||
| 640 | * @param int $offset [optional] |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | abstract protected function prepareLimit($limit = 0, $offset = 0); |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Prepare a SELECT statement using the given columns, table, clauses and |
||
| 647 | * options. |
||
| 648 | * |
||
| 649 | * TODO: Simplify this so that prepareSelect only actually prepares the |
||
| 650 | * SELECT and FROM clauses. The rest could be concatenated by |
||
| 651 | * translateRead(). |
||
| 652 | * |
||
| 653 | * @param string $table |
||
| 654 | * @param array|string $columns |
||
| 655 | * @param string $joins [optional] |
||
| 656 | * @param string $where [optional] |
||
| 657 | * @param string $order [optional] |
||
| 658 | * @param string $limit [optional] |
||
| 659 | * @param bool $distinct [optional] |
||
| 660 | * @return string |
||
| 661 | */ |
||
| 662 | abstract protected function prepareSelect($table, $columns, $joins = null, $where = null, $order = null, $limit = null, $distinct = false); |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Prepare an INSERT INTO statement using the given table and data. |
||
| 666 | * |
||
| 667 | * @param string $table |
||
| 668 | * @param array $data |
||
| 669 | * @return string |
||
| 670 | */ |
||
| 671 | protected function prepareInsert($table, array $data) { |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Prepare an INSERT SELECT statement using the given table and |
||
| 685 | * subquery. |
||
| 686 | * |
||
| 687 | * @param string $table |
||
| 688 | * @param array $columns |
||
| 689 | * @param Storage\Query $subquery |
||
| 690 | * @return string |
||
| 691 | */ |
||
| 692 | public function prepareInsertSelect($table, array $columns, Storage\Query $subquery) { |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Prepare an UPDATE statement with the given table, data and clauses. |
||
| 707 | * |
||
| 708 | * @param string $table |
||
| 709 | * @param array $data |
||
| 710 | * @param string $where [optional] |
||
| 711 | * @param string $limit [optional] |
||
| 712 | * @return string |
||
| 713 | */ |
||
| 714 | abstract protected function prepareUpdate($table, $data, $where = null, $limit = null); |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Prepare a DELETE statement with the given table and clauses. |
||
| 718 | * |
||
| 719 | * @param string $table |
||
| 720 | * @param string $where [optional] |
||
| 721 | * @param string $limit [optional] |
||
| 722 | * @return string |
||
| 723 | */ |
||
| 724 | abstract protected function prepareDelete($table, $where = null, $limit = null); |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Prepare a set of query parameters from the given set of columns. |
||
| 728 | * |
||
| 729 | * @param array $columns |
||
| 730 | * @return array |
||
| 731 | */ |
||
| 732 | protected function columnParameters($columns) { |
||
| 747 | |||
| 748 | /** |
||
| 749 | * Prepare a set of query parameters from the given data. |
||
| 750 | * |
||
| 751 | * @param array $data |
||
| 752 | * @return array |
||
| 753 | */ |
||
| 754 | protected function dataParameters($data) { |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Prepare a set of query parameters from the given set of joins. |
||
| 768 | * |
||
| 769 | * @param Join[] $joins |
||
| 770 | * @return array |
||
| 771 | */ |
||
| 772 | protected function joinParameters($joins) { |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Prepare a set of query parameters from the given filter. |
||
| 784 | * |
||
| 785 | * @param array $filter |
||
| 786 | * @return array |
||
| 787 | */ |
||
| 788 | protected function filterParameters($filter) { |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Retrieve an array of parameters from the given query for executing a |
||
| 826 | * prepared query. |
||
| 827 | * |
||
| 828 | * @param Storage\Query $storageQuery |
||
| 829 | * @return array |
||
| 830 | */ |
||
| 831 | public function parameters(Storage\Query $storageQuery) { |
||
| 851 | |||
| 852 | } |
||
| 853 |
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.