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 $eventDispatcher; |
||
| 39 | |||
| 40 | private $stopwatch; |
||
| 41 | private $bulkCommitSize = 100; |
||
| 42 | private $bulkQueries = []; |
||
| 43 | private $indexSettings = []; |
||
| 44 | private $serializer; |
||
| 45 | private $tracer; |
||
| 46 | |||
| 47 | public function __construct( |
||
| 48 | string $namespace, |
||
| 49 | Converter $converter, |
||
| 50 | EventDispatcherInterface $eventDispatcher, |
||
| 51 | Serializer $serializer, |
||
| 52 | IndexSettings $indexSettings, |
||
| 53 | $tracer = null |
||
| 54 | ) { |
||
| 55 | $this->namespace = $namespace; |
||
| 56 | $this->converter = $converter; |
||
| 57 | $this->eventDispatcher = $eventDispatcher; |
||
| 58 | $this->serializer = $serializer; |
||
| 59 | $this->indexSettings = $indexSettings; |
||
|
|
|||
| 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 getIndexSettings() |
||
| 77 | { |
||
| 80 | |||
| 81 | public function setIndexSettings($indexSettings): self |
||
| 86 | |||
| 87 | public function getClient(): Client |
||
| 103 | |||
| 104 | public function getIndexName(): string |
||
| 108 | |||
| 109 | public function getEventDispatcher(): EventDispatcherInterface |
||
| 113 | |||
| 114 | public function getConverter(): Converter |
||
| 118 | |||
| 119 | public function getBulkCommitSize(): int |
||
| 123 | |||
| 124 | public function setBulkCommitSize(int $bulkCommitSize) |
||
| 129 | |||
| 130 | public function getStopwatch() |
||
| 134 | |||
| 135 | public function setStopwatch($stopwatch) |
||
| 140 | |||
| 141 | public function createIndex($noMapping = false, $params = []): array |
||
| 152 | |||
| 153 | public function dropIndex(): array |
||
| 164 | |||
| 165 | public function dropAndCreateIndex($noMapping = false, $params = []): array |
||
| 177 | |||
| 178 | public function indexExists(): bool |
||
| 182 | |||
| 183 | public function clearCache(): array |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns a single document by provided ID or null if a document was not found. |
||
| 190 | */ |
||
| 191 | public function find($id, $params = []) |
||
| 211 | |||
| 212 | public function findByIds(array $ids): DocumentIterator |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Finds documents by a set of criteria. |
||
| 222 | * |
||
| 223 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
| 224 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
| 225 | * @param int|null $limit Default is 10. |
||
| 226 | * @param int|null $offset Default is 0. |
||
| 227 | * |
||
| 228 | * @return array|DocumentIterator The objects. |
||
| 229 | */ |
||
| 230 | public function findBy( |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Finds a single document by a set of criteria. |
||
| 253 | * |
||
| 254 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
| 255 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
| 256 | * |
||
| 257 | * @return object|null The object. |
||
| 258 | */ |
||
| 259 | public function findOneBy(array $criteria, array $orderBy = []) |
||
| 263 | |||
| 264 | public function createSearch(): Search |
||
| 268 | |||
| 269 | public function getScrollConfiguration($raw, $scrollDuration): array |
||
| 279 | |||
| 280 | View Code Duplication | public function findDocuments(Search $search): DocumentIterator |
|
| 292 | |||
| 293 | View Code Duplication | public function findArray(Search $search): ArrayIterator |
|
| 305 | |||
| 306 | View Code Duplication | public function findRaw(Search $search): RawIterator |
|
| 318 | |||
| 319 | private function executeSearch(Search $search): array |
||
| 323 | |||
| 324 | public function getIndexDocumentCount(): int |
||
| 336 | |||
| 337 | View Code Duplication | public function remove($id, $routing = null) |
|
| 353 | |||
| 354 | public function update($id, array $fields = [], $script = null, array $params = []): array |
||
| 375 | |||
| 376 | View Code Duplication | public function search(array $query, array $params = []): array |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Usage example |
||
| 398 | * |
||
| 399 | * $im->bulk('index', ['_id' => 1, 'title' => 'foo']); |
||
| 400 | * $im->bulk('delete', ['_id' => 2]); |
||
| 401 | * $im->bulk('create', ['title' => 'foo']); |
||
| 402 | */ |
||
| 403 | public function bulk(string $operation, array $data = [], $autoCommit = true): array |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Adds document to next flush. |
||
| 436 | * |
||
| 437 | * @param object $document |
||
| 438 | */ |
||
| 439 | public function persist($document): void |
||
| 445 | |||
| 446 | public function commit($commitMode = 'flush', array $params = []): array |
||
| 501 | |||
| 502 | public function flush(array $params = []): array |
||
| 506 | |||
| 507 | public function refresh(array $params = []): array |
||
| 511 | |||
| 512 | public function scroll($scrollId, $scrollDuration = '5m'): array |
||
| 518 | |||
| 519 | public function clearScroll($scrollId): array |
||
| 523 | |||
| 524 | public function resetClient(): void |
||
| 528 | |||
| 529 | public function clearElasticIndexCache(): array |
||
| 533 | |||
| 534 | private function stopwatch($action, $name): void |
||
| 540 | } |
||
| 541 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..