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( |
||
50 | $index, array $mappings = null, array $settings = null, array $aliases = null, array &$return = null |
||
51 | ) { |
||
52 | if ($this->indexExists($index)) { |
||
53 | throw new \InvalidArgumentException(sprintf("'%s' index already exists", $index)); |
||
54 | } |
||
55 | |||
56 | $params = []; |
||
57 | |||
58 | if (is_array($mappings) && !empty($mappings)) { |
||
59 | $params['mappings'] = MappingHelper::patchMappings($mappings, floor($this->getElasticsearchVersion())); |
||
60 | } |
||
61 | |||
62 | if (is_array($settings) && !empty($settings)) { |
||
63 | $params['settings'] = $settings; |
||
64 | } |
||
65 | |||
66 | if (is_array($aliases) && !empty($aliases)) { |
||
67 | $params['aliases'] = $aliases; |
||
68 | } |
||
69 | |||
70 | $response = $this->curlRequest->request($index, $params, 'PUT'); |
||
71 | $return = $response['content']; |
||
72 | |||
73 | if (isset($return['acknowledged']) && $return['acknowledged']) { |
||
74 | return $return['acknowledged']; |
||
75 | } |
||
76 | |||
77 | $this->setErrorFromElasticReturn($return); |
||
78 | |||
79 | return false; |
||
80 | } |
||
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) { |
||
143 | |||
144 | /** |
||
145 | * @param string $index |
||
146 | * @param string $type |
||
147 | * @param array $body |
||
148 | * @param array $queryParams |
||
149 | * @param array|null $return |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function insert($index, $type, array $body, array $queryParams = [], array &$return = null) { |
||
174 | |||
175 | /** |
||
176 | * @param string $index |
||
177 | * @param string $type |
||
178 | * @param string $_id |
||
179 | * @param array $body |
||
180 | * @param array $queryParams |
||
181 | * @param array|null $return |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function update($index, $type, $_id, array $body = [], array $queryParams = [], array &$return = null) { |
||
210 | |||
211 | /** |
||
212 | * @param string $index |
||
213 | * @param string $type |
||
214 | * @param string $_id |
||
215 | * @param array $queryParams |
||
216 | * @param array|null $return |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function delete($index, $type, $_id, array $queryParams = [], array &$return = null) { |
||
241 | |||
242 | public function updateWhere($index, $type, array $where, array &$return = null) { |
||
245 | |||
246 | public function deleteWhere($index, $type, array $where, array &$return = null) { |
||
249 | |||
250 | /** |
||
251 | * |
||
252 | * @param string $index |
||
253 | * @param string $type |
||
254 | * @param string $_id |
||
255 | * @param array $queryParams |
||
256 | * @param array|null $return |
||
257 | * @return array|null |
||
258 | */ |
||
259 | public function get($index, $type, $_id, array $queryParams = [], array &$return = null) { |
||
278 | |||
279 | /** |
||
280 | * Returns the [hits][hits] array from query |
||
281 | * |
||
282 | * @param string $index |
||
283 | * @param string $type |
||
284 | * @param array $body |
||
285 | * @param array $queryParams |
||
286 | * @param array|null $return |
||
287 | * @return array |
||
288 | */ |
||
289 | public function search($index, $type, array $body = [], array $queryParams = [], array &$return = null) { |
||
338 | |||
339 | private function unsetEmpties(array &$array, array &$parent = null) { |
||
358 | |||
359 | /** |
||
360 | * @param string $index |
||
361 | * @return bool |
||
362 | */ |
||
363 | public function indexExists($index) { |
||
368 | |||
369 | /** |
||
370 | * @param string $index |
||
371 | * @param string $type |
||
372 | * @return bool |
||
373 | */ |
||
374 | public function typeExists($index, $type) { |
||
379 | |||
380 | private function throwExceptionFromResponse($response, $appendPrefix = '') { |
||
389 | |||
390 | public function hasConnection() { |
||
395 | |||
396 | private function setErrorFromElasticReturn($return) { |
||
403 | |||
404 | public function getElasticsearchVersion() { |
||
417 | } |
||
418 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.