Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
30 | class Manager |
||
31 | { |
||
32 | /** |
||
33 | * @var string Manager name |
||
34 | */ |
||
35 | private $name; |
||
36 | |||
37 | /** |
||
38 | * @var array Manager configuration |
||
39 | */ |
||
40 | private $config = []; |
||
41 | |||
42 | /** |
||
43 | * @var Client |
||
44 | */ |
||
45 | private $client; |
||
46 | |||
47 | /** |
||
48 | * @var Converter |
||
49 | */ |
||
50 | private $converter; |
||
51 | |||
52 | /** |
||
53 | * @var array Container for bulk queries |
||
54 | */ |
||
55 | private $bulkQueries = []; |
||
56 | |||
57 | /** |
||
58 | * @var array Holder for consistency, refresh and replication parameters |
||
59 | */ |
||
60 | private $bulkParams = []; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | private $indexSettings; |
||
66 | |||
67 | /** |
||
68 | * @var MetadataCollector |
||
69 | */ |
||
70 | private $metadataCollector; |
||
71 | |||
72 | /** |
||
73 | * After commit to make data available the refresh or flush operation is needed |
||
74 | * so one of those methods has to be defined, the default is refresh. |
||
75 | * |
||
76 | * @var string |
||
77 | */ |
||
78 | private $commitMode = 'refresh'; |
||
79 | |||
80 | /** |
||
81 | * The size that defines after how much document inserts call commit function. |
||
82 | * |
||
83 | * @var int |
||
84 | */ |
||
85 | private $bulkCommitSize = 100; |
||
86 | |||
87 | /** |
||
88 | * Container to count how many documents was passed to the bulk query. |
||
89 | * |
||
90 | * @var int |
||
91 | */ |
||
92 | private $bulkCount = 0; |
||
93 | |||
94 | /** |
||
95 | * @var Repository[] Repository local cache |
||
96 | */ |
||
97 | private $repositories; |
||
98 | |||
99 | /** |
||
100 | * @var EventDispatcherInterface |
||
101 | */ |
||
102 | private $eventDispatcher; |
||
103 | |||
104 | /** |
||
105 | * @var Stopwatch |
||
106 | */ |
||
107 | private $stopwatch; |
||
108 | |||
109 | /** |
||
110 | * @param string $name Manager name |
||
111 | * @param array $config Manager configuration |
||
112 | * @param Client $client |
||
113 | * @param array $indexSettings |
||
114 | * @param MetadataCollector $metadataCollector |
||
115 | * @param Converter $converter |
||
116 | */ |
||
117 | public function __construct( |
||
118 | $name, |
||
119 | array $config, |
||
120 | $client, |
||
121 | array $indexSettings, |
||
122 | $metadataCollector, |
||
123 | $converter |
||
124 | ) { |
||
125 | $this->name = $name; |
||
126 | $this->config = $config; |
||
127 | $this->client = $client; |
||
128 | $this->indexSettings = $indexSettings; |
||
129 | $this->metadataCollector = $metadataCollector; |
||
130 | $this->converter = $converter; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Returns Elasticsearch connection. |
||
135 | * |
||
136 | * @return Client |
||
137 | */ |
||
138 | public function getClient() |
||
139 | { |
||
140 | return $this->client; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | public function getName() |
||
147 | { |
||
148 | return $this->name; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @return array |
||
153 | */ |
||
154 | public function getConfig() |
||
155 | { |
||
156 | return $this->config; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param EventDispatcherInterface $eventDispatcher |
||
161 | */ |
||
162 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
163 | { |
||
164 | $this->eventDispatcher = $eventDispatcher; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param Stopwatch $stopwatch |
||
169 | */ |
||
170 | public function setStopwatch(Stopwatch $stopwatch) |
||
171 | { |
||
172 | $this->stopwatch = $stopwatch; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Returns repository by document class. |
||
177 | * |
||
178 | * @param string $className FQCN or string in Bundle:Document format |
||
179 | * |
||
180 | * @return Repository |
||
181 | */ |
||
182 | public function getRepository($className) |
||
183 | { |
||
184 | if (!is_string($className)) { |
||
185 | throw new \InvalidArgumentException('Document class must be a string.'); |
||
186 | } |
||
187 | |||
188 | $directory = null; |
||
189 | |||
190 | if (strpos($className, ':')) { |
||
191 | $bundle = explode(':', $className)[0]; |
||
192 | |||
193 | if (isset($this->config['mappings'][$bundle]['document_dir'])) { |
||
194 | $directory = $this->config['mappings'][$bundle]['document_dir']; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | $namespace = $this->getMetadataCollector()->getClassName($className, $directory); |
||
199 | |||
200 | if (isset($this->repositories[$namespace])) { |
||
201 | return $this->repositories[$namespace]; |
||
202 | } |
||
203 | |||
204 | $repository = $this->createRepository($namespace); |
||
205 | $this->repositories[$namespace] = $repository; |
||
206 | |||
207 | return $repository; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @return MetadataCollector |
||
212 | */ |
||
213 | public function getMetadataCollector() |
||
214 | { |
||
215 | return $this->metadataCollector; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * @return Converter |
||
220 | */ |
||
221 | public function getConverter() |
||
222 | { |
||
223 | return $this->converter; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | public function getCommitMode() |
||
230 | { |
||
231 | return $this->commitMode; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param string $commitMode |
||
236 | */ |
||
237 | public function setCommitMode($commitMode) |
||
238 | { |
||
239 | if ($commitMode === 'refresh' || $commitMode === 'flush' || $commitMode === 'none') { |
||
240 | $this->commitMode = $commitMode; |
||
241 | } else { |
||
242 | throw new \LogicException('The commit method must be either refresh, flush or none.'); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @return int |
||
248 | */ |
||
249 | public function getBulkCommitSize() |
||
250 | { |
||
251 | return $this->bulkCommitSize; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @param int $bulkCommitSize |
||
256 | */ |
||
257 | public function setBulkCommitSize($bulkCommitSize) |
||
258 | { |
||
259 | $this->bulkCommitSize = $bulkCommitSize; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Creates a repository. |
||
264 | * |
||
265 | * @param string $className |
||
266 | * |
||
267 | * @return Repository |
||
268 | */ |
||
269 | private function createRepository($className) |
||
270 | { |
||
271 | return new Repository($this, $className); |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * Executes search query in the index. |
||
276 | * |
||
277 | * @param array $types List of types to search in. |
||
278 | * @param array $query Query to execute. |
||
279 | * @param array $queryStringParams Query parameters. |
||
280 | * |
||
281 | * @return array |
||
282 | */ |
||
283 | public function search(array $types, array $query, array $queryStringParams = []) |
||
284 | { |
||
285 | $params = []; |
||
286 | $params['index'] = $this->getIndexName(); |
||
287 | |||
288 | $resolvedTypes = []; |
||
289 | foreach ($types as $type) { |
||
290 | $resolvedTypes[] = $this->resolveTypeName($type); |
||
291 | } |
||
292 | |||
293 | if (!empty($resolvedTypes)) { |
||
294 | $params['type'] = implode(',', $resolvedTypes); |
||
295 | } |
||
296 | |||
297 | $params['body'] = $query; |
||
298 | |||
299 | if (!empty($queryStringParams)) { |
||
300 | $params = array_merge($queryStringParams, $params); |
||
301 | } |
||
302 | |||
303 | $this->stopwatch('start', 'search'); |
||
304 | $result = $this->client->search($params); |
||
305 | $this->stopwatch('stop', 'search'); |
||
306 | |||
307 | return $result; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Execute search queries using multisearch api |
||
312 | * $body - is array of requests described in elastic Multi Search API |
||
313 | * |
||
314 | * @param $body |
||
315 | * @return array |
||
316 | */ |
||
317 | public function msearch(array $body) |
||
318 | { |
||
319 | $result = $this->client->msearch( |
||
320 | [ |
||
321 | 'index' => $this->getIndexName(), // set default index |
||
322 | 'body' => $body |
||
323 | ] |
||
324 | ); |
||
325 | return $result; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Adds document to next flush. |
||
330 | * |
||
331 | * @param object $document |
||
332 | */ |
||
333 | public function persist($document) |
||
334 | { |
||
335 | $this->dispatch( |
||
336 | Events::PRE_PERSIST, |
||
337 | new PrePersistEvent($document) |
||
338 | ); |
||
339 | |||
340 | $documentArray = $this->converter->convertToArray($document); |
||
341 | $type = $this->getMetadataCollector()->getDocumentType(get_class($document)); |
||
342 | |||
343 | $this->bulk('index', $type, $documentArray); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Adds document for removal. |
||
348 | * |
||
349 | * @param object $document |
||
350 | */ |
||
351 | public function remove($document) |
||
365 | |||
366 | /** |
||
367 | * Flushes elasticsearch index. |
||
368 | * |
||
369 | * @param array $params |
||
370 | * |
||
371 | * @return array |
||
372 | */ |
||
373 | public function flush(array $params = []) |
||
377 | |||
378 | /** |
||
379 | * Refreshes elasticsearch index. |
||
380 | * |
||
381 | * @param array $params |
||
382 | * |
||
383 | * @return array |
||
384 | */ |
||
385 | public function refresh(array $params = []) |
||
389 | |||
390 | /** |
||
391 | * Inserts the current query container to the index, used for bulk queries execution. |
||
392 | * |
||
393 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
394 | * |
||
395 | * @return null|array |
||
396 | * |
||
397 | * @throws BulkWithErrorsException |
||
398 | */ |
||
399 | public function commit(array $params = []) |
||
400 | { |
||
401 | if (!empty($this->bulkQueries)) { |
||
402 | $bulkQueries = array_merge($this->bulkQueries, $this->bulkParams); |
||
403 | $bulkQueries['index']['_index'] = $this->getIndexName(); |
||
404 | $this->dispatch( |
||
405 | Events::PRE_COMMIT, |
||
406 | new CommitEvent($this->getCommitMode(), $bulkQueries) |
||
407 | ); |
||
408 | |||
409 | $this->stopwatch('start', 'bulk'); |
||
410 | $bulkResponse = $this->client->bulk($bulkQueries); |
||
411 | $this->stopwatch('stop', 'bulk'); |
||
412 | |||
413 | if (isset($bulkResponse['errors']) && $bulkResponse['errors']) { |
||
414 | throw new BulkWithErrorsException( |
||
415 | json_encode($bulkResponse), |
||
416 | 0, |
||
417 | null, |
||
418 | $bulkResponse |
||
|
|||
419 | ); |
||
420 | } |
||
421 | |||
422 | $this->bulkQueries = []; |
||
423 | $this->bulkCount = 0; |
||
424 | |||
425 | $this->stopwatch('start', 'refresh'); |
||
426 | |||
427 | switch ($this->getCommitMode()) { |
||
428 | case 'flush': |
||
429 | $this->flush($params); |
||
430 | break; |
||
431 | case 'refresh': |
||
432 | $this->refresh($params); |
||
433 | break; |
||
434 | } |
||
435 | |||
436 | $this->dispatch( |
||
437 | Events::POST_COMMIT, |
||
438 | new CommitEvent($this->getCommitMode(), $bulkResponse) |
||
439 | ); |
||
440 | |||
441 | $this->stopwatch('stop', 'refresh'); |
||
442 | |||
443 | return $bulkResponse; |
||
444 | } |
||
445 | |||
446 | return null; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * Adds query to bulk queries container. |
||
451 | * |
||
452 | * @param string $operation One of: index, update, delete, create. |
||
453 | * @param string|array $type Elasticsearch type name. |
||
454 | * @param array $query DSL to execute. |
||
455 | * |
||
456 | * @throws \InvalidArgumentException |
||
457 | * |
||
458 | * @return null|array |
||
459 | */ |
||
460 | public function bulk($operation, $type, array $query) |
||
508 | |||
509 | /** |
||
510 | * Optional setter to change bulk query params. |
||
511 | * |
||
512 | * @param array $params Possible keys: |
||
513 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
514 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
515 | * ['replication'] = (enum) Explicitly set the replication type. |
||
516 | */ |
||
517 | public function setBulkParams(array $params) |
||
521 | |||
522 | /** |
||
523 | * Creates fresh elasticsearch index. |
||
524 | * |
||
525 | * @param bool $noMapping Determines if mapping should be included. |
||
526 | * |
||
527 | * @return array |
||
528 | */ |
||
529 | public function createIndex($noMapping = false) |
||
537 | |||
538 | /** |
||
539 | * Drops elasticsearch index. |
||
540 | */ |
||
541 | public function dropIndex() |
||
545 | |||
546 | /** |
||
547 | * Tries to drop and create fresh elasticsearch index. |
||
548 | * |
||
549 | * @param bool $noMapping Determines if mapping should be included. |
||
550 | * |
||
551 | * @return array |
||
552 | */ |
||
553 | public function dropAndCreateIndex($noMapping = false) |
||
565 | |||
566 | /** |
||
567 | * Checks if connection index is already created. |
||
568 | * |
||
569 | * @return bool |
||
570 | */ |
||
571 | public function indexExists() |
||
575 | |||
576 | /** |
||
577 | * Returns index name this connection is attached to. |
||
578 | * |
||
579 | * @return string |
||
580 | */ |
||
581 | public function getIndexName() |
||
585 | |||
586 | /** |
||
587 | * Sets index name for this connection. |
||
588 | * |
||
589 | * @param string $name |
||
590 | */ |
||
591 | public function setIndexName($name) |
||
595 | |||
596 | /** |
||
597 | * Returns mappings of the index for this connection. |
||
598 | * |
||
599 | * @return array |
||
600 | */ |
||
601 | public function getIndexMappings() |
||
605 | |||
606 | /** |
||
607 | * Returns Elasticsearch version number. |
||
608 | * |
||
609 | * @return string |
||
610 | */ |
||
611 | public function getVersionNumber() |
||
615 | |||
616 | /** |
||
617 | * Clears elasticsearch client cache. |
||
618 | */ |
||
619 | public function clearCache() |
||
623 | |||
624 | /** |
||
625 | * Returns a single document by ID. Returns NULL if document was not found. |
||
626 | * |
||
627 | * @param string $className Document class name or Elasticsearch type name |
||
628 | * @param string $id Document ID to find |
||
629 | * @param string $routing Custom routing for the document |
||
630 | * |
||
631 | * @return object |
||
632 | */ |
||
633 | public function find($className, $id, $routing = null) |
||
655 | |||
656 | /** |
||
657 | * Fetches next set of results. |
||
658 | * |
||
659 | * @param string $scrollId |
||
660 | * @param string $scrollDuration |
||
661 | * |
||
662 | * @return mixed |
||
663 | * |
||
664 | * @throws \Exception |
||
665 | */ |
||
666 | public function scroll( |
||
674 | |||
675 | /** |
||
676 | * Clears scroll. |
||
677 | * |
||
678 | * @param string $scrollId |
||
679 | */ |
||
680 | public function clearScroll($scrollId) |
||
684 | |||
685 | /** |
||
686 | * Calls "Get Settings API" in Elasticsearch and will return you the currently configured settings. |
||
687 | * |
||
688 | * return array |
||
689 | */ |
||
690 | public function getSettings() |
||
694 | |||
695 | /** |
||
696 | * Gets Elasticsearch aliases information. |
||
697 | * @param $params |
||
698 | * |
||
699 | * @return array |
||
700 | */ |
||
701 | public function getAliases($params = []) |
||
705 | |||
706 | /** |
||
707 | * Resolves type name by class name. |
||
708 | * |
||
709 | * @param string $className |
||
710 | * |
||
711 | * @return string |
||
712 | */ |
||
713 | private function resolveTypeName($className) |
||
721 | |||
722 | /** |
||
723 | * Starts and stops an event in the stopwatch |
||
724 | * |
||
725 | * @param string $action only 'start' and 'stop' |
||
726 | * @param string $name name of the event |
||
727 | */ |
||
728 | private function stopwatch($action, $name) |
||
734 | |||
735 | View Code Duplication | private function dispatch($eventName, $event) |
|
736 | { |
||
737 | if (class_exists(LegacyEventDispatcherProxy::class)) { |
||
738 | return $this->eventDispatcher->dispatch($event, $eventName); |
||
739 | } else { |
||
740 | return $this->eventDispatcher->dispatch($eventName, $event); |
||
741 | } |
||
742 | } |
||
743 | } |
||
744 |
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: