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 |
||
19 | class Client |
||
20 | { |
||
21 | /** |
||
22 | * Config with defaults. |
||
23 | * |
||
24 | * log: Set to true, to enable logging, set a string to log to a specific file |
||
25 | * retryOnConflict: Use in \Elastica\Client::updateDocument |
||
26 | * bigintConversion: Set to true to enable the JSON bigint to string conversion option (see issue #717) |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $_config = array( |
||
31 | 'host' => null, |
||
32 | 'port' => null, |
||
33 | 'path' => null, |
||
34 | 'url' => null, |
||
35 | 'proxy' => null, |
||
36 | 'transport' => null, |
||
37 | 'persistent' => true, |
||
38 | 'timeout' => null, |
||
39 | 'connections' => array(), // host, port, path, timeout, transport, compression, persistent, timeout, config -> (curl, headers, url) |
||
40 | 'roundRobin' => false, |
||
41 | 'log' => false, |
||
42 | 'retryOnConflict' => 0, |
||
43 | 'bigintConversion' => false, |
||
44 | 'username' => null, |
||
45 | 'password' => null, |
||
46 | ); |
||
47 | |||
48 | /** |
||
49 | * @var callback |
||
50 | */ |
||
51 | protected $_callback = null; |
||
52 | |||
53 | /** |
||
54 | * @var Connection\ConnectionPool |
||
55 | */ |
||
56 | protected $_connectionPool = null; |
||
57 | |||
58 | /** |
||
59 | * @var \Elastica\Request |
||
60 | */ |
||
61 | protected $_lastRequest; |
||
62 | |||
63 | /** |
||
64 | * @var \Elastica\Response |
||
65 | */ |
||
66 | protected $_lastResponse; |
||
67 | |||
68 | /** |
||
69 | * @var LoggerInterface |
||
70 | */ |
||
71 | protected $_logger = null; |
||
72 | |||
73 | /** |
||
74 | * @var BuilderInterface |
||
75 | */ |
||
76 | private $_resultSetBuilder; |
||
77 | |||
78 | /** |
||
79 | * Creates a new Elastica client. |
||
80 | * |
||
81 | * @param array $config OPTIONAL Additional config options |
||
82 | * @param callback $callback OPTIONAL Callback function which can be used to be notified about errors (for example connection down) |
||
83 | * @param BuilderInterface $resultSetBuilder |
||
84 | */ |
||
85 | public function __construct(array $config = array(), $callback = null, BuilderInterface $resultSetBuilder = null) |
||
93 | |||
94 | /** |
||
95 | * Inits the client connections. |
||
96 | */ |
||
97 | protected function _initConnections() |
||
128 | |||
129 | /** |
||
130 | * Creates a Connection params array from a Client or server config array. |
||
131 | * |
||
132 | * @param array $config |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | protected function _prepareConnectionParams(array $config) |
||
150 | |||
151 | /** |
||
152 | * Sets specific config values (updates and keeps default values). |
||
153 | * |
||
154 | * @param array $config Params |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function setConfig(array $config) |
||
166 | |||
167 | /** |
||
168 | * Returns a specific config key or the whole |
||
169 | * config array if not set. |
||
170 | * |
||
171 | * @param string $key Config key |
||
172 | * |
||
173 | * @throws \Elastica\Exception\InvalidException |
||
174 | * |
||
175 | * @return array|string Config value |
||
176 | */ |
||
177 | public function getConfig($key = '') |
||
189 | |||
190 | /** |
||
191 | * Sets / overwrites a specific config value. |
||
192 | * |
||
193 | * @param string $key Key to set |
||
194 | * @param mixed $value Value |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function setConfigValue($key, $value) |
||
202 | |||
203 | /** |
||
204 | * @param array|string $keys config key or path of config keys |
||
205 | * @param mixed $default default value will be returned if key was not found |
||
206 | * |
||
207 | * @return mixed |
||
208 | */ |
||
209 | public function getConfigValue($keys, $default = null) |
||
222 | |||
223 | /** |
||
224 | * Returns the index for the given connection. |
||
225 | * |
||
226 | * @param string $name Index name to create connection to |
||
227 | * |
||
228 | * @return \Elastica\Index Index for the given name |
||
229 | */ |
||
230 | public function getIndex($name) |
||
234 | |||
235 | /** |
||
236 | * Adds a HTTP Header. |
||
237 | * |
||
238 | * @param string $header The HTTP Header |
||
239 | * @param string $headerValue The HTTP Header Value |
||
240 | * |
||
241 | * @throws \Elastica\Exception\InvalidException If $header or $headerValue is not a string |
||
242 | * |
||
243 | * @return $this |
||
244 | */ |
||
245 | View Code Duplication | public function addHeader($header, $headerValue) |
|
255 | |||
256 | /** |
||
257 | * Remove a HTTP Header. |
||
258 | * |
||
259 | * @param string $header The HTTP Header to remove |
||
260 | * |
||
261 | * @throws \Elastica\Exception\InvalidException If $header is not a string |
||
262 | * |
||
263 | * @return $this |
||
264 | */ |
||
265 | View Code Duplication | public function removeHeader($header) |
|
277 | |||
278 | /** |
||
279 | * Uses _bulk to send documents to the server. |
||
280 | * |
||
281 | * Array of \Elastica\Document as input. Index and type has to be |
||
282 | * set inside the document, because for bulk settings documents, |
||
283 | * documents can belong to any type and index |
||
284 | * |
||
285 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
286 | * |
||
287 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
288 | * |
||
289 | * @throws \Elastica\Exception\InvalidException If docs is empty |
||
290 | * |
||
291 | * @return \Elastica\Bulk\ResponseSet Response object |
||
292 | */ |
||
293 | View Code Duplication | public function updateDocuments(array $docs) |
|
305 | |||
306 | /** |
||
307 | * Uses _bulk to send documents to the server. |
||
308 | * |
||
309 | * Array of \Elastica\Document as input. Index and type has to be |
||
310 | * set inside the document, because for bulk settings documents, |
||
311 | * documents can belong to any type and index |
||
312 | * |
||
313 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
314 | * |
||
315 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
316 | * |
||
317 | * @throws \Elastica\Exception\InvalidException If docs is empty |
||
318 | * |
||
319 | * @return \Elastica\Bulk\ResponseSet Response object |
||
320 | */ |
||
321 | View Code Duplication | public function addDocuments(array $docs) |
|
333 | |||
334 | /** |
||
335 | * Update document, using update script. Requires elasticsearch >= 0.19.0. |
||
336 | * |
||
337 | * @param int $id document id |
||
338 | * @param array|\Elastica\Script\AbstractScript|\Elastica\Document $data raw data for request body |
||
339 | * @param string $index index to update |
||
340 | * @param string $type type of index to update |
||
341 | * @param array $options array of query params to use for query. For possible options check es api |
||
342 | * |
||
343 | * @return \Elastica\Response |
||
344 | * |
||
345 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html |
||
346 | */ |
||
347 | public function updateDocument($id, $data, $index, $type, array $options = array()) |
||
416 | |||
417 | /** |
||
418 | * @param \Elastica\Response $response |
||
419 | * @param \Elastica\Document $document |
||
420 | * @param string $fields Array of field names to be populated or '_source' if whole document data should be updated |
||
421 | */ |
||
422 | protected function _populateDocumentFieldsFromResponse(Response $response, Document $document, $fields) |
||
442 | |||
443 | /** |
||
444 | * Bulk deletes documents. |
||
445 | * |
||
446 | * @param array|\Elastica\Document[] $docs |
||
447 | * |
||
448 | * @throws \Elastica\Exception\InvalidException |
||
449 | * |
||
450 | * @return \Elastica\Bulk\ResponseSet |
||
451 | */ |
||
452 | View Code Duplication | public function deleteDocuments(array $docs) |
|
463 | |||
464 | /** |
||
465 | * Returns the status object for all indices. |
||
466 | * |
||
467 | * @return \Elastica\Status Status object |
||
468 | */ |
||
469 | public function getStatus() |
||
473 | |||
474 | /** |
||
475 | * Returns the current cluster. |
||
476 | * |
||
477 | * @return \Elastica\Cluster Cluster object |
||
478 | */ |
||
479 | public function getCluster() |
||
483 | |||
484 | /** |
||
485 | * @param \Elastica\Connection $connection |
||
486 | * |
||
487 | * @return $this |
||
488 | */ |
||
489 | public function addConnection(Connection $connection) |
||
495 | |||
496 | /** |
||
497 | * Determines whether a valid connection is available for use. |
||
498 | * |
||
499 | * @return bool |
||
500 | */ |
||
501 | public function hasConnection() |
||
505 | |||
506 | /** |
||
507 | * @throws \Elastica\Exception\ClientException |
||
508 | * |
||
509 | * @return \Elastica\Connection |
||
510 | */ |
||
511 | public function getConnection() |
||
515 | |||
516 | /** |
||
517 | * @return \Elastica\Connection[] |
||
518 | */ |
||
519 | public function getConnections() |
||
523 | |||
524 | /** |
||
525 | * @return \Elastica\Connection\Strategy\StrategyInterface |
||
526 | */ |
||
527 | public function getConnectionStrategy() |
||
531 | |||
532 | /** |
||
533 | * @param array|\Elastica\Connection[] $connections |
||
534 | * |
||
535 | * @return $this |
||
536 | */ |
||
537 | public function setConnections(array $connections) |
||
543 | |||
544 | /** |
||
545 | * Deletes documents with the given ids, index, type from the index. |
||
546 | * |
||
547 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
548 | * |
||
549 | * @param array $ids Document ids |
||
550 | * @param string|\Elastica\Index $index Index name |
||
551 | * @param string|\Elastica\Type $type Type of documents |
||
552 | * @param string|bool $routing Optional routing key for all ids |
||
553 | * |
||
554 | * @throws \Elastica\Exception\InvalidException |
||
555 | * |
||
556 | * @return \Elastica\Bulk\ResponseSet Response object |
||
557 | */ |
||
558 | public function deleteIds(array $ids, $index, $type, $routing = false) |
||
581 | |||
582 | /** |
||
583 | * Bulk operation. |
||
584 | * |
||
585 | * Every entry in the params array has to exactly on array |
||
586 | * of the bulk operation. An example param array would be: |
||
587 | * |
||
588 | * array( |
||
589 | * array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')), |
||
590 | * array('user' => array('name' => 'hans')), |
||
591 | * array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '2')) |
||
592 | * ); |
||
593 | * |
||
594 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
595 | * |
||
596 | * @param array $params Parameter array |
||
597 | * |
||
598 | * @throws \Elastica\Exception\ResponseException |
||
599 | * @throws \Elastica\Exception\InvalidException |
||
600 | * |
||
601 | * @return \Elastica\Bulk\ResponseSet Response object |
||
602 | */ |
||
603 | public function bulk(array $params) |
||
615 | |||
616 | /** |
||
617 | * Makes calls to the elasticsearch server based on this index. |
||
618 | * |
||
619 | * It's possible to make any REST query directly over this method |
||
620 | * |
||
621 | * @param string $path Path to call |
||
622 | * @param string $method Rest method to use (GET, POST, DELETE, PUT) |
||
623 | * @param array $data OPTIONAL Arguments as array |
||
624 | * @param array $query OPTIONAL Query params |
||
625 | * |
||
626 | * @throws Exception\ConnectionException|\Exception |
||
627 | * |
||
628 | * @return \Elastica\Response Response object |
||
629 | */ |
||
630 | public function request($path, $method = Request::GET, $data = array(), array $query = array()) |
||
655 | |||
656 | /** |
||
657 | * Optimizes all search indices. |
||
658 | * |
||
659 | * @param array $args OPTIONAL Optional arguments |
||
660 | * |
||
661 | * @return \Elastica\Response Response object |
||
662 | * |
||
663 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-optimize.html |
||
664 | */ |
||
665 | public function optimizeAll($args = array()) |
||
669 | |||
670 | /** |
||
671 | * Refreshes all search indices. |
||
672 | * |
||
673 | * @return \Elastica\Response Response object |
||
674 | * |
||
675 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html |
||
676 | */ |
||
677 | public function refreshAll() |
||
681 | |||
682 | /** |
||
683 | * logging. |
||
684 | * |
||
685 | * @param string|\Elastica\Request $context |
||
686 | * |
||
687 | * @throws Exception\RuntimeException |
||
688 | */ |
||
689 | protected function _log($context) |
||
706 | |||
707 | /** |
||
708 | * @return \Elastica\Request |
||
709 | */ |
||
710 | public function getLastRequest() |
||
714 | |||
715 | /** |
||
716 | * @return \Elastica\Response |
||
717 | */ |
||
718 | public function getLastResponse() |
||
722 | |||
723 | /** |
||
724 | * set Logger. |
||
725 | * |
||
726 | * @param LoggerInterface $logger |
||
727 | * |
||
728 | * @return $this |
||
729 | */ |
||
730 | public function setLogger(LoggerInterface $logger) |
||
736 | |||
737 | /** |
||
738 | * @return BuilderInterface |
||
739 | */ |
||
740 | public function getResultSetBuilder() |
||
744 | } |
||
745 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: