Complex classes like Jetpack_WPES_Query_Builder 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 Jetpack_WPES_Query_Builder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Jetpack_WPES_Query_Builder { |
||
| 26 | |||
| 27 | protected $es_filters = array(); |
||
| 28 | |||
| 29 | // Custom boosting with function_score |
||
| 30 | protected $functions = array(); |
||
| 31 | protected $weighting_functions = array(); |
||
| 32 | protected $decays = array(); |
||
| 33 | protected $scripts = array(); |
||
| 34 | protected $functions_max_boost = 2.0; |
||
| 35 | protected $functions_score_mode = 'multiply'; |
||
| 36 | protected $functions_boost_mode = 'multiply'; |
||
| 37 | protected $query_bool_boost = null; |
||
| 38 | |||
| 39 | // General aggregations for buckets and metrics |
||
| 40 | protected $aggs_query = false; |
||
| 41 | protected $aggs = array(); |
||
| 42 | |||
| 43 | // The set of top level text queries to combine |
||
| 44 | protected $must_queries = array(); |
||
| 45 | protected $should_queries = array(); |
||
| 46 | protected $dis_max_queries = array(); |
||
| 47 | |||
| 48 | protected $diverse_buckets_query = false; |
||
| 49 | protected $bucket_filters = array(); |
||
| 50 | protected $bucket_sub_aggs = array(); |
||
| 51 | |||
| 52 | //////////////////////////////////// |
||
| 53 | // Methods for building a query |
||
| 54 | |||
| 55 | public function add_filter( $filter ) { |
||
| 60 | |||
| 61 | public function add_query( $query, $type = 'must' ) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Add any weighting function to the query |
||
| 82 | * |
||
| 83 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
||
| 84 | * |
||
| 85 | * @param $function array A function structure to apply to the query |
||
| 86 | * |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | public function add_weighting_function( $function ) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Add a scoring function to the query |
||
| 97 | * |
||
| 98 | * NOTE: For decays (linear, exp, or gauss), use Jetpack_WPES_Query_Builder::add_decay() instead |
||
| 99 | * |
||
| 100 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
||
| 101 | * |
||
| 102 | * @param $function string name of the function |
||
| 103 | * @param $params array functions parameters |
||
| 104 | * |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | public function add_function( $function, $params ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Add a decay function to score results |
||
| 115 | * |
||
| 116 | * This method should be used instead of Jetpack_WPES_Query_Builder::add_function() for decays, as the internal ES structure |
||
| 117 | * is slightly different for them. |
||
| 118 | * |
||
| 119 | * @see https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html |
||
| 120 | * |
||
| 121 | * @param $function string name of the decay function - linear, exp, or gauss |
||
| 122 | * @param $params array The decay functions parameters, passed to ES directly |
||
| 123 | * |
||
| 124 | * @return void |
||
| 125 | */ |
||
| 126 | public function add_decay( $function, $params ) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Add a scoring mode to the query |
||
| 134 | * |
||
| 135 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
||
| 136 | * |
||
| 137 | * @param $mode string name of how to score |
||
| 138 | * |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | public function add_score_mode_to_functions( $mode='multiply' ) { |
||
| 146 | |||
| 147 | public function add_boost_mode_to_functions( $mode='multiply' ) { |
||
| 152 | |||
| 153 | public function add_max_boost_to_functions( $boost ) { |
||
| 158 | |||
| 159 | public function add_boost_to_query_bool( $boost ) { |
||
| 164 | |||
| 165 | public function add_aggs( $aggs_name, $aggs ) { |
||
| 171 | |||
| 172 | public function add_aggs_sub_aggs( $aggs_name, $sub_aggs ) { |
||
| 180 | |||
| 181 | public function add_bucketed_query( $name, $query ) { |
||
| 188 | |||
| 189 | public function add_bucketed_terms( $name, $field, $terms, $boost = 1 ) { |
||
| 213 | |||
| 214 | public function add_bucket_sub_aggs( $agg ) { |
||
| 219 | |||
| 220 | protected function _add_bucket_filter( $name, $filter ) { |
||
| 224 | |||
| 225 | //////////////////////////////////// |
||
| 226 | // Building Final Query |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Combine all the queries, functions, decays, scripts, and max_boost into an ES query |
||
| 230 | * |
||
| 231 | * @return array Array representation of the built ES query |
||
| 232 | */ |
||
| 233 | public function build_query() { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Assemble the 'filter' portion of an ES query, from all registered filters |
||
| 332 | * |
||
| 333 | * @return array|null Combined ES filters, or null if none have been defined |
||
| 334 | */ |
||
| 335 | public function build_filter() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Assemble the 'aggregation' portion of an ES query, from all general aggregations. |
||
| 351 | * |
||
| 352 | * @return array An aggregation query as an array of topics, filters, and bucket names |
||
| 353 | */ |
||
| 354 | public function build_aggregation() { |
||
| 385 | |||
| 386 | } |
||
| 387 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.