Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Manager |
||
32 | { |
||
33 | /** |
||
34 | * @var string Manager name |
||
35 | */ |
||
36 | private $name; |
||
37 | |||
38 | /** |
||
39 | * @var array Manager configuration |
||
40 | */ |
||
41 | private $config = []; |
||
42 | |||
43 | /** |
||
44 | * @var Client |
||
45 | */ |
||
46 | private $client; |
||
47 | |||
48 | /** |
||
49 | * @var Converter |
||
50 | */ |
||
51 | private $converter; |
||
52 | |||
53 | /** |
||
54 | * @var array Container for bulk queries |
||
55 | */ |
||
56 | private $bulkQueries = []; |
||
57 | |||
58 | /** |
||
59 | * @var array Holder for consistency, refresh and replication parameters |
||
60 | */ |
||
61 | private $bulkParams = []; |
||
62 | |||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | private $indexSettings; |
||
67 | |||
68 | /** |
||
69 | * @var MetadataCollector |
||
70 | */ |
||
71 | private $metadataCollector; |
||
72 | |||
73 | /** |
||
74 | * After commit to make data available the refresh or flush operation is needed |
||
75 | * so one of those methods has to be defined, the default is refresh. |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | private $commitMode = 'refresh'; |
||
80 | |||
81 | /** |
||
82 | * The size that defines after how much document inserts call commit function. |
||
83 | * |
||
84 | * @var int |
||
85 | */ |
||
86 | private $bulkCommitSize = 100; |
||
87 | |||
88 | /** |
||
89 | * Container to count how many documents was passed to the bulk query. |
||
90 | * |
||
91 | * @var int |
||
92 | */ |
||
93 | private $bulkCount = 0; |
||
94 | |||
95 | /** |
||
96 | * @var Repository[] Repository local cache |
||
97 | */ |
||
98 | private $repositories; |
||
99 | |||
100 | /** |
||
101 | * @var EventDispatcherInterface |
||
102 | */ |
||
103 | private $eventDispatcher; |
||
104 | |||
105 | /** |
||
106 | * @param string $name Manager name |
||
107 | * @param array $config Manager configuration |
||
108 | * @param Client $client |
||
109 | * @param array $indexSettings |
||
110 | * @param MetadataCollector $metadataCollector |
||
111 | * @param Converter $converter |
||
112 | * @param EventDispatcherInterface $eventDispatcher |
||
113 | */ |
||
114 | public function __construct( |
||
131 | |||
132 | /** |
||
133 | * Returns Elasticsearch connection. |
||
134 | * |
||
135 | * @return Client |
||
136 | */ |
||
137 | public function getClient() |
||
138 | { |
||
139 | return $this->client; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getName() |
||
146 | { |
||
147 | return $this->name; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @return array |
||
152 | */ |
||
153 | public function getConfig() |
||
154 | { |
||
155 | return $this->config; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Returns repository by document class. |
||
160 | * |
||
161 | * @param string $className FQCN or string in Bundle:Document format |
||
162 | * |
||
163 | * @return Repository |
||
164 | */ |
||
165 | public function getRepository($className) |
||
182 | |||
183 | /** |
||
184 | * @return MetadataCollector |
||
185 | */ |
||
186 | public function getMetadataCollector() |
||
187 | { |
||
188 | return $this->metadataCollector; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return Converter |
||
193 | */ |
||
194 | public function getConverter() |
||
195 | { |
||
196 | return $this->converter; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getCommitMode() |
||
203 | { |
||
204 | return $this->commitMode; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param string $commitMode |
||
209 | */ |
||
210 | public function setCommitMode($commitMode) |
||
218 | |||
219 | /** |
||
220 | * @return int |
||
221 | */ |
||
222 | public function getBulkCommitSize() |
||
223 | { |
||
224 | return $this->bulkCommitSize; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param int $bulkCommitSize |
||
229 | */ |
||
230 | public function setBulkCommitSize($bulkCommitSize) |
||
234 | |||
235 | /** |
||
236 | * Creates a repository. |
||
237 | * |
||
238 | * @param string $className |
||
239 | * |
||
240 | * @return Repository |
||
241 | */ |
||
242 | private function createRepository($className) |
||
246 | |||
247 | /** |
||
248 | * Executes search query in the index. |
||
249 | * |
||
250 | * @param array $types List of types to search in. |
||
251 | * @param array $query Query to execute. |
||
252 | * @param array $queryStringParams Query parameters. |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | public function search(array $types, array $query, array $queryStringParams = []) |
||
257 | { |
||
258 | $params = []; |
||
259 | $params['index'] = $this->getIndexName(); |
||
260 | $params['type'] = implode(',', $types); |
||
261 | $params['body'] = $query; |
||
262 | |||
263 | if (!empty($queryStringParams)) { |
||
264 | $params = array_merge($queryStringParams, $params); |
||
265 | } |
||
266 | |||
267 | return $this->client->search($params); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Adds document to next flush. |
||
272 | * |
||
273 | * @param object $document |
||
274 | */ |
||
275 | public function persist($document) |
||
282 | |||
283 | /** |
||
284 | * Adds document for removal. |
||
285 | * |
||
286 | * @param object $document |
||
287 | */ |
||
288 | public function remove($document) |
||
302 | |||
303 | /** |
||
304 | * Flushes elasticsearch index. |
||
305 | * |
||
306 | * @param array $params |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | public function flush(array $params = []) |
||
311 | { |
||
312 | return $this->client->indices()->flush(array_merge(['index' => $this->getIndexName()], $params)); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Refreshes elasticsearch index. |
||
317 | * |
||
318 | * @param array $params |
||
319 | * |
||
320 | * @return array |
||
321 | */ |
||
322 | public function refresh(array $params = []) |
||
323 | { |
||
324 | return $this->client->indices()->refresh(array_merge(['index' => $this->getIndexName()], $params)); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Inserts the current query container to the index, used for bulk queries execution. |
||
329 | * |
||
330 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
331 | * |
||
332 | * @return null|array |
||
333 | */ |
||
334 | public function commit(array $params = []) |
||
362 | |||
363 | /** |
||
364 | * Adds query to bulk queries container. |
||
365 | * |
||
366 | * @param string $operation One of: index, update, delete, create. |
||
367 | * @param string|array $type Elasticsearch type name. |
||
368 | * @param array $query DSL to execute. |
||
369 | * |
||
370 | * @throws \InvalidArgumentException |
||
371 | * |
||
372 | * @return null|array |
||
373 | */ |
||
374 | public function bulk($operation, $type, array $query) |
||
375 | { |
||
376 | if (!in_array($operation, ['index', 'create', 'update', 'delete'])) { |
||
377 | throw new \InvalidArgumentException('Wrong bulk operation selected'); |
||
378 | } |
||
379 | |||
380 | $this->bulkQueries['body'][] = [ |
||
381 | $operation => array_filter( |
||
382 | [ |
||
383 | '_index' => $this->getIndexName(), |
||
384 | '_type' => $type, |
||
385 | '_id' => isset($query['_id']) ? $query['_id'] : null, |
||
386 | '_ttl' => isset($query['_ttl']) ? $query['_ttl'] : null, |
||
387 | '_parent' => isset($query['_parent']) ? $query['_parent'] : null, |
||
388 | ] |
||
389 | ), |
||
390 | ]; |
||
391 | unset($query['_id'], $query['_ttl'], $query['_parent']); |
||
392 | |||
393 | $this->eventDispatcher->dispatch( |
||
394 | constant('ONGR\ElasticsearchBundle\ONGRElasticsearchEvents::PRE_' . strtoupper($operation)), |
||
395 | new OperationEvent($operation, $type, $query) |
||
396 | ); |
||
397 | |||
398 | switch ($operation) { |
||
399 | case 'index': |
||
400 | case 'create': |
||
401 | case 'update': |
||
402 | $this->bulkQueries['body'][] = $query; |
||
403 | break; |
||
404 | case 'delete': |
||
405 | // Body for delete operation is not needed to apply. |
||
406 | default: |
||
407 | // Do nothing. |
||
408 | break; |
||
409 | } |
||
410 | |||
411 | // We are using counter because there is to difficult to resolve this from bulkQueries array. |
||
412 | $this->bulkCount++; |
||
413 | |||
414 | $response = null; |
||
415 | if ($this->bulkCommitSize === $this->bulkCount) { |
||
416 | $response = $this->commit(); |
||
417 | } |
||
418 | |||
419 | return $response; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Optional setter to change bulk query params. |
||
424 | * |
||
425 | * @param array $params Possible keys: |
||
426 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
427 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
428 | * ['replication'] = (enum) Explicitly set the replication type. |
||
429 | */ |
||
430 | public function setBulkParams(array $params) |
||
431 | { |
||
432 | $this->bulkParams = $params; |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Creates fresh elasticsearch index. |
||
437 | * |
||
438 | * @param bool $noMapping Determines if mapping should be included. |
||
439 | * |
||
440 | * @return array |
||
441 | */ |
||
442 | public function createIndex($noMapping = false) |
||
443 | { |
||
444 | if ($noMapping) { |
||
445 | unset($this->indexSettings['body']['mappings']); |
||
446 | } |
||
447 | |||
448 | return $this->getClient()->indices()->create($this->indexSettings); |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Drops elasticsearch index. |
||
453 | */ |
||
454 | public function dropIndex() |
||
455 | { |
||
456 | return $this->getClient()->indices()->delete(['index' => $this->getIndexName()]); |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Tries to drop and create fresh elasticsearch index. |
||
461 | * |
||
462 | * @param bool $noMapping Determines if mapping should be included. |
||
463 | * |
||
464 | * @return array |
||
465 | */ |
||
466 | public function dropAndCreateIndex($noMapping = false) |
||
467 | { |
||
468 | try { |
||
469 | $this->dropIndex(); |
||
470 | } catch (\Exception $e) { |
||
471 | // Do nothing, our target is to create new index. |
||
472 | } |
||
473 | |||
474 | return $this->createIndex($noMapping); |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * Checks if connection index is already created. |
||
479 | * |
||
480 | * @return bool |
||
481 | */ |
||
482 | public function indexExists() |
||
486 | |||
487 | /** |
||
488 | * Returns index name this connection is attached to. |
||
489 | * |
||
490 | * @return string |
||
491 | */ |
||
492 | public function getIndexName() |
||
496 | |||
497 | /** |
||
498 | * Sets index name for this connection. |
||
499 | * |
||
500 | * @param string $name |
||
501 | */ |
||
502 | public function setIndexName($name) |
||
506 | |||
507 | /** |
||
508 | * Returns Elasticsearch version number. |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | public function getVersionNumber() |
||
516 | |||
517 | /** |
||
518 | * Clears elasticsearch client cache. |
||
519 | */ |
||
520 | public function clearCache() |
||
524 | |||
525 | /** |
||
526 | * Returns a single document by ID. Returns NULL if document was not found. |
||
527 | * |
||
528 | * @param string $className Document class name or Elasticsearch type name |
||
529 | * @param string $id Document ID to find |
||
530 | * |
||
531 | * @return object |
||
532 | */ |
||
533 | public function find($className, $id) |
||
551 | |||
552 | /** |
||
553 | * Executes given search. |
||
554 | * |
||
555 | * @param array $types |
||
556 | * @param Search $search |
||
557 | * @param string $resultsType |
||
558 | * |
||
559 | * @return DocumentIterator|RawIterator|array |
||
560 | */ |
||
561 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
571 | |||
572 | /** |
||
573 | * Parses raw result. |
||
574 | * |
||
575 | * @param array $raw |
||
576 | * @param string $resultsType |
||
577 | * @param string $scrollDuration |
||
578 | * |
||
579 | * @return DocumentIterator|RawIterator|array |
||
580 | * |
||
581 | * @throws \Exception |
||
582 | */ |
||
583 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
604 | |||
605 | /** |
||
606 | * Normalizes response array. |
||
607 | * |
||
608 | * @param array $data |
||
609 | * |
||
610 | * @return array |
||
611 | */ |
||
612 | private function convertToNormalizedArray($data) |
||
632 | |||
633 | /** |
||
634 | * Fetches next set of results. |
||
635 | * |
||
636 | * @param string $scrollId |
||
637 | * @param string $scrollDuration |
||
638 | * @param string $resultsType |
||
639 | * |
||
640 | * @return AbstractResultsIterator |
||
641 | * |
||
642 | * @throws \Exception |
||
643 | */ |
||
644 | public function scroll( |
||
653 | |||
654 | /** |
||
655 | * Clears scroll. |
||
656 | * |
||
657 | * @param string $scrollId |
||
658 | */ |
||
659 | public function clearScroll($scrollId) |
||
663 | |||
664 | /** |
||
665 | * Resolves type name by class name. |
||
666 | * |
||
667 | * @param string $className |
||
668 | * |
||
669 | * @return string |
||
670 | */ |
||
671 | private function resolveTypeName($className) |
||
679 | } |
||
680 |
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: