Complex classes like Search 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 Search, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Search |
||
| 15 | { |
||
| 16 | /* |
||
| 17 | * Options |
||
| 18 | */ |
||
| 19 | const OPTION_SEARCH_TYPE = 'search_type'; |
||
| 20 | const OPTION_ROUTING = 'routing'; |
||
| 21 | const OPTION_PREFERENCE = 'preference'; |
||
| 22 | const OPTION_VERSION = 'version'; |
||
| 23 | const OPTION_TIMEOUT = 'timeout'; |
||
| 24 | const OPTION_FROM = 'from'; |
||
| 25 | const OPTION_SIZE = 'size'; |
||
| 26 | const OPTION_SCROLL = 'scroll'; |
||
| 27 | const OPTION_SCROLL_ID = 'scroll_id'; |
||
| 28 | const OPTION_QUERY_CACHE = 'query_cache'; |
||
| 29 | const OPTION_TERMINATE_AFTER = 'terminate_after'; |
||
| 30 | const OPTION_SHARD_REQUEST_CACHE = 'request_cache'; |
||
| 31 | const OPTION_FILTER_PATH = 'filter_path'; |
||
| 32 | const OPTION_TYPED_KEYS = 'typed_keys'; |
||
| 33 | |||
| 34 | /* |
||
| 35 | * Search types |
||
| 36 | */ |
||
| 37 | const OPTION_SEARCH_TYPE_DFS_QUERY_THEN_FETCH = 'dfs_query_then_fetch'; |
||
| 38 | const OPTION_SEARCH_TYPE_QUERY_THEN_FETCH = 'query_then_fetch'; |
||
| 39 | const OPTION_SEARCH_TYPE_SUGGEST = 'suggest'; |
||
| 40 | const OPTION_SEARCH_IGNORE_UNAVAILABLE = 'ignore_unavailable'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var BuilderInterface |
||
| 44 | */ |
||
| 45 | private $_builder; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Array of indices. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | protected $_indices = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Array of types. |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $_types = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var \Elastica\Query |
||
| 63 | */ |
||
| 64 | protected $_query; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $_options = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Client object. |
||
| 73 | * |
||
| 74 | * @var \Elastica\Client |
||
| 75 | */ |
||
| 76 | protected $_client; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Constructs search object. |
||
| 80 | * |
||
| 81 | * @param \Elastica\Client $client Client object |
||
| 82 | * @param BuilderInterface $builder |
||
| 83 | */ |
||
| 84 | public function __construct(Client $client, BuilderInterface $builder = null) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Adds a index to the list. |
||
| 92 | * |
||
| 93 | * @param Index|string $index Index object or string |
||
| 94 | * |
||
| 95 | * @throws InvalidException |
||
| 96 | * |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | public function addIndex($index) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Add array of indices at once. |
||
| 116 | * |
||
| 117 | * @param array $indices |
||
| 118 | * |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function addIndices(array $indices = []) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string|array|Query|Suggest|Query\AbstractQuery $query |
||
| 132 | * |
||
| 133 | * @return $this |
||
| 134 | */ |
||
| 135 | public function setQuery($query) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $key |
||
| 144 | * @param mixed $value |
||
| 145 | * |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | public function setOption($key, $value) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param array $options |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | public function setOptions(array $options) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function clearOptions() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $key |
||
| 185 | * @param mixed $value |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function addOption($key, $value) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $key |
||
| 200 | * |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | public function hasOption($key) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $key |
||
| 210 | * |
||
| 211 | * @throws InvalidException |
||
| 212 | * |
||
| 213 | * @return mixed |
||
| 214 | */ |
||
| 215 | public function getOption($key) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function getOptions() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $key |
||
| 234 | * |
||
| 235 | * @throws InvalidException |
||
| 236 | * |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | protected function _validateOption($key) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Return client object. |
||
| 266 | * |
||
| 267 | * @return \Elastica\Client Client object |
||
| 268 | */ |
||
| 269 | public function getClient() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Return array of indices. |
||
| 276 | * |
||
| 277 | * @return array List of index names |
||
| 278 | */ |
||
| 279 | public function getIndices() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public function hasIndices() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param Index|string $index |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | public function hasIndex($index) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return Query |
||
| 308 | */ |
||
| 309 | public function getQuery() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Creates new search object. |
||
| 320 | * |
||
| 321 | * @param SearchableInterface $searchObject |
||
| 322 | * |
||
| 323 | * @return Search |
||
| 324 | */ |
||
| 325 | public static function create(SearchableInterface $searchObject) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Combines indices to the search request path. |
||
| 332 | * |
||
| 333 | * @return string Search path |
||
| 334 | */ |
||
| 335 | public function getPath() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Search in the set indices. |
||
| 346 | * |
||
| 347 | * @param mixed $query |
||
| 348 | * @param int|array $options OPTIONAL Limit or associative array of options (option=>value) |
||
| 349 | * @param string $method OPTIONAL Request method (use const's) (default = Request::POST) |
||
| 350 | * |
||
| 351 | * @throws InvalidException |
||
| 352 | * |
||
| 353 | * @return ResultSet |
||
| 354 | */ |
||
| 355 | public function search($query = '', $options = null, $method = Request::POST) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param mixed $query |
||
| 384 | * @param $fullResult (default = false) By default only the total hit count is returned. If set to true, the full ResultSet including aggregations is returned |
||
| 385 | * @param string $method OPTIONAL Request method (use const's) (default = Request::POST) |
||
| 386 | * |
||
| 387 | * @return int|ResultSet |
||
| 388 | */ |
||
| 389 | public function count($query = '', $fullResult = false, $method = Request::POST) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param array|int $options |
||
| 411 | * @param string|array|Query $query |
||
| 412 | * |
||
| 413 | * @return $this |
||
| 414 | */ |
||
| 415 | public function setOptionsAndQuery($options = null, $query = '') |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param Suggest $suggest |
||
| 440 | * |
||
| 441 | * @return $this |
||
| 442 | */ |
||
| 443 | public function setSuggest(Suggest $suggest) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Returns the Scroll Iterator. |
||
| 450 | * |
||
| 451 | * @see Scroll |
||
| 452 | * |
||
| 453 | * @param string $expiryTime |
||
| 454 | * |
||
| 455 | * @return Scroll |
||
| 456 | */ |
||
| 457 | public function scroll($expiryTime = '1m') |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @return BuilderInterface |
||
| 464 | */ |
||
| 465 | public function getResultSetBuilder() |
||
| 469 | } |
||
| 470 |
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: