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 | |||
37 | private $indexName; |
||
38 | |||
39 | private $converter; |
||
40 | |||
41 | private $eventDispatcher; |
||
42 | |||
43 | private $stopwatch; |
||
44 | |||
45 | private $bulkCommitSize = 100; |
||
46 | |||
47 | private $bulkQueries = []; |
||
48 | |||
49 | private $commitMode = 'refresh'; |
||
50 | |||
51 | /** |
||
52 | * @deprecated will be removed in v7 since there will be no more types in the indexes. |
||
53 | */ |
||
54 | private $typeName; |
||
55 | |||
56 | public function __construct(Client $client, Converter $converter, EventDispatcherInterface $eventDispatcher, array $mapping = []) |
||
63 | |||
64 | /** |
||
65 | * @deprecated will be removed in v7 since there will be no more types in the indexes. |
||
66 | */ |
||
67 | public function getTypeName(): string |
||
71 | |||
72 | public function getClient(): Client |
||
76 | |||
77 | public function getIndexName(): string |
||
81 | |||
82 | public function getConverter(): Converter |
||
86 | |||
87 | public function getEventDispatcher(): EventDispatcherInterface |
||
91 | |||
92 | public function getBulkCommitSize(): int |
||
96 | |||
97 | public function setBulkCommitSize(int $bulkCommitSize) |
||
102 | |||
103 | public function getCommitMode(): string |
||
107 | |||
108 | public function setCommitMode(string $commitMode): IndexService |
||
113 | |||
114 | public function getStopwatch() |
||
118 | |||
119 | public function setStopwatch($stopwatch) |
||
124 | |||
125 | public function createIndex($noMapping = false): array |
||
129 | |||
130 | public function dropIndex(): array |
||
134 | |||
135 | public function dropAndCreateIndex($noMapping = false) |
||
147 | |||
148 | public function indexExists(): bool |
||
152 | |||
153 | /** |
||
154 | * Returns a single document by provided ID or null if a document was not found. |
||
155 | * |
||
156 | * @param string $id Document ID to find |
||
157 | * @param array $params Custom parameters added to the query url |
||
158 | * |
||
159 | * @return object |
||
160 | */ |
||
161 | public function find($id, $params = []) |
||
175 | |||
176 | public function findByIds(array $ids): DocumentIterator |
||
183 | |||
184 | /** |
||
185 | * Finds documents by a set of criteria. |
||
186 | * |
||
187 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
188 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
189 | * @param int|null $limit Default is 10. |
||
190 | * @param int|null $offset Default is 0. |
||
191 | * |
||
192 | * @return array|DocumentIterator The objects. |
||
193 | */ |
||
194 | public function findBy( |
||
224 | |||
225 | /** |
||
226 | * Finds a single document by a set of criteria. |
||
227 | * |
||
228 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
229 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
230 | * |
||
231 | * @return object|null The object. |
||
232 | */ |
||
233 | public function findOneBy(array $criteria, array $orderBy = []) |
||
237 | |||
238 | public function createSearch(): Search |
||
242 | |||
243 | public function getScrollConfiguration($raw, $scrollDuration): array |
||
253 | |||
254 | View Code Duplication | public function findDocuments(Search $search): DocumentIterator |
|
264 | |||
265 | View Code Duplication | public function findArray(Search $search): ArrayIterator |
|
275 | |||
276 | View Code Duplication | public function findRaw(Search $search): RawIterator |
|
286 | |||
287 | private function executeSearch(Search $search): array |
||
291 | |||
292 | public function getIndexDocumentCount(): int |
||
304 | |||
305 | public function remove($id, $routing = null) |
||
321 | |||
322 | public function update($id, array $fields = [], $script = null, array $params = []): array |
||
343 | |||
344 | public function search(array $query, array $params = []): array |
||
363 | |||
364 | public function bulk(string $operation, array $header, array $query = []): array |
||
383 | |||
384 | /** |
||
385 | * Adds document to next flush. |
||
386 | * |
||
387 | * @param object $document |
||
388 | */ |
||
389 | View Code Duplication | public function persist($document) |
|
396 | |||
397 | public function commit(array $params = []): array |
||
453 | |||
454 | public function flush(array $params = []): array |
||
458 | |||
459 | public function refresh(array $params = []): array |
||
463 | |||
464 | public function scroll($scrollId, $scrollDuration = '5m'): array |
||
470 | |||
471 | public function clearScroll($scrollId): array |
||
475 | |||
476 | public function clearElasticIndexCache(): array |
||
480 | |||
481 | private function stopwatch($action, $name): void |
||
487 | } |
||
488 |
This property 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 property will be removed from the class and what other property to use instead.