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_Meta_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_Meta_Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class WP_Meta_Query { |
||
| 25 | /** |
||
| 26 | * Array of metadata queries. |
||
| 27 | * |
||
| 28 | * See WP_Meta_Query::__construct() for information on meta query arguments. |
||
| 29 | * |
||
| 30 | * @since 3.2.0 |
||
| 31 | * @access public |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | public $queries = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The relation between the queries. Can be one of 'AND' or 'OR'. |
||
| 38 | * |
||
| 39 | * @since 3.2.0 |
||
| 40 | * @access public |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | public $relation; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Database table to query for the metadata. |
||
| 47 | * |
||
| 48 | * @since 4.1.0 |
||
| 49 | * @access public |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | public $meta_table; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Column in meta_table that represents the ID of the object the metadata belongs to. |
||
| 56 | * |
||
| 57 | * @since 4.1.0 |
||
| 58 | * @access public |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | public $meta_id_column; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Database table that where the metadata's objects are stored (eg $wpdb->users). |
||
| 65 | * |
||
| 66 | * @since 4.1.0 |
||
| 67 | * @access public |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | public $primary_table; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Column in primary_table that represents the ID of the object. |
||
| 74 | * |
||
| 75 | * @since 4.1.0 |
||
| 76 | * @access public |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | public $primary_id_column; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * A flat list of table aliases used in JOIN clauses. |
||
| 83 | * |
||
| 84 | * @since 4.1.0 |
||
| 85 | * @access protected |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $table_aliases = array(); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * A flat list of clauses, keyed by clause 'name'. |
||
| 92 | * |
||
| 93 | * @since 4.2.0 |
||
| 94 | * @access protected |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $clauses = array(); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Whether the query contains any OR relations. |
||
| 101 | * |
||
| 102 | * @since 4.3.0 |
||
| 103 | * @access protected |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | protected $has_or_relation = false; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Constructor. |
||
| 110 | * |
||
| 111 | * @since 3.2.0 |
||
| 112 | * @since 4.2.0 Introduced support for naming query clauses by associative array keys. |
||
| 113 | * |
||
| 114 | * @access public |
||
| 115 | * |
||
| 116 | * @param array $meta_query { |
||
|
|
|||
| 117 | * Array of meta query clauses. When first-order clauses or sub-clauses use strings as |
||
| 118 | * their array keys, they may be referenced in the 'orderby' parameter of the parent query. |
||
| 119 | * |
||
| 120 | * @type string $relation Optional. The MySQL keyword used to join |
||
| 121 | * the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'. |
||
| 122 | * @type array { |
||
| 123 | * Optional. An array of first-order clause parameters, or another fully-formed meta query. |
||
| 124 | * |
||
| 125 | * @type string $key Meta key to filter by. |
||
| 126 | * @type string $value Meta value to filter by. |
||
| 127 | * @type string $compare MySQL operator used for comparing the $value. Accepts '=', |
||
| 128 | * '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', |
||
| 129 | * 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'REGEXP', |
||
| 130 | * 'NOT REGEXP', 'RLIKE', 'EXISTS' or 'NOT EXISTS'. |
||
| 131 | * Default is 'IN' when `$value` is an array, '=' otherwise. |
||
| 132 | * @type string $type MySQL data type that the meta_value column will be CAST to for |
||
| 133 | * comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE', |
||
| 134 | * 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', or 'UNSIGNED'. |
||
| 135 | * Default is 'CHAR'. |
||
| 136 | * } |
||
| 137 | * } |
||
| 138 | */ |
||
| 139 | public function __construct( $meta_query = false ) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Ensure the 'meta_query' argument passed to the class constructor is well-formed. |
||
| 154 | * |
||
| 155 | * Eliminates empty items and ensures that a 'relation' is set. |
||
| 156 | * |
||
| 157 | * @since 4.1.0 |
||
| 158 | * @access public |
||
| 159 | * |
||
| 160 | * @param array $queries Array of query clauses. |
||
| 161 | * @return array Sanitized array of query clauses. |
||
| 162 | */ |
||
| 163 | public function sanitize_query( $queries ) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Determine whether a query clause is first-order. |
||
| 222 | * |
||
| 223 | * A first-order meta query clause is one that has either a 'key' or |
||
| 224 | * a 'value' array key. |
||
| 225 | * |
||
| 226 | * @since 4.1.0 |
||
| 227 | * @access protected |
||
| 228 | * |
||
| 229 | * @param array $query Meta query arguments. |
||
| 230 | * @return bool Whether the query clause is a first-order clause. |
||
| 231 | */ |
||
| 232 | protected function is_first_order_clause( $query ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Constructs a meta query based on 'meta_*' query vars |
||
| 238 | * |
||
| 239 | * @since 3.2.0 |
||
| 240 | * @access public |
||
| 241 | * |
||
| 242 | * @param array $qv The query variables |
||
| 243 | */ |
||
| 244 | public function parse_query_vars( $qv ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Return the appropriate alias for the given meta type if applicable. |
||
| 286 | * |
||
| 287 | * @since 3.7.0 |
||
| 288 | * @access public |
||
| 289 | * |
||
| 290 | * @param string $type MySQL type to cast meta_value. |
||
| 291 | * @return string MySQL type. |
||
| 292 | */ |
||
| 293 | public function get_cast_for_type( $type = '' ) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Generates SQL clauses to be appended to a main query. |
||
| 310 | * |
||
| 311 | * @since 3.2.0 |
||
| 312 | * @access public |
||
| 313 | * |
||
| 314 | * @param string $type Type of meta, eg 'user', 'post'. |
||
| 315 | * @param string $primary_table Database table where the object being filtered is stored (eg wp_users). |
||
| 316 | * @param string $primary_id_column ID column for the filtered object in $primary_table. |
||
| 317 | * @param object $context Optional. The main query object. |
||
| 318 | * @return false|array { |
||
| 319 | * Array containing JOIN and WHERE SQL clauses to append to the main query. |
||
| 320 | * |
||
| 321 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 322 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 323 | * } |
||
| 324 | */ |
||
| 325 | public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) { |
||
| 326 | if ( ! $meta_table = _get_meta_table( $type ) ) { |
||
| 327 | return false; |
||
| 328 | } |
||
| 329 | |||
| 330 | $this->table_aliases = array(); |
||
| 331 | |||
| 332 | $this->meta_table = $meta_table; |
||
| 333 | $this->meta_id_column = sanitize_key( $type . '_id' ); |
||
| 334 | |||
| 335 | $this->primary_table = $primary_table; |
||
| 336 | $this->primary_id_column = $primary_id_column; |
||
| 337 | |||
| 338 | $sql = $this->get_sql_clauses(); |
||
| 339 | |||
| 340 | /* |
||
| 341 | * If any JOINs are LEFT JOINs (as in the case of NOT EXISTS), then all JOINs should |
||
| 342 | * be LEFT. Otherwise posts with no metadata will be excluded from results. |
||
| 343 | */ |
||
| 344 | if ( false !== strpos( $sql['join'], 'LEFT JOIN' ) ) { |
||
| 345 | $sql['join'] = str_replace( 'INNER JOIN', 'LEFT JOIN', $sql['join'] ); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Filters the meta query's generated SQL. |
||
| 350 | * |
||
| 351 | * @since 3.1.0 |
||
| 352 | * |
||
| 353 | * @param array $clauses Array containing the query's JOIN and WHERE clauses. |
||
| 354 | * @param array $queries Array of meta queries. |
||
| 355 | * @param string $type Type of meta. |
||
| 356 | * @param string $primary_table Primary table. |
||
| 357 | * @param string $primary_id_column Primary column ID. |
||
| 358 | * @param object $context The main query object. |
||
| 359 | */ |
||
| 360 | return apply_filters_ref_array( 'get_meta_sql', array( $sql, $this->queries, $type, $primary_table, $primary_id_column, $context ) ); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Generate SQL clauses to be appended to a main query. |
||
| 365 | * |
||
| 366 | * Called by the public WP_Meta_Query::get_sql(), this method is abstracted |
||
| 367 | * out to maintain parity with the other Query classes. |
||
| 368 | * |
||
| 369 | * @since 4.1.0 |
||
| 370 | * @access protected |
||
| 371 | * |
||
| 372 | * @return array { |
||
| 373 | * Array containing JOIN and WHERE SQL clauses to append to the main query. |
||
| 374 | * |
||
| 375 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 376 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 377 | * } |
||
| 378 | */ |
||
| 379 | View Code Duplication | protected function get_sql_clauses() { |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Generate SQL clauses for a single query array. |
||
| 396 | * |
||
| 397 | * If nested subqueries are found, this method recurses the tree to |
||
| 398 | * produce the properly nested SQL. |
||
| 399 | * |
||
| 400 | * @since 4.1.0 |
||
| 401 | * @access protected |
||
| 402 | * |
||
| 403 | * @param array $query Query to parse, passed by reference. |
||
| 404 | * @param int $depth Optional. Number of tree levels deep we currently are. |
||
| 405 | * Used to calculate indentation. Default 0. |
||
| 406 | * @return array { |
||
| 407 | * Array containing JOIN and WHERE SQL clauses to append to a single query array. |
||
| 408 | * |
||
| 409 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 410 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 411 | * } |
||
| 412 | */ |
||
| 413 | View Code Duplication | protected function get_sql_for_query( &$query, $depth = 0 ) { |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Generate SQL JOIN and WHERE clauses for a first-order query clause. |
||
| 481 | * |
||
| 482 | * "First-order" means that it's an array with a 'key' or 'value'. |
||
| 483 | * |
||
| 484 | * @since 4.1.0 |
||
| 485 | * @access public |
||
| 486 | * |
||
| 487 | * @global wpdb $wpdb WordPress database abstraction object. |
||
| 488 | * |
||
| 489 | * @param array $clause Query clause, passed by reference. |
||
| 490 | * @param array $parent_query Parent query array. |
||
| 491 | * @param string $clause_key Optional. The array key used to name the clause in the original `$meta_query` |
||
| 492 | * parameters. If not provided, a key will be generated automatically. |
||
| 493 | * @return array { |
||
| 494 | * Array containing JOIN and WHERE SQL clauses to append to a first-order query. |
||
| 495 | * |
||
| 496 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 497 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 498 | * } |
||
| 499 | */ |
||
| 500 | public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Get a flattened list of sanitized meta clauses. |
||
| 658 | * |
||
| 659 | * This array should be used for clause lookup, as when the table alias and CAST type must be determined for |
||
| 660 | * a value of 'orderby' corresponding to a meta clause. |
||
| 661 | * |
||
| 662 | * @since 4.2.0 |
||
| 663 | * @access public |
||
| 664 | * |
||
| 665 | * @return array Meta clauses. |
||
| 666 | */ |
||
| 667 | public function get_clauses() { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Identify an existing table alias that is compatible with the current |
||
| 673 | * query clause. |
||
| 674 | * |
||
| 675 | * We avoid unnecessary table joins by allowing each clause to look for |
||
| 676 | * an existing table alias that is compatible with the query that it |
||
| 677 | * needs to perform. |
||
| 678 | * |
||
| 679 | * An existing alias is compatible if (a) it is a sibling of `$clause` |
||
| 680 | * (ie, it's under the scope of the same relation), and (b) the combination |
||
| 681 | * of operator and relation between the clauses allows for a shared table join. |
||
| 682 | * In the case of WP_Meta_Query, this only applies to 'IN' clauses that are |
||
| 683 | * connected by the relation 'OR'. |
||
| 684 | * |
||
| 685 | * @since 4.1.0 |
||
| 686 | * @access protected |
||
| 687 | * |
||
| 688 | * @param array $clause Query clause. |
||
| 689 | * @param array $parent_query Parent query of $clause. |
||
| 690 | * @return string|bool Table alias if found, otherwise false. |
||
| 691 | */ |
||
| 692 | protected function find_compatible_table_alias( $clause, $parent_query ) { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Checks whether the current query has any OR relations. |
||
| 740 | * |
||
| 741 | * In some cases, the presence of an OR relation somewhere in the query will require |
||
| 742 | * the use of a `DISTINCT` or `GROUP BY` keyword in the `SELECT` clause. The current |
||
| 743 | * method can be used in these cases to determine whether such a clause is necessary. |
||
| 744 | * |
||
| 745 | * @since 4.3.0 |
||
| 746 | * |
||
| 747 | * @return bool True if the query contains any `OR` relations, otherwise false. |
||
| 748 | */ |
||
| 749 | public function has_or_relation() { |
||
| 752 | } |
||
| 753 |
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.