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:
Complex classes like ElasticConnection often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ElasticConnection, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class ElasticConnection implements ElasticConnectionInterface { |
||
14 | |||
15 | /** Override default elastic limit size query */ |
||
16 | const DEFAULT_MAX_RESULTS = 10000; |
||
17 | |||
18 | /** @var Client */ |
||
19 | protected $elastic; |
||
20 | |||
21 | public function __construct(Client $elastic) { |
||
24 | |||
25 | /** |
||
26 | * @param string $index |
||
27 | * @param array|null $mappings |
||
28 | * @param array|null $settings |
||
29 | * @param array|null $aliases |
||
30 | * @param array|null $return |
||
31 | * @return bool |
||
32 | */ |
||
33 | public function createIndex( |
||
79 | |||
80 | /** |
||
81 | * @param string $index |
||
82 | * @param array|null $return |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function deleteIndex($index, array &$return = null) { |
||
100 | |||
101 | /** |
||
102 | * @param string $index |
||
103 | * @param string $type |
||
104 | * @param array $mappings |
||
105 | * @param array|null $return |
||
106 | * @return bool |
||
107 | */ |
||
108 | public function createType($index, $type, array $mappings = [], array &$return = null) { |
||
130 | |||
131 | /** |
||
132 | * @param string $index |
||
133 | * @param string $type |
||
134 | * @param array $body |
||
135 | * @param array $mergeParams |
||
136 | * @param array|null $return |
||
137 | * @return bool |
||
138 | */ |
||
139 | public function insert($index, $type, array $body, array $mergeParams = [], array &$return = null) { |
||
169 | |||
170 | /** |
||
171 | * @param string $index |
||
172 | * @param string $type |
||
173 | * @param string $_id |
||
174 | * @param array $body |
||
175 | * @param array $mergeParams |
||
176 | * @param array|null $return |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function update($index, $type, $_id, array $body = [], array $mergeParams = [], array &$return = null) { |
||
205 | |||
206 | /** |
||
207 | * @param string $index |
||
208 | * @param string $type |
||
209 | * @param string $_id |
||
210 | * @param array $mergeParams |
||
211 | * @param array|null $return |
||
212 | * @return bool |
||
213 | */ |
||
214 | public function delete($index, $type, $_id, array $mergeParams = [], array &$return = null) { |
||
236 | |||
237 | public function updateWhere($index, $type, array $where, array &$return = null) { |
||
240 | |||
241 | public function deleteWhere($index, $type, array $where, array &$return = null) { |
||
244 | |||
245 | /** |
||
246 | * |
||
247 | * @param string $index |
||
248 | * @param string $type |
||
249 | * @param string $_id |
||
250 | * @param array $mergeParams |
||
251 | * @param array|null $return |
||
252 | * @return array|null |
||
253 | */ |
||
254 | public function get($index, $type, $_id, array $mergeParams = [], array &$return = null) { |
||
283 | |||
284 | /** |
||
285 | * Returns the [hits][hits] array from query |
||
286 | * |
||
287 | * @param string $index |
||
288 | * @param string $type |
||
289 | * @param array $body |
||
290 | * @param array $mergeParams |
||
291 | * @param array|null $return |
||
292 | * @return array |
||
293 | */ |
||
294 | public function search($index, $type, array $body = [], array $mergeParams = [], array &$return = null) { |
||
326 | |||
327 | private function unsetEmpties(array &$array, array &$parent = null) { |
||
342 | |||
343 | /** |
||
344 | * @param string $index |
||
345 | * @return bool |
||
346 | */ |
||
347 | public function indexExists($index) { |
||
350 | |||
351 | /** |
||
352 | * @param string $index |
||
353 | * @param string $type |
||
354 | * @return bool |
||
355 | */ |
||
356 | public function typeExists($index, $type) { |
||
365 | |||
366 | /** |
||
367 | * @return Client |
||
368 | */ |
||
369 | public function getElasticClient() { |
||
372 | } |
||
373 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.