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 |
||
| 16 | class Client |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Config with defaults. |
||
| 20 | * |
||
| 21 | * log: Set to true, to enable logging, set a string to log to a specific file |
||
| 22 | * retryOnConflict: Use in \Elastica\Client::updateDocument |
||
| 23 | * bigintConversion: Set to true to enable the JSON bigint to string conversion option (see issue #717) |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $_config = [ |
||
| 28 | 'host' => null, |
||
| 29 | 'port' => null, |
||
| 30 | 'path' => null, |
||
| 31 | 'url' => null, |
||
| 32 | 'proxy' => null, |
||
| 33 | 'transport' => null, |
||
| 34 | 'persistent' => true, |
||
| 35 | 'timeout' => null, |
||
| 36 | 'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, config -> (curl, headers, url) |
||
| 37 | 'roundRobin' => false, |
||
| 38 | 'log' => false, |
||
| 39 | 'retryOnConflict' => 0, |
||
| 40 | 'bigintConversion' => false, |
||
| 41 | 'username' => null, |
||
| 42 | 'password' => null, |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var callback |
||
| 47 | */ |
||
| 48 | protected $_callback; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var Connection\ConnectionPool |
||
| 52 | */ |
||
| 53 | protected $_connectionPool; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var \Elastica\Request|null |
||
| 57 | */ |
||
| 58 | protected $_lastRequest; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var \Elastica\Response|null |
||
| 62 | */ |
||
| 63 | protected $_lastResponse; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var LoggerInterface |
||
| 67 | */ |
||
| 68 | protected $_logger; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $_version; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Creates a new Elastica client. |
||
| 77 | * |
||
| 78 | * @param array $config OPTIONAL Additional config options |
||
| 79 | * @param callback $callback OPTIONAL Callback function which can be used to be notified about errors (for example connection down) |
||
| 80 | * @param LoggerInterface $logger |
||
| 81 | */ |
||
| 82 | public function __construct(array $config = [], $callback = null, LoggerInterface $logger = null) |
||
| 83 | { |
||
| 84 | $this->_callback = $callback; |
||
| 85 | |||
| 86 | if (!$logger && isset($config['log']) && $config['log']) { |
||
| 87 | $logger = new Log($config['log']); |
||
| 88 | } |
||
| 89 | $this->_logger = $logger ?: new NullLogger(); |
||
| 90 | |||
| 91 | $this->setConfig($config); |
||
| 92 | $this->_initConnections(); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get current version. |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public function getVersion() |
||
| 101 | { |
||
| 102 | if ($this->_version) { |
||
| 103 | return $this->_version; |
||
| 104 | } |
||
| 105 | |||
| 106 | $data = $this->request('/')->getData(); |
||
| 107 | |||
| 108 | return $this->_version = $data['version']['number']; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Inits the client connections. |
||
| 113 | */ |
||
| 114 | protected function _initConnections() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Creates a Connection params array from a Client or server config array. |
||
| 148 | * |
||
| 149 | * @param array $config |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | protected function _prepareConnectionParams(array $config) |
||
| 154 | { |
||
| 155 | $params = []; |
||
| 156 | $params['config'] = []; |
||
| 157 | foreach ($config as $key => $value) { |
||
| 158 | if (in_array($key, ['bigintConversion', 'curl', 'headers', 'url'])) { |
||
| 159 | $params['config'][$key] = $value; |
||
| 160 | } else { |
||
| 161 | $params[$key] = $value; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | return $params; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Sets specific config values (updates and keeps default values). |
||
| 170 | * |
||
| 171 | * @param array $config Params |
||
| 172 | * |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | public function setConfig(array $config) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Returns a specific config key or the whole |
||
| 186 | * config array if not set. |
||
| 187 | * |
||
| 188 | * @param string $key Config key |
||
| 189 | * |
||
| 190 | * @throws \Elastica\Exception\InvalidException |
||
| 191 | * |
||
| 192 | * @return array|string Config value |
||
| 193 | */ |
||
| 194 | public function getConfig($key = '') |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Sets / overwrites a specific config value. |
||
| 209 | * |
||
| 210 | * @param string $key Key to set |
||
| 211 | * @param mixed $value Value |
||
| 212 | * |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | public function setConfigValue($key, $value) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param array|string $keys config key or path of config keys |
||
| 222 | * @param mixed $default default value will be returned if key was not found |
||
| 223 | * |
||
| 224 | * @return mixed |
||
| 225 | */ |
||
| 226 | public function getConfigValue($keys, $default = null) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Returns the index for the given connection. |
||
| 242 | * |
||
| 243 | * @param string $name Index name to create connection to |
||
| 244 | * |
||
| 245 | * @return \Elastica\Index Index for the given name |
||
| 246 | */ |
||
| 247 | public function getIndex($name) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Adds a HTTP Header. |
||
| 254 | * |
||
| 255 | * @param string $header The HTTP Header |
||
| 256 | * @param string $headerValue The HTTP Header Value |
||
| 257 | * |
||
| 258 | * @throws \Elastica\Exception\InvalidException If $header or $headerValue is not a string |
||
| 259 | * |
||
| 260 | * @return $this |
||
| 261 | */ |
||
| 262 | View Code Duplication | public function addHeader($header, $headerValue) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Remove a HTTP Header. |
||
| 275 | * |
||
| 276 | * @param string $header The HTTP Header to remove |
||
| 277 | * |
||
| 278 | * @throws \Elastica\Exception\InvalidException If $header is not a string |
||
| 279 | * |
||
| 280 | * @return $this |
||
| 281 | */ |
||
| 282 | View Code Duplication | public function removeHeader($header) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Uses _bulk to send documents to the server. |
||
| 297 | * |
||
| 298 | * Array of \Elastica\Document as input. Index and type has to be |
||
| 299 | * set inside the document, because for bulk settings documents, |
||
| 300 | * documents can belong to any type and index |
||
| 301 | * |
||
| 302 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 303 | * |
||
| 304 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
| 305 | * |
||
| 306 | * @throws \Elastica\Exception\InvalidException If docs is empty |
||
| 307 | * |
||
| 308 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 309 | */ |
||
| 310 | View Code Duplication | public function updateDocuments(array $docs) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Uses _bulk to send documents to the server. |
||
| 325 | * |
||
| 326 | * Array of \Elastica\Document as input. Index and type has to be |
||
| 327 | * set inside the document, because for bulk settings documents, |
||
| 328 | * documents can belong to any type and index |
||
| 329 | * |
||
| 330 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 331 | * |
||
| 332 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
| 333 | * |
||
| 334 | * @throws \Elastica\Exception\InvalidException If docs is empty |
||
| 335 | * |
||
| 336 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 337 | */ |
||
| 338 | View Code Duplication | public function addDocuments(array $docs) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Update document, using update script. Requires elasticsearch >= 0.19.0. |
||
| 353 | * |
||
| 354 | * @param int|string $id document id |
||
| 355 | * @param array|\Elastica\Script\AbstractScript|\Elastica\Document $data raw data for request body |
||
| 356 | * @param string $index index to update |
||
| 357 | * @param string $type type of index to update |
||
| 358 | * @param array $options array of query params to use for query. For possible options check es api |
||
| 359 | * |
||
| 360 | * @return \Elastica\Response |
||
| 361 | * |
||
| 362 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html |
||
| 363 | */ |
||
| 364 | public function updateDocument($id, $data, $index, $type, array $options = []) |
||
| 365 | { |
||
| 366 | $path = $index.'/'.$type.'/'.$id.'/_update'; |
||
| 367 | |||
| 368 | if ($data instanceof AbstractScript) { |
||
| 369 | $requestData = $data->toArray(); |
||
| 370 | } elseif ($data instanceof Document) { |
||
| 371 | $requestData = ['doc' => $data->getData()]; |
||
| 372 | |||
| 373 | if ($data->getDocAsUpsert()) { |
||
| 374 | $requestData['doc_as_upsert'] = true; |
||
| 375 | } |
||
| 376 | |||
| 377 | $docOptions = $data->getOptions( |
||
| 378 | [ |
||
| 379 | 'version', |
||
| 380 | 'version_type', |
||
| 381 | 'routing', |
||
| 382 | 'percolate', |
||
| 383 | 'parent', |
||
| 384 | 'fields', |
||
| 385 | 'retry_on_conflict', |
||
| 386 | 'consistency', |
||
| 387 | 'replication', |
||
| 388 | 'refresh', |
||
| 389 | 'timeout', |
||
| 390 | ] |
||
| 391 | ); |
||
| 392 | $options += $docOptions; |
||
| 393 | // set fields param to source only if options was not set before |
||
| 394 | if ($data instanceof Document && ($data->isAutoPopulate() |
||
| 395 | || $this->getConfigValue(['document', 'autoPopulate'], false)) |
||
| 396 | && !isset($options['fields']) |
||
| 397 | ) { |
||
| 398 | $options['fields'] = '_source'; |
||
| 399 | } |
||
| 400 | } else { |
||
| 401 | $requestData = $data; |
||
| 402 | } |
||
| 403 | |||
| 404 | //If an upsert document exists |
||
| 405 | if ($data instanceof AbstractScript || $data instanceof Document) { |
||
| 406 | if ($data->hasUpsert()) { |
||
| 407 | $requestData['upsert'] = $data->getUpsert()->getData(); |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | if (!isset($options['retry_on_conflict'])) { |
||
| 412 | $retryOnConflict = $this->getConfig('retryOnConflict'); |
||
| 413 | $options['retry_on_conflict'] = $retryOnConflict; |
||
| 414 | } |
||
| 415 | |||
| 416 | $response = $this->request($path, Request::POST, $requestData, $options); |
||
| 417 | |||
| 418 | View Code Duplication | if ($response->isOk() |
|
| 419 | && $data instanceof Document |
||
| 420 | && ($data->isAutoPopulate() || $this->getConfigValue(['document', 'autoPopulate'], false)) |
||
| 421 | ) { |
||
| 422 | $responseData = $response->getData(); |
||
| 423 | if (isset($responseData['_version'])) { |
||
| 424 | $data->setVersion($responseData['_version']); |
||
| 425 | } |
||
| 426 | if (isset($options['fields'])) { |
||
| 427 | $this->_populateDocumentFieldsFromResponse($response, $data, $options['fields']); |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | return $response; |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param \Elastica\Response $response |
||
| 436 | * @param \Elastica\Document $document |
||
| 437 | * @param string $fields Array of field names to be populated or '_source' if whole document data should be updated |
||
| 438 | */ |
||
| 439 | protected function _populateDocumentFieldsFromResponse(Response $response, Document $document, $fields) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Bulk deletes documents. |
||
| 462 | * |
||
| 463 | * @param array|\Elastica\Document[] $docs |
||
| 464 | * |
||
| 465 | * @throws \Elastica\Exception\InvalidException |
||
| 466 | * |
||
| 467 | * @return \Elastica\Bulk\ResponseSet |
||
| 468 | */ |
||
| 469 | View Code Duplication | public function deleteDocuments(array $docs) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Returns the status object for all indices. |
||
| 483 | * |
||
| 484 | * @return \Elastica\Status Status object |
||
| 485 | */ |
||
| 486 | public function getStatus() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Returns the current cluster. |
||
| 493 | * |
||
| 494 | * @return \Elastica\Cluster Cluster object |
||
| 495 | */ |
||
| 496 | public function getCluster() |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Establishes the client connections. |
||
| 503 | */ |
||
| 504 | public function connect() |
||
| 505 | { |
||
| 506 | return $this->_initConnections(); |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @param \Elastica\Connection $connection |
||
| 511 | * |
||
| 512 | * @return $this |
||
| 513 | */ |
||
| 514 | public function addConnection(Connection $connection) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Determines whether a valid connection is available for use. |
||
| 523 | * |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | public function hasConnection() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @throws \Elastica\Exception\ClientException |
||
| 533 | * |
||
| 534 | * @return \Elastica\Connection |
||
| 535 | */ |
||
| 536 | public function getConnection() |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @return \Elastica\Connection[] |
||
| 543 | */ |
||
| 544 | public function getConnections() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @return \Elastica\Connection\Strategy\StrategyInterface |
||
| 551 | */ |
||
| 552 | public function getConnectionStrategy() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @param array|\Elastica\Connection[] $connections |
||
| 559 | * |
||
| 560 | * @return $this |
||
| 561 | */ |
||
| 562 | public function setConnections(array $connections) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Deletes documents with the given ids, index, type from the index. |
||
| 571 | * |
||
| 572 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 573 | * |
||
| 574 | * @param array $ids Document ids |
||
| 575 | * @param string|\Elastica\Index $index Index name |
||
| 576 | * @param string|\Elastica\Type $type Type of documents |
||
| 577 | * @param string|bool $routing Optional routing key for all ids |
||
| 578 | * |
||
| 579 | * @throws \Elastica\Exception\InvalidException |
||
| 580 | * |
||
| 581 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 582 | */ |
||
| 583 | public function deleteIds(array $ids, $index, $type, $routing = false) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Bulk operation. |
||
| 609 | * |
||
| 610 | * Every entry in the params array has to exactly on array |
||
| 611 | * of the bulk operation. An example param array would be: |
||
| 612 | * |
||
| 613 | * array( |
||
| 614 | * array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')), |
||
| 615 | * array('user' => array('name' => 'hans')), |
||
| 616 | * array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '2')) |
||
| 617 | * ); |
||
| 618 | * |
||
| 619 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
| 620 | * |
||
| 621 | * @param array $params Parameter array |
||
| 622 | * |
||
| 623 | * @throws \Elastica\Exception\ResponseException |
||
| 624 | * @throws \Elastica\Exception\InvalidException |
||
| 625 | * |
||
| 626 | * @return \Elastica\Bulk\ResponseSet Response object |
||
| 627 | */ |
||
| 628 | public function bulk(array $params) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Makes calls to the elasticsearch server based on this index. |
||
| 643 | * |
||
| 644 | * It's possible to make any REST query directly over this method |
||
| 645 | * |
||
| 646 | * @param string $path Path to call |
||
| 647 | * @param string $method Rest method to use (GET, POST, DELETE, PUT) |
||
| 648 | * @param array|string $data OPTIONAL Arguments as array or pre-encoded string |
||
| 649 | * @param array $query OPTIONAL Query params |
||
| 650 | * |
||
| 651 | * @throws Exception\ConnectionException|\Exception |
||
| 652 | * |
||
| 653 | * @return Response Response object |
||
| 654 | */ |
||
| 655 | public function request($path, $method = Request::GET, $data = [], array $query = []) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * logging. |
||
| 683 | * |
||
| 684 | * @deprecated Overwriting Client->_log is deprecated. Handle logging functionality by using a custom LoggerInterface. |
||
| 685 | * |
||
| 686 | * @param mixed $context |
||
| 687 | */ |
||
| 688 | protected function _log($context) |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Optimizes all search indices. |
||
| 717 | * |
||
| 718 | * @param array $args OPTIONAL Optional arguments |
||
| 719 | * |
||
| 720 | * @return \Elastica\Response Response object |
||
| 721 | * |
||
| 722 | * @deprecated Replaced by forcemergeAll |
||
| 723 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-optimize.html |
||
| 724 | */ |
||
| 725 | public function optimizeAll($args = []) |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Force merges all search indices. |
||
| 734 | * |
||
| 735 | * @param array $args OPTIONAL Optional arguments |
||
| 736 | * |
||
| 737 | * @return \Elastica\Response Response object |
||
| 738 | * |
||
| 739 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html |
||
| 740 | */ |
||
| 741 | public function forcemergeAll($args = []) |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Refreshes all search indices. |
||
| 748 | * |
||
| 749 | * @return \Elastica\Response Response object |
||
| 750 | * |
||
| 751 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html |
||
| 752 | */ |
||
| 753 | public function refreshAll() |
||
| 757 | |||
| 758 | /** |
||
| 759 | * @return Request|null |
||
| 760 | */ |
||
| 761 | public function getLastRequest() |
||
| 765 | |||
| 766 | /** |
||
| 767 | * @return Response|null |
||
| 768 | */ |
||
| 769 | public function getLastResponse() |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Replace the existing logger. |
||
| 776 | * |
||
| 777 | * @param LoggerInterface $logger |
||
| 778 | * |
||
| 779 | * @return $this |
||
| 780 | */ |
||
| 781 | public function setLogger(LoggerInterface $logger) |
||
| 787 | } |
||
| 788 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.