Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 59 | class Query implements DSL |
||
| 60 | { |
||
| 61 | /** |
||
| 62 | * must return type for QueryBuilder usage. |
||
| 63 | */ |
||
| 64 | public function getType(): string |
||
| 65 | { |
||
| 66 | return self::TYPE_QUERY; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * match query. |
||
| 71 | * |
||
| 72 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html |
||
| 73 | * |
||
| 74 | * @param mixed $values |
||
| 75 | */ |
||
| 76 | public function match(?string $field = null, $values = null): Match |
||
| 77 | { |
||
| 78 | return new Match($field, $values); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * multi match query. |
||
| 83 | * |
||
| 84 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html |
||
| 85 | */ |
||
| 86 | public function multi_match(): MultiMatch |
||
| 90 | |||
| 91 | /** |
||
| 92 | * bool query. |
||
| 93 | * |
||
| 94 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html |
||
| 95 | */ |
||
| 96 | public function bool(): BoolQuery |
||
| 97 | { |
||
| 98 | return new BoolQuery(); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * boosting query. |
||
| 103 | * |
||
| 104 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html |
||
| 105 | */ |
||
| 106 | public function boosting(): Boosting |
||
| 107 | { |
||
| 108 | return new Boosting(); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * common terms query. |
||
| 113 | * |
||
| 114 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-common-terms-query.html |
||
| 115 | * |
||
| 116 | * @param float $cutoffFrequency percentage in decimal form (.001 == 0.1%) |
||
| 117 | */ |
||
| 118 | public function common_terms(string $field, string $query, float $cutoffFrequency): Common |
||
| 119 | { |
||
| 120 | return new Common($field, $query, $cutoffFrequency); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * constant score query. |
||
| 125 | * |
||
| 126 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html |
||
| 127 | */ |
||
| 128 | public function constant_score(?AbstractQuery $filter = null): ConstantScore |
||
| 129 | { |
||
| 130 | return new ConstantScore($filter); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * dis max query. |
||
| 135 | * |
||
| 136 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-dis-max-query.html |
||
| 137 | */ |
||
| 138 | public function dis_max(): DisMax |
||
| 139 | { |
||
| 140 | return new DisMax(); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * distance feature query. |
||
| 145 | * |
||
| 146 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-distance-feature-query.html |
||
| 147 | * |
||
| 148 | * @param string|array $origin |
||
| 149 | */ |
||
| 150 | public function distance_feature(string $field, $origin, string $pivot): DistanceFeature |
||
| 151 | { |
||
| 152 | return new DistanceFeature($field, $origin, $pivot); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * function score query. |
||
| 157 | * |
||
| 158 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html |
||
| 159 | */ |
||
| 160 | public function function_score(): FunctionScore |
||
| 161 | { |
||
| 162 | return new FunctionScore(); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * fuzzy query. |
||
| 167 | * |
||
| 168 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html |
||
| 169 | * |
||
| 170 | * @param string $value String to search for |
||
| 171 | */ |
||
| 172 | public function fuzzy(?string $fieldName = null, ?string $value = null): Fuzzy |
||
| 173 | { |
||
| 174 | return new Fuzzy($fieldName, $value); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * geo bounding box query. |
||
| 179 | * |
||
| 180 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html |
||
| 181 | */ |
||
| 182 | public function geo_bounding_box(string $key, array $coordinates): GeoBoundingBox |
||
| 183 | { |
||
| 184 | return new GeoBoundingBox($key, $coordinates); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * geo distance query. |
||
| 189 | * |
||
| 190 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html |
||
| 191 | * |
||
| 192 | * @param array|string $location |
||
| 193 | */ |
||
| 194 | public function geo_distance(string $key, $location, string $distance): GeoDistance |
||
| 195 | { |
||
| 196 | return new GeoDistance($key, $location, $distance); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * geo polygon query. |
||
| 201 | * |
||
| 202 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-polygon-query.html |
||
| 203 | */ |
||
| 204 | public function geo_polygon(string $key, array $points): GeoPolygon |
||
| 205 | { |
||
| 206 | return new GeoPolygon($key, $points); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * geo shape query. |
||
| 211 | * |
||
| 212 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html |
||
| 213 | */ |
||
| 214 | public function geo_shape() |
||
| 215 | { |
||
| 216 | throw new NotImplementedException(); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * has child query. |
||
| 221 | * |
||
| 222 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html |
||
| 223 | * |
||
| 224 | * @param string|BaseQuery|AbstractQuery $query |
||
| 225 | * @param string $type Parent document type |
||
| 226 | */ |
||
| 227 | public function has_child($query, ?string $type = null): HasChild |
||
| 228 | { |
||
| 229 | return new HasChild($query, $type); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * has parent query. |
||
| 234 | * |
||
| 235 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html |
||
| 236 | * |
||
| 237 | * @param string|BaseQuery|AbstractQuery $query |
||
| 238 | * @param string $type Parent document type |
||
| 239 | */ |
||
| 240 | public function has_parent($query, string $type): HasParent |
||
| 241 | { |
||
| 242 | return new HasParent($query, $type); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * ids query. |
||
| 247 | * |
||
| 248 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html |
||
| 249 | */ |
||
| 250 | public function ids(array $ids = []): Ids |
||
| 251 | { |
||
| 252 | return new Ids($ids); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * match all query. |
||
| 257 | * |
||
| 258 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html |
||
| 259 | */ |
||
| 260 | public function match_all(): MatchAll |
||
| 261 | { |
||
| 262 | return new MatchAll(); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * match none query. |
||
| 267 | * |
||
| 268 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html#query-dsl-match-none-query |
||
| 269 | */ |
||
| 270 | public function match_none(): MatchNone |
||
| 271 | { |
||
| 272 | return new MatchNone(); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * match phrase query. |
||
| 277 | * |
||
| 278 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html |
||
| 279 | */ |
||
| 280 | public function match_phrase(?string $field = null, $values = null): MatchPhrase |
||
| 281 | { |
||
| 282 | return new MatchPhrase($field, $values); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * match phrase prefix query. |
||
| 287 | * |
||
| 288 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html |
||
| 289 | */ |
||
| 290 | public function match_phrase_prefix(?string $field = null, $values = null): MatchPhrasePrefix |
||
| 291 | { |
||
| 292 | return new MatchPhrasePrefix($field, $values); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * more like this query. |
||
| 297 | * |
||
| 298 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html |
||
| 299 | */ |
||
| 300 | public function more_like_this(): MoreLikeThis |
||
| 301 | { |
||
| 302 | return new MoreLikeThis(); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * nested query. |
||
| 307 | * |
||
| 308 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html |
||
| 309 | */ |
||
| 310 | public function nested(): Nested |
||
| 311 | { |
||
| 312 | return new Nested(); |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param int|string $id |
||
| 317 | */ |
||
| 318 | public function parent_id(string $type, $id, bool $ignoreUnmapped = false): ParentId |
||
| 319 | { |
||
| 320 | return new ParentId($type, $id, $ignoreUnmapped); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * prefix query. |
||
| 325 | * |
||
| 326 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-prefix-query.html |
||
| 327 | * |
||
| 328 | * @param array $prefix Prefix array |
||
| 329 | */ |
||
| 330 | public function prefix(array $prefix = []): Prefix |
||
| 331 | { |
||
| 332 | return new Prefix($prefix); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * query string query. |
||
| 337 | * |
||
| 338 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html |
||
| 339 | * |
||
| 340 | * @param string $queryString OPTIONAL Query string for object |
||
| 341 | */ |
||
| 342 | public function query_string(string $queryString = ''): QueryString |
||
| 343 | { |
||
| 344 | return new QueryString($queryString); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * simple_query_string query. |
||
| 349 | * |
||
| 350 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html |
||
| 351 | */ |
||
| 352 | public function simple_query_string(string $query, array $fields = []): SimpleQueryString |
||
| 353 | { |
||
| 354 | return new SimpleQueryString($query, $fields); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * range query. |
||
| 359 | * |
||
| 360 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html |
||
| 361 | */ |
||
| 362 | public function range(?string $fieldName = null, array $args = []): Range |
||
| 363 | { |
||
| 364 | return new Range($fieldName, $args); |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * regexp query. |
||
| 369 | * |
||
| 370 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html |
||
| 371 | */ |
||
| 372 | public function regexp(string $key = '', ?string $value = null, float $boost = 1.0): Regexp |
||
| 373 | { |
||
| 374 | return new Regexp($key, $value, $boost); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * span first query. |
||
| 379 | * |
||
| 380 | * @param AbstractQuery|array $match |
||
| 381 | * |
||
| 382 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html |
||
| 383 | */ |
||
| 384 | public function span_first($match = null, ?int $end = null): SpanFirst |
||
| 385 | { |
||
| 386 | return new SpanFirst($match, $end); |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * span multi term query. |
||
| 391 | * |
||
| 392 | * @param AbstractQuery|array $match |
||
| 393 | * |
||
| 394 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-multi-term-query.html |
||
| 395 | */ |
||
| 396 | public function span_multi_term($match = null): SpanMulti |
||
| 400 | |||
| 401 | /** |
||
| 402 | * span near query. |
||
| 403 | * |
||
| 404 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html |
||
| 405 | */ |
||
| 406 | public function span_near(array $clauses = [], int $slop = 1, bool $inOrder = false): SpanNear |
||
| 407 | { |
||
| 408 | return new SpanNear($clauses, $slop, $inOrder); |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * span not query. |
||
| 413 | * |
||
| 414 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-not-query.html |
||
| 415 | */ |
||
| 416 | public function span_not(?AbstractSpanQuery $include = null, ?AbstractSpanQuery $exclude = null): SpanNot |
||
| 417 | { |
||
| 418 | return new SpanNot($include, $exclude); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * span_or query. |
||
| 423 | * |
||
| 424 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-or-query.html |
||
| 425 | */ |
||
| 426 | public function span_or(array $clauses = []): SpanOr |
||
| 427 | { |
||
| 428 | return new SpanOr($clauses); |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * span_term query. |
||
| 433 | * |
||
| 434 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-term-query.html |
||
| 435 | */ |
||
| 436 | public function span_term(array $term = []): SpanTerm |
||
| 440 | |||
| 441 | /** |
||
| 442 | * span_containing query. |
||
| 443 | * |
||
| 444 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-containing-query.html |
||
| 445 | */ |
||
| 446 | public function span_containing(?AbstractSpanQuery $little = null, ?AbstractSpanQuery $big = null): SpanContaining |
||
| 450 | |||
| 451 | /** |
||
| 452 | * span_within query. |
||
| 453 | * |
||
| 454 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-within-query.html |
||
| 455 | */ |
||
| 456 | public function span_within(?AbstractSpanQuery $little = null, ?AbstractSpanQuery $big = null): SpanWithin |
||
| 460 | |||
| 461 | /** |
||
| 462 | * term query. |
||
| 463 | * |
||
| 464 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html |
||
| 465 | */ |
||
| 466 | public function term(array $term = []): Term |
||
| 470 | |||
| 471 | /** |
||
| 472 | * terms query. |
||
| 473 | * |
||
| 474 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html |
||
| 475 | */ |
||
| 476 | public function terms(string $key = '', array $terms = []): Terms |
||
| 480 | |||
| 481 | /** |
||
| 482 | * wildcard query. |
||
| 483 | * |
||
| 484 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html |
||
| 485 | * |
||
| 486 | * @param string $key OPTIONAL Wildcard key |
||
| 487 | * @param string|null $value OPTIONAL Wildcard value |
||
| 488 | * @param float $boost OPTIONAL Boost value (default = 1) |
||
| 489 | */ |
||
| 490 | public function wildcard(string $key = '', ?string $value = null, float $boost = 1.0): Wildcard |
||
| 494 | |||
| 495 | /** |
||
| 496 | * exists query. |
||
| 497 | * |
||
| 498 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html |
||
| 499 | */ |
||
| 500 | public function exists(string $field): Exists |
||
| 504 | |||
| 505 | /** |
||
| 506 | * type query. |
||
| 507 | * |
||
| 508 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-percolate-query.html |
||
| 509 | */ |
||
| 510 | public function percolate(): Percolate |
||
| 514 | } |
||
| 515 |