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 |
||
17 | class Client |
||
18 | { |
||
19 | /** |
||
20 | * Config with defaults. |
||
21 | * |
||
22 | * log: Set to true, to enable logging, set a string to log to a specific file |
||
23 | * retryOnConflict: Use in \Elastica\Client::updateDocument |
||
24 | * bigintConversion: Set to true to enable the JSON bigint to string conversion option (see issue #717) |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $_config = [ |
||
29 | 'host' => null, |
||
30 | 'port' => null, |
||
31 | 'path' => null, |
||
32 | 'url' => null, |
||
33 | 'proxy' => null, |
||
34 | 'transport' => null, |
||
35 | 'persistent' => true, |
||
36 | 'timeout' => null, |
||
37 | 'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, config -> (curl, headers, url) |
||
38 | 'roundRobin' => false, |
||
39 | 'log' => false, |
||
40 | 'retryOnConflict' => 0, |
||
41 | 'bigintConversion' => false, |
||
42 | 'username' => null, |
||
43 | 'password' => null, |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * @var callback |
||
48 | */ |
||
49 | protected $_callback; |
||
50 | |||
51 | /** |
||
52 | * @var Connection\ConnectionPool |
||
53 | */ |
||
54 | protected $_connectionPool; |
||
55 | |||
56 | /** |
||
57 | * @var \Elastica\Request|null |
||
58 | */ |
||
59 | protected $_lastRequest; |
||
60 | |||
61 | /** |
||
62 | * @var \Elastica\Response|null |
||
63 | */ |
||
64 | protected $_lastResponse; |
||
65 | |||
66 | /** |
||
67 | * @var LoggerInterface |
||
68 | */ |
||
69 | protected $_logger; |
||
70 | |||
71 | /** |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $_version; |
||
75 | |||
76 | /** |
||
77 | * Creates a new Elastica client. |
||
78 | * |
||
79 | * @param array $config OPTIONAL Additional config options |
||
80 | * @param callback $callback OPTIONAL Callback function which can be used to be notified about errors (for example connection down) |
||
81 | * @param LoggerInterface $logger |
||
82 | */ |
||
83 | public function __construct(array $config = [], $callback = null, LoggerInterface $logger = null) |
||
95 | |||
96 | /** |
||
97 | * Get current version. |
||
98 | * |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getVersion() |
||
111 | |||
112 | /** |
||
113 | * Inits the client connections. |
||
114 | */ |
||
115 | protected function _initConnections() |
||
146 | |||
147 | /** |
||
148 | * Creates a Connection params array from a Client or server config array. |
||
149 | * |
||
150 | * @param array $config |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | protected function _prepareConnectionParams(array $config) |
||
168 | |||
169 | /** |
||
170 | * Sets specific config values (updates and keeps default values). |
||
171 | * |
||
172 | * @param array $config Params |
||
173 | * |
||
174 | * @return $this |
||
175 | */ |
||
176 | public function setConfig(array $config) |
||
184 | |||
185 | /** |
||
186 | * Returns a specific config key or the whole |
||
187 | * config array if not set. |
||
188 | * |
||
189 | * @param string $key Config key |
||
190 | * |
||
191 | * @throws \Elastica\Exception\InvalidException |
||
192 | * |
||
193 | * @return array|string Config value |
||
194 | */ |
||
195 | public function getConfig($key = '') |
||
207 | |||
208 | /** |
||
209 | * Sets / overwrites a specific config value. |
||
210 | * |
||
211 | * @param string $key Key to set |
||
212 | * @param mixed $value Value |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function setConfigValue($key, $value) |
||
220 | |||
221 | /** |
||
222 | * @param array|string $keys config key or path of config keys |
||
223 | * @param mixed $default default value will be returned if key was not found |
||
224 | * |
||
225 | * @return mixed |
||
226 | */ |
||
227 | public function getConfigValue($keys, $default = null) |
||
240 | |||
241 | /** |
||
242 | * Returns the index for the given connection. |
||
243 | * |
||
244 | * @param string $name Index name to create connection to |
||
245 | * |
||
246 | * @return \Elastica\Index Index for the given name |
||
247 | */ |
||
248 | public function getIndex($name) |
||
252 | |||
253 | /** |
||
254 | * Adds a HTTP Header. |
||
255 | * |
||
256 | * @param string $header The HTTP Header |
||
257 | * @param string $headerValue The HTTP Header Value |
||
258 | * |
||
259 | * @throws \Elastica\Exception\InvalidException If $header or $headerValue is not a string |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | View Code Duplication | public function addHeader($header, $headerValue) |
|
273 | |||
274 | /** |
||
275 | * Remove a HTTP Header. |
||
276 | * |
||
277 | * @param string $header The HTTP Header to remove |
||
278 | * |
||
279 | * @throws \Elastica\Exception\InvalidException If $header is not a string |
||
280 | * |
||
281 | * @return $this |
||
282 | */ |
||
283 | View Code Duplication | public function removeHeader($header) |
|
295 | |||
296 | /** |
||
297 | * Uses _bulk to send documents to the server. |
||
298 | * |
||
299 | * Array of \Elastica\Document as input. Index and type has to be |
||
300 | * set inside the document, because for bulk settings documents, |
||
301 | * documents can belong to any type and index |
||
302 | * |
||
303 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
304 | * |
||
305 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
306 | * |
||
307 | * @throws \Elastica\Exception\InvalidException If docs is empty |
||
308 | * |
||
309 | * @return \Elastica\Bulk\ResponseSet Response object |
||
310 | */ |
||
311 | View Code Duplication | public function updateDocuments(array $docs) |
|
323 | |||
324 | /** |
||
325 | * Uses _bulk to send documents to the server. |
||
326 | * |
||
327 | * Array of \Elastica\Document as input. Index and type has to be |
||
328 | * set inside the document, because for bulk settings documents, |
||
329 | * documents can belong to any type and index |
||
330 | * |
||
331 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
332 | * |
||
333 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
334 | * |
||
335 | * @throws \Elastica\Exception\InvalidException If docs is empty |
||
336 | * |
||
337 | * @return \Elastica\Bulk\ResponseSet Response object |
||
338 | */ |
||
339 | View Code Duplication | public function addDocuments(array $docs) |
|
351 | |||
352 | /** |
||
353 | * Update document, using update script. Requires elasticsearch >= 0.19.0. |
||
354 | * |
||
355 | * @param int|string $id document id |
||
356 | * @param array|\Elastica\Script\AbstractScript|\Elastica\Document $data raw data for request body |
||
357 | * @param string $index index to update |
||
358 | * @param string $type type of index to update |
||
359 | * @param array $options array of query params to use for query. For possible options check es api |
||
360 | * |
||
361 | * @return \Elastica\Response |
||
362 | * |
||
363 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html |
||
364 | */ |
||
365 | public function updateDocument($id, $data, $index, $type, array $options = []) |
||
435 | |||
436 | /** |
||
437 | * @param \Elastica\Response $response |
||
438 | * @param \Elastica\Document $document |
||
439 | * @param string $fields Array of field names to be populated or '_source' if whole document data should be updated |
||
440 | */ |
||
441 | protected function _populateDocumentFieldsFromResponse(Response $response, Document $document, $fields) |
||
461 | |||
462 | /** |
||
463 | * Bulk deletes documents. |
||
464 | * |
||
465 | * @param array|\Elastica\Document[] $docs |
||
466 | * |
||
467 | * @throws \Elastica\Exception\InvalidException |
||
468 | * |
||
469 | * @return \Elastica\Bulk\ResponseSet |
||
470 | */ |
||
471 | View Code Duplication | public function deleteDocuments(array $docs) |
|
482 | |||
483 | /** |
||
484 | * Returns the status object for all indices. |
||
485 | * |
||
486 | * @return \Elastica\Status Status object |
||
487 | */ |
||
488 | public function getStatus() |
||
492 | |||
493 | /** |
||
494 | * Returns the current cluster. |
||
495 | * |
||
496 | * @return \Elastica\Cluster Cluster object |
||
497 | */ |
||
498 | public function getCluster() |
||
502 | |||
503 | /** |
||
504 | * Establishes the client connections. |
||
505 | */ |
||
506 | public function connect() |
||
510 | |||
511 | /** |
||
512 | * @param \Elastica\Connection $connection |
||
513 | * |
||
514 | * @return $this |
||
515 | */ |
||
516 | public function addConnection(Connection $connection) |
||
522 | |||
523 | /** |
||
524 | * Determines whether a valid connection is available for use. |
||
525 | * |
||
526 | * @return bool |
||
527 | */ |
||
528 | public function hasConnection() |
||
532 | |||
533 | /** |
||
534 | * @throws \Elastica\Exception\ClientException |
||
535 | * |
||
536 | * @return \Elastica\Connection |
||
537 | */ |
||
538 | public function getConnection() |
||
542 | |||
543 | /** |
||
544 | * @return \Elastica\Connection[] |
||
545 | */ |
||
546 | public function getConnections() |
||
550 | |||
551 | /** |
||
552 | * @return \Elastica\Connection\Strategy\StrategyInterface |
||
553 | */ |
||
554 | public function getConnectionStrategy() |
||
558 | |||
559 | /** |
||
560 | * @param array|\Elastica\Connection[] $connections |
||
561 | * |
||
562 | * @return $this |
||
563 | */ |
||
564 | public function setConnections(array $connections) |
||
570 | |||
571 | /** |
||
572 | * Deletes documents with the given ids, index, type from the index. |
||
573 | * |
||
574 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
575 | * |
||
576 | * @param array $ids Document ids |
||
577 | * @param string|\Elastica\Index $index Index name |
||
578 | * @param string|\Elastica\Type $type Type of documents |
||
579 | * @param string|bool $routing Optional routing key for all ids |
||
580 | * |
||
581 | * @throws \Elastica\Exception\InvalidException |
||
582 | * |
||
583 | * @return \Elastica\Bulk\ResponseSet Response object |
||
584 | */ |
||
585 | public function deleteIds(array $ids, $index, $type, $routing = false) |
||
608 | |||
609 | /** |
||
610 | * Bulk operation. |
||
611 | * |
||
612 | * Every entry in the params array has to exactly on array |
||
613 | * of the bulk operation. An example param array would be: |
||
614 | * |
||
615 | * array( |
||
616 | * array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')), |
||
617 | * array('user' => array('name' => 'hans')), |
||
618 | * array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '2')) |
||
619 | * ); |
||
620 | * |
||
621 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
622 | * |
||
623 | * @param array $params Parameter array |
||
624 | * |
||
625 | * @throws \Elastica\Exception\ResponseException |
||
626 | * @throws \Elastica\Exception\InvalidException |
||
627 | * |
||
628 | * @return \Elastica\Bulk\ResponseSet Response object |
||
629 | */ |
||
630 | public function bulk(array $params) |
||
642 | |||
643 | /** |
||
644 | * Makes calls to the elasticsearch server based on this index. |
||
645 | * |
||
646 | * It's possible to make any REST query directly over this method |
||
647 | * |
||
648 | * @param string $path Path to call |
||
649 | * @param string $method Rest method to use (GET, POST, DELETE, PUT) |
||
650 | * @param array|string $data OPTIONAL Arguments as array or pre-encoded string |
||
651 | * @param array $query OPTIONAL Query params |
||
652 | * |
||
653 | * @throws Exception\ConnectionException|\Exception |
||
654 | * |
||
655 | * @return Response Response object |
||
656 | */ |
||
657 | public function request($path, $method = Request::GET, $data = [], array $query = []) |
||
682 | |||
683 | /** |
||
684 | * Makes calls to the elasticsearch server with usage official client Endpoint |
||
685 | * |
||
686 | * @param AbstractEndpoint $endpoint |
||
687 | * @return Response |
||
688 | */ |
||
689 | public function requestEndpoint(AbstractEndpoint $endpoint) |
||
698 | |||
699 | /** |
||
700 | * logging. |
||
701 | * |
||
702 | * @deprecated Overwriting Client->_log is deprecated. Handle logging functionality by using a custom LoggerInterface. |
||
703 | * |
||
704 | * @param mixed $context |
||
705 | */ |
||
706 | protected function _log($context) |
||
732 | |||
733 | /** |
||
734 | * Optimizes all search indices. |
||
735 | * |
||
736 | * @param array $args OPTIONAL Optional arguments |
||
737 | * |
||
738 | * @return \Elastica\Response Response object |
||
739 | * |
||
740 | * @deprecated Replaced by forcemergeAll |
||
741 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-optimize.html |
||
742 | */ |
||
743 | public function optimizeAll($args = []) |
||
749 | |||
750 | /** |
||
751 | * Force merges all search indices. |
||
752 | * |
||
753 | * @param array $args OPTIONAL Optional arguments |
||
754 | * |
||
755 | * @return \Elastica\Response Response object |
||
756 | * |
||
757 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html |
||
758 | */ |
||
759 | public function forcemergeAll($args = []) |
||
763 | |||
764 | /** |
||
765 | * Refreshes all search indices. |
||
766 | * |
||
767 | * @return \Elastica\Response Response object |
||
768 | * |
||
769 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html |
||
770 | */ |
||
771 | public function refreshAll() |
||
775 | |||
776 | /** |
||
777 | * @return Request|null |
||
778 | */ |
||
779 | public function getLastRequest() |
||
783 | |||
784 | /** |
||
785 | * @return Response|null |
||
786 | */ |
||
787 | public function getLastResponse() |
||
791 | |||
792 | /** |
||
793 | * Replace the existing logger. |
||
794 | * |
||
795 | * @param LoggerInterface $logger |
||
796 | * |
||
797 | * @return $this |
||
798 | */ |
||
799 | public function setLogger(LoggerInterface $logger) |
||
805 | } |
||
806 |
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.