Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Client |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var ClientConfiguration |
||
| 25 | */ |
||
| 26 | protected $_config; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var callback |
||
| 30 | */ |
||
| 31 | protected $_callback; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Connection\ConnectionPool |
||
| 35 | */ |
||
| 36 | protected $_connectionPool; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \Elastica\Request|null |
||
| 40 | */ |
||
| 41 | protected $_lastRequest; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \Elastica\Response|null |
||
| 45 | */ |
||
| 46 | protected $_lastResponse; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var LoggerInterface |
||
| 50 | */ |
||
| 51 | protected $_logger; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $_version; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Creates a new Elastica client. |
||
| 60 | * |
||
| 61 | * @param array|string $config OPTIONAL Additional config or DSN of options |
||
| 62 | * @param callback|null $callback OPTIONAL Callback function which can be used to be notified about errors (for example connection down) |
||
| 63 | * @param LoggerInterface $logger |
||
| 64 | * |
||
| 65 | * @throws \Elastica\Exception\InvalidException |
||
| 66 | */ |
||
| 67 | public function __construct($config = [], callable $callback = null, LoggerInterface $logger = null) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get current version. |
||
| 86 | * |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | public function getVersion() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Inits the client connections. |
||
| 102 | */ |
||
| 103 | protected function _initConnections() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Creates a Connection params array from a Client or server config array. |
||
| 138 | * |
||
| 139 | * @param array $config |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | protected function _prepareConnectionParams(array $config) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sets specific config values (updates and keeps default values). |
||
| 160 | * |
||
| 161 | * @param array $config Params |
||
| 162 | * |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | public function setConfig(array $config) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns a specific config key or the whole |
||
| 176 | * config array if not set. |
||
| 177 | * |
||
| 178 | * @param string $key Config key |
||
| 179 | * |
||
| 180 | * @throws \Elastica\Exception\InvalidException |
||
| 181 | * |
||
| 182 | * @return array|string Config value |
||
| 183 | */ |
||
| 184 | public function getConfig($key = '') |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Sets / overwrites a specific config value. |
||
| 191 | * |
||
| 192 | * @param string $key Key to set |
||
| 193 | * @param mixed $value Value |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | public function setConfigValue($key, $value) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param array|string $keys config key or path of config keys |
||
| 204 | * @param mixed $default default value will be returned if key was not found |
||
| 205 | * |
||
| 206 | * @return mixed |
||
| 207 | */ |
||
| 208 | public function getConfigValue($keys, $default = null) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Returns the index for the given connection. |
||
| 224 | * |
||
| 225 | * @param string $name Index name to create connection to |
||
| 226 | * |
||
| 227 | * @return \Elastica\Index Index for the given name |
||
| 228 | */ |
||
| 229 | public function getIndex($name) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Adds a HTTP Header. |
||
| 236 | * |
||
| 237 | * @param string $header The HTTP Header |
||
| 238 | * @param string $headerValue The HTTP Header Value |
||
| 239 | * |
||
| 240 | * @throws \Elastica\Exception\InvalidException If $header or $headerValue is not a string |
||
| 241 | * |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function addHeader($header, $headerValue) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Remove a HTTP Header. |
||
| 263 | * |
||
| 264 | * @param string $header The HTTP Header to remove |
||
| 265 | * |
||
| 266 | * @throws \Elastica\Exception\InvalidException If $header is not a string |
||
| 267 | * |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | public function removeHeader($header) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Uses _bulk to send documents to the server. |
||
| 287 | * |
||
| 288 | * Array of \Elastica\Document as input. Index and type has to be |
||
| 289 | * set inside the document, because for bulk settings documents, |
||
| 290 | * documents can belong to any type and index |
||
| 291 | * |
||
| 292 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 293 | * |
||
| 294 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
| 295 | * @param array $requestParams |
||
| 296 | * |
||
| 297 | * @throws \Elastica\Exception\InvalidException If docs is empty |
||
| 298 | * |
||
| 299 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 300 | */ |
||
| 301 | View Code Duplication | public function updateDocuments(array $docs, array $requestParams = []) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Uses _bulk to send documents to the server. |
||
| 319 | * |
||
| 320 | * Array of \Elastica\Document as input. Index and type has to be |
||
| 321 | * set inside the document, because for bulk settings documents, |
||
| 322 | * documents can belong to any type and index |
||
| 323 | * |
||
| 324 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 325 | * |
||
| 326 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
| 327 | * @param array $requestParams |
||
| 328 | * |
||
| 329 | * @throws \Elastica\Exception\InvalidException If docs is empty |
||
| 330 | * |
||
| 331 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 332 | */ |
||
| 333 | View Code Duplication | public function addDocuments(array $docs, array $requestParams = []) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Update document, using update script. Requires elasticsearch >= 0.19.0. |
||
| 352 | * |
||
| 353 | * @param int|string $id document id |
||
| 354 | * @param array|\Elastica\Script\AbstractScript|\Elastica\Document $data raw data for request body |
||
| 355 | * @param string $index index to update |
||
| 356 | * @param string $type type of index to update |
||
| 357 | * @param array $options array of query params to use for query. For possible options check es api |
||
| 358 | * |
||
| 359 | * @return \Elastica\Response |
||
| 360 | * |
||
| 361 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html |
||
| 362 | */ |
||
| 363 | public function updateDocument($id, $data, $index, $type, array $options = []) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Bulk deletes documents. |
||
| 425 | * |
||
| 426 | * @param array|\Elastica\Document[] $docs |
||
| 427 | * @param array $requestParams |
||
| 428 | * |
||
| 429 | * @throws \Elastica\Exception\InvalidException |
||
| 430 | * |
||
| 431 | * @return \Elastica\Bulk\ResponseSet |
||
| 432 | */ |
||
| 433 | View Code Duplication | public function deleteDocuments(array $docs, array $requestParams = []) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Returns the status object for all indices. |
||
| 451 | * |
||
| 452 | * @return \Elastica\Status Status object |
||
| 453 | */ |
||
| 454 | public function getStatus() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Returns the current cluster. |
||
| 461 | * |
||
| 462 | * @return \Elastica\Cluster Cluster object |
||
| 463 | */ |
||
| 464 | public function getCluster() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Establishes the client connections. |
||
| 471 | */ |
||
| 472 | public function connect() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param \Elastica\Connection $connection |
||
| 479 | * |
||
| 480 | * @return $this |
||
| 481 | */ |
||
| 482 | public function addConnection(Connection $connection) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Determines whether a valid connection is available for use. |
||
| 491 | * |
||
| 492 | * @return bool |
||
| 493 | */ |
||
| 494 | public function hasConnection() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @throws \Elastica\Exception\ClientException |
||
| 501 | * |
||
| 502 | * @return \Elastica\Connection |
||
| 503 | */ |
||
| 504 | public function getConnection() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @return \Elastica\Connection[] |
||
| 511 | */ |
||
| 512 | public function getConnections() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * @return \Elastica\Connection\Strategy\StrategyInterface |
||
| 519 | */ |
||
| 520 | public function getConnectionStrategy() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param array|\Elastica\Connection[] $connections |
||
| 527 | * |
||
| 528 | * @return $this |
||
| 529 | */ |
||
| 530 | public function setConnections(array $connections) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Deletes documents with the given ids, index, type from the index. |
||
| 539 | * |
||
| 540 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 541 | * |
||
| 542 | * @param array $ids Document ids |
||
| 543 | * @param string|\Elastica\Index $index Index name |
||
| 544 | * @param string|\Elastica\Type $type Type of documents |
||
| 545 | * @param string|bool $routing Optional routing key for all ids |
||
| 546 | * |
||
| 547 | * @throws \Elastica\Exception\InvalidException |
||
| 548 | * |
||
| 549 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 550 | */ |
||
| 551 | public function deleteIds(array $ids, $index, $type, $routing = false) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Bulk operation. |
||
| 577 | * |
||
| 578 | * Every entry in the params array has to exactly on array |
||
| 579 | * of the bulk operation. An example param array would be: |
||
| 580 | * |
||
| 581 | * array( |
||
| 582 | * array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')), |
||
| 583 | * array('user' => array('name' => 'hans')), |
||
| 584 | * array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '2')) |
||
| 585 | * ); |
||
| 586 | * |
||
| 587 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 588 | * |
||
| 589 | * @param array $params Parameter array |
||
| 590 | * |
||
| 591 | * @throws \Elastica\Exception\ResponseException |
||
| 592 | * @throws \Elastica\Exception\InvalidException |
||
| 593 | * |
||
| 594 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 595 | */ |
||
| 596 | public function bulk(array $params) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Makes calls to the elasticsearch server based on this index. |
||
| 611 | * |
||
| 612 | * It's possible to make any REST query directly over this method |
||
| 613 | * |
||
| 614 | * @param string $path Path to call |
||
| 615 | * @param string $method Rest method to use (GET, POST, DELETE, PUT) |
||
| 616 | * @param array|string $data OPTIONAL Arguments as array or pre-encoded string |
||
| 617 | * @param array $query OPTIONAL Query params |
||
| 618 | * @param string $contentType Content-Type sent with this request |
||
| 619 | * |
||
| 620 | * @throws Exception\ConnectionException|Exception\ClientException |
||
| 621 | * |
||
| 622 | * @return Response Response object |
||
| 623 | */ |
||
| 624 | public function request($path, $method = Request::GET, $data = [], array $query = [], $contentType = Request::DEFAULT_CONTENT_TYPE) |
||
| 656 | |||
| 657 | /** |
||
| 658 | * Makes calls to the elasticsearch server with usage official client Endpoint. |
||
| 659 | * |
||
| 660 | * @param AbstractEndpoint $endpoint |
||
| 661 | * |
||
| 662 | * @return Response |
||
| 663 | */ |
||
| 664 | public function requestEndpoint(AbstractEndpoint $endpoint) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Force merges all search indices. |
||
| 676 | * |
||
| 677 | * @param array $args OPTIONAL Optional arguments |
||
| 678 | * |
||
| 679 | * @return \Elastica\Response Response object |
||
| 680 | * |
||
| 681 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html |
||
| 682 | */ |
||
| 683 | public function forcemergeAll($args = []) |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Refreshes all search indices. |
||
| 693 | * |
||
| 694 | * @return \Elastica\Response Response object |
||
| 695 | * |
||
| 696 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html |
||
| 697 | */ |
||
| 698 | public function refreshAll() |
||
| 702 | |||
| 703 | /** |
||
| 704 | * @return Request|null |
||
| 705 | */ |
||
| 706 | public function getLastRequest() |
||
| 710 | |||
| 711 | /** |
||
| 712 | * @return Response|null |
||
| 713 | */ |
||
| 714 | public function getLastResponse() |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Replace the existing logger. |
||
| 721 | * |
||
| 722 | * @param LoggerInterface $logger |
||
| 723 | * |
||
| 724 | * @return $this |
||
| 725 | */ |
||
| 726 | public function setLogger(LoggerInterface $logger) |
||
| 732 | } |
||
| 733 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..