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 |
||
26 | class Jetpack_WPES_Query_Builder { |
||
27 | |||
28 | protected $es_filters = array(); |
||
29 | |||
30 | // Custom boosting with function_score |
||
31 | protected $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 $query_bool_boost = null; |
||
37 | |||
38 | // General aggregations for buckets and metrics |
||
39 | protected $aggs_query = false; |
||
40 | protected $aggs = array(); |
||
41 | |||
42 | // The set of top level text queries to combine |
||
43 | protected $must_queries = array(); |
||
44 | protected $should_queries = array(); |
||
45 | protected $dis_max_queries = array(); |
||
46 | |||
47 | protected $diverse_buckets_query = false; |
||
48 | protected $bucket_filters = array(); |
||
49 | protected $bucket_sub_aggs = array(); |
||
50 | |||
51 | //////////////////////////////////// |
||
52 | // Methods for building a query |
||
53 | |||
54 | public function add_filter( $filter ) { |
||
59 | |||
60 | public function add_query( $query, $type = 'must' ) { |
||
78 | |||
79 | /** |
||
80 | * Add any weighting function to the query |
||
81 | * |
||
82 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
||
83 | * |
||
84 | * @param $function array A function structure to apply to the query |
||
85 | * |
||
86 | * @return void |
||
87 | */ |
||
88 | public function add_weighting_function( $function ) { |
||
93 | |||
94 | /** |
||
95 | * Add a scoring function to the query |
||
96 | * |
||
97 | * NOTE: For decays (linear, exp, or gauss), use Jetpack_WPES_Query_Builder::add_decay() instead |
||
98 | * |
||
99 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
||
100 | * |
||
101 | * @param $function string name of the function |
||
102 | * @param $params array functions parameters |
||
103 | * |
||
104 | * @return void |
||
105 | */ |
||
106 | public function add_function( $function, $params ) { |
||
111 | |||
112 | /** |
||
113 | * Add a decay function to score results |
||
114 | * |
||
115 | * This method should be used instead of Jetpack_WPES_Query_Builder::add_function() for decays, as the internal ES structure |
||
116 | * is slightly different for them. |
||
117 | * |
||
118 | * @see https://www.elastic.co/guide/en/elasticsearch/guide/current/decay-functions.html |
||
119 | * |
||
120 | * @param $function string name of the decay function - linear, exp, or gauss |
||
121 | * @param $params array The decay functions parameters, passed to ES directly |
||
122 | * |
||
123 | * @return void |
||
124 | */ |
||
125 | public function add_decay( $function, $params ) { |
||
130 | |||
131 | /** |
||
132 | * Add a scoring mode to the query |
||
133 | * |
||
134 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
||
135 | * |
||
136 | * @param $mode string name of how to score |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | public function add_score_mode_to_functions( $mode='multiply' ) { |
||
145 | |||
146 | public function add_boost_mode_to_functions( $mode='multiply' ) { |
||
151 | |||
152 | public function add_max_boost_to_functions( $boost ) { |
||
157 | |||
158 | public function add_boost_to_query_bool( $boost ) { |
||
163 | |||
164 | public function add_aggs( $aggs_name, $aggs ) { |
||
170 | |||
171 | public function add_aggs_sub_aggs( $aggs_name, $sub_aggs ) { |
||
179 | |||
180 | public function add_bucketed_query( $name, $query ) { |
||
187 | |||
188 | public function add_bucketed_terms( $name, $field, $terms, $boost = 1 ) { |
||
212 | |||
213 | public function add_bucket_sub_aggs( $agg ) { |
||
218 | |||
219 | protected function _add_bucket_filter( $name, $filter ) { |
||
225 | |||
226 | //////////////////////////////////// |
||
227 | // Building Final Query |
||
228 | |||
229 | /** |
||
230 | * Combine all the queries, functions, decays, scripts, and max_boost into an ES query |
||
231 | * |
||
232 | * @return array Array representation of the built ES query |
||
233 | */ |
||
234 | public function build_query() { |
||
342 | |||
343 | /** |
||
344 | * Assemble the 'filter' portion of an ES query, from all registered filters |
||
345 | * |
||
346 | * @return array|null Combined ES filters, or null if none have been defined |
||
347 | */ |
||
348 | public function build_filter() { |
||
361 | |||
362 | /** |
||
363 | * Assemble the 'aggregation' portion of an ES query, from all general aggregations. |
||
364 | * |
||
365 | * @return array An aggregation query as an array of topics, filters, and bucket names |
||
366 | */ |
||
367 | public function build_aggregation() { |
||
398 | |||
399 | } |
||
400 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.