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 |
||
| 25 | class Resetter implements ResetterInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var AliasProcessor |
||
| 29 | */ |
||
| 30 | private $aliasProcessor; |
||
| 31 | |||
| 32 | /*** |
||
| 33 | * @var ManagerInterface |
||
| 34 | */ |
||
| 35 | private $configManager; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var EventDispatcherInterface |
||
| 39 | */ |
||
| 40 | private $dispatcher; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var IndexManager |
||
| 44 | */ |
||
| 45 | private $indexManager; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var MappingBuilder |
||
| 49 | */ |
||
| 50 | private $mappingBuilder; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param ManagerInterface $configManager |
||
| 54 | * @param IndexManager $indexManager |
||
| 55 | * @param AliasProcessor $aliasProcessor |
||
| 56 | * @param MappingBuilder $mappingBuilder |
||
| 57 | * @param EventDispatcherInterface $eventDispatcher |
||
| 58 | * @param Client $client |
||
|
|
|||
| 59 | */ |
||
| 60 | 20 | View Code Duplication | public function __construct( |
| 73 | |||
| 74 | /** |
||
| 75 | * Deletes and recreates all indexes. |
||
| 76 | * |
||
| 77 | * @param bool $populating |
||
| 78 | * @param bool $force |
||
| 79 | */ |
||
| 80 | 1 | public function resetAllIndexes($populating = false, $force = false) |
|
| 81 | { |
||
| 82 | 1 | foreach ($this->configManager->getIndexNames() as $name) { |
|
| 83 | 1 | $this->resetIndex($name, $populating, $force); |
|
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Deletes and recreates the named index. If populating, creates a new index |
||
| 89 | * with a randomised name for an alias to be set after population. |
||
| 90 | * |
||
| 91 | * @param string $indexName |
||
| 92 | * @param bool $populating |
||
| 93 | * @param bool $force If index exists with same name as alias, remove it |
||
| 94 | * |
||
| 95 | * @throws \InvalidArgumentException if no index exists for the given name |
||
| 96 | */ |
||
| 97 | 12 | public function resetIndex($indexName, $populating = false, $force = false) |
|
| 98 | { |
||
| 99 | 12 | $indexConfig = $this->configManager->getIndexConfiguration($indexName); |
|
| 100 | 11 | $index = $this->indexManager->getIndex($indexName); |
|
| 101 | |||
| 102 | 11 | if ($indexConfig->isUseAlias()) { |
|
| 103 | 1 | $this->aliasProcessor->setRootName($indexConfig, $index); |
|
| 104 | } |
||
| 105 | |||
| 106 | 11 | $event = new IndexResetEvent($indexName, $populating, $force); |
|
| 107 | 11 | $this->dispatcher->dispatch($event, IndexResetEvent::PRE_INDEX_RESET); |
|
| 108 | |||
| 109 | 6 | $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig); |
|
| 110 | 6 | $index->create($mapping, true); |
|
| 111 | |||
| 112 | 6 | if (!$populating and $indexConfig->isUseAlias()) { |
|
| 113 | $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force); |
||
| 114 | } |
||
| 115 | |||
| 116 | 6 | $this->dispatcher->dispatch($event, IndexResetEvent::POST_INDEX_RESET); |
|
| 117 | 6 | } |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Deletes and recreates a mapping type for the named index. |
||
| 121 | * |
||
| 122 | * @param string $indexName |
||
| 123 | * @param string $typeName |
||
| 124 | * |
||
| 125 | * @throws \InvalidArgumentException if no index or type mapping exists for the given names |
||
| 126 | * @throws ResponseException |
||
| 127 | */ |
||
| 128 | 5 | public function resetIndexType($indexName, $typeName) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * A command run when a population has finished. |
||
| 152 | * |
||
| 153 | * @param string $indexName |
||
| 154 | * |
||
| 155 | * @deprecated |
||
| 156 | */ |
||
| 157 | public function postPopulate($indexName) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Switching aliases. |
||
| 164 | * |
||
| 165 | * @param string $indexName |
||
| 166 | * @param bool $delete Delete or close index |
||
| 167 | * |
||
| 168 | * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException |
||
| 169 | */ |
||
| 170 | 2 | public function switchIndexAlias($indexName, $delete = true) |
|
| 179 | } |
||
| 180 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.