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 Index 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 Index, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class Index implements SearchableInterface |
||
36 | { |
||
37 | /** |
||
38 | * Index name. |
||
39 | * |
||
40 | * @var string Index name |
||
41 | */ |
||
42 | protected $_name; |
||
43 | |||
44 | /** |
||
45 | * Client object. |
||
46 | * |
||
47 | * @var \Elastica\Client Client object |
||
48 | */ |
||
49 | protected $_client; |
||
50 | |||
51 | /** |
||
52 | * Creates a new index object. |
||
53 | * |
||
54 | * All the communication to and from an index goes of this object |
||
55 | * |
||
56 | * @param \Elastica\Client $client Client object |
||
57 | * @param string $name Index name |
||
58 | */ |
||
59 | View Code Duplication | public function __construct(Client $client, $name) |
|
68 | |||
69 | /** |
||
70 | * Returns a type object for the current index with the given name. |
||
71 | * |
||
72 | * @param string $type Type name |
||
73 | * |
||
74 | * @return \Elastica\Type Type object |
||
75 | */ |
||
76 | public function getType($type) |
||
80 | |||
81 | /** |
||
82 | * Return Index Stats. |
||
83 | * |
||
84 | * @return \Elastica\Index\Stats |
||
85 | */ |
||
86 | public function getStats() |
||
90 | |||
91 | /** |
||
92 | * Return Index Recovery. |
||
93 | * |
||
94 | * @return \Elastica\Index\Recovery |
||
95 | */ |
||
96 | public function getRecovery() |
||
100 | |||
101 | /** |
||
102 | * Gets all the type mappings for an index. |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | View Code Duplication | public function getMapping() |
|
120 | |||
121 | /** |
||
122 | * Returns the index settings object. |
||
123 | * |
||
124 | * @return \Elastica\Index\Settings Settings object |
||
125 | */ |
||
126 | public function getSettings() |
||
130 | |||
131 | /** |
||
132 | * Uses _bulk to send documents to the server. |
||
133 | * |
||
134 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
135 | * @param array $options Array of query params to use for query. For possible options check es api |
||
136 | * |
||
137 | * @return \Elastica\Bulk\ResponseSet |
||
138 | * |
||
139 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
140 | */ |
||
141 | public function updateDocuments(array $docs, array $options = []) |
||
149 | |||
150 | /** |
||
151 | * Update entries in the db based on a query. |
||
152 | * |
||
153 | * @param \Elastica\Query|string|array $query Query object or array |
||
154 | * @param AbstractScript $script Script |
||
155 | * @param array $options Optional params |
||
156 | * |
||
157 | * @return \Elastica\Response |
||
158 | * |
||
159 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html |
||
160 | */ |
||
161 | public function updateByQuery($query, AbstractScript $script, array $options = []) |
||
176 | |||
177 | /** |
||
178 | * Uses _bulk to send documents to the server. |
||
179 | * |
||
180 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
181 | * @param array $options Array of query params to use for query. For possible options check es api |
||
182 | * |
||
183 | * @return \Elastica\Bulk\ResponseSet |
||
184 | * |
||
185 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
186 | */ |
||
187 | public function addDocuments(array $docs, array $options = []) |
||
195 | |||
196 | /** |
||
197 | * Deletes entries in the db based on a query. |
||
198 | * |
||
199 | * @param \Elastica\Query|\Elastica\Query\AbstractQuery|string|array $query Query object or array |
||
200 | * @param array $options Optional params |
||
201 | * |
||
202 | * @return \Elastica\Response |
||
203 | * |
||
204 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/5.0/docs-delete-by-query.html |
||
205 | */ |
||
206 | public function deleteByQuery($query, array $options = []) |
||
216 | |||
217 | /** |
||
218 | * Deletes the index. |
||
219 | * |
||
220 | * @return \Elastica\Response Response object |
||
221 | */ |
||
222 | public function delete() |
||
226 | |||
227 | /** |
||
228 | * Uses _bulk to delete documents from the server. |
||
229 | * |
||
230 | * @param array|\Elastica\Document[] $docs Array of Elastica\Document |
||
231 | * |
||
232 | * @return \Elastica\Bulk\ResponseSet |
||
233 | * |
||
234 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
235 | */ |
||
236 | public function deleteDocuments(array $docs) |
||
244 | |||
245 | /** |
||
246 | * Force merges index. |
||
247 | * |
||
248 | * Detailed arguments can be found here in the link |
||
249 | * |
||
250 | * @param array $args OPTIONAL Additional arguments |
||
251 | * |
||
252 | * @return Response |
||
253 | * |
||
254 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html |
||
255 | */ |
||
256 | public function forcemerge($args = []) |
||
263 | |||
264 | /** |
||
265 | * Refreshes the index. |
||
266 | * |
||
267 | * @return \Elastica\Response Response object |
||
268 | * |
||
269 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html |
||
270 | */ |
||
271 | public function refresh() |
||
275 | |||
276 | /** |
||
277 | * Creates a new index with the given arguments. |
||
278 | * |
||
279 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html |
||
280 | * |
||
281 | * @param array $args OPTIONAL Arguments to use |
||
282 | * @param bool|array $options OPTIONAL |
||
283 | * bool=> Deletes index first if already exists (default = false). |
||
284 | * array => Associative array of options (option=>value) |
||
285 | * |
||
286 | * @throws \Elastica\Exception\InvalidException |
||
287 | * @throws \Elastica\Exception\ResponseException |
||
288 | * |
||
289 | * @return \Elastica\Response Server response |
||
290 | */ |
||
291 | public function create(array $args = [], $options = null) |
||
321 | |||
322 | /** |
||
323 | * Checks if the given index is already created. |
||
324 | * |
||
325 | * @return bool True if index exists |
||
326 | */ |
||
327 | public function exists() |
||
333 | |||
334 | /** |
||
335 | * @param string|array|\Elastica\Query $query |
||
336 | * @param int|array $options |
||
337 | * @param BuilderInterface $builder |
||
338 | * |
||
339 | * @return Search |
||
340 | */ |
||
341 | public function createSearch($query = '', $options = null, BuilderInterface $builder = null) |
||
349 | |||
350 | /** |
||
351 | * Searches in this index. |
||
352 | * |
||
353 | * @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object |
||
354 | * @param int|array $options OPTIONAL Limit or associative array of options (option=>value) |
||
355 | * |
||
356 | * @return \Elastica\ResultSet with all results inside |
||
357 | * |
||
358 | * @see \Elastica\SearchableInterface::search |
||
359 | */ |
||
360 | public function search($query = '', $options = null) |
||
366 | |||
367 | /** |
||
368 | * Counts results of query. |
||
369 | * |
||
370 | * @param string|array|\Elastica\Query $query Array with all query data inside or a Elastica\Query object |
||
371 | * |
||
372 | * @return int number of documents matching the query |
||
373 | * |
||
374 | * @see \Elastica\SearchableInterface::count |
||
375 | */ |
||
376 | public function count($query = '') |
||
382 | |||
383 | /** |
||
384 | * Opens an index. |
||
385 | * |
||
386 | * @return \Elastica\Response Response object |
||
387 | * |
||
388 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html |
||
389 | */ |
||
390 | public function open() |
||
394 | |||
395 | /** |
||
396 | * Closes the index. |
||
397 | * |
||
398 | * @return \Elastica\Response Response object |
||
399 | * |
||
400 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html |
||
401 | */ |
||
402 | public function close() |
||
406 | |||
407 | /** |
||
408 | * Returns the index name. |
||
409 | * |
||
410 | * @return string Index name |
||
411 | */ |
||
412 | public function getName() |
||
416 | |||
417 | /** |
||
418 | * Returns index client. |
||
419 | * |
||
420 | * @return \Elastica\Client Index client object |
||
421 | */ |
||
422 | public function getClient() |
||
426 | |||
427 | /** |
||
428 | * Adds an alias to the current index. |
||
429 | * |
||
430 | * @param string $name Alias name |
||
431 | * @param bool $replace OPTIONAL If set, an existing alias will be replaced |
||
432 | * |
||
433 | * @return \Elastica\Response Response |
||
434 | * |
||
435 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html |
||
436 | */ |
||
437 | public function addAlias($name, $replace = false) |
||
455 | |||
456 | /** |
||
457 | * Removes an alias pointing to the current index. |
||
458 | * |
||
459 | * @param string $name Alias name |
||
460 | * |
||
461 | * @return \Elastica\Response Response |
||
462 | * |
||
463 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html |
||
464 | */ |
||
465 | public function removeAlias($name) |
||
472 | |||
473 | /** |
||
474 | * Returns all index aliases. |
||
475 | * |
||
476 | * @return array Aliases |
||
477 | */ |
||
478 | public function getAliases() |
||
496 | |||
497 | /** |
||
498 | * Checks if the index has the given alias. |
||
499 | * |
||
500 | * @param string $name Alias name |
||
501 | * |
||
502 | * @return bool |
||
503 | */ |
||
504 | public function hasAlias($name) |
||
508 | |||
509 | /** |
||
510 | * Clears the cache of an index. |
||
511 | * |
||
512 | * @return \Elastica\Response Response object |
||
513 | * |
||
514 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-clearcache.html |
||
515 | */ |
||
516 | public function clearCache() |
||
521 | |||
522 | /** |
||
523 | * Flushes the index to storage. |
||
524 | * |
||
525 | * @param array $options |
||
526 | * |
||
527 | * @return Response Response object |
||
528 | * |
||
529 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-flush.html |
||
530 | */ |
||
531 | public function flush(array $options = []) |
||
538 | |||
539 | /** |
||
540 | * Can be used to change settings during runtime. One example is to use it for bulk updating. |
||
541 | * |
||
542 | * @param array $data Data array |
||
543 | * |
||
544 | * @return \Elastica\Response Response object |
||
545 | * |
||
546 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html |
||
547 | */ |
||
548 | public function setSettings(array $data) |
||
555 | |||
556 | /** |
||
557 | * Makes calls to the elasticsearch server based on this index. |
||
558 | * |
||
559 | * @param string $path Path to call |
||
560 | * @param string $method Rest method to use (GET, POST, DELETE, PUT) |
||
561 | * @param array|string $data OPTIONAL Arguments as array or encoded string |
||
562 | * @param array $query OPTIONAL Query params |
||
563 | * |
||
564 | * @return \Elastica\Response Response object |
||
565 | */ |
||
566 | public function request($path, $method, $data = [], array $query = []) |
||
572 | |||
573 | /** |
||
574 | * Makes calls to the elasticsearch server with usage official client Endpoint based on this index. |
||
575 | * |
||
576 | * @param AbstractEndpoint $endpoint |
||
577 | * |
||
578 | * @return Response |
||
579 | */ |
||
580 | public function requestEndpoint(AbstractEndpoint $endpoint) |
||
587 | |||
588 | /** |
||
589 | * Analyzes a string. |
||
590 | * |
||
591 | * Detailed arguments can be found here in the link |
||
592 | * |
||
593 | * @param array $body String to be analyzed |
||
594 | * @param array $args OPTIONAL Additional arguments |
||
595 | * |
||
596 | * @return array Server response |
||
597 | * |
||
598 | * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html |
||
599 | */ |
||
600 | public function analyze(array $body, $args = []) |
||
616 | } |
||
617 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.