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 SQLQuery |
||
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 SQLQuery} 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 | public function removeFilterOn($fieldExpression) { |
||
99 | |||
100 | /** |
||
101 | * Set up the simplest initial query |
||
102 | */ |
||
103 | public function initialiseQuery() { |
||
135 | |||
136 | public function setQueriedColumns($queriedColumns) { |
||
139 | |||
140 | /** |
||
141 | * Ensure that the query is ready to execute. |
||
142 | * |
||
143 | * @return SQLQuery |
||
144 | */ |
||
145 | public function getFinalisedQuery($queriedColumns = null) { |
||
246 | |||
247 | /** |
||
248 | * Ensure that if a query has an order by clause, those columns are present in the select. |
||
249 | * |
||
250 | * @param SQLQuery $query |
||
251 | * @return null |
||
252 | */ |
||
253 | protected function ensureSelectContainsOrderbyColumns($query, $originalSelect = array()) { |
||
322 | |||
323 | /** |
||
324 | * Execute the query and return the result as {@link Query} object. |
||
325 | */ |
||
326 | public function execute() { |
||
329 | |||
330 | /** |
||
331 | * Return this query's SQL |
||
332 | */ |
||
333 | public function sql() { |
||
336 | |||
337 | /** |
||
338 | * Return the number of records in this query. |
||
339 | * Note that this will issue a separate SELECT COUNT() query. |
||
340 | */ |
||
341 | public function count() { |
||
345 | |||
346 | /** |
||
347 | * Return the maximum value of the given field in this DataList |
||
348 | * |
||
349 | * @param String $field Unquoted database column name (will be escaped automatically) |
||
350 | */ |
||
351 | public function max($field) { |
||
354 | |||
355 | /** |
||
356 | * Return the minimum value of the given field in this DataList |
||
357 | * |
||
358 | * @param String $field Unquoted database column name (will be escaped automatically) |
||
359 | */ |
||
360 | public function min($field) { |
||
363 | |||
364 | /** |
||
365 | * Return the average value of the given field in this DataList |
||
366 | * |
||
367 | * @param String $field Unquoted database column name (will be escaped automatically) |
||
368 | */ |
||
369 | public function avg($field) { |
||
372 | |||
373 | /** |
||
374 | * Return the sum of the values of the given field in this DataList |
||
375 | * |
||
376 | * @param String $field Unquoted database column name (will be escaped automatically) |
||
377 | */ |
||
378 | public function sum($field) { |
||
381 | |||
382 | /** |
||
383 | * Runs a raw aggregate expression. Please handle escaping yourself |
||
384 | */ |
||
385 | public function aggregate($expression) { |
||
388 | |||
389 | /** |
||
390 | * Return the first row that would be returned by this full DataQuery |
||
391 | * Note that this will issue a separate SELECT ... LIMIT 1 query. |
||
392 | */ |
||
393 | public function firstRow() { |
||
396 | |||
397 | /** |
||
398 | * Return the last row that would be returned by this full DataQuery |
||
399 | * Note that this will issue a separate SELECT ... LIMIT query. |
||
400 | */ |
||
401 | public function lastRow() { |
||
404 | |||
405 | /** |
||
406 | * Update the SELECT clause of the query with the columns from the given table |
||
407 | */ |
||
408 | protected function selectColumnsFromTable(SQLQuery &$query, $tableClass, $columns = null) { |
||
431 | |||
432 | /** |
||
433 | * Append a GROUP BY clause to this query. |
||
434 | * |
||
435 | * @param String $groupby Escaped SQL statement |
||
436 | */ |
||
437 | public function groupby($groupby) { |
||
441 | |||
442 | /** |
||
443 | * Append a HAVING clause to this query. |
||
444 | * |
||
445 | * @param String $having Escaped SQL statement |
||
446 | */ |
||
447 | public function having($having) { |
||
451 | |||
452 | /** |
||
453 | * Create a disjunctive subgroup. |
||
454 | * |
||
455 | * That is a subgroup joined by OR |
||
456 | * |
||
457 | * @return DataQuery_SubGroup |
||
458 | */ |
||
459 | public function disjunctiveGroup() { |
||
462 | |||
463 | /** |
||
464 | * Create a conjunctive subgroup |
||
465 | * |
||
466 | * That is a subgroup joined by AND |
||
467 | * |
||
468 | * @return DataQuery_SubGroup |
||
469 | */ |
||
470 | public function conjunctiveGroup() { |
||
473 | |||
474 | /** |
||
475 | * Append a WHERE clause to this query. |
||
476 | * There are two different ways of doing this: |
||
477 | * |
||
478 | * <code> |
||
479 | * // the entire predicate as a single string |
||
480 | * $query->where("\"Column\" = 'Value'"); |
||
481 | * |
||
482 | * // multiple predicates as an array |
||
483 | * $query->where(array("\"Column\" = 'Value'", "\"Column\" != 'Value'")); |
||
484 | * </code> |
||
485 | * |
||
486 | * @param string|array $where Predicate(s) to set, as escaped SQL statements. |
||
487 | */ |
||
488 | public function where($filter) { |
||
494 | |||
495 | /** |
||
496 | * Append a WHERE with OR. |
||
497 | * |
||
498 | * @example $dataQuery->whereAny(array("\"Monkey\" = 'Chimp'", "\"Color\" = 'Brown'")); |
||
499 | * @see where() |
||
500 | * |
||
501 | * @param array $filter Escaped SQL statement. |
||
502 | * @return DataQuery |
||
503 | */ |
||
504 | public function whereAny($filter) { |
||
510 | |||
511 | /** |
||
512 | * Set the ORDER BY clause of this query |
||
513 | * |
||
514 | * @see SQLQuery::orderby() |
||
515 | * |
||
516 | * @param String $sort Column to sort on (escaped SQL statement) |
||
517 | * @param String $direction Direction ("ASC" or "DESC", escaped SQL statement) |
||
518 | * @param Boolean $clear Clear existing values |
||
519 | * @return DataQuery |
||
520 | */ |
||
521 | public function sort($sort = null, $direction = null, $clear = true) { |
||
530 | |||
531 | /** |
||
532 | * Reverse order by clause |
||
533 | * |
||
534 | * @return DataQuery |
||
535 | */ |
||
536 | public function reverseSort() { |
||
540 | |||
541 | /** |
||
542 | * Set the limit of this query. |
||
543 | * |
||
544 | * @param int $limit |
||
545 | * @param int $offset |
||
546 | */ |
||
547 | public function limit($limit, $offset = 0) { |
||
551 | |||
552 | /** |
||
553 | * Set whether this query should be distinct or not. |
||
554 | * |
||
555 | * @param bool $value |
||
556 | * @return DataQuery |
||
557 | */ |
||
558 | public function distinct($value) { |
||
562 | |||
563 | /** |
||
564 | * Add an INNER JOIN clause to this query. |
||
565 | * |
||
566 | * @param String $table The unquoted table name to join to. |
||
567 | * @param String $onClause The filter for the join (escaped SQL statement) |
||
568 | * @param String $alias An optional alias name (unquoted) |
||
569 | */ |
||
570 | public function innerJoin($table, $onClause, $alias = null) { |
||
576 | |||
577 | /** |
||
578 | * Add a LEFT JOIN clause to this query. |
||
579 | * |
||
580 | * @param String $table The unquoted table to join to. |
||
581 | * @param String $onClause The filter for the join (escaped SQL statement). |
||
582 | * @param String $alias An optional alias name (unquoted) |
||
583 | */ |
||
584 | public function leftJoin($table, $onClause, $alias = null) { |
||
590 | |||
591 | /** |
||
592 | * Traverse the relationship fields, and add the table |
||
593 | * mappings to the query object state. This has to be called |
||
594 | * in any overloaded {@link SearchFilter->apply()} methods manually. |
||
595 | * |
||
596 | * @param String|array $relation The array/dot-syntax relation to follow |
||
597 | * @return The model class of the related item |
||
598 | */ |
||
599 | public function applyRelation($relation) { |
||
673 | |||
674 | /** |
||
675 | * Removes the result of query from this query. |
||
676 | * |
||
677 | * @param DataQuery $subtractQuery |
||
678 | * @param string $field |
||
679 | */ |
||
680 | public function subtract(DataQuery $subtractQuery, $field='ID') { |
||
690 | |||
691 | /** |
||
692 | * Select the given fields from the given table. |
||
693 | * |
||
694 | * @param String $table Unquoted table name (will be escaped automatically) |
||
695 | * @param Array $fields Database column names (will be escaped automatically) |
||
696 | */ |
||
697 | public function selectFromTable($table, $fields) { |
||
706 | |||
707 | /** |
||
708 | * Query the given field column from the database and return as an array. |
||
709 | * |
||
710 | * @param string $field See {@link expressionForField()}. |
||
711 | * @return array List of column values for the specified column |
||
712 | */ |
||
713 | public function column($field = 'ID') { |
||
723 | |||
724 | /** |
||
725 | * @param String $field Select statement identifier, either the unquoted column name, |
||
726 | * the full composite SQL statement, or the alias set through {@link SQLQuery->selectField()}. |
||
727 | * @return String The expression used to query this field via this DataQuery |
||
728 | */ |
||
729 | protected function expressionForField($field) { |
||
744 | |||
745 | /** |
||
746 | * Select the given field expressions. |
||
747 | * |
||
748 | * @param $fieldExpression String The field to select (escaped SQL statement) |
||
749 | * @param $alias String The alias of that field (escaped SQL statement) |
||
750 | */ |
||
751 | protected function selectField($fieldExpression, $alias = null) { |
||
754 | |||
755 | //// QUERY PARAMS |
||
756 | |||
757 | /** |
||
758 | * An arbitrary store of query parameters that can be used by decorators. |
||
759 | * @todo This will probably be made obsolete if we have subclasses of DataList and/or DataQuery. |
||
760 | */ |
||
761 | private $queryParams; |
||
762 | |||
763 | /** |
||
764 | * Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query. |
||
765 | * It's expected that the $key will be namespaced, e.g, 'Versioned.stage' instead of just 'stage'. |
||
766 | */ |
||
767 | public function setQueryParam($key, $value) { |
||
770 | |||
771 | /** |
||
772 | * Set an arbitrary query parameter, that can be used by decorators to add additional meta-data to the query. |
||
773 | */ |
||
774 | public function getQueryParam($key) { |
||
778 | |||
779 | /** |
||
780 | * Returns all query parameters |
||
781 | * @return array query parameters array |
||
782 | */ |
||
783 | public function getQueryParams() { |
||
786 | } |
||
787 | |||
867 |
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.