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 | protected $chunk = 0; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @param Search|array|null $query |
||
| 16 | */ |
||
| 17 | public function __construct($query = null) |
||
| 24 | 7 | ||
| 25 | 7 | protected function getMatchAllQuery() |
|
| 29 | |||
| 30 | public function from(int $from): QueryBuilder |
||
| 35 | |||
| 36 | public function size(int $size): QueryBuilder |
||
| 41 | 2 | ||
| 42 | public function hasSort(): bool |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param array|string $sort |
||
| 49 | * @return $this |
||
| 50 | */ |
||
| 51 | public function sort($sort) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param Filter|array $filter |
||
| 59 | * @param string $mode |
||
| 60 | * @return $this |
||
| 61 | */ |
||
| 62 | public function filter($filter = [], $mode = Filter::MODE_INCLUDE) |
||
| 85 | 5 | ||
| 86 | /** |
||
| 87 | 5 | * Set _source to search query. |
|
| 88 | * |
||
| 89 | 5 | * @param mixed $fields |
|
| 90 | * @return $this |
||
| 91 | 5 | */ |
|
| 92 | public function fields($fields = false) |
||
| 103 | 1 | ||
| 104 | 1 | /** |
|
| 105 | * Return only _id. |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | 1 | public function getOnlyIds() |
|
| 113 | |||
| 114 | public function build(): array |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | public function __toString() |
||
| 126 | |||
| 127 | 7 | /** |
|
| 128 | * @param int $options |
||
| 129 | 7 | * @return string |
|
| 130 | */ |
||
| 131 | public function toJson($options = 0) |
||
| 135 | |||
| 136 | protected function merge(array $query, $mode = 'must') |
||
| 146 | |||
| 147 | public function aggregation(Aggregation $aggregation) |
||
| 152 | |||
| 153 | public function where($field, $value) |
||
| 159 | 5 | ||
| 160 | public function notWhere($field, $value) |
||
| 166 | |||
| 167 | 5 | public function orWhere($field, $value) |
|
| 173 | 1 | ||
| 174 | public function between($field, $start, $end) |
||
| 180 | |||
| 181 | public function betweenOrEquals($field, $start, $end) |
||
| 187 | |||
| 188 | public function range($field, $start, $end) |
||
| 192 | |||
| 193 | public function greaterThan($field, $start) |
||
| 199 | |||
| 200 | public function gt($field, $start) |
||
| 204 | 1 | ||
| 205 | public function greaterThanOrEquals($field, $start) |
||
| 211 | |||
| 212 | public function gte($field, $start) |
||
| 216 | 1 | ||
| 217 | public function lessThan($field, $end) |
||
| 223 | |||
| 224 | public function lt($field, $start) |
||
| 228 | |||
| 229 | public function lessThanOrEquals($field, $end) |
||
| 235 | |||
| 236 | public function lte($field, $start) |
||
| 240 | |||
| 241 | public function match($field, $value) |
||
| 247 | |||
| 248 | public function notMatch($field, $value) |
||
| 254 | |||
| 255 | public function orMatch($field, $value) |
||
| 261 | |||
| 262 | public function columns(array $columns): IQueryBuilder |
||
| 267 | 1 | ||
| 268 | 1 | public function noColumns(): IQueryBuilder |
|
| 273 | |||
| 274 | public function chunk(int $count = null): IQueryBuilder |
||
| 281 | } |
||
| 282 |