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 | public const OPTION_SEARCH_TYPE = 'search_type'; |
||
| 20 | public const OPTION_ROUTING = 'routing'; |
||
| 21 | public const OPTION_PREFERENCE = 'preference'; |
||
| 22 | public const OPTION_VERSION = 'version'; |
||
| 23 | public const OPTION_TIMEOUT = 'timeout'; |
||
| 24 | public const OPTION_FROM = 'from'; |
||
| 25 | public const OPTION_SIZE = 'size'; |
||
| 26 | public const OPTION_SCROLL = 'scroll'; |
||
| 27 | public const OPTION_SCROLL_ID = 'scroll_id'; |
||
| 28 | public const OPTION_QUERY_CACHE = 'query_cache'; |
||
| 29 | public const OPTION_TERMINATE_AFTER = 'terminate_after'; |
||
| 30 | public const OPTION_SHARD_REQUEST_CACHE = 'request_cache'; |
||
| 31 | public const OPTION_FILTER_PATH = 'filter_path'; |
||
| 32 | public const OPTION_TYPED_KEYS = 'typed_keys'; |
||
| 33 | |||
| 34 | /* |
||
| 35 | * Search types |
||
| 36 | */ |
||
| 37 | public const OPTION_SEARCH_TYPE_DFS_QUERY_THEN_FETCH = 'dfs_query_then_fetch'; |
||
| 38 | public const OPTION_SEARCH_TYPE_QUERY_THEN_FETCH = 'query_then_fetch'; |
||
| 39 | public const OPTION_SEARCH_TYPE_SUGGEST = 'suggest'; |
||
| 40 | public const OPTION_SEARCH_IGNORE_UNAVAILABLE = 'ignore_unavailable'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var BuilderInterface|null |
||
| 44 | */ |
||
| 45 | private $builder; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Array of indices names. |
||
| 49 | * |
||
| 50 | * @var string[] |
||
| 51 | */ |
||
| 52 | protected $_indices = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Query |
||
| 56 | */ |
||
| 57 | protected $_query; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $_options = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Client object. |
||
| 66 | * |
||
| 67 | * @var Client |
||
| 68 | */ |
||
| 69 | protected $_client; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Constructs search object. |
||
| 73 | */ |
||
| 74 | public function __construct(Client $client, ?BuilderInterface $builder = null) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Adds a index to the list. |
||
| 82 | * |
||
| 83 | * @param Index|string $index Index object or string |
||
| 84 | * |
||
| 85 | * @throws InvalidException |
||
| 86 | */ |
||
| 87 | public function addIndex($index): self |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Add array of indices at once. |
||
| 104 | * |
||
| 105 | * @param Index[]|string[] $indices |
||
| 106 | */ |
||
| 107 | public function addIndices(array $indices = []): self |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string|array|Query|Suggest|Query\AbstractQuery $query |
||
| 118 | */ |
||
| 119 | public function setQuery($query): self |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param mixed $value |
||
| 128 | */ |
||
| 129 | public function setOption(string $key, $value): self |
||
| 137 | |||
| 138 | public function setOptions(array $options): self |
||
| 148 | |||
| 149 | public function clearOptions(): self |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param mixed $value |
||
| 158 | */ |
||
| 159 | public function addOption(string $key, $value): self |
||
| 167 | |||
| 168 | public function hasOption(string $key): bool |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @throws InvalidException if the given key does not exists as an option |
||
| 175 | * |
||
| 176 | * @return mixed |
||
| 177 | */ |
||
| 178 | public function getOption(string $key) |
||
| 186 | |||
| 187 | public function getOptions(): array |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @throws InvalidException If the given key is not a valid option |
||
| 194 | */ |
||
| 195 | protected function validateOption(string $key): void |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Return client object. |
||
| 222 | */ |
||
| 223 | public function getClient(): Client |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Return array of indices names. |
||
| 230 | * |
||
| 231 | * @return string[] |
||
| 232 | */ |
||
| 233 | public function getIndices(): array |
||
| 237 | |||
| 238 | public function hasIndices(): bool |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param Index|string $index |
||
| 245 | */ |
||
| 246 | public function hasIndex($index): bool |
||
| 254 | |||
| 255 | public function getQuery(): Query |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Creates new search object. |
||
| 266 | */ |
||
| 267 | public static function create(SearchableInterface $searchObject): Search |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Combines indices to the search request path. |
||
| 274 | */ |
||
| 275 | public function getPath(): string |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Search in the set indices. |
||
| 286 | * |
||
| 287 | * @param string|array|Query $query |
||
| 288 | * @param int|array $options Limit or associative array of options (option=>value) |
||
| 289 | * |
||
| 290 | * @throws InvalidException |
||
| 291 | */ |
||
| 292 | public function search($query = '', $options = null, string $method = Request::POST): ResultSet |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string|array|Query $query |
||
| 316 | * @param bool $fullResult By default only the total hit count is returned. If set to true, the full ResultSet including aggregations is returned |
||
| 317 | * |
||
| 318 | * @return int|ResultSet |
||
| 319 | */ |
||
| 320 | public function count($query = '', bool $fullResult = false, string $method = Request::POST) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param array|int $options |
||
| 342 | * @param string|array|Query $query |
||
| 343 | */ |
||
| 344 | public function setOptionsAndQuery($options = null, $query = ''): self |
||
| 366 | |||
| 367 | public function setSuggest(Suggest $suggest): self |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Returns the Scroll Iterator. |
||
| 374 | * |
||
| 375 | * @see Scroll |
||
| 376 | */ |
||
| 377 | public function scroll(string $expiryTime = '1m'): Scroll |
||
| 381 | |||
| 382 | public function getResultSetBuilder(): BuilderInterface |
||
| 386 | } |
||
| 387 |
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: