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 |
||
24 | class Jetpack_WPES_Query_Builder { |
||
25 | |||
26 | public $es_filters = array(); |
||
27 | |||
28 | // Custom boosting with function_score |
||
29 | public $functions = array(); |
||
30 | public $decays = array(); |
||
31 | public $scripts = array(); |
||
32 | public $functions_max_boost = 2.0; |
||
33 | public $functions_score_mode = 'multiply'; |
||
34 | public $query_bool_boost = null; |
||
35 | |||
36 | // General aggregations for buckets and metrics |
||
37 | public $aggs_query = false; |
||
38 | public $aggs = array(); |
||
39 | |||
40 | // The set of top level text queries to combine |
||
41 | public $must_queries = array(); |
||
42 | public $should_queries = array(); |
||
43 | public $dis_max_queries = array(); |
||
44 | |||
45 | public $diverse_buckets_query = false; |
||
46 | public $bucket_filters = array(); |
||
47 | public $bucket_sub_aggs = array(); |
||
48 | |||
49 | //////////////////////////////////// |
||
50 | // Methods for building a query |
||
51 | |||
52 | public function add_filter( $filter ) { |
||
55 | |||
56 | public function add_query( $query, $type = 'must' ) { |
||
72 | |||
73 | /** |
||
74 | * Add a scoring function to the query |
||
75 | * |
||
76 | * NOTE: For decays (linear, exp, or gauss), use Jetpack_WPES_Query_Builder::add_decay() instead |
||
77 | * |
||
78 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
||
79 | * |
||
80 | * @param $function string name of the function |
||
81 | * @param $params array functions parameters |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | public function add_function( $function, $params ) { |
||
88 | |||
89 | /** |
||
90 | * Add a decay function to score results |
||
91 | * |
||
92 | * This method should be used instead of Jetpack_WPES_Query_Builder::add_function() for decays, as the internal ES structure |
||
93 | * is slightly different for them. |
||
94 | * |
||
95 | * @see https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html |
||
96 | * |
||
97 | * @param $function string name of the decay function - linear, exp, or gauss |
||
98 | * @param $params array The decay functions parameters, passed to ES directly |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public function add_decay( $function, $params ) { |
||
105 | |||
106 | /** |
||
107 | * Add a scoring mode to the query |
||
108 | * |
||
109 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
||
110 | * |
||
111 | * @param $mode string name of how to score |
||
112 | * |
||
113 | * @return void |
||
114 | */ |
||
115 | public function add_score_mode_to_functions( $mode='multiply' ) { |
||
118 | |||
119 | public function add_max_boost_to_functions( $boost ) { |
||
122 | |||
123 | public function add_boost_to_query_bool( $boost ) { |
||
126 | |||
127 | public function add_aggs( $aggs_name, $aggs ) { |
||
131 | |||
132 | public function add_aggs_sub_aggs( $aggs_name, $sub_aggs ) { |
||
138 | |||
139 | public function add_bucketed_query( $name, $query ) { |
||
144 | |||
145 | public function add_bucketed_terms( $name, $field, $terms, $boost = 1 ) { |
||
167 | |||
168 | public function add_bucket_sub_aggs( $agg ) { |
||
171 | |||
172 | public function _add_bucket_filter( $name, $filter ) { |
||
176 | |||
177 | //////////////////////////////////// |
||
178 | // Building Final Query |
||
179 | |||
180 | /** |
||
181 | * Combine all the queries, functions, decays, scripts, and max_boost into an ES query |
||
182 | * |
||
183 | * @return array Array representation of the built ES query |
||
184 | */ |
||
185 | public function build_query() { |
||
284 | |||
285 | /** |
||
286 | * Assemble the 'filter' portion of an ES query, from all registered filters |
||
287 | * |
||
288 | * @return array|null Combiled ES filters, or null if none have been defined |
||
289 | */ |
||
290 | public function build_filter() { |
||
303 | |||
304 | /** |
||
305 | * Assemble the 'aggregation' portion of an ES query, from all general aggregations. |
||
306 | * |
||
307 | * @return an aggregation query as an array of topics, filters, and bucket names |
||
308 | */ |
||
309 | function build_aggregation() { |
||
340 | |||
341 | } |
||
342 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.