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 WP_Date_Query 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 WP_Date_Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class WP_Date_Query { |
||
| 18 | /** |
||
| 19 | * Array of date queries. |
||
| 20 | * |
||
| 21 | * See WP_Date_Query::__construct() for information on date query arguments. |
||
| 22 | * |
||
| 23 | * @since 3.7.0 |
||
| 24 | * @access public |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | public $queries = array(); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The default relation between top-level queries. Can be either 'AND' or 'OR'. |
||
| 31 | * |
||
| 32 | * @since 3.7.0 |
||
| 33 | * @access public |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | public $relation = 'AND'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The column to query against. Can be changed via the query arguments. |
||
| 40 | * |
||
| 41 | * @since 3.7.0 |
||
| 42 | * @access public |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | public $column = 'post_date'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The value comparison operator. Can be changed via the query arguments. |
||
| 49 | * |
||
| 50 | * @since 3.7.0 |
||
| 51 | * @access public |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | public $compare = '='; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Supported time-related parameter keys. |
||
| 58 | * |
||
| 59 | * @since 4.1.0 |
||
| 60 | * @access public |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | public $time_keys = array( 'after', 'before', 'year', 'month', 'monthnum', 'week', 'w', 'dayofyear', 'day', 'dayofweek', 'dayofweek_iso', 'hour', 'minute', 'second' ); |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Constructor. |
||
| 67 | * |
||
| 68 | * Time-related parameters that normally require integer values ('year', 'month', 'week', 'dayofyear', 'day', |
||
| 69 | * 'dayofweek', 'dayofweek_iso', 'hour', 'minute', 'second') accept arrays of integers for some values of |
||
| 70 | * 'compare'. When 'compare' is 'IN' or 'NOT IN', arrays are accepted; when 'compare' is 'BETWEEN' or 'NOT |
||
| 71 | * BETWEEN', arrays of two valid values are required. See individual argument descriptions for accepted values. |
||
| 72 | * |
||
| 73 | * @since 3.7.0 |
||
| 74 | * @since 4.0.0 The $inclusive logic was updated to include all times within the date range. |
||
| 75 | * @since 4.1.0 Introduced 'dayofweek_iso' time type parameter. |
||
| 76 | * @access public |
||
| 77 | * |
||
| 78 | * @param array $date_query { |
||
| 79 | * Array of date query clauses. |
||
| 80 | * |
||
| 81 | * @type array { |
||
| 82 | * @type string $column Optional. The column to query against. If undefined, inherits the value of |
||
| 83 | * the `$default_column` parameter. Accepts 'post_date', 'post_date_gmt', |
||
| 84 | * 'post_modified','post_modified_gmt', 'comment_date', 'comment_date_gmt'. |
||
| 85 | * Default 'post_date'. |
||
| 86 | * @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=', '<', '<=', |
||
| 87 | * 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. Default '='. |
||
| 88 | * @type string $relation Optional. The boolean relationship between the date queries. Accepts 'OR' or 'AND'. |
||
| 89 | * Default 'OR'. |
||
| 90 | * @type array { |
||
| 91 | * Optional. An array of first-order clause parameters, or another fully-formed date query. |
||
| 92 | * |
||
| 93 | * @type string|array $before { |
||
| 94 | * Optional. Date to retrieve posts before. Accepts `strtotime()`-compatible string, |
||
| 95 | * or array of 'year', 'month', 'day' values. |
||
| 96 | * |
||
| 97 | * @type string $year The four-digit year. Default empty. Accepts any four-digit year. |
||
| 98 | * @type string $month Optional when passing array.The month of the year. |
||
| 99 | * Default (string:empty)|(array:1). Accepts numbers 1-12. |
||
| 100 | * @type string $day Optional when passing array.The day of the month. |
||
| 101 | * Default (string:empty)|(array:1). Accepts numbers 1-31. |
||
| 102 | * } |
||
| 103 | * @type string|array $after { |
||
| 104 | * Optional. Date to retrieve posts after. Accepts `strtotime()`-compatible string, |
||
| 105 | * or array of 'year', 'month', 'day' values. |
||
| 106 | * |
||
| 107 | * @type string $year The four-digit year. Accepts any four-digit year. Default empty. |
||
| 108 | * @type string $month Optional when passing array. The month of the year. Accepts numbers 1-12. |
||
| 109 | * Default (string:empty)|(array:12). |
||
| 110 | * @type string $day Optional when passing array.The day of the month. Accepts numbers 1-31. |
||
| 111 | * Default (string:empty)|(array:last day of month). |
||
| 112 | * } |
||
| 113 | * @type string $column Optional. Used to add a clause comparing a column other than the |
||
| 114 | * column specified in the top-level `$column` parameter. Accepts |
||
| 115 | * 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt', |
||
| 116 | * 'comment_date', 'comment_date_gmt'. Default is the value of |
||
| 117 | * top-level `$column`. |
||
| 118 | * @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=', |
||
| 119 | * '<', '<=', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. 'IN', |
||
| 120 | * 'NOT IN', 'BETWEEN', and 'NOT BETWEEN'. Comparisons support |
||
| 121 | * arrays in some time-related parameters. Default '='. |
||
| 122 | * @type bool $inclusive Optional. Include results from dates specified in 'before' or |
||
| 123 | * 'after'. Default false. |
||
| 124 | * @type int|array $year Optional. The four-digit year number. Accepts any four-digit year |
||
| 125 | * or an array of years if `$compare` supports it. Default empty. |
||
| 126 | * @type int|array $month Optional. The two-digit month number. Accepts numbers 1-12 or an |
||
| 127 | * array of valid numbers if `$compare` supports it. Default empty. |
||
| 128 | * @type int|array $week Optional. The week number of the year. Accepts numbers 0-53 or an |
||
| 129 | * array of valid numbers if `$compare` supports it. Default empty. |
||
| 130 | * @type int|array $dayofyear Optional. The day number of the year. Accepts numbers 1-366 or an |
||
| 131 | * array of valid numbers if `$compare` supports it. |
||
| 132 | * @type int|array $day Optional. The day of the month. Accepts numbers 1-31 or an array |
||
| 133 | * of valid numbers if `$compare` supports it. Default empty. |
||
| 134 | * @type int|array $dayofweek Optional. The day number of the week. Accepts numbers 1-7 (1 is |
||
| 135 | * Sunday) or an array of valid numbers if `$compare` supports it. |
||
| 136 | * Default empty. |
||
| 137 | * @type int|array $dayofweek_iso Optional. The day number of the week (ISO). Accepts numbers 1-7 |
||
| 138 | * (1 is Monday) or an array of valid numbers if `$compare` supports it. |
||
| 139 | * Default empty. |
||
| 140 | * @type int|array $hour Optional. The hour of the day. Accepts numbers 0-23 or an array |
||
| 141 | * of valid numbers if `$compare` supports it. Default empty. |
||
| 142 | * @type int|array $minute Optional. The minute of the hour. Accepts numbers 0-60 or an array |
||
| 143 | * of valid numbers if `$compare` supports it. Default empty. |
||
| 144 | * @type int|array $second Optional. The second of the minute. Accepts numbers 0-60 or an |
||
| 145 | * array of valid numbers if `$compare` supports it. Default empty. |
||
| 146 | * } |
||
| 147 | * } |
||
| 148 | * } |
||
| 149 | * @param array $default_column Optional. Default column to query against. Default 'post_date'. |
||
|
|
|||
| 150 | * Accepts 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt', |
||
| 151 | * 'comment_date', 'comment_date_gmt'. |
||
| 152 | */ |
||
| 153 | public function __construct( $date_query, $default_column = 'post_date' ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Recursive-friendly query sanitizer. |
||
| 188 | * |
||
| 189 | * Ensures that each query-level clause has a 'relation' key, and that |
||
| 190 | * each first-order clause contains all the necessary keys from |
||
| 191 | * `$defaults`. |
||
| 192 | * |
||
| 193 | * @since 4.1.0 |
||
| 194 | * @access public |
||
| 195 | * |
||
| 196 | * @param array $queries |
||
| 197 | * @param array $parent_query |
||
| 198 | * |
||
| 199 | * @return array Sanitized queries. |
||
| 200 | */ |
||
| 201 | public function sanitize_query( $queries, $parent_query = null ) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Determine whether this is a first-order clause. |
||
| 250 | * |
||
| 251 | * Checks to see if the current clause has any time-related keys. |
||
| 252 | * If so, it's first-order. |
||
| 253 | * |
||
| 254 | * @since 4.1.0 |
||
| 255 | * @access protected |
||
| 256 | * |
||
| 257 | * @param array $query Query clause. |
||
| 258 | * @return bool True if this is a first-order clause. |
||
| 259 | */ |
||
| 260 | protected function is_first_order_clause( $query ) { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Determines and validates what comparison operator to use. |
||
| 267 | * |
||
| 268 | * @since 3.7.0 |
||
| 269 | * @access public |
||
| 270 | * |
||
| 271 | * @param array $query A date query or a date subquery. |
||
| 272 | * @return string The comparison operator. |
||
| 273 | */ |
||
| 274 | public function get_compare( $query ) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Validates the given date_query values and triggers errors if something is not valid. |
||
| 283 | * |
||
| 284 | * Note that date queries with invalid date ranges are allowed to |
||
| 285 | * continue (though of course no items will be found for impossible dates). |
||
| 286 | * This method only generates debug notices for these cases. |
||
| 287 | * |
||
| 288 | * @since 4.1.0 |
||
| 289 | * @access public |
||
| 290 | * |
||
| 291 | * @param array $date_query The date_query array. |
||
| 292 | * @return bool True if all values in the query are valid, false if one or more fail. |
||
| 293 | */ |
||
| 294 | public function validate_date_values( $date_query = array() ) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Validates a column name parameter. |
||
| 478 | * |
||
| 479 | * Column names without a table prefix (like 'post_date') are checked against a whitelist of |
||
| 480 | * known tables, and then, if found, have a table prefix (such as 'wp_posts.') prepended. |
||
| 481 | * Prefixed column names (such as 'wp_posts.post_date') bypass this whitelist check, |
||
| 482 | * and are only sanitized to remove illegal characters. |
||
| 483 | * |
||
| 484 | * @since 3.7.0 |
||
| 485 | * @access public |
||
| 486 | * |
||
| 487 | * @param string $column The user-supplied column name. |
||
| 488 | * @return string A validated column name value. |
||
| 489 | */ |
||
| 490 | public function validate_column( $column ) { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Generate WHERE clause to be appended to a main query. |
||
| 552 | * |
||
| 553 | * @since 3.7.0 |
||
| 554 | * @access public |
||
| 555 | * |
||
| 556 | * @return string MySQL WHERE clause. |
||
| 557 | */ |
||
| 558 | public function get_sql() { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Generate SQL clauses to be appended to a main query. |
||
| 576 | * |
||
| 577 | * Called by the public WP_Date_Query::get_sql(), this method is abstracted |
||
| 578 | * out to maintain parity with the other Query classes. |
||
| 579 | * |
||
| 580 | * @since 4.1.0 |
||
| 581 | * @access protected |
||
| 582 | * |
||
| 583 | * @return array { |
||
| 584 | * Array containing JOIN and WHERE SQL clauses to append to the main query. |
||
| 585 | * |
||
| 586 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 587 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 588 | * } |
||
| 589 | */ |
||
| 590 | View Code Duplication | protected function get_sql_clauses() { |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Generate SQL clauses for a single query array. |
||
| 602 | * |
||
| 603 | * If nested subqueries are found, this method recurses the tree to |
||
| 604 | * produce the properly nested SQL. |
||
| 605 | * |
||
| 606 | * @since 4.1.0 |
||
| 607 | * @access protected |
||
| 608 | * |
||
| 609 | * @param array $query Query to parse. |
||
| 610 | * @param int $depth Optional. Number of tree levels deep we currently are. |
||
| 611 | * Used to calculate indentation. Default 0. |
||
| 612 | * @return array { |
||
| 613 | * Array containing JOIN and WHERE SQL clauses to append to a single query array. |
||
| 614 | * |
||
| 615 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 616 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 617 | * } |
||
| 618 | */ |
||
| 619 | View Code Duplication | protected function get_sql_for_query( $query, $depth = 0 ) { |
|
| 684 | |||
| 685 | /** |
||
| 686 | * Turns a single date clause into pieces for a WHERE clause. |
||
| 687 | * |
||
| 688 | * A wrapper for get_sql_for_clause(), included here for backward |
||
| 689 | * compatibility while retaining the naming convention across Query classes. |
||
| 690 | * |
||
| 691 | * @since 3.7.0 |
||
| 692 | * @access protected |
||
| 693 | * |
||
| 694 | * @param array $query Date query arguments. |
||
| 695 | * @return array { |
||
| 696 | * Array containing JOIN and WHERE SQL clauses to append to the main query. |
||
| 697 | * |
||
| 698 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 699 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 700 | * } |
||
| 701 | */ |
||
| 702 | protected function get_sql_for_subquery( $query ) { |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Turns a first-order date query into SQL for a WHERE clause. |
||
| 708 | * |
||
| 709 | * @since 4.1.0 |
||
| 710 | * @access protected |
||
| 711 | * |
||
| 712 | * @param array $query Date query clause. |
||
| 713 | * @param array $parent_query Parent query of the current date query. |
||
| 714 | * @return array { |
||
| 715 | * Array containing JOIN and WHERE SQL clauses to append to the main query. |
||
| 716 | * |
||
| 717 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 718 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 719 | * } |
||
| 720 | */ |
||
| 721 | protected function get_sql_for_clause( $query, $parent_query ) { |
||
| 800 | |||
| 801 | /** |
||
| 802 | * Builds and validates a value string based on the comparison operator. |
||
| 803 | * |
||
| 804 | * @since 3.7.0 |
||
| 805 | * @access public |
||
| 806 | * |
||
| 807 | * @param string $compare The compare operator to use |
||
| 808 | * @param string|array $value The value |
||
| 809 | * @return string|false|int The value to be used in SQL or false on error. |
||
| 810 | */ |
||
| 811 | public function build_value( $compare, $value ) { |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Builds a MySQL format date/time based on some query parameters. |
||
| 859 | * |
||
| 860 | * You can pass an array of values (year, month, etc.) with missing parameter values being defaulted to |
||
| 861 | * either the maximum or minimum values (controlled by the $default_to parameter). Alternatively you can |
||
| 862 | * pass a string that will be run through strtotime(). |
||
| 863 | * |
||
| 864 | * @since 3.7.0 |
||
| 865 | * @access public |
||
| 866 | * |
||
| 867 | * @param string|array $datetime An array of parameters or a strotime() string |
||
| 868 | * @param bool $default_to_max Whether to round up incomplete dates. Supported by values |
||
| 869 | * of $datetime that are arrays, or string values that are a |
||
| 870 | * subset of MySQL date format ('Y', 'Y-m', 'Y-m-d', 'Y-m-d H:i'). |
||
| 871 | * Default: false. |
||
| 872 | * @return string|false A MySQL format date/time or false on failure |
||
| 873 | */ |
||
| 874 | public function build_mysql_datetime( $datetime, $default_to_max = false ) { |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Builds a query string for comparing time values (hour, minute, second). |
||
| 947 | * |
||
| 948 | * If just hour, minute, or second is set than a normal comparison will be done. |
||
| 949 | * However if multiple values are passed, a pseudo-decimal time will be created |
||
| 950 | * in order to be able to accurately compare against. |
||
| 951 | * |
||
| 952 | * @since 3.7.0 |
||
| 953 | * @access public |
||
| 954 | * |
||
| 955 | * @param string $column The column to query against. Needs to be pre-validated! |
||
| 956 | * @param string $compare The comparison operator. Needs to be pre-validated! |
||
| 957 | * @param int|null $hour Optional. An hour value (0-23). |
||
| 958 | * @param int|null $minute Optional. A minute value (0-59). |
||
| 959 | * @param int|null $second Optional. A second value (0-59). |
||
| 960 | * @return string|false A query part or false on failure. |
||
| 961 | */ |
||
| 962 | public function build_time_query( $column, $compare, $hour = null, $minute = null, $second = null ) { |
||
| 1020 | } |
||
| 1021 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.