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 IndexService 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 IndexService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class IndexService |
||
34 | { |
||
35 | private $client; |
||
36 | private $namespace; |
||
37 | private $converter; |
||
38 | private $parser; |
||
39 | private $eventDispatcher; |
||
40 | |||
41 | private $stopwatch; |
||
42 | private $bulkCommitSize = 100; |
||
43 | private $bulkQueries = []; |
||
44 | private $serializer; |
||
45 | private $tracer; |
||
46 | |||
47 | public function __construct( |
||
48 | string $namespace, |
||
49 | Converter $converter, |
||
50 | DocumentParser $parser, |
||
51 | EventDispatcherInterface $eventDispatcher, |
||
52 | Serializer $serializer, |
||
53 | $tracer = null |
||
54 | ) { |
||
55 | $this->namespace = $namespace; |
||
56 | $this->converter = $converter; |
||
57 | $this->parser = $parser; |
||
58 | $this->eventDispatcher = $eventDispatcher; |
||
59 | $this->serializer = $serializer; |
||
60 | $this->tracer = $tracer; |
||
61 | } |
||
62 | |||
63 | public function getNamespace(): string |
||
64 | { |
||
65 | return $this->namespace; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @deprecated will be removed in v7 since there will be no more types in the indexes. |
||
70 | */ |
||
71 | public function getTypeName(): string |
||
75 | |||
76 | public function getClient(): Client |
||
77 | { |
||
93 | |||
94 | public function getIndexName(): string |
||
98 | |||
99 | public function getEventDispatcher(): EventDispatcherInterface |
||
103 | |||
104 | public function getConverter(): Converter |
||
108 | |||
109 | public function getParser(): DocumentParser |
||
113 | |||
114 | public function getBulkCommitSize(): int |
||
118 | |||
119 | public function setBulkCommitSize(int $bulkCommitSize) |
||
124 | |||
125 | public function getStopwatch() |
||
129 | |||
130 | public function setStopwatch($stopwatch) |
||
135 | |||
136 | public function createIndex($noMapping = false, $params = []): array |
||
147 | |||
148 | public function dropIndex(): array |
||
159 | |||
160 | public function dropAndCreateIndex($noMapping = false, $params = []): array |
||
172 | |||
173 | public function indexExists(): bool |
||
177 | |||
178 | public function clearCache(): array |
||
182 | |||
183 | /** |
||
184 | * Returns a single document by provided ID or null if a document was not found. |
||
185 | */ |
||
186 | public function find($id, $params = []) |
||
206 | |||
207 | public function findByIds(array $ids): DocumentIterator |
||
214 | |||
215 | /** |
||
216 | * Finds documents by a set of criteria. |
||
217 | * |
||
218 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
219 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
220 | * @param int|null $limit Default is 10. |
||
221 | * @param int|null $offset Default is 0. |
||
222 | * |
||
223 | * @return array|DocumentIterator The objects. |
||
224 | */ |
||
225 | public function findBy( |
||
245 | |||
246 | /** |
||
247 | * Finds a single document by a set of criteria. |
||
248 | * |
||
249 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
250 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
251 | * |
||
252 | * @return object|null The object. |
||
253 | */ |
||
254 | public function findOneBy(array $criteria, array $orderBy = []) |
||
258 | |||
259 | public function createSearch(): Search |
||
263 | |||
264 | public function getScrollConfiguration($raw, $scrollDuration): array |
||
274 | |||
275 | View Code Duplication | public function findDocuments(Search $search): DocumentIterator |
|
287 | |||
288 | View Code Duplication | public function findArray(Search $search): ArrayIterator |
|
300 | |||
301 | View Code Duplication | public function findRaw(Search $search): RawIterator |
|
313 | |||
314 | private function executeSearch(Search $search): array |
||
318 | |||
319 | public function getIndexDocumentCount(): int |
||
331 | |||
332 | View Code Duplication | public function remove($id, $routing = null) |
|
348 | |||
349 | public function update($id, array $fields = [], $script = null, array $params = []): array |
||
370 | |||
371 | View Code Duplication | public function search(array $query, array $params = []): array |
|
390 | |||
391 | /** |
||
392 | * Usage example |
||
393 | * |
||
394 | * $im->bulk('index', ['_id' => 1, 'title' => 'foo']); |
||
395 | * $im->bulk('delete', ['_id' => 2]); |
||
396 | * $im->bulk('create', ['title' => 'foo']); |
||
397 | */ |
||
398 | public function bulk(string $operation, array $data = [], $autoCommit = true): array |
||
428 | |||
429 | /** |
||
430 | * Adds document to next flush. |
||
431 | * |
||
432 | * @param object $document |
||
433 | */ |
||
434 | public function persist($document): void |
||
440 | |||
441 | public function commit($commitMode = 'flush', array $params = []): array |
||
496 | |||
497 | public function flush(array $params = []): array |
||
501 | |||
502 | public function refresh(array $params = []): array |
||
506 | |||
507 | public function scroll($scrollId, $scrollDuration = '5m'): array |
||
513 | |||
514 | public function clearScroll($scrollId): array |
||
518 | |||
519 | public function resetClient(): void |
||
523 | |||
524 | public function clearElasticIndexCache(): array |
||
528 | |||
529 | private function stopwatch($action, $name): void |
||
535 | } |
||
536 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.