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 |
||
28 | class Manager |
||
29 | { |
||
30 | /** |
||
31 | * @var string Manager name |
||
32 | */ |
||
33 | private $name; |
||
34 | |||
35 | /** |
||
36 | * @var array Manager configuration |
||
37 | */ |
||
38 | private $config = []; |
||
39 | |||
40 | /** |
||
41 | * @var Client |
||
42 | */ |
||
43 | private $client; |
||
44 | |||
45 | /** |
||
46 | * @var Converter |
||
47 | */ |
||
48 | private $converter; |
||
49 | |||
50 | /** |
||
51 | * @var array Container for bulk queries |
||
52 | */ |
||
53 | private $bulkQueries = []; |
||
54 | |||
55 | /** |
||
56 | * @var array Holder for consistency, refresh and replication parameters |
||
57 | */ |
||
58 | private $bulkParams = []; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | private $indexSettings; |
||
64 | |||
65 | /** |
||
66 | * @var MetadataCollector |
||
67 | */ |
||
68 | private $metadataCollector; |
||
69 | |||
70 | /** |
||
71 | * After commit to make data available the refresh or flush operation is needed |
||
72 | * so one of those methods has to be defined, the default is refresh. |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | private $commitMode = 'refresh'; |
||
77 | |||
78 | /** |
||
79 | * The size that defines after how much document inserts call commit function. |
||
80 | * |
||
81 | * @var int |
||
82 | */ |
||
83 | private $bulkCommitSize = 100; |
||
84 | |||
85 | /** |
||
86 | * Container to count how many documents was passed to the bulk query. |
||
87 | * |
||
88 | * @var int |
||
89 | */ |
||
90 | private $bulkCount = 0; |
||
91 | |||
92 | /** |
||
93 | * @var Repository[] Repository local cache |
||
94 | */ |
||
95 | private $repositories; |
||
96 | |||
97 | /** |
||
98 | * @param string $name Manager name |
||
99 | * @param array $config Manager configuration |
||
100 | * @param Client $client |
||
101 | * @param array $indexSettings |
||
102 | * @param MetadataCollector $metadataCollector |
||
103 | * @param Converter $converter |
||
104 | */ |
||
105 | public function __construct( |
||
106 | $name, |
||
107 | array $config, |
||
108 | $client, |
||
109 | array $indexSettings, |
||
110 | $metadataCollector, |
||
111 | $converter |
||
112 | ) { |
||
113 | $this->name = $name; |
||
114 | $this->config = $config; |
||
115 | $this->client = $client; |
||
116 | $this->indexSettings = $indexSettings; |
||
117 | $this->metadataCollector = $metadataCollector; |
||
118 | $this->converter = $converter; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Returns Elasticsearch connection. |
||
123 | * |
||
124 | * @return Client |
||
125 | */ |
||
126 | public function getClient() |
||
127 | { |
||
128 | return $this->client; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | public function getName() |
||
135 | { |
||
136 | return $this->name; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return array |
||
141 | */ |
||
142 | public function getConfig() |
||
143 | { |
||
144 | return $this->config; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Returns repository by document class. |
||
149 | * |
||
150 | * @param string $className FQCN or string in Bundle:Document format |
||
151 | * |
||
152 | * @return Repository |
||
153 | */ |
||
154 | public function getRepository($className) |
||
171 | |||
172 | /** |
||
173 | * @return MetadataCollector |
||
174 | */ |
||
175 | public function getMetadataCollector() |
||
176 | { |
||
177 | return $this->metadataCollector; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @return Converter |
||
182 | */ |
||
183 | public function getConverter() |
||
184 | { |
||
185 | return $this->converter; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | public function getCommitMode() |
||
192 | { |
||
193 | return $this->commitMode; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param string $commitMode |
||
198 | */ |
||
199 | public function setCommitMode($commitMode) |
||
207 | |||
208 | /** |
||
209 | * @return int |
||
210 | */ |
||
211 | public function getBulkCommitSize() |
||
212 | { |
||
213 | return $this->bulkCommitSize; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param int $bulkCommitSize |
||
218 | */ |
||
219 | public function setBulkCommitSize($bulkCommitSize) |
||
223 | |||
224 | /** |
||
225 | * Creates a repository. |
||
226 | * |
||
227 | * @param string $className |
||
228 | * |
||
229 | * @return Repository |
||
230 | */ |
||
231 | private function createRepository($className) |
||
235 | |||
236 | /** |
||
237 | * Executes search query in the index. |
||
238 | * |
||
239 | * @param array $types List of types to search in. |
||
240 | * @param array $query Query to execute. |
||
241 | * @param array $queryStringParams Query parameters. |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | public function search(array $types, array $query, array $queryStringParams = []) |
||
246 | { |
||
247 | $params = []; |
||
248 | $params['index'] = $this->getIndexName(); |
||
249 | $params['type'] = implode(',', $types); |
||
250 | $params['body'] = $query; |
||
251 | |||
252 | if (!empty($queryStringParams)) { |
||
253 | $params = array_merge($queryStringParams, $params); |
||
254 | } |
||
255 | |||
256 | return $this->client->search($params); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Adds document to next flush. |
||
261 | * |
||
262 | * @param object $document |
||
263 | */ |
||
264 | public function persist($document) |
||
271 | |||
272 | /** |
||
273 | * Adds document for removal. |
||
274 | * |
||
275 | * @param object $document |
||
276 | */ |
||
277 | public function remove($document) |
||
291 | |||
292 | /** |
||
293 | * Flushes elasticsearch index. |
||
294 | * |
||
295 | * @param array $params |
||
296 | * |
||
297 | * @return array |
||
298 | */ |
||
299 | public function flush(array $params = []) |
||
300 | { |
||
304 | |||
305 | /** |
||
306 | * Refreshes elasticsearch index. |
||
307 | * |
||
308 | * @param array $params |
||
309 | * |
||
310 | * @return array |
||
311 | */ |
||
312 | public function refresh(array $params = []) |
||
317 | |||
318 | /** |
||
319 | * Inserts the current query container to the index, used for bulk queries execution. |
||
320 | * |
||
321 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
322 | * |
||
323 | * @return null|array |
||
324 | */ |
||
325 | public function commit(array $params = []) |
||
348 | |||
349 | /** |
||
350 | * Adds query to bulk queries container. |
||
351 | * |
||
352 | * @param string $operation One of: index, update, delete, create. |
||
353 | * @param string|array $type Elasticsearch type name. |
||
354 | * @param array $query DSL to execute. |
||
355 | * |
||
356 | * @throws \InvalidArgumentException |
||
357 | * |
||
358 | * @return null|array |
||
359 | */ |
||
360 | public function bulk($operation, $type, array $query) |
||
402 | |||
403 | /** |
||
404 | * Optional setter to change bulk query params. |
||
405 | * |
||
406 | * @param array $params Possible keys: |
||
407 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
408 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
409 | * ['replication'] = (enum) Explicitly set the replication type. |
||
410 | */ |
||
411 | public function setBulkParams(array $params) |
||
415 | |||
416 | /** |
||
417 | * Creates fresh elasticsearch index. |
||
418 | * |
||
419 | * @param bool $noMapping Determines if mapping should be included. |
||
420 | * |
||
421 | * @return array |
||
422 | */ |
||
423 | public function createIndex($noMapping = false) |
||
431 | |||
432 | /** |
||
433 | * Drops elasticsearch index. |
||
434 | */ |
||
435 | public function dropIndex() |
||
439 | |||
440 | /** |
||
441 | * Tries to drop and create fresh elasticsearch index. |
||
442 | * |
||
443 | * @param bool $noMapping Determines if mapping should be included. |
||
444 | * |
||
445 | * @return array |
||
446 | */ |
||
447 | public function dropAndCreateIndex($noMapping = false) |
||
457 | |||
458 | /** |
||
459 | * Checks if connection index is already created. |
||
460 | * |
||
461 | * @return bool |
||
462 | */ |
||
463 | public function indexExists() |
||
467 | |||
468 | /** |
||
469 | * Returns index name this connection is attached to. |
||
470 | * |
||
471 | * @return string |
||
472 | */ |
||
473 | public function getIndexName() |
||
477 | |||
478 | /** |
||
479 | * Sets index name for this connection. |
||
480 | * |
||
481 | * @param string $name |
||
482 | */ |
||
483 | public function setIndexName($name) |
||
487 | |||
488 | /** |
||
489 | * Returns Elasticsearch version number. |
||
490 | * |
||
491 | * @return string |
||
492 | */ |
||
493 | public function getVersionNumber() |
||
497 | |||
498 | /** |
||
499 | * Clears elasticsearch client cache. |
||
500 | */ |
||
501 | public function clearCache() |
||
505 | |||
506 | /** |
||
507 | * Returns a single document by ID. Returns NULL if document was not found. |
||
508 | * |
||
509 | * @param string $className Document class name or Elasticsearch type name |
||
510 | * @param string $id Document ID to find |
||
511 | * |
||
512 | * @return object |
||
513 | */ |
||
514 | public function find($className, $id) |
||
532 | |||
533 | /** |
||
534 | * Executes given search. |
||
535 | * |
||
536 | * @param array $types |
||
537 | * @param Search $search |
||
538 | * @param string $resultsType |
||
539 | * |
||
540 | * @return DocumentIterator|RawIterator|array |
||
541 | */ |
||
542 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
552 | |||
553 | /** |
||
554 | * Parses raw result. |
||
555 | * |
||
556 | * @param array $raw |
||
557 | * @param string $resultsType |
||
558 | * @param string $scrollDuration |
||
559 | * |
||
560 | * @return DocumentIterator|RawIterator|array |
||
561 | * |
||
562 | * @throws \Exception |
||
563 | */ |
||
564 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
585 | |||
586 | /** |
||
587 | * Normalizes response array. |
||
588 | * |
||
589 | * @param array $data |
||
590 | * |
||
591 | * @return array |
||
592 | */ |
||
593 | private function convertToNormalizedArray($data) |
||
613 | |||
614 | /** |
||
615 | * Fetches next set of results. |
||
616 | * |
||
617 | * @param string $scrollId |
||
618 | * @param string $scrollDuration |
||
619 | * @param string $resultsType |
||
620 | * |
||
621 | * @return AbstractResultsIterator |
||
622 | * |
||
623 | * @throws \Exception |
||
624 | */ |
||
625 | public function scroll( |
||
634 | |||
635 | /** |
||
636 | * Clears scroll. |
||
637 | * |
||
638 | * @param string $scrollId |
||
639 | */ |
||
640 | public function clearScroll($scrollId) |
||
644 | |||
645 | /** |
||
646 | * Resolves type name by class name. |
||
647 | * |
||
648 | * @param string $className |
||
649 | * |
||
650 | * @return string |
||
651 | */ |
||
652 | private function resolveTypeName($className) |
||
660 | } |
||
661 |
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: