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) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Transforms a string or an array to a query object. |
||
| 50 | * |
||
| 51 | * If query is empty, |
||
| 52 | * |
||
| 53 | * @param mixed $query |
||
| 54 | * |
||
| 55 | * @throws \Elastica\Exception\NotImplementedException |
||
| 56 | * |
||
| 57 | * @return self |
||
| 58 | */ |
||
| 59 | public static function create($query) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Sets query as raw array. Will overwrite all already set arguments. |
||
| 86 | * |
||
| 87 | * @param array $query Query array |
||
| 88 | * |
||
| 89 | * @return $this |
||
| 90 | */ |
||
| 91 | public function setRawQuery(array $query) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Sets the query. |
||
| 100 | * |
||
| 101 | * @param \Elastica\Query\AbstractQuery $query Query object |
||
| 102 | * |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function setQuery(AbstractQuery $query) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Gets the query object. |
||
| 112 | * |
||
| 113 | * @return \Elastica\Query\AbstractQuery |
||
| 114 | **/ |
||
| 115 | public function getQuery() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Sets the start from which the search results should be returned. |
||
| 122 | * |
||
| 123 | * @param int $from |
||
| 124 | * |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | public function setFrom($from) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Sets sort arguments for the query |
||
| 134 | * Replaces existing values. |
||
| 135 | * |
||
| 136 | * @param array $sortArgs Sorting arguments |
||
| 137 | * |
||
| 138 | * @return $this |
||
| 139 | * |
||
| 140 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html |
||
| 141 | */ |
||
| 142 | public function setSort(array $sortArgs) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Adds a sort param to the query. |
||
| 149 | * |
||
| 150 | * @param mixed $sort Sort parameter |
||
| 151 | * |
||
| 152 | * @return $this |
||
| 153 | * |
||
| 154 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html |
||
| 155 | */ |
||
| 156 | public function addSort($sort) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Keep track of the scores when sorting results. |
||
| 163 | * |
||
| 164 | * @param bool $trackScores |
||
| 165 | * |
||
| 166 | * @return $this |
||
| 167 | * |
||
| 168 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_track_scores |
||
| 169 | */ |
||
| 170 | public function setTrackScores($trackScores = true) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Sets highlight arguments for the query. |
||
| 177 | * |
||
| 178 | * @param array $highlightArgs Set all highlight arguments |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | * |
||
| 182 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html |
||
| 183 | */ |
||
| 184 | public function setHighlight(array $highlightArgs) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Adds a highlight argument. |
||
| 191 | * |
||
| 192 | * @param mixed $highlight Add highlight argument |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | * |
||
| 196 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-highlighting.html |
||
| 197 | */ |
||
| 198 | public function addHighlight($highlight) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Sets maximum number of results for this query. |
||
| 205 | * |
||
| 206 | * @param int $size OPTIONAL Maximal number of results for query (default = 10) |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function setSize($size = 10) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Enables explain on the query. |
||
| 217 | * |
||
| 218 | * @param bool $explain OPTIONAL Enabled or disable explain (default = true) |
||
| 219 | * |
||
| 220 | * @return $this |
||
| 221 | * |
||
| 222 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-explain.html |
||
| 223 | */ |
||
| 224 | public function setExplain($explain = true) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Enables version on the query. |
||
| 231 | * |
||
| 232 | * @param bool $version OPTIONAL Enabled or disable version (default = true) |
||
| 233 | * |
||
| 234 | * @return $this |
||
| 235 | * |
||
| 236 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-version.html |
||
| 237 | */ |
||
| 238 | public function setVersion($version = true) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Sets the fields to be returned by the search |
||
| 245 | * NOTICE php will encode modified(or named keys) array into object format in json format request |
||
| 246 | * so the fields array must a sequence(list) type of array. |
||
| 247 | * |
||
| 248 | * @param array $fields Fields to be returned |
||
| 249 | * |
||
| 250 | * @return $this |
||
| 251 | * |
||
| 252 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html |
||
| 253 | */ |
||
| 254 | public function setStoredFields(array $fields) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Sets the fields not stored to be returned by the search. |
||
| 261 | * |
||
| 262 | * @param array $fieldDataFields Fields not stored to be returned |
||
| 263 | * |
||
| 264 | * @return $this |
||
| 265 | * |
||
| 266 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fielddata-fields.html |
||
| 267 | */ |
||
| 268 | public function setFieldDataFields(array $fieldDataFields) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Set script fields. |
||
| 275 | * |
||
| 276 | * @param array|\Elastica\Script\ScriptFields $scriptFields Script fields |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | * |
||
| 280 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-script-fields.html |
||
| 281 | */ |
||
| 282 | public function setScriptFields($scriptFields) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Adds a Script to the query. |
||
| 293 | * |
||
| 294 | * @param string $name |
||
| 295 | * @param \Elastica\Script\AbstractScript $script Script object |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function addScriptField($name, AbstractScript $script) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Adds an Aggregation to the query. |
||
| 312 | * |
||
| 313 | * @param AbstractAggregation $agg |
||
| 314 | * |
||
| 315 | * @return $this |
||
| 316 | */ |
||
| 317 | public function addAggregation(AbstractAggregation $agg) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Converts all query params to an array. |
||
| 326 | * |
||
| 327 | * @return array Query array |
||
| 328 | */ |
||
| 329 | public function toArray() |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Allows filtering of documents based on a minimum score. |
||
| 350 | * |
||
| 351 | * @param float $minScore Minimum score to filter documents by |
||
| 352 | * |
||
| 353 | * @throws \Elastica\Exception\InvalidException |
||
| 354 | * |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | public function setMinScore($minScore) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Add a suggest term. |
||
| 368 | * |
||
| 369 | * @param \Elastica\Suggest $suggest suggestion object |
||
| 370 | * |
||
| 371 | * @return $this |
||
| 372 | */ |
||
| 373 | public function setSuggest(Suggest $suggest) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Add a Rescore. |
||
| 384 | * |
||
| 385 | * @param mixed $rescore suggestion object |
||
| 386 | * |
||
| 387 | * @return $this |
||
| 388 | */ |
||
| 389 | public function setRescore($rescore) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Sets the _source field to be returned with every hit. |
||
| 406 | * |
||
| 407 | * @param array|bool $params Fields to be returned or false to disable source |
||
| 408 | * |
||
| 409 | * @return $this |
||
| 410 | * |
||
| 411 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html |
||
| 412 | */ |
||
| 413 | public function setSource($params) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Sets post_filter argument for the query. The filter is applied after the query has executed. |
||
| 420 | * |
||
| 421 | * @param array|\Elastica\Query\AbstractQuery $filter |
||
| 422 | * |
||
| 423 | * @return $this |
||
| 424 | * |
||
| 425 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html |
||
| 426 | */ |
||
| 427 | public function setPostFilter(AbstractQuery $filter) |
||
| 431 | } |
||
| 432 |
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: