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 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | 5 | private function buildAliasUpdateRequest($aliasedIndex, $aliasName, $newIndexName) |
|
117 | |||
118 | /** |
||
119 | * Cleans up an index when we encounter a failure to rename the alias. |
||
120 | * |
||
121 | * @param Client $client |
||
122 | * @param string $indexName |
||
123 | * @param \Exception $renameAliasException |
||
124 | */ |
||
125 | 1 | private function cleanupRenameFailure(Client $client, $indexName, \Exception $renameAliasException) |
|
144 | |||
145 | /** |
||
146 | * Delete an index. |
||
147 | * |
||
148 | * @param Client $client |
||
149 | * @param string $indexName Index name to delete |
||
150 | */ |
||
151 | 4 | View Code Duplication | private function deleteIndex(Client $client, $indexName) |
164 | |||
165 | /** |
||
166 | * Close an index. |
||
167 | * |
||
168 | * @param Client $client |
||
169 | * @param string $indexName |
||
170 | */ |
||
171 | View Code Duplication | private function closeIndex(Client $client, $indexName) |
|
188 | |||
189 | /** |
||
190 | * Returns the name of a single index that an alias points to or throws |
||
191 | * an exception if there is more than one. |
||
192 | * |
||
193 | * @param Client $client |
||
194 | * @param string $aliasName Alias name |
||
195 | * |
||
196 | * @return string|null |
||
197 | * |
||
198 | * @throws AliasIsIndexException |
||
199 | */ |
||
200 | 7 | private function getAliasedIndex(Client $client, $aliasName) |
|
230 | } |
||
231 |