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 |
||
30 | class AliasProcessor |
||
31 | { |
||
32 | /** |
||
33 | * Sets the randomised root name for an index. |
||
34 | * |
||
35 | * @param IndexConfig $indexConfig |
||
36 | * @param Index $index |
||
37 | */ |
||
38 | 2 | public function setRootName(IndexConfig $indexConfig, Index $index) |
|
47 | |||
48 | /** |
||
49 | * Switches an index to become the new target for an alias. Only applies for |
||
50 | * indexes that are set to use aliases. |
||
51 | * |
||
52 | * $force will delete an index encountered where an alias is expected. |
||
53 | * |
||
54 | * @param IndexConfig $indexConfig |
||
55 | * @param Index $index |
||
56 | * @param bool $force |
||
57 | * @param bool $delete |
||
58 | * |
||
59 | * @throws AliasIsIndexException |
||
60 | */ |
||
61 | 7 | public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false, $delete = true) |
|
99 | |||
100 | /** |
||
101 | * Builds an ElasticSearch request to rename or create an alias. |
||
102 | * |
||
103 | * @param string|null $aliasedIndex |
||
104 | * @param string $aliasName |
||
105 | * @param string $newIndexName |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | 5 | private function buildAliasUpdateRequest($aliasedIndex, $aliasName, $newIndexName) |
|
126 | |||
127 | /** |
||
128 | * Cleans up an index when we encounter a failure to rename the alias. |
||
129 | * |
||
130 | * @param Client $client |
||
131 | * @param string $indexName |
||
132 | * @param \Exception $renameAliasException |
||
133 | */ |
||
134 | 1 | private function cleanupRenameFailure(Client $client, $indexName, \Exception $renameAliasException) |
|
153 | |||
154 | /** |
||
155 | * Delete an index. |
||
156 | * |
||
157 | * @param Client $client |
||
158 | * @param string $indexName Index name to delete |
||
159 | */ |
||
160 | 4 | View Code Duplication | private function deleteIndex(Client $client, $indexName) |
173 | |||
174 | /** |
||
175 | * Close an index. |
||
176 | * |
||
177 | * @param Client $client |
||
178 | * @param string $indexName |
||
179 | */ |
||
180 | View Code Duplication | private function closeIndex(Client $client, $indexName) |
|
197 | |||
198 | /** |
||
199 | * Returns the name of a single index that an alias points to or throws |
||
200 | * an exception if there is more than one. |
||
201 | * |
||
202 | * @param Client $client |
||
203 | * @param string $aliasName Alias name |
||
204 | * |
||
205 | * @return string|null |
||
206 | * |
||
207 | * @throws AliasIsIndexException |
||
208 | */ |
||
209 | 7 | private function getAliasedIndex(Client $client, $aliasName) |
|
239 | } |
||
240 |