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 |
||
26 | class Resetter implements ResetterInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var AliasProcessor |
||
30 | */ |
||
31 | private $aliasProcessor; |
||
32 | |||
33 | /*** |
||
34 | * @var ManagerInterface |
||
35 | */ |
||
36 | private $configManager; |
||
37 | |||
38 | /** |
||
39 | * @var EventDispatcherInterface |
||
40 | */ |
||
41 | private $dispatcher; |
||
42 | |||
43 | /** |
||
44 | * @var IndexManager |
||
45 | */ |
||
46 | private $indexManager; |
||
47 | |||
48 | /** |
||
49 | * @var MappingBuilder |
||
50 | */ |
||
51 | private $mappingBuilder; |
||
52 | |||
53 | /** |
||
54 | * @param ManagerInterface $configManager |
||
55 | * @param IndexManager $indexManager |
||
56 | * @param AliasProcessor $aliasProcessor |
||
57 | * @param MappingBuilder $mappingBuilder |
||
58 | * @param EventDispatcherInterface $eventDispatcher |
||
59 | * @param Client $client |
||
|
|||
60 | */ |
||
61 | 20 | View Code Duplication | public function __construct( |
62 | ManagerInterface $configManager, |
||
63 | IndexManager $indexManager, |
||
64 | AliasProcessor $aliasProcessor, |
||
65 | MappingBuilder $mappingBuilder, |
||
66 | EventDispatcherInterface $eventDispatcher |
||
67 | ) { |
||
68 | 20 | $this->aliasProcessor = $aliasProcessor; |
|
69 | 20 | $this->configManager = $configManager; |
|
70 | 20 | $this->dispatcher = $eventDispatcher; |
|
71 | |||
72 | 20 | if (class_exists(LegacyEventDispatcherProxy::class)) { |
|
73 | $this->dispatcher = LegacyEventDispatcherProxy::decorate($eventDispatcher); |
||
74 | } |
||
75 | |||
76 | 20 | $this->indexManager = $indexManager; |
|
77 | 20 | $this->mappingBuilder = $mappingBuilder; |
|
78 | 20 | } |
|
79 | |||
80 | /** |
||
81 | * Deletes and recreates all indexes. |
||
82 | * |
||
83 | * @param bool $populating |
||
84 | * @param bool $force |
||
85 | */ |
||
86 | 1 | public function resetAllIndexes($populating = false, $force = false) |
|
87 | { |
||
88 | 1 | foreach ($this->configManager->getIndexNames() as $name) { |
|
89 | 1 | $this->resetIndex($name, $populating, $force); |
|
90 | } |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Deletes and recreates the named index. If populating, creates a new index |
||
95 | * with a randomised name for an alias to be set after population. |
||
96 | * |
||
97 | * @param string $indexName |
||
98 | * @param bool $populating |
||
99 | * @param bool $force If index exists with same name as alias, remove it |
||
100 | * |
||
101 | * @throws \InvalidArgumentException if no index exists for the given name |
||
102 | */ |
||
103 | 12 | public function resetIndex($indexName, $populating = false, $force = false) |
|
104 | { |
||
105 | 12 | $indexConfig = $this->configManager->getIndexConfiguration($indexName); |
|
106 | 11 | $index = $this->indexManager->getIndex($indexName); |
|
107 | |||
108 | 11 | if ($indexConfig->isUseAlias()) { |
|
109 | 1 | $this->aliasProcessor->setRootName($indexConfig, $index); |
|
110 | } |
||
111 | |||
112 | 11 | $event = new IndexResetEvent($indexName, $populating, $force); |
|
113 | 11 | $this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event); |
|
114 | |||
115 | $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig); |
||
116 | $index->create($mapping, true); |
||
117 | |||
118 | if (!$populating and $indexConfig->isUseAlias()) { |
||
119 | $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force); |
||
120 | } |
||
121 | |||
122 | $this->dispatcher->dispatch(IndexResetEvent::POST_INDEX_RESET, $event); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Deletes and recreates a mapping type for the named index. |
||
127 | * |
||
128 | * @param string $indexName |
||
129 | * @param string $typeName |
||
130 | * |
||
131 | * @throws \InvalidArgumentException if no index or type mapping exists for the given names |
||
132 | * @throws ResponseException |
||
133 | */ |
||
134 | 5 | public function resetIndexType($indexName, $typeName) |
|
135 | { |
||
136 | 5 | $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName); |
|
137 | |||
138 | 4 | $this->resetIndex($indexName, true); |
|
139 | |||
140 | $index = $this->indexManager->getIndex($indexName); |
||
141 | $type = $index->getType($typeName); |
||
142 | |||
143 | $event = new TypeResetEvent($indexName, $typeName); |
||
144 | $this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event); |
||
145 | |||
146 | $mapping = new Mapping(); |
||
147 | foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) { |
||
148 | $mapping->setParam($name, $field); |
||
149 | } |
||
150 | |||
151 | $type->setMapping($mapping); |
||
152 | |||
153 | $this->dispatcher->dispatch(TypeResetEvent::POST_TYPE_RESET, $event); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * A command run when a population has finished. |
||
158 | * |
||
159 | * @param string $indexName |
||
160 | * |
||
161 | * @deprecated |
||
162 | */ |
||
163 | public function postPopulate($indexName) |
||
167 | |||
168 | /** |
||
169 | * Switching aliases. |
||
170 | * |
||
171 | * @param string $indexName |
||
172 | * @param bool $delete Delete or close index |
||
173 | * |
||
174 | * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException |
||
175 | */ |
||
176 | 2 | public function switchIndexAlias($indexName, $delete = true) |
|
185 | } |
||
186 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.