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:
| 1 | <?php |
||
| 22 | class Resetter implements ResetterInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var AliasProcessor |
||
| 26 | */ |
||
| 27 | private $aliasProcessor; |
||
| 28 | |||
| 29 | /*** |
||
| 30 | * @var ManagerInterface |
||
| 31 | */ |
||
| 32 | private $configManager; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var EventDispatcherInterface |
||
| 36 | */ |
||
| 37 | private $dispatcher; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var IndexManager |
||
| 41 | */ |
||
| 42 | private $indexManager; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var MappingBuilder |
||
| 46 | */ |
||
| 47 | private $mappingBuilder; |
||
| 48 | |||
| 49 | 15 | View Code Duplication | public function __construct( |
| 50 | ManagerInterface $configManager, |
||
| 51 | IndexManager $indexManager, |
||
| 52 | AliasProcessor $aliasProcessor, |
||
| 53 | MappingBuilder $mappingBuilder, |
||
| 54 | EventDispatcherInterface $eventDispatcher |
||
| 55 | ) { |
||
| 56 | 15 | $this->aliasProcessor = $aliasProcessor; |
|
| 57 | 15 | $this->configManager = $configManager; |
|
| 58 | 15 | $this->dispatcher = $eventDispatcher; |
|
| 59 | 15 | $this->indexManager = $indexManager; |
|
| 60 | 15 | $this->mappingBuilder = $mappingBuilder; |
|
| 61 | 15 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Deletes and recreates all indexes. |
||
| 65 | */ |
||
| 66 | 1 | public function resetAllIndexes(bool $populating = false, bool $force = false) |
|
| 67 | { |
||
| 68 | 1 | foreach ($this->configManager->getIndexNames() as $name) { |
|
| 69 | 1 | $this->resetIndex($name, $populating, $force); |
|
| 70 | } |
||
| 71 | 1 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Deletes and recreates the named index. If populating, creates a new index |
||
| 75 | * with a randomised name for an alias to be set after population. |
||
| 76 | * |
||
| 77 | * @throws \InvalidArgumentException if no index exists for the given name |
||
| 78 | */ |
||
| 79 | 8 | public function resetIndex(string $indexName, bool $populating = false, bool $force = false) |
|
| 80 | { |
||
| 81 | 8 | $indexConfig = $this->configManager->getIndexConfiguration($indexName); |
|
| 82 | 7 | $index = $this->indexManager->getIndex($indexName); |
|
| 83 | |||
| 84 | 7 | if ($indexConfig->isUseAlias()) { |
|
| 85 | 1 | $this->aliasProcessor->setRootName($indexConfig, $index); |
|
|
|
|||
| 86 | } |
||
| 87 | |||
| 88 | 7 | $this->dispatcher->dispatch($event = new PreIndexResetEvent($indexName, $populating, $force)); |
|
| 89 | |||
| 90 | 7 | $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig); |
|
| 91 | 7 | $index->create($mapping, ['recreate' => true]); |
|
| 92 | |||
| 93 | 7 | if (!$populating and $indexConfig->isUseAlias()) { |
|
| 94 | 1 | $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force); |
|
| 95 | } |
||
| 96 | |||
| 97 | 7 | $this->dispatcher->dispatch(new PostIndexResetEvent($indexName, $populating, $force)); |
|
| 98 | 7 | } |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Switch index alias. |
||
| 102 | * |
||
| 103 | * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException |
||
| 104 | */ |
||
| 105 | 2 | public function switchIndexAlias(string $indexName, bool $delete = true) |
|
| 114 | } |
||
| 115 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.