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_Tax_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_Tax_Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class WP_Tax_Query { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Array of taxonomy queries. |
||
| 26 | * |
||
| 27 | * See WP_Tax_Query::__construct() for information on tax query arguments. |
||
| 28 | * |
||
| 29 | * @since 3.1.0 |
||
| 30 | * @access public |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | public $queries = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The relation between the queries. Can be one of 'AND' or 'OR'. |
||
| 37 | * |
||
| 38 | * @since 3.1.0 |
||
| 39 | * @access public |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | public $relation; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Standard response when the query should not return any rows. |
||
| 46 | * |
||
| 47 | * @since 3.2.0 |
||
| 48 | * |
||
| 49 | * @static |
||
| 50 | * @access private |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private static $no_results = array( 'join' => array( '' ), 'where' => array( '0 = 1' ) ); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * A flat list of table aliases used in the JOIN clauses. |
||
| 57 | * |
||
| 58 | * @since 4.1.0 |
||
| 59 | * @access protected |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $table_aliases = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Terms and taxonomies fetched by this query. |
||
| 66 | * |
||
| 67 | * We store this data in a flat array because they are referenced in a |
||
| 68 | * number of places by WP_Query. |
||
| 69 | * |
||
| 70 | * @since 4.1.0 |
||
| 71 | * @access public |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | public $queried_terms = array(); |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Database table that where the metadata's objects are stored (eg $wpdb->users). |
||
| 78 | * |
||
| 79 | * @since 4.1.0 |
||
| 80 | * @access public |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | public $primary_table; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Column in 'primary_table' that represents the ID of the object. |
||
| 87 | * |
||
| 88 | * @since 4.1.0 |
||
| 89 | * @access public |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | public $primary_id_column; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @since 4.7.0 |
||
| 96 | * @access protected |
||
| 97 | * @var wpdb |
||
| 98 | */ |
||
| 99 | protected $db; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Constructor. |
||
| 103 | * |
||
| 104 | * @since 3.1.0 |
||
| 105 | * @since 4.1.0 Added support for `$operator` 'NOT EXISTS' and 'EXISTS' values. |
||
| 106 | * @access public |
||
| 107 | * |
||
| 108 | * @param array $tax_query { |
||
| 109 | * Array of taxonomy query clauses. |
||
| 110 | * |
||
| 111 | * @type string $relation Optional. The MySQL keyword used to join |
||
| 112 | * the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'. |
||
| 113 | * @type array { |
||
| 114 | * Optional. An array of first-order clause parameters, or another fully-formed tax query. |
||
| 115 | * |
||
| 116 | * @type string $taxonomy Taxonomy being queried. Optional when field=term_taxonomy_id. |
||
| 117 | * @type string|int|array $terms Term or terms to filter by. |
||
| 118 | * @type string $field Field to match $terms against. Accepts 'term_id', 'slug', |
||
| 119 | * 'name', or 'term_taxonomy_id'. Default: 'term_id'. |
||
| 120 | * @type string $operator MySQL operator to be used with $terms in the WHERE clause. |
||
| 121 | * Accepts 'AND', 'IN', 'NOT IN', 'EXISTS', 'NOT EXISTS'. |
||
| 122 | * Default: 'IN'. |
||
| 123 | * @type bool $include_children Optional. Whether to include child terms. |
||
| 124 | * Requires a $taxonomy. Default: true. |
||
| 125 | * } |
||
| 126 | * } |
||
| 127 | */ |
||
| 128 | public function __construct( $tax_query ) { |
||
| 129 | $this->db = $GLOBALS['wpdb']; |
||
| 130 | |||
| 131 | if ( isset( $tax_query['relation'] ) ) { |
||
| 132 | $this->relation = $this->sanitize_relation( $tax_query['relation'] ); |
||
| 133 | } else { |
||
| 134 | $this->relation = 'AND'; |
||
| 135 | } |
||
| 136 | |||
| 137 | $this->queries = $this->sanitize_query( $tax_query ); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Ensure the 'tax_query' argument passed to the class constructor is well-formed. |
||
| 142 | * |
||
| 143 | * Ensures that each query-level clause has a 'relation' key, and that |
||
| 144 | * each first-order clause contains all the necessary keys from `$defaults`. |
||
| 145 | * |
||
| 146 | * @since 4.1.0 |
||
| 147 | * @access public |
||
| 148 | * |
||
| 149 | * @param array $queries Array of queries clauses. |
||
| 150 | * @return array Sanitized array of query clauses. |
||
| 151 | */ |
||
| 152 | public function sanitize_query( $queries ) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Sanitize a 'relation' operator. |
||
| 217 | * |
||
| 218 | * @since 4.1.0 |
||
| 219 | * @access public |
||
| 220 | * |
||
| 221 | * @param string $relation Raw relation key from the query argument. |
||
| 222 | * @return string Sanitized relation ('AND' or 'OR'). |
||
| 223 | */ |
||
| 224 | public function sanitize_relation( $relation ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Determine whether a clause is first-order. |
||
| 234 | * |
||
| 235 | * A "first-order" clause is one that contains any of the first-order |
||
| 236 | * clause keys ('terms', 'taxonomy', 'include_children', 'field', |
||
| 237 | * 'operator'). An empty clause also counts as a first-order clause, |
||
| 238 | * for backward compatibility. Any clause that doesn't meet this is |
||
| 239 | * determined, by process of elimination, to be a higher-order query. |
||
| 240 | * |
||
| 241 | * @since 4.1.0 |
||
| 242 | * |
||
| 243 | * @static |
||
| 244 | * @access protected |
||
| 245 | * |
||
| 246 | * @param array $query Tax query arguments. |
||
| 247 | * @return bool Whether the query clause is a first-order clause. |
||
| 248 | */ |
||
| 249 | protected static function is_first_order_clause( $query ) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Generates SQL clauses to be appended to a main query. |
||
| 255 | * |
||
| 256 | * @since 3.1.0 |
||
| 257 | * |
||
| 258 | * @static |
||
| 259 | * @access public |
||
| 260 | * |
||
| 261 | * @param string $primary_table Database table where the object being filtered is stored (eg wp_users). |
||
| 262 | * @param string $primary_id_column ID column for the filtered object in $primary_table. |
||
| 263 | * @return array { |
||
| 264 | * Array containing JOIN and WHERE SQL clauses to append to the main query. |
||
| 265 | * |
||
| 266 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 267 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 268 | * } |
||
| 269 | */ |
||
| 270 | public function get_sql( $primary_table, $primary_id_column ) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Generate SQL clauses to be appended to a main query. |
||
| 279 | * |
||
| 280 | * Called by the public WP_Tax_Query::get_sql(), this method |
||
| 281 | * is abstracted out to maintain parity with the other Query classes. |
||
| 282 | * |
||
| 283 | * @since 4.1.0 |
||
| 284 | * @access protected |
||
| 285 | * |
||
| 286 | * @return array { |
||
| 287 | * Array containing JOIN and WHERE SQL clauses to append to the main query. |
||
| 288 | * |
||
| 289 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 290 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 291 | * } |
||
| 292 | */ |
||
| 293 | View Code Duplication | protected function get_sql_clauses() { |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Generate SQL clauses for a single query array. |
||
| 310 | * |
||
| 311 | * If nested subqueries are found, this method recurses the tree to |
||
| 312 | * produce the properly nested SQL. |
||
| 313 | * |
||
| 314 | * @since 4.1.0 |
||
| 315 | * @access protected |
||
| 316 | * |
||
| 317 | * @param array $query Query to parse, passed by reference. |
||
| 318 | * @param int $depth Optional. Number of tree levels deep we currently are. |
||
| 319 | * Used to calculate indentation. Default 0. |
||
| 320 | * @return array { |
||
| 321 | * Array containing JOIN and WHERE SQL clauses to append to a single query array. |
||
| 322 | * |
||
| 323 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 324 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 325 | * } |
||
| 326 | */ |
||
| 327 | View Code Duplication | protected function get_sql_for_query( &$query, $depth = 0 ) { |
|
| 328 | $sql_chunks = array( |
||
| 329 | 'join' => array(), |
||
| 330 | 'where' => array(), |
||
| 331 | ); |
||
| 332 | |||
| 333 | $sql = array( |
||
| 334 | 'join' => '', |
||
| 335 | 'where' => '', |
||
| 336 | ); |
||
| 337 | |||
| 338 | $indent = ''; |
||
| 339 | for ( $i = 0; $i < $depth; $i++ ) { |
||
| 340 | $indent .= " "; |
||
| 341 | } |
||
| 342 | |||
| 343 | foreach ( $query as $key => &$clause ) { |
||
| 344 | if ( 'relation' === $key ) { |
||
| 345 | $relation = $query['relation']; |
||
| 346 | } elseif ( is_array( $clause ) ) { |
||
| 347 | |||
| 348 | // This is a first-order clause. |
||
| 349 | if ( $this->is_first_order_clause( $clause ) ) { |
||
| 350 | $clause_sql = $this->get_sql_for_clause( $clause, $query ); |
||
| 351 | |||
| 352 | $where_count = count( $clause_sql['where'] ); |
||
| 353 | if ( ! $where_count ) { |
||
| 354 | $sql_chunks['where'][] = ''; |
||
| 355 | } elseif ( 1 === $where_count ) { |
||
| 356 | $sql_chunks['where'][] = $clause_sql['where'][0]; |
||
| 357 | } else { |
||
| 358 | $sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )'; |
||
| 359 | } |
||
| 360 | |||
| 361 | $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] ); |
||
| 362 | // This is a subquery, so we recurse. |
||
| 363 | } else { |
||
| 364 | $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 ); |
||
| 365 | |||
| 366 | $sql_chunks['where'][] = $clause_sql['where']; |
||
| 367 | $sql_chunks['join'][] = $clause_sql['join']; |
||
| 368 | } |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | // Filter to remove empties. |
||
| 373 | $sql_chunks['join'] = array_filter( $sql_chunks['join'] ); |
||
| 374 | $sql_chunks['where'] = array_filter( $sql_chunks['where'] ); |
||
| 375 | |||
| 376 | if ( empty( $relation ) ) { |
||
| 377 | $relation = 'AND'; |
||
| 378 | } |
||
| 379 | |||
| 380 | // Filter duplicate JOIN clauses and combine into a single string. |
||
| 381 | if ( ! empty( $sql_chunks['join'] ) ) { |
||
| 382 | $sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) ); |
||
| 383 | } |
||
| 384 | |||
| 385 | // Generate a single WHERE clause with proper brackets and indentation. |
||
| 386 | if ( ! empty( $sql_chunks['where'] ) ) { |
||
| 387 | $sql['where'] = '( ' . "\n " . $indent . implode( ' ' . "\n " . $indent . $relation . ' ' . "\n " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')'; |
||
| 388 | } |
||
| 389 | |||
| 390 | return $sql; |
||
| 391 | } |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Generate SQL JOIN and WHERE clauses for a "first-order" query clause. |
||
| 395 | * |
||
| 396 | * @since 4.1.0 |
||
| 397 | * @access public |
||
| 398 | * |
||
| 399 | * @param array $clause Query clause, passed by reference. |
||
| 400 | * @param array $parent_query Parent query array. |
||
| 401 | * @return array { |
||
| 402 | * Array containing JOIN and WHERE SQL clauses to append to a first-order query. |
||
| 403 | * |
||
| 404 | * @type string $join SQL fragment to append to the main JOIN clause. |
||
| 405 | * @type string $where SQL fragment to append to the main WHERE clause. |
||
| 406 | * } |
||
| 407 | */ |
||
| 408 | public function get_sql_for_clause( &$clause, $parent_query ) { |
||
| 409 | $sql = array( |
||
| 410 | 'where' => array(), |
||
| 411 | 'join' => array(), |
||
| 412 | ); |
||
| 413 | |||
| 414 | $join = $where = ''; |
||
| 415 | |||
| 416 | $this->clean_query( $clause ); |
||
| 417 | |||
| 418 | if ( is_wp_error( $clause ) ) { |
||
| 419 | return self::$no_results; |
||
| 420 | } |
||
| 421 | |||
| 422 | $terms = $clause['terms']; |
||
| 423 | $operator = strtoupper( $clause['operator'] ); |
||
| 424 | |||
| 425 | if ( 'IN' == $operator ) { |
||
| 426 | |||
| 427 | if ( empty( $terms ) ) { |
||
| 428 | return self::$no_results; |
||
| 429 | } |
||
| 430 | |||
| 431 | $terms = implode( ',', $terms ); |
||
| 432 | |||
| 433 | /* |
||
| 434 | * Before creating another table join, see if this clause has a |
||
| 435 | * sibling with an existing join that can be shared. |
||
| 436 | */ |
||
| 437 | $alias = $this->find_compatible_table_alias( $clause, $parent_query ); |
||
| 438 | if ( false === $alias ) { |
||
| 439 | $i = count( $this->table_aliases ); |
||
| 440 | $alias = $i ? 'tt' . $i : $this->db->term_relationships; |
||
| 441 | |||
| 442 | // Store the alias as part of a flat array to build future iterators. |
||
| 443 | $this->table_aliases[] = $alias; |
||
| 444 | |||
| 445 | // Store the alias with this clause, so later siblings can use it. |
||
| 446 | $clause['alias'] = $alias; |
||
| 447 | |||
| 448 | $join .= " LEFT JOIN {$this->db->term_relationships}"; |
||
| 449 | $join .= $i ? " AS $alias" : ''; |
||
| 450 | $join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)"; |
||
| 451 | } |
||
| 452 | |||
| 453 | |||
| 454 | $where = "$alias.term_taxonomy_id $operator ($terms)"; |
||
| 455 | |||
| 456 | } elseif ( 'NOT IN' == $operator ) { |
||
| 457 | |||
| 458 | if ( empty( $terms ) ) { |
||
| 459 | return $sql; |
||
| 460 | } |
||
| 461 | |||
| 462 | $terms = implode( ',', $terms ); |
||
| 463 | |||
| 464 | $where = "$this->primary_table.$this->primary_id_column NOT IN ( |
||
| 465 | SELECT object_id |
||
| 466 | FROM {$this->db->term_relationships} |
||
| 467 | WHERE term_taxonomy_id IN ($terms) |
||
| 468 | )"; |
||
| 469 | |||
| 470 | } elseif ( 'AND' == $operator ) { |
||
| 471 | |||
| 472 | if ( empty( $terms ) ) { |
||
| 473 | return $sql; |
||
| 474 | } |
||
| 475 | |||
| 476 | $num_terms = count( $terms ); |
||
| 477 | |||
| 478 | $terms = implode( ',', $terms ); |
||
| 479 | |||
| 480 | $where = "( |
||
| 481 | SELECT COUNT(1) |
||
| 482 | FROM {$this->db->term_relationships} |
||
| 483 | WHERE term_taxonomy_id IN ($terms) |
||
| 484 | AND object_id = $this->primary_table.$this->primary_id_column |
||
| 485 | ) = $num_terms"; |
||
| 486 | |||
| 487 | } elseif ( 'NOT EXISTS' === $operator || 'EXISTS' === $operator ) { |
||
| 488 | |||
| 489 | $where = $this->db->prepare( "$operator ( |
||
| 490 | SELECT 1 |
||
| 491 | FROM {$this->db->term_relationships} |
||
| 492 | INNER JOIN {$this->db->term_taxonomy} |
||
| 493 | ON {$this->db->term_taxonomy}.term_taxonomy_id = {$this->db->term_relationships}.term_taxonomy_id |
||
| 494 | WHERE {$this->db->term_taxonomy}.taxonomy = %s |
||
| 495 | AND {$this->db->term_relationships}.object_id = $this->primary_table.$this->primary_id_column |
||
| 496 | )", $clause['taxonomy'] ); |
||
| 497 | |||
| 498 | } |
||
| 499 | |||
| 500 | $sql['join'][] = $join; |
||
| 501 | $sql['where'][] = $where; |
||
| 502 | return $sql; |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Identify an existing table alias that is compatible with the current query clause. |
||
| 507 | * |
||
| 508 | * We avoid unnecessary table joins by allowing each clause to look for |
||
| 509 | * an existing table alias that is compatible with the query that it |
||
| 510 | * needs to perform. |
||
| 511 | * |
||
| 512 | * An existing alias is compatible if (a) it is a sibling of `$clause` |
||
| 513 | * (ie, it's under the scope of the same relation), and (b) the combination |
||
| 514 | * of operator and relation between the clauses allows for a shared table |
||
| 515 | * join. In the case of WP_Tax_Query, this only applies to 'IN' |
||
| 516 | * clauses that are connected by the relation 'OR'. |
||
| 517 | * |
||
| 518 | * @since 4.1.0 |
||
| 519 | * @access protected |
||
| 520 | * |
||
| 521 | * @param array $clause Query clause. |
||
| 522 | * @param array $parent_query Parent query of $clause. |
||
| 523 | * @return string|false Table alias if found, otherwise false. |
||
| 524 | */ |
||
| 525 | protected function find_compatible_table_alias( $clause, $parent_query ) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Validates a single query. |
||
| 561 | * |
||
| 562 | * @since 3.2.0 |
||
| 563 | * @access private |
||
| 564 | * |
||
| 565 | * @param array $query The single query. Passed by reference. |
||
| 566 | */ |
||
| 567 | private function clean_query( &$query ) { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Transforms a single query, from one field to another. |
||
| 602 | * |
||
| 603 | * @since 3.2.0 |
||
| 604 | * |
||
| 605 | * @param array $query The single query. Passed by reference. |
||
| 606 | * @param string $resulting_field The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id', |
||
| 607 | * or 'term_id'. Default 'term_id'. |
||
| 608 | */ |
||
| 609 | public function transform_query( &$query, $resulting_field ) { |
||
| 666 | } |
||
| 667 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.