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 |
||
| 23 | class Query extends Param |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Suggest query or not. |
||
| 27 | * |
||
| 28 | * @var int Suggest |
||
| 29 | */ |
||
| 30 | protected $_suggest = 0; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Creates a query object. |
||
| 34 | * |
||
| 35 | * @param array|\Elastica\Query\AbstractQuery $query OPTIONAL Query object (default = null) |
||
| 36 | */ |
||
| 37 | public function __construct($query = null) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Transforms the argument to a query object. |
||
| 52 | * |
||
| 53 | * For example, an empty argument will return a \Elastica\Query with a \Elastica\Query\MatchAll. |
||
| 54 | * |
||
| 55 | * @param mixed $query |
||
| 56 | * |
||
| 57 | * @throws InvalidException For an invalid argument |
||
| 58 | * |
||
| 59 | * @return self |
||
| 60 | */ |
||
| 61 | public static function create($query) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Sets query as raw array. Will overwrite all already set arguments. |
||
| 89 | * |
||
| 90 | * @param array $query Query array |
||
| 91 | * |
||
| 92 | * @return $this |
||
| 93 | */ |
||
| 94 | public function setRawQuery(array $query) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Sets the query. |
||
| 103 | * |
||
| 104 | * @param \Elastica\Query\AbstractQuery $query Query object |
||
| 105 | * |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | public function setQuery(AbstractQuery $query) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Gets the query object. |
||
| 115 | * |
||
| 116 | * @return array|\Elastica\Query\AbstractQuery |
||
| 117 | **/ |
||
| 118 | public function getQuery() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Sets the start from which the search results should be returned. |
||
| 125 | * |
||
| 126 | * @param int $from |
||
| 127 | * |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function setFrom($from) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Sets sort arguments for the query |
||
| 137 | * Replaces existing values. |
||
| 138 | * |
||
| 139 | * @param array $sortArgs Sorting arguments |
||
| 140 | * |
||
| 141 | * @return $this |
||
| 142 | * |
||
| 143 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html |
||
| 144 | */ |
||
| 145 | public function setSort(array $sortArgs) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Adds a sort param to the query. |
||
| 152 | * |
||
| 153 | * @param mixed $sort Sort parameter |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | * |
||
| 157 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html |
||
| 158 | */ |
||
| 159 | public function addSort($sort) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Keep track of the scores when sorting results. |
||
| 166 | * |
||
| 167 | * @param bool $trackScores |
||
| 168 | * |
||
| 169 | * @return $this |
||
| 170 | * |
||
| 171 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_track_scores |
||
| 172 | */ |
||
| 173 | public function setTrackScores($trackScores = true): self |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Sets highlight arguments for the query. |
||
| 180 | * |
||
| 181 | * @param array $highlightArgs Set all highlight arguments |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | * |
||
| 185 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html |
||
| 186 | */ |
||
| 187 | public function setHighlight(array $highlightArgs) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Adds a highlight argument. |
||
| 194 | * |
||
| 195 | * @param mixed $highlight Add highlight argument |
||
| 196 | * |
||
| 197 | * @return $this |
||
| 198 | * |
||
| 199 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html |
||
| 200 | */ |
||
| 201 | public function addHighlight($highlight) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Sets maximum number of results for this query. |
||
| 208 | * |
||
| 209 | * @param int $size OPTIONAL Maximal number of results for query (default = 10) |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function setSize($size = 10) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Enables explain on the query. |
||
| 220 | * |
||
| 221 | * @param bool $explain OPTIONAL Enabled or disable explain (default = true) |
||
| 222 | * |
||
| 223 | * @return $this |
||
| 224 | * |
||
| 225 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-explain.html |
||
| 226 | */ |
||
| 227 | public function setExplain($explain = true) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Enables version on the query. |
||
| 234 | * |
||
| 235 | * @param bool $version OPTIONAL Enabled or disable version (default = true) |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | * |
||
| 239 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-version.html |
||
| 240 | */ |
||
| 241 | public function setVersion($version = true) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Sets the fields to be returned by the search |
||
| 248 | * NOTICE php will encode modified(or named keys) array into object format in json format request |
||
| 249 | * so the fields array must a sequence(list) type of array. |
||
| 250 | * |
||
| 251 | * @param array $fields Fields to be returned |
||
| 252 | * |
||
| 253 | * @return $this |
||
| 254 | * |
||
| 255 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html |
||
| 256 | */ |
||
| 257 | public function setStoredFields(array $fields) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Sets the fields not stored to be returned by the search. |
||
| 264 | * |
||
| 265 | * @param array $fieldDataFields Fields not stored to be returned |
||
| 266 | * |
||
| 267 | * @return $this |
||
| 268 | * |
||
| 269 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fielddata-fields.html |
||
| 270 | */ |
||
| 271 | public function setFieldDataFields(array $fieldDataFields) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set script fields. |
||
| 278 | * |
||
| 279 | * @param array|\Elastica\Script\ScriptFields $scriptFields Script fields |
||
| 280 | * |
||
| 281 | * @return $this |
||
| 282 | * |
||
| 283 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html |
||
| 284 | */ |
||
| 285 | public function setScriptFields($scriptFields) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Adds a Script to the query. |
||
| 296 | * |
||
| 297 | * @param string $name |
||
| 298 | * @param \Elastica\Script\AbstractScript $script Script object |
||
| 299 | * |
||
| 300 | * @return $this |
||
| 301 | */ |
||
| 302 | public function addScriptField($name, AbstractScript $script) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Adds an Aggregation to the query. |
||
| 315 | * |
||
| 316 | * @param AbstractAggregation $agg |
||
| 317 | * |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function addAggregation(AbstractAggregation $agg) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Converts all query params to an array. |
||
| 329 | * |
||
| 330 | * @return array Query array |
||
| 331 | */ |
||
| 332 | public function toArray() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Allows filtering of documents based on a minimum score. |
||
| 353 | * |
||
| 354 | * @param float $minScore Minimum score to filter documents by |
||
| 355 | * |
||
| 356 | * @throws \Elastica\Exception\InvalidException |
||
| 357 | * |
||
| 358 | * @return $this |
||
| 359 | */ |
||
| 360 | public function setMinScore($minScore) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Add a suggest term. |
||
| 371 | * |
||
| 372 | * @param \Elastica\Suggest $suggest suggestion object |
||
| 373 | * |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | public function setSuggest(Suggest $suggest) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Add a Rescore. |
||
| 387 | * |
||
| 388 | * @param mixed $rescore suggestion object |
||
| 389 | * |
||
| 390 | * @return $this |
||
| 391 | */ |
||
| 392 | public function setRescore($rescore) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Sets the _source field to be returned with every hit. |
||
| 409 | * |
||
| 410 | * @param array|bool $params Fields to be returned or false to disable source |
||
| 411 | * |
||
| 412 | * @return $this |
||
| 413 | * |
||
| 414 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html |
||
| 415 | */ |
||
| 416 | public function setSource($params) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Sets post_filter argument for the query. The filter is applied after the query has executed. |
||
| 423 | * |
||
| 424 | * @param array|\Elastica\Query\AbstractQuery $filter |
||
| 425 | * |
||
| 426 | * @return $this |
||
| 427 | * |
||
| 428 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html |
||
| 429 | */ |
||
| 430 | public function setPostFilter(AbstractQuery $filter) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param Collapse $collapse |
||
| 437 | * |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | public function setCollapse(Collapse $collapse): self |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Adds a track_total_hits argument. |
||
| 447 | * |
||
| 448 | * @param bool|int $trackTotalHits OPTIONAL Track total hits parameter (default = true) |
||
| 449 | * |
||
| 450 | * @return $this |
||
| 451 | * |
||
| 452 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-track-total-hits |
||
| 453 | */ |
||
| 454 | public function setTrackTotalHits($trackTotalHits = true) |
||
| 458 | } |
||
| 459 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: