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 |
||
42 | class Index implements SearchableInterface |
||
43 | { |
||
44 | /** |
||
45 | * Index name. |
||
46 | * |
||
47 | * @var string Index name |
||
48 | */ |
||
49 | protected $_name; |
||
50 | |||
51 | /** |
||
52 | * Client object. |
||
53 | * |
||
54 | * @var Client Client object |
||
55 | */ |
||
56 | protected $_client; |
||
57 | |||
58 | /** |
||
59 | * Creates a new index object. |
||
60 | * |
||
61 | * All the communication to and from an index goes of this object |
||
62 | * |
||
63 | * @param Client $client Client object |
||
64 | * @param string $name Index name |
||
65 | */ |
||
66 | public function __construct(Client $client, string $name) |
||
71 | |||
72 | /** |
||
73 | * Return Index Stats. |
||
74 | * |
||
75 | * @return \Elastica\Index\Stats |
||
76 | */ |
||
77 | public function getStats() |
||
81 | |||
82 | /** |
||
83 | * Return Index Recovery. |
||
84 | * |
||
85 | * @return \Elastica\Index\Recovery |
||
86 | */ |
||
87 | public function getRecovery() |
||
91 | |||
92 | /** |
||
93 | * Sets the mappings for the current index. |
||
94 | * |
||
95 | * @param Mapping $mapping MappingType object |
||
96 | * @param array $query querystring when put mapping (for example update_all_types) |
||
97 | */ |
||
98 | public function setMapping(Mapping $mapping, array $query = []): Response |
||
102 | |||
103 | /** |
||
104 | * Gets all mappings for the current index. |
||
105 | */ |
||
106 | public function getMapping(): array |
||
116 | |||
117 | /** |
||
118 | * Returns the index settings object. |
||
119 | * |
||
120 | * @return \Elastica\Index\Settings Settings object |
||
121 | */ |
||
122 | public function getSettings() |
||
126 | |||
127 | /** |
||
128 | * @param array|string $data |
||
129 | * |
||
130 | * @return Document |
||
131 | */ |
||
132 | public function createDocument(string $id = '', $data = []) |
||
136 | |||
137 | /** |
||
138 | * Uses _bulk to send documents to the server. |
||
139 | * |
||
140 | * @param Document[] $docs Array of Elastica\Document |
||
141 | * @param array $options Array of query params to use for query. For possible options check es api |
||
142 | * |
||
143 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
144 | */ |
||
145 | public function updateDocuments(array $docs, array $options = []): ResponseSet |
||
153 | |||
154 | /** |
||
155 | * Update entries in the db based on a query. |
||
156 | * |
||
157 | * @param array|Query|string $query Query object or array |
||
158 | * @param AbstractScript $script Script |
||
159 | * @param array $options Optional params |
||
160 | * |
||
161 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html |
||
162 | */ |
||
163 | public function updateByQuery($query, AbstractScript $script, array $options = []): Response |
||
177 | |||
178 | /** |
||
179 | * Adds the given document to the search index. |
||
180 | */ |
||
181 | public function addDocument(Document $doc): Response |
||
231 | |||
232 | /** |
||
233 | * Uses _bulk to send documents to the server. |
||
234 | * |
||
235 | * @param array|Document[] $docs Array of Elastica\Document |
||
236 | * @param array $options Array of query params to use for query. For possible options check es api |
||
237 | * |
||
238 | * @return ResponseSet |
||
239 | * |
||
240 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
241 | */ |
||
242 | public function addDocuments(array $docs, array $options = []) |
||
250 | |||
251 | /** |
||
252 | * Get the document from search index. |
||
253 | * |
||
254 | * @param int|string $id Document id |
||
255 | * @param array $options options for the get request |
||
256 | * |
||
257 | * @throws \Elastica\Exception\ResponseException |
||
258 | * @throws NotFoundException |
||
259 | */ |
||
260 | public function getDocument($id, array $options = []): Document |
||
294 | |||
295 | /** |
||
296 | * Deletes a document by its unique identifier. |
||
297 | * |
||
298 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete.html |
||
299 | */ |
||
300 | public function deleteById(string $id, array $options = []): Response |
||
312 | |||
313 | /** |
||
314 | * Deletes documents matching the given query. |
||
315 | * |
||
316 | * @param AbstractQuery|array|Query|string $query Query object or array |
||
317 | * @param array $options Optional params |
||
318 | * |
||
319 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html |
||
320 | */ |
||
321 | public function deleteByQuery($query, array $options = []): Response |
||
331 | |||
332 | /** |
||
333 | * Deletes the index. |
||
334 | */ |
||
335 | public function delete(): Response |
||
339 | |||
340 | /** |
||
341 | * Uses the "_bulk" endpoint to delete documents from the server. |
||
342 | * |
||
343 | * @param Document[] $docs Array of documents |
||
344 | * |
||
345 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
||
346 | */ |
||
347 | public function deleteDocuments(array $docs): ResponseSet |
||
355 | |||
356 | /** |
||
357 | * Force merges index. |
||
358 | * |
||
359 | * Detailed arguments can be found here in the ES documentation. |
||
360 | * |
||
361 | * @param array $args Additional arguments |
||
362 | * |
||
363 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html |
||
364 | */ |
||
365 | public function forcemerge($args = []): Response |
||
372 | |||
373 | /** |
||
374 | * Refreshes the index. |
||
375 | * |
||
376 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html |
||
377 | */ |
||
378 | public function refresh(): Response |
||
382 | |||
383 | /** |
||
384 | * Creates a new index with the given arguments. |
||
385 | * |
||
386 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html |
||
387 | * |
||
388 | * @param array $args Additional arguments to pass to the Create endpoint |
||
389 | * @param array|bool $options OPTIONAL |
||
390 | * bool=> Deletes index first if already exists (default = false). |
||
391 | * array => Associative array of options (option=>value) |
||
392 | * |
||
393 | * @throws InvalidException |
||
394 | * @throws \Elastica\Exception\ResponseException |
||
395 | * |
||
396 | * @return Response Server response |
||
397 | */ |
||
398 | public function create(array $args = [], $options = null): Response |
||
428 | |||
429 | /** |
||
430 | * Checks if the given index exists ans is created. |
||
431 | */ |
||
432 | public function exists(): bool |
||
438 | |||
439 | /** |
||
440 | * @param array|Query|string $query |
||
441 | * @param array|int $options |
||
442 | * @param BuilderInterface $builder |
||
443 | */ |
||
444 | public function createSearch($query = '', $options = null, ?BuilderInterface $builder = null): Search |
||
452 | |||
453 | /** |
||
454 | * Searches in this index. |
||
455 | * |
||
456 | * @param array|Query|string $query Array with all query data inside or a Elastica\Query object |
||
457 | * @param array|int $options Limit or associative array of options (option=>value) |
||
458 | * @param string $method Request method, see Request's constants |
||
459 | * |
||
460 | * @see \Elastica\SearchableInterface::search |
||
461 | */ |
||
462 | public function search($query = '', $options = null, string $method = Request::POST): ResultSet |
||
468 | |||
469 | /** |
||
470 | * Counts results of query. |
||
471 | * |
||
472 | * @param array|Query|string $query Array with all query data inside or a Elastica\Query object |
||
473 | * @param string $method Request method, see Request's constants |
||
474 | * |
||
475 | * @see \Elastica\SearchableInterface::count |
||
476 | */ |
||
477 | public function count($query = '', string $method = Request::POST): int |
||
483 | |||
484 | /** |
||
485 | * Opens an index. |
||
486 | * |
||
487 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html |
||
488 | */ |
||
489 | public function open(): Response |
||
493 | |||
494 | /** |
||
495 | * Closes the index. |
||
496 | * |
||
497 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-open-close.html |
||
498 | */ |
||
499 | public function close(): Response |
||
503 | |||
504 | /** |
||
505 | * Returns the index name. |
||
506 | */ |
||
507 | public function getName(): string |
||
511 | |||
512 | /** |
||
513 | * Returns index client. |
||
514 | */ |
||
515 | public function getClient(): Client |
||
519 | |||
520 | /** |
||
521 | * Adds an alias to the current index. |
||
522 | * |
||
523 | * @param bool $replace If set, an existing alias will be replaced |
||
524 | * |
||
525 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html |
||
526 | */ |
||
527 | public function addAlias(string $name, bool $replace = false): Response |
||
545 | |||
546 | /** |
||
547 | * Removes an alias pointing to the current index. |
||
548 | * |
||
549 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html |
||
550 | */ |
||
551 | public function removeAlias(string $name): Response |
||
558 | |||
559 | /** |
||
560 | * Returns all index aliases. |
||
561 | * |
||
562 | * @return string[] |
||
563 | */ |
||
564 | public function getAliases(): array |
||
582 | |||
583 | /** |
||
584 | * Checks if the index has the given alias. |
||
585 | */ |
||
586 | public function hasAlias(string $name): bool |
||
590 | |||
591 | /** |
||
592 | * Clears the cache of an index. |
||
593 | * |
||
594 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-clearcache.html |
||
595 | */ |
||
596 | public function clearCache(): Response |
||
601 | |||
602 | /** |
||
603 | * Flushes the index to storage. |
||
604 | * |
||
605 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-flush.html |
||
606 | */ |
||
607 | public function flush(array $options = []): Response |
||
614 | |||
615 | /** |
||
616 | * Can be used to change settings during runtime. One example is to use it for bulk updating. |
||
617 | * |
||
618 | * @param array $data Data array |
||
619 | * |
||
620 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html |
||
621 | */ |
||
622 | public function setSettings(array $data): Response |
||
629 | |||
630 | /** |
||
631 | * Makes calls to the elasticsearch server based on this index. |
||
632 | * |
||
633 | * @param string $path Path to call |
||
634 | * @param string $method Rest method to use (GET, POST, DELETE, PUT) |
||
635 | * @param array|string $data Arguments as array or encoded string |
||
636 | */ |
||
637 | public function request(string $path, string $method, $data = [], array $queryParameters = []): Response |
||
643 | |||
644 | /** |
||
645 | * Makes calls to the elasticsearch server with usage official client Endpoint based on this index. |
||
646 | */ |
||
647 | public function requestEndpoint(AbstractEndpoint $endpoint): Response |
||
654 | |||
655 | /** |
||
656 | * Run the analysis on the index. |
||
657 | * |
||
658 | * @param array $body request body for the `_analyze` API, see API documentation for the required properties |
||
659 | * @param array $args Additional arguments |
||
660 | * |
||
661 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html |
||
662 | */ |
||
663 | public function analyze(array $body, $args = []): array |
||
679 | |||
680 | /** |
||
681 | * Update document, using update script. |
||
682 | * |
||
683 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html |
||
684 | * |
||
685 | * @param AbstractScript|Document $data Document or Script with update data |
||
686 | * @param array $options array of query params to use for query |
||
687 | */ |
||
688 | public function updateDocument($data, array $options = []): Response |
||
700 | } |
||
701 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.