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 $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 = [], $callback = null, LoggerInterface $logger = null) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Get current version. |
||
| 90 | * |
||
| 91 | * @return string |
||
| 92 | */ |
||
| 93 | public function getVersion() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Inits the client connections. |
||
| 106 | */ |
||
| 107 | protected function _initConnections() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Creates a Connection params array from a Client or server config array. |
||
| 142 | * |
||
| 143 | * @param array $config |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | protected function _prepareConnectionParams(array $config) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Sets specific config values (updates and keeps default values). |
||
| 164 | * |
||
| 165 | * @param array $config Params |
||
| 166 | * |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | public function setConfig(array $config) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Returns a specific config key or the whole |
||
| 180 | * config array if not set. |
||
| 181 | * |
||
| 182 | * @param string $key Config key |
||
| 183 | * |
||
| 184 | * @throws \Elastica\Exception\InvalidException |
||
| 185 | * |
||
| 186 | * @return array|string Config value |
||
| 187 | */ |
||
| 188 | public function getConfig($key = '') |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Sets / overwrites a specific config value. |
||
| 195 | * |
||
| 196 | * @param string $key Key to set |
||
| 197 | * @param mixed $value Value |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function setConfigValue($key, $value) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param array|string $keys config key or path of config keys |
||
| 208 | * @param mixed $default default value will be returned if key was not found |
||
| 209 | * |
||
| 210 | * @return mixed |
||
| 211 | */ |
||
| 212 | public function getConfigValue($keys, $default = null) |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns the index for the given connection. |
||
| 228 | * |
||
| 229 | * @param string $name Index name to create connection to |
||
| 230 | * |
||
| 231 | * @return \Elastica\Index Index for the given name |
||
| 232 | */ |
||
| 233 | public function getIndex($name) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Adds a HTTP Header. |
||
| 240 | * |
||
| 241 | * @param string $header The HTTP Header |
||
| 242 | * @param string $headerValue The HTTP Header Value |
||
| 243 | * |
||
| 244 | * @throws \Elastica\Exception\InvalidException If $header or $headerValue is not a string |
||
| 245 | * |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | public function addHeader($header, $headerValue) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Remove a HTTP Header. |
||
| 267 | * |
||
| 268 | * @param string $header The HTTP Header to remove |
||
| 269 | * |
||
| 270 | * @throws \Elastica\Exception\InvalidException If $header is not a string |
||
| 271 | * |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | public function removeHeader($header) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Uses _bulk to send documents to the server. |
||
| 291 | * |
||
| 292 | * Array of \Elastica\Document as input. Index and type has to be |
||
| 293 | * set inside the document, because for bulk settings documents, |
||
| 294 | * documents can belong to any type and index |
||
| 295 | * |
||
| 296 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 297 | * |
||
| 298 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
| 299 | * @param array $requestParams |
||
| 300 | * |
||
| 301 | * @throws \Elastica\Exception\InvalidException If docs is empty |
||
| 302 | * |
||
| 303 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 304 | */ |
||
| 305 | View Code Duplication | public function updateDocuments(array $docs, array $requestParams = []) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Uses _bulk to send documents to the server. |
||
| 323 | * |
||
| 324 | * Array of \Elastica\Document as input. Index and type has to be |
||
| 325 | * set inside the document, because for bulk settings documents, |
||
| 326 | * documents can belong to any type and index |
||
| 327 | * |
||
| 328 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 329 | * |
||
| 330 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
| 331 | * @param array $requestParams |
||
| 332 | * |
||
| 333 | * @throws \Elastica\Exception\InvalidException If docs is empty |
||
| 334 | * |
||
| 335 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function addDocuments(array $docs, array $requestParams = []) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Update document, using update script. Requires elasticsearch >= 0.19.0. |
||
| 356 | * |
||
| 357 | * @param int|string $id document id |
||
| 358 | * @param array|\Elastica\Script\AbstractScript|\Elastica\Document $data raw data for request body |
||
| 359 | * @param string $index index to update |
||
| 360 | * @param string $type type of index to update |
||
| 361 | * @param array $options array of query params to use for query. For possible options check es api |
||
| 362 | * |
||
| 363 | * @return \Elastica\Response |
||
| 364 | * |
||
| 365 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html |
||
| 366 | */ |
||
| 367 | public function updateDocument($id, $data, $index, $type, array $options = []) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Bulk deletes documents. |
||
| 429 | * |
||
| 430 | * @param array|\Elastica\Document[] $docs |
||
| 431 | * @param array $requestParams |
||
| 432 | * |
||
| 433 | * @throws \Elastica\Exception\InvalidException |
||
| 434 | * |
||
| 435 | * @return \Elastica\Bulk\ResponseSet |
||
| 436 | */ |
||
| 437 | View Code Duplication | public function deleteDocuments(array $docs, array $requestParams = []) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Returns the status object for all indices. |
||
| 455 | * |
||
| 456 | * @return \Elastica\Status Status object |
||
| 457 | */ |
||
| 458 | public function getStatus() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns the current cluster. |
||
| 465 | * |
||
| 466 | * @return \Elastica\Cluster Cluster object |
||
| 467 | */ |
||
| 468 | public function getCluster() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Establishes the client connections. |
||
| 475 | */ |
||
| 476 | public function connect() |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param \Elastica\Connection $connection |
||
| 483 | * |
||
| 484 | * @return $this |
||
| 485 | */ |
||
| 486 | public function addConnection(Connection $connection) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Determines whether a valid connection is available for use. |
||
| 495 | * |
||
| 496 | * @return bool |
||
| 497 | */ |
||
| 498 | public function hasConnection() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @throws \Elastica\Exception\ClientException |
||
| 505 | * |
||
| 506 | * @return \Elastica\Connection |
||
| 507 | */ |
||
| 508 | public function getConnection() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @return \Elastica\Connection[] |
||
| 515 | */ |
||
| 516 | public function getConnections() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @return \Elastica\Connection\Strategy\StrategyInterface |
||
| 523 | */ |
||
| 524 | public function getConnectionStrategy() |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @param array|\Elastica\Connection[] $connections |
||
| 531 | * |
||
| 532 | * @return $this |
||
| 533 | */ |
||
| 534 | public function setConnections(array $connections) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Deletes documents with the given ids, index, type from the index. |
||
| 543 | * |
||
| 544 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 545 | * |
||
| 546 | * @param array $ids Document ids |
||
| 547 | * @param string|\Elastica\Index $index Index name |
||
| 548 | * @param string|\Elastica\Type $type Type of documents |
||
| 549 | * @param string|bool $routing Optional routing key for all ids |
||
| 550 | * |
||
| 551 | * @throws \Elastica\Exception\InvalidException |
||
| 552 | * |
||
| 553 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 554 | */ |
||
| 555 | public function deleteIds(array $ids, $index, $type, $routing = false) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Bulk operation. |
||
| 581 | * |
||
| 582 | * Every entry in the params array has to exactly on array |
||
| 583 | * of the bulk operation. An example param array would be: |
||
| 584 | * |
||
| 585 | * array( |
||
| 586 | * array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')), |
||
| 587 | * array('user' => array('name' => 'hans')), |
||
| 588 | * array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '2')) |
||
| 589 | * ); |
||
| 590 | * |
||
| 591 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 592 | * |
||
| 593 | * @param array $params Parameter array |
||
| 594 | * |
||
| 595 | * @throws \Elastica\Exception\ResponseException |
||
| 596 | * @throws \Elastica\Exception\InvalidException |
||
| 597 | * |
||
| 598 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 599 | */ |
||
| 600 | public function bulk(array $params) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Makes calls to the elasticsearch server based on this index. |
||
| 615 | * |
||
| 616 | * It's possible to make any REST query directly over this method |
||
| 617 | * |
||
| 618 | * @param string $path Path to call |
||
| 619 | * @param string $method Rest method to use (GET, POST, DELETE, PUT) |
||
| 620 | * @param array|string $data OPTIONAL Arguments as array or pre-encoded string |
||
| 621 | * @param array $query OPTIONAL Query params |
||
| 622 | * @param string $contentType Content-Type sent with this request |
||
| 623 | * |
||
| 624 | * @throws Exception\ConnectionException|Exception\ClientException |
||
| 625 | * |
||
| 626 | * @return Response Response object |
||
| 627 | */ |
||
| 628 | public function request($path, $method = Request::GET, $data = [], array $query = [], $contentType = Request::DEFAULT_CONTENT_TYPE) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Makes calls to the elasticsearch server with usage official client Endpoint. |
||
| 655 | * |
||
| 656 | * @param AbstractEndpoint $endpoint |
||
| 657 | * |
||
| 658 | * @return Response |
||
| 659 | */ |
||
| 660 | public function requestEndpoint(AbstractEndpoint $endpoint) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * logging. |
||
| 672 | * |
||
| 673 | * @deprecated Overwriting Client->_log is deprecated. Handle logging functionality by using a custom LoggerInterface. |
||
| 674 | * |
||
| 675 | * @param mixed $context |
||
| 676 | */ |
||
| 677 | protected function _log($context) |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Force merges all search indices. |
||
| 706 | * |
||
| 707 | * @param array $args OPTIONAL Optional arguments |
||
| 708 | * |
||
| 709 | * @return \Elastica\Response Response object |
||
| 710 | * |
||
| 711 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html |
||
| 712 | */ |
||
| 713 | public function forcemergeAll($args = []) |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Refreshes all search indices. |
||
| 723 | * |
||
| 724 | * @return \Elastica\Response Response object |
||
| 725 | * |
||
| 726 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html |
||
| 727 | */ |
||
| 728 | public function refreshAll() |
||
| 732 | |||
| 733 | /** |
||
| 734 | * @return Request|null |
||
| 735 | */ |
||
| 736 | public function getLastRequest() |
||
| 740 | |||
| 741 | /** |
||
| 742 | * @return Response|null |
||
| 743 | */ |
||
| 744 | public function getLastResponse() |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Replace the existing logger. |
||
| 751 | * |
||
| 752 | * @param LoggerInterface $logger |
||
| 753 | * |
||
| 754 | * @return $this |
||
| 755 | */ |
||
| 756 | public function setLogger(LoggerInterface $logger) |
||
| 762 | } |
||
| 763 |
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..