Complex classes like Bulk 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 Bulk, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Bulk |
||
15 | { |
||
16 | public const DELIMITER = "\n"; |
||
17 | |||
18 | /** |
||
19 | * @var Client |
||
20 | */ |
||
21 | protected $_client; |
||
22 | |||
23 | /** |
||
24 | * @var Action[] |
||
25 | */ |
||
26 | protected $_actions = []; |
||
27 | |||
28 | /** |
||
29 | * @var string|null |
||
30 | */ |
||
31 | protected $_index; |
||
32 | |||
33 | /** |
||
34 | * @var string|null |
||
35 | */ |
||
36 | protected $_type; |
||
37 | |||
38 | /** |
||
39 | * @var array request parameters to the bulk api |
||
40 | */ |
||
41 | protected $_requestParams = []; |
||
42 | |||
43 | public function __construct(Client $client) |
||
47 | |||
48 | public function __toString(): string |
||
52 | |||
53 | /** |
||
54 | * @param Index|string $index |
||
55 | * |
||
56 | * @return $this |
||
57 | */ |
||
58 | public function setIndex($index): self |
||
68 | |||
69 | /** |
||
70 | * @return string|null |
||
71 | */ |
||
72 | public function getIndex() |
||
76 | |||
77 | public function hasIndex(): bool |
||
81 | |||
82 | public function getPath(): string |
||
92 | |||
93 | /** |
||
94 | * @return $this |
||
95 | */ |
||
96 | public function addAction(Action $action): self |
||
102 | |||
103 | /** |
||
104 | * @param Action[] $actions |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function addActions(array $actions): self |
||
116 | |||
117 | /** |
||
118 | * @return Action[] |
||
119 | */ |
||
120 | public function getActions(): array |
||
124 | |||
125 | /** |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function addDocument(Document $document, ?string $opType = null): self |
||
134 | |||
135 | /** |
||
136 | * @param Document[] $documents |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function addDocuments(array $documents, ?string $opType = null): self |
||
148 | |||
149 | /** |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function addScript(AbstractScript $script, ?string $opType = null): self |
||
158 | |||
159 | /** |
||
160 | * @param Document[] $scripts |
||
161 | * @param mixed|null $opType |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function addScripts(array $scripts, $opType = null): self |
||
173 | |||
174 | /** |
||
175 | * @param array|\Elastica\Document|\Elastica\Script\AbstractScript $data |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function addData($data, ?string $opType = null) |
||
197 | |||
198 | /** |
||
199 | * @throws InvalidException |
||
200 | * |
||
201 | * @return $this |
||
202 | */ |
||
203 | public function addRawData(array $data): self |
||
234 | |||
235 | /** |
||
236 | * Set a url parameter on the request bulk request. |
||
237 | * |
||
238 | * @param string $name name of the parameter |
||
239 | * @param mixed $value value of the parameter |
||
240 | * |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function setRequestParam(string $name, $value): self |
||
249 | |||
250 | /** |
||
251 | * Set the amount of time that the request will wait the shards to come on line. |
||
252 | * Requires Elasticsearch version >= 0.90.8. |
||
253 | * |
||
254 | * @param string $time timeout in Elasticsearch time format |
||
255 | * |
||
256 | * @return $this |
||
257 | */ |
||
258 | public function setShardTimeout(string $time): self |
||
262 | |||
263 | public function toString(): string |
||
272 | |||
273 | public function toArray(): array |
||
284 | |||
285 | public function send(): ResponseSet |
||
294 | |||
295 | /** |
||
296 | * @throws ResponseException |
||
297 | * @throws InvalidException |
||
298 | */ |
||
299 | protected function _processResponse(Response $response): ResponseSet |
||
347 | } |
||
348 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: