Complex classes like DataQuery 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 DataQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class DataQuery { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $dataClass; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var SQLSelect |
||
| 23 | */ |
||
| 24 | protected $query; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $collidingFields = array(); |
||
| 30 | |||
| 31 | private $queriedColumns = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Boolean |
||
| 35 | */ |
||
| 36 | private $queryFinalised = false; |
||
| 37 | |||
| 38 | // TODO: replace subclass_access with this |
||
| 39 | protected $querySubclasses = true; |
||
| 40 | // TODO: replace restrictclasses with this |
||
| 41 | protected $filterByClassName = true; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Create a new DataQuery. |
||
| 45 | * |
||
| 46 | * @param String The name of the DataObject class that you wish to query |
||
| 47 | */ |
||
| 48 | public function __construct($dataClass) { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Clone this object |
||
| 55 | */ |
||
| 56 | public function __clone() { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Return the {@link DataObject} class that is being queried. |
||
| 62 | */ |
||
| 63 | public function dataClass() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Return the {@link SQLSelect} object that represents the current query; note that it will |
||
| 69 | * be a clone of the object. |
||
| 70 | */ |
||
| 71 | public function query() { |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * Remove a filter from the query |
||
| 78 | * |
||
| 79 | * @param string|array $fieldExpression The predicate of the condition to remove |
||
| 80 | * (ignoring parameters). The expression will be considered a match if it's |
||
| 81 | * contained within any other predicate. |
||
| 82 | * @return DataQuery Self reference |
||
| 83 | */ |
||
| 84 | public function removeFilterOn($fieldExpression) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Set up the simplest initial query |
||
| 128 | */ |
||
| 129 | public function initialiseQuery() { |
||
| 161 | |||
| 162 | public function setQueriedColumns($queriedColumns) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Ensure that the query is ready to execute. |
||
| 168 | * |
||
| 169 | * @param array|null $queriedColumns Any columns to filter the query by |
||
| 170 | * @return SQLSelect The finalised sql query |
||
| 171 | */ |
||
| 172 | public function getFinalisedQuery($queriedColumns = null) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Ensure that if a query has an order by clause, those columns are present in the select. |
||
| 286 | * |
||
| 287 | * @param SQLSelect $query |
||
| 288 | * @return null |
||
| 289 | */ |
||
| 290 | protected function ensureSelectContainsOrderbyColumns($query, $originalSelect = array()) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Execute the query and return the result as {@link SS_Query} object. |
||
| 357 | * |
||
| 358 | * @return SS_Query |
||
| 359 | */ |
||
| 360 | public function execute() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Return this query's SQL |
||
| 366 | * |
||
| 367 | * @param array $parameters Out variable for parameters required for this query |
||
| 368 | * @return string The resulting SQL query (may be paramaterised) |
||
| 369 | */ |
||
| 370 | public function sql(&$parameters = array()) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Return the number of records in this query. |
||
| 376 | * Note that this will issue a separate SELECT COUNT() query. |
||
| 377 | */ |
||
| 378 | public function count() { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Return the maximum value of the given field in this DataList |
||
| 385 | * |
||
| 386 | * @param String $field Unquoted database column name. Will be ANSI quoted |
||
| 387 | * automatically so must not contain double quotes. |
||
| 388 | */ |
||
| 389 | public function max($field) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Return the minimum value of the given field in this DataList |
||
| 395 | * |
||
| 396 | * @param String $field Unquoted database column name. Will be ANSI quoted |
||
| 397 | * automatically so must not contain double quotes. |
||
| 398 | */ |
||
| 399 | public function min($field) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Return the average value of the given field in this DataList |
||
| 405 | * |
||
| 406 | * @param String $field Unquoted database column name. Will be ANSI quoted |
||
| 407 | * automatically so must not contain double quotes. |
||
| 408 | */ |
||
| 409 | public function avg($field) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Return the sum of the values of the given field in this DataList |
||
| 415 | * |
||
| 416 | * @param String $field Unquoted database column name. Will be ANSI quoted |
||
| 417 | * automatically so must not contain double quotes. |
||
| 418 | */ |
||
| 419 | public function sum($field) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Runs a raw aggregate expression. Please handle escaping yourself |
||
| 425 | */ |
||
| 426 | public function aggregate($expression) { |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Return the first row that would be returned by this full DataQuery |
||
| 432 | * Note that this will issue a separate SELECT ... LIMIT 1 query. |
||
| 433 | */ |
||
| 434 | public function firstRow() { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Return the last row that would be returned by this full DataQuery |
||
| 440 | * Note that this will issue a separate SELECT ... LIMIT query. |
||
| 441 | */ |
||
| 442 | public function lastRow() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Update the SELECT clause of the query with the columns from the given table |
||
| 448 | */ |
||
| 449 | protected function selectColumnsFromTable(SQLSelect &$query, $tableClass, $columns = null) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Append a GROUP BY clause to this query. |
||
| 477 | * |
||
| 478 | * @param String $groupby Escaped SQL statement |
||
| 479 | */ |
||
| 480 | public function groupby($groupby) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Append a HAVING clause to this query. |
||
| 487 | * |
||
| 488 | * @param String $having Escaped SQL statement |
||
| 489 | */ |
||
| 490 | public function having($having) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Create a disjunctive subgroup. |
||
| 497 | * |
||
| 498 | * That is a subgroup joined by OR |
||
| 499 | * |
||
| 500 | * @return DataQuery_SubGroup |
||
| 501 | */ |
||
| 502 | public function disjunctiveGroup() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Create a conjunctive subgroup |
||
| 508 | * |
||
| 509 | * That is a subgroup joined by AND |
||
| 510 | * |
||
| 511 | * @return DataQuery_SubGroup |
||
| 512 | */ |
||
| 513 | public function conjunctiveGroup() { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Adds a WHERE clause. |
||
| 519 | * |
||
| 520 | * @see SQLSelect::addWhere() for syntax examples, although DataQuery |
||
| 521 | * won't expand multiple arguments as SQLSelect does. |
||
| 522 | * |
||
| 523 | * @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or |
||
| 524 | * paramaterised queries |
||
| 525 | * @return DataQuery |
||
| 526 | */ |
||
| 527 | public function where($filter) { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Append a WHERE with OR. |
||
| 536 | * |
||
| 537 | * @see SQLSelect::addWhere() for syntax examples, although DataQuery |
||
| 538 | * won't expand multiple method arguments as SQLSelect does. |
||
| 539 | * |
||
| 540 | * @param string|array|SQLConditionGroup $filter Predicate(s) to set, as escaped SQL statements or |
||
| 541 | * paramaterised queries |
||
| 542 | * @return DataQuery |
||
| 543 | */ |
||
| 544 | public function whereAny($filter) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Set the ORDER BY clause of this query |
||
| 553 | * |
||
| 554 | * @see SQLSelect::orderby() |
||
| 555 | * |
||
| 556 | * @param String $sort Column to sort on (escaped SQL statement) |
||
| 557 | * @param String $direction Direction ("ASC" or "DESC", escaped SQL statement) |
||
| 558 | * @param Boolean $clear Clear existing values |
||
| 559 | * @return DataQuery |
||
| 560 | */ |
||
| 561 | public function sort($sort = null, $direction = null, $clear = true) { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Reverse order by clause |
||
| 573 | * |
||
| 574 | * @return DataQuery |
||
| 575 | */ |
||
| 576 | public function reverseSort() { |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Set the limit of this query. |
||
| 583 | * |
||
| 584 | * @param int $limit |
||
| 585 | * @param int $offset |
||
| 586 | */ |
||
| 587 | public function limit($limit, $offset = 0) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Set whether this query should be distinct or not. |
||
| 594 | * |
||
| 595 | * @param bool $value |
||
| 596 | * @return DataQuery |
||
| 597 | */ |
||
| 598 | public function distinct($value) { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Add an INNER JOIN clause to this query. |
||
| 605 | * |
||
| 606 | * @param String $table The unquoted table name to join to. |
||
| 607 | * @param String $onClause The filter for the join (escaped SQL statement) |
||
| 608 | * @param String $alias An optional alias name (unquoted) |
||
| 609 | * @param int $order A numerical index to control the order that joins are added to the query; lower order values |
||
| 610 | * will cause the query to appear first. The default is 20, and joins created automatically by the |
||
| 611 | * ORM have a value of 10. |
||
| 612 | * @param array $parameters Any additional parameters if the join is a parameterised subquery |
||
| 613 | */ |
||
| 614 | public function innerJoin($table, $onClause, $alias = null, $order = 20, $parameters = array()) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Add a LEFT JOIN clause to this query. |
||
| 623 | * |
||
| 624 | * @param string $table The unquoted table to join to. |
||
| 625 | * @param string $onClause The filter for the join (escaped SQL statement). |
||
| 626 | * @param string $alias An optional alias name (unquoted) |
||
| 627 | * @param int $order A numerical index to control the order that joins are added to the query; lower order values |
||
| 628 | * will cause the query to appear first. The default is 20, and joins created automatically by the |
||
| 629 | * ORM have a value of 10. |
||
| 630 | * @param array $parameters Any additional parameters if the join is a parameterised subquery |
||
| 631 | */ |
||
| 632 | public function leftJoin($table, $onClause, $alias = null, $order = 20, $parameters = array()) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Traverse the relationship fields, and add the table |
||
| 641 | * mappings to the query object state. This has to be called |
||
| 642 | * in any overloaded {@link SearchFilter->apply()} methods manually. |
||
| 643 | * |
||
| 644 | * @param String|array $relation The array/dot-syntax relation to follow |
||
| 645 | * @param bool $linearOnly Set to true to restrict to linear relations only. Set this |
||
| 646 | * if this relation will be used for sorting, and should not include duplicate rows. |
||
| 647 | * @return The model class of the related item |
||
| 648 | */ |
||
| 649 | public function applyRelation($relation, $linearOnly = false) { |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Join the given class to this query with the given key |
||
| 693 | * |
||
| 694 | * @param string $localClass Name of class that has the has_one to the joined class |
||
| 695 | * @param string $localField Name of the has_one relationship to joi |
||
| 696 | * @param string $foreignClass Class to join |
||
| 697 | */ |
||
| 698 | protected function joinHasOneRelation($localClass, $localField, $foreignClass) |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Join the given has_many relation to this query. |
||
| 739 | * |
||
| 740 | * Doesn't work with polymorphic relationships |
||
| 741 | * |
||
| 742 | * @param string $localClass Name of class that has the has_many to the joined class |
||
| 743 | * @param string $localField Name of the has_many relationship to join |
||
| 744 | * @param string $foreignClass Class to join |
||
| 745 | */ |
||
| 746 | protected function joinHasManyRelation($localClass, $localField, $foreignClass) { |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Join table via many_many relationship |
||
| 788 | * |
||
| 789 | * @param string $parentClass |
||
| 790 | * @param string $componentClass |
||
| 791 | * @param string $parentField |
||
| 792 | * @param string $componentField |
||
| 793 | * @param string $relationTable Name of relation table |
||
| 794 | */ |
||
| 795 | protected function joinManyManyRelationship($parentClass, $componentClass, $parentField, $componentField, $relationTable) { |
||
| 819 | |||
| 820 | /** |
||
| 821 | * Removes the result of query from this query. |
||
| 822 | * |
||
| 823 | * @param DataQuery $subtractQuery |
||
| 824 | * @param string $field |
||
| 825 | */ |
||
| 826 | public function subtract(DataQuery $subtractQuery, $field='ID') { |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Select the given fields from the given table. |
||
| 840 | * |
||
| 841 | * @param String $table Unquoted table name (will be escaped automatically) |
||
| 842 | * @param Array $fields Database column names (will be escaped automatically) |
||
| 843 | */ |
||
| 844 | public function selectFromTable($table, $fields) { |
||
| 853 | |||
| 854 | /** |
||
| 855 | * Query the given field column from the database and return as an array. |
||
| 856 | * |
||
| 857 | * @param string $field See {@link expressionForField()}. |
||
| 858 | * @return array List of column values for the specified column |
||
| 859 | */ |
||
| 860 | public function column($field = 'ID') { |
||
| 870 | |||
| 871 | /** |
||
| 872 | * @param String $field Select statement identifier, either the unquoted column name, |
||
| 873 | * the full composite SQL statement, or the alias set through {@link SQLSelect->selectField()}. |
||
| 874 | * @return String The expression used to query this field via this DataQuery |
||
| 875 | */ |
||
| 876 | protected function expressionForField($field) { |
||
| 891 | |||
| 892 | /** |
||
| 893 | * Select the given field expressions. |
||
| 894 | * |
||
| 895 | * @param $fieldExpression String The field to select (escaped SQL statement) |
||
| 896 | * @param $alias String The alias of that field (escaped SQL statement) |
||
| 897 | */ |
||
| 898 | public function selectField($fieldExpression, $alias = null) { |
||
| 901 | |||
| 902 | //// QUERY PARAMS |
||
| 903 | |||
| 904 | /** |
||
| 905 | * An arbitrary store of query parameters that can be used by decorators. |
||
| 906 | * @todo This will probably be made obsolete if we have subclasses of DataList and/or DataQuery. |
||
| 907 | */ |
||
| 908 | private $queryParams; |
||
| 909 | |||
| 910 | /** |
||
| 911 | * Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query. |
||
| 912 | * It's expected that the $key will be namespaced, e.g, 'Versioned.stage' instead of just 'stage'. |
||
| 913 | */ |
||
| 914 | public function setQueryParam($key, $value) { |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query. |
||
| 920 | */ |
||
| 921 | public function getQueryParam($key) { |
||
| 925 | |||
| 926 | /** |
||
| 927 | * Returns all query parameters |
||
| 928 | * @return array query parameters array |
||
| 929 | */ |
||
| 930 | public function getQueryParams() { |
||
| 933 | } |
||
| 934 | |||
| 991 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.