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 |
||
21 | class AliasProcessor |
||
22 | { |
||
23 | /** |
||
24 | * Sets the randomised root name for an index. |
||
25 | * |
||
26 | * @param IndexConfig $indexConfig |
||
27 | * @param Index $index |
||
28 | */ |
||
29 | 2 | public function setRootName(IndexConfig $indexConfig, Index $index) |
|
38 | |||
39 | /** |
||
40 | * Switches an index to become the new target for an alias. Only applies for |
||
41 | * indexes that are set to use aliases. |
||
42 | * |
||
43 | * $force will delete an index encountered where an alias is expected. |
||
44 | * |
||
45 | * @param IndexConfig $indexConfig |
||
46 | * @param Index $index |
||
47 | * @param bool $force |
||
48 | * @param bool $delete |
||
49 | * |
||
50 | * @throws AliasIsIndexException |
||
51 | */ |
||
52 | 7 | public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false, $delete = true) |
|
90 | |||
91 | /** |
||
92 | * Builds an ElasticSearch request to rename or create an alias. |
||
93 | * |
||
94 | * @param string|null $aliasedIndex |
||
95 | * @param string $aliasName |
||
96 | * @param string $newIndexName |
||
97 | * @return array |
||
98 | */ |
||
99 | 5 | private function buildAliasUpdateRequest($aliasedIndex, $aliasName, $newIndexName) |
|
116 | |||
117 | /** |
||
118 | * Cleans up an index when we encounter a failure to rename the alias. |
||
119 | * |
||
120 | * @param Client $client |
||
121 | * @param string $indexName |
||
122 | * @param \Exception $renameAliasException |
||
123 | */ |
||
124 | 1 | private function cleanupRenameFailure(Client $client, $indexName, \Exception $renameAliasException) |
|
143 | |||
144 | /** |
||
145 | * Delete an index. |
||
146 | * |
||
147 | * @param Client $client |
||
148 | * @param string $indexName Index name to delete |
||
149 | */ |
||
150 | 4 | View Code Duplication | private function deleteIndex(Client $client, $indexName) |
163 | |||
164 | /** |
||
165 | * Close an index |
||
166 | * |
||
167 | * @param Client $client |
||
168 | * @param string $indexName |
||
169 | */ |
||
170 | View Code Duplication | private function closeIndex(Client $client, $indexName) |
|
187 | |||
188 | /** |
||
189 | * Returns the name of a single index that an alias points to or throws |
||
190 | * an exception if there is more than one. |
||
191 | * |
||
192 | * @param Client $client |
||
193 | * @param string $aliasName Alias name |
||
194 | * |
||
195 | * @return string|null |
||
196 | * |
||
197 | * @throws AliasIsIndexException |
||
198 | */ |
||
199 | 7 | private function getAliasedIndex(Client $client, $aliasName) |
|
229 | } |
||
230 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.