Complex classes like QueryBuilder 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 QueryBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class QueryBuilder implements IQueryBuilder |
||
10 | { |
||
11 | protected $query = []; |
||
12 | |||
13 | /** |
||
14 | * @param Search|array|null $query |
||
15 | */ |
||
16 | public function __construct($query = null) |
||
23 | |||
24 | 7 | protected function getMatchAllQuery() |
|
28 | |||
29 | public function from(int $from): QueryBuilder |
||
34 | 1 | ||
35 | public function size(int $size): QueryBuilder |
||
40 | |||
41 | 2 | public function hasSort(): bool |
|
45 | |||
46 | /** |
||
47 | * @param array|string $sort |
||
48 | * @return $this |
||
49 | */ |
||
50 | public function sort($sort) |
||
55 | |||
56 | /** |
||
57 | * @param Filter|array $filter |
||
58 | * @param string $mode |
||
59 | * @return $this |
||
60 | */ |
||
61 | public function filter($filter = [], $mode = Filter::MODE_INCLUDE) |
||
84 | |||
85 | 5 | /** |
|
86 | * Set _source to search query. |
||
87 | 5 | * |
|
88 | * @param mixed $fields |
||
89 | 5 | * @return $this |
|
90 | */ |
||
91 | 5 | public function fields($fields = false) |
|
102 | 1 | ||
103 | 1 | /** |
|
104 | 1 | * Return only _id. |
|
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function getOnlyIds() |
||
112 | |||
113 | public function build(): array |
||
117 | |||
118 | /** |
||
119 | * @return string |
||
120 | */ |
||
121 | public function __toString() |
||
125 | |||
126 | /** |
||
127 | 7 | * @param int $options |
|
128 | * @return string |
||
129 | 7 | */ |
|
130 | public function toJson($options = 0) |
||
134 | |||
135 | protected function merge(array $query, $mode = 'must') |
||
145 | |||
146 | public function aggregation(Aggregation $aggregation) |
||
151 | |||
152 | public function where($field, $value) |
||
158 | |||
159 | 5 | public function notWhere($field, $value) |
|
165 | 5 | ||
166 | public function orWhere($field, $value) |
||
172 | 1 | ||
173 | 1 | public function between($field, $start, $end) |
|
179 | |||
180 | public function betweenOrEquals($field, $start, $end) |
||
186 | |||
187 | public function range($field, $start, $end) |
||
191 | |||
192 | public function greaterThan($field, $start) |
||
198 | |||
199 | public function gt($field, $start) |
||
203 | |||
204 | 1 | public function greaterThanOrEquals($field, $start) |
|
210 | |||
211 | public function gte($field, $start) |
||
215 | |||
216 | 1 | public function lessThan($field, $end) |
|
222 | |||
223 | public function lt($field, $start) |
||
227 | |||
228 | public function lessThanOrEquals($field, $end) |
||
234 | |||
235 | public function lte($field, $start) |
||
239 | |||
240 | public function match($field, $value) |
||
246 | |||
247 | public function notMatch($field, $value) |
||
253 | |||
254 | public function orMatch($field, $value) |
||
260 | |||
261 | public function columns(array $columns): IQueryBuilder |
||
266 | 1 | ||
267 | 1 | public function noColumns(): IQueryBuilder |
|
272 | } |
||
273 |