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 |
||
17 | class ElasticConnection implements ElasticConnectionInterface { |
||
18 | |||
19 | use ErrorGetterTrait; |
||
20 | |||
21 | /** Override default elastic limit size query */ |
||
22 | const DEFAULT_MAX_RESULTS = 10000; |
||
23 | |||
24 | /** @var CurlRequest */ |
||
25 | protected $curlRequest; |
||
26 | |||
27 | /** @var float */ |
||
28 | protected $esVersion; |
||
29 | |||
30 | public function __construct(array $hosts) { |
||
40 | |||
41 | /** |
||
42 | * @param string $index |
||
43 | * @param array|null $mappings |
||
44 | * @param array|null $settings |
||
45 | * @param array|null $aliases |
||
46 | * @param array|null $return |
||
47 | * @return bool |
||
48 | */ |
||
49 | public function createIndex( |
||
81 | |||
82 | /** |
||
83 | * @param string $index |
||
84 | * @param array|null $return |
||
85 | * @return bool |
||
86 | * @throws ElasticOperationException |
||
87 | */ |
||
88 | public function deleteIndex($index, array &$return = null) { |
||
108 | |||
109 | /** |
||
110 | * @param string $index |
||
111 | * @param string $type |
||
112 | * @param array $mappings |
||
113 | * @param array|null $return |
||
114 | * @return bool |
||
115 | * @throws ElasticOperationException |
||
116 | */ |
||
117 | public function createType($index, $type, array $mappings = [], array &$return = null) { |
||
142 | |||
143 | /** |
||
144 | * @param string $index |
||
145 | * @param string $type |
||
146 | * @param array $body |
||
147 | * @param array $mergeParams |
||
148 | * @param array|null $return |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function insert($index, $type, array $body, array $mergeParams = [], array &$return = null) { |
||
185 | |||
186 | /** |
||
187 | * @param string $index |
||
188 | * @param string $type |
||
189 | * @param string $_id |
||
190 | * @param array $body |
||
191 | * @param array $mergeParams |
||
192 | * @param array|null $return |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function update($index, $type, $_id, array $body = [], array $mergeParams = [], array &$return = null) { |
||
220 | |||
221 | /** |
||
222 | * @param string $index |
||
223 | * @param string $type |
||
224 | * @param string $_id |
||
225 | * @param array $mergeParams |
||
226 | * @param array|null $return |
||
227 | * @return bool |
||
228 | */ |
||
229 | public function delete($index, $type, $_id, array $mergeParams = [], array &$return = null) { |
||
250 | |||
251 | public function updateWhere($index, $type, array $where, array &$return = null) { |
||
254 | |||
255 | public function deleteWhere($index, $type, array $where, array &$return = null) { |
||
258 | |||
259 | /** |
||
260 | * |
||
261 | * @param string $index |
||
262 | * @param string $type |
||
263 | * @param string $_id |
||
264 | * @param array $mergeParams |
||
265 | * @param array|null $return |
||
266 | * @return array|null |
||
267 | */ |
||
268 | public function get($index, $type, $_id, array $mergeParams = [], array &$return = null) { |
||
286 | |||
287 | /** |
||
288 | * Returns the [hits][hits] array from query |
||
289 | * |
||
290 | * @param string $index |
||
291 | * @param string $type |
||
292 | * @param array $body |
||
293 | * @param array $mergeParams |
||
294 | * @param array|null $return |
||
295 | * @return array |
||
296 | */ |
||
297 | public function search($index, $type, array $body = [], array $mergeParams = [], array &$return = null) { |
||
318 | |||
319 | private function unsetEmpties(array &$array, array &$parent = null) { |
||
334 | |||
335 | /** |
||
336 | * @param string $index |
||
337 | * @return bool |
||
338 | */ |
||
339 | public function indexExists($index) { |
||
344 | |||
345 | /** |
||
346 | * @param string $index |
||
347 | * @param string $type |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function typeExists($index, $type) { |
||
355 | |||
356 | private function throwExceptionFromResponse($response, $appendPrefix = '') { |
||
365 | |||
366 | public function hasConnection() { |
||
371 | |||
372 | private function setErrorFromElasticReturn($return) { |
||
379 | |||
380 | public function getElasticsearchVersion() { |
||
393 | } |
||
394 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.