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 |
||
33 | class Manager |
||
34 | { |
||
35 | /** |
||
36 | * @var string Manager name |
||
37 | */ |
||
38 | private $name; |
||
39 | |||
40 | /** |
||
41 | * @var array Manager configuration |
||
42 | */ |
||
43 | private $config = []; |
||
44 | |||
45 | /** |
||
46 | * @var Client |
||
47 | */ |
||
48 | private $client; |
||
49 | |||
50 | /** |
||
51 | * @var Converter |
||
52 | */ |
||
53 | private $converter; |
||
54 | |||
55 | /** |
||
56 | * @var array Container for bulk queries |
||
57 | */ |
||
58 | private $bulkQueries = []; |
||
59 | |||
60 | /** |
||
61 | * @var array Holder for consistency, refresh and replication parameters |
||
62 | */ |
||
63 | private $bulkParams = []; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | private $indexSettings; |
||
69 | |||
70 | /** |
||
71 | * @var MetadataCollector |
||
72 | */ |
||
73 | private $metadataCollector; |
||
74 | |||
75 | /** |
||
76 | * After commit to make data available the refresh or flush operation is needed |
||
77 | * so one of those methods has to be defined, the default is refresh. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private $commitMode = 'refresh'; |
||
82 | |||
83 | /** |
||
84 | * The size that defines after how much document inserts call commit function. |
||
85 | * |
||
86 | * @var int |
||
87 | */ |
||
88 | private $bulkCommitSize = 100; |
||
89 | |||
90 | /** |
||
91 | * Container to count how many documents was passed to the bulk query. |
||
92 | * |
||
93 | * @var int |
||
94 | */ |
||
95 | private $bulkCount = 0; |
||
96 | |||
97 | /** |
||
98 | * @var Repository[] Repository local cache |
||
99 | */ |
||
100 | private $repositories; |
||
101 | |||
102 | /** |
||
103 | * @var EventDispatcherInterface |
||
104 | */ |
||
105 | private $eventDispatcher; |
||
106 | |||
107 | /** |
||
108 | * @var Stopwatch |
||
109 | */ |
||
110 | private $stopwatch; |
||
111 | |||
112 | /** |
||
113 | * @param string $name Manager name |
||
114 | * @param array $config Manager configuration |
||
115 | * @param Client $client |
||
116 | * @param array $indexSettings |
||
117 | * @param MetadataCollector $metadataCollector |
||
118 | * @param Converter $converter |
||
119 | */ |
||
120 | public function __construct( |
||
135 | |||
136 | /** |
||
137 | * Returns Elasticsearch connection. |
||
138 | * |
||
139 | * @return Client |
||
140 | */ |
||
141 | public function getClient() |
||
145 | |||
146 | /** |
||
147 | * @return string |
||
148 | */ |
||
149 | public function getName() |
||
153 | |||
154 | /** |
||
155 | * @return array |
||
156 | */ |
||
157 | public function getConfig() |
||
161 | |||
162 | /** |
||
163 | * @param EventDispatcherInterface $eventDispatcher |
||
164 | */ |
||
165 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
166 | { |
||
167 | $this->eventDispatcher = $eventDispatcher; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param Stopwatch $stopwatch |
||
172 | */ |
||
173 | public function setStopwatch(Stopwatch $stopwatch) |
||
174 | { |
||
175 | $this->stopwatch = $stopwatch; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Returns repository by document class. |
||
180 | * |
||
181 | * @param string $className FQCN or string in Bundle:Document format |
||
182 | * |
||
183 | * @return Repository |
||
184 | */ |
||
185 | public function getRepository($className) |
||
202 | |||
203 | /** |
||
204 | * @return MetadataCollector |
||
205 | */ |
||
206 | public function getMetadataCollector() |
||
210 | |||
211 | /** |
||
212 | * @return Converter |
||
213 | */ |
||
214 | public function getConverter() |
||
218 | |||
219 | /** |
||
220 | * @return string |
||
221 | */ |
||
222 | public function getCommitMode() |
||
226 | |||
227 | /** |
||
228 | * @param string $commitMode |
||
229 | */ |
||
230 | public function setCommitMode($commitMode) |
||
231 | { |
||
232 | if ($commitMode === 'refresh' || $commitMode === 'flush' || $commitMode === 'none') { |
||
233 | $this->commitMode = $commitMode; |
||
234 | } else { |
||
235 | throw new \LogicException('The commit method must be either refresh, flush or none.'); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @return int |
||
241 | */ |
||
242 | public function getBulkCommitSize() |
||
246 | |||
247 | /** |
||
248 | * @param int $bulkCommitSize |
||
249 | */ |
||
250 | public function setBulkCommitSize($bulkCommitSize) |
||
254 | |||
255 | /** |
||
256 | * Creates a repository. |
||
257 | * |
||
258 | * @param string $className |
||
259 | * |
||
260 | * @return Repository |
||
261 | */ |
||
262 | private function createRepository($className) |
||
266 | |||
267 | /** |
||
268 | * Executes search query in the index. |
||
269 | * |
||
270 | * @param array $types List of types to search in. |
||
271 | * @param array $query Query to execute. |
||
272 | * @param array $queryStringParams Query parameters. |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | public function search(array $types, array $query, array $queryStringParams = []) |
||
297 | |||
298 | /** |
||
299 | * Adds document to next flush. |
||
300 | * |
||
301 | * @param object $document |
||
302 | */ |
||
303 | public function persist($document) |
||
310 | |||
311 | /** |
||
312 | * Adds document for removal. |
||
313 | * |
||
314 | * @param object $document |
||
315 | */ |
||
316 | public function remove($document) |
||
330 | |||
331 | /** |
||
332 | * Flushes elasticsearch index. |
||
333 | * |
||
334 | * @param array $params |
||
335 | * |
||
336 | * @return array |
||
337 | */ |
||
338 | public function flush(array $params = []) |
||
342 | |||
343 | /** |
||
344 | * Refreshes elasticsearch index. |
||
345 | * |
||
346 | * @param array $params |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | public function refresh(array $params = []) |
||
354 | |||
355 | /** |
||
356 | * Inserts the current query container to the index, used for bulk queries execution. |
||
357 | * |
||
358 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
359 | * |
||
360 | * @return null|array |
||
361 | * |
||
362 | * @throws BulkWithErrorsException |
||
363 | */ |
||
364 | public function commit(array $params = []) |
||
413 | |||
414 | /** |
||
415 | * Adds query to bulk queries container. |
||
416 | * |
||
417 | * @param string $operation One of: index, update, delete, create. |
||
418 | * @param string|array $type Elasticsearch type name. |
||
419 | * @param array $query DSL to execute. |
||
420 | * |
||
421 | * @throws \InvalidArgumentException |
||
422 | * |
||
423 | * @return null|array |
||
424 | */ |
||
425 | public function bulk($operation, $type, array $query) |
||
474 | |||
475 | /** |
||
476 | * Optional setter to change bulk query params. |
||
477 | * |
||
478 | * @param array $params Possible keys: |
||
479 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
480 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
481 | * ['replication'] = (enum) Explicitly set the replication type. |
||
482 | */ |
||
483 | public function setBulkParams(array $params) |
||
487 | |||
488 | /** |
||
489 | * Creates fresh elasticsearch index. |
||
490 | * |
||
491 | * @param bool $noMapping Determines if mapping should be included. |
||
492 | * |
||
493 | * @return array |
||
494 | */ |
||
495 | public function createIndex($noMapping = false) |
||
503 | |||
504 | /** |
||
505 | * Drops elasticsearch index. |
||
506 | */ |
||
507 | public function dropIndex() |
||
511 | |||
512 | /** |
||
513 | * Tries to drop and create fresh elasticsearch index. |
||
514 | * |
||
515 | * @param bool $noMapping Determines if mapping should be included. |
||
516 | * |
||
517 | * @return array |
||
518 | */ |
||
519 | public function dropAndCreateIndex($noMapping = false) |
||
529 | |||
530 | /** |
||
531 | * Checks if connection index is already created. |
||
532 | * |
||
533 | * @return bool |
||
534 | */ |
||
535 | public function indexExists() |
||
539 | |||
540 | /** |
||
541 | * Returns index name this connection is attached to. |
||
542 | * |
||
543 | * @return string |
||
544 | */ |
||
545 | public function getIndexName() |
||
549 | |||
550 | /** |
||
551 | * Sets index name for this connection. |
||
552 | * |
||
553 | * @param string $name |
||
554 | */ |
||
555 | public function setIndexName($name) |
||
559 | |||
560 | /** |
||
561 | * Returns mappings of the index for this connection. |
||
562 | * |
||
563 | * @return array |
||
564 | */ |
||
565 | public function getIndexMappings() |
||
569 | |||
570 | /** |
||
571 | * Returns Elasticsearch version number. |
||
572 | * |
||
573 | * @return string |
||
574 | */ |
||
575 | public function getVersionNumber() |
||
579 | |||
580 | /** |
||
581 | * Clears elasticsearch client cache. |
||
582 | */ |
||
583 | public function clearCache() |
||
587 | |||
588 | /** |
||
589 | * Returns a single document by ID. Returns NULL if document was not found. |
||
590 | * |
||
591 | * @param string $className Document class name or Elasticsearch type name |
||
592 | * @param string $id Document ID to find |
||
593 | * @param string $routing Custom routing for the document |
||
594 | * |
||
595 | * @return object |
||
596 | */ |
||
597 | public function find($className, $id, $routing = null) |
||
619 | |||
620 | /** |
||
621 | * Executes given search. |
||
622 | * |
||
623 | * @deprecated use strict return type functions from Repository instead. |
||
624 | * |
||
625 | * @param array $types |
||
626 | * @param Search $search |
||
627 | * @param string $resultsType |
||
628 | * |
||
629 | * @return DocumentIterator|RawIterator|array |
||
630 | */ |
||
631 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
641 | |||
642 | /** |
||
643 | * Parses raw result. |
||
644 | * |
||
645 | * @deprecated use strict return type functions from Repository class instead. |
||
646 | * |
||
647 | * @param array $raw |
||
648 | * @param string $resultsType |
||
649 | * @param string $scrollDuration |
||
650 | * |
||
651 | * @return DocumentIterator|RawIterator|array |
||
652 | * |
||
653 | * @throws \Exception |
||
654 | */ |
||
655 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
676 | |||
677 | /** |
||
678 | * Normalizes response array. |
||
679 | * |
||
680 | * @deprecated Use ArrayIterator from Result namespace instead. |
||
681 | * |
||
682 | * @param array $data |
||
683 | * |
||
684 | * @return array |
||
685 | */ |
||
686 | private function convertToNormalizedArray($data) |
||
706 | |||
707 | /** |
||
708 | * Fetches next set of results. |
||
709 | * |
||
710 | * @param string $scrollId |
||
711 | * @param string $scrollDuration |
||
712 | * @param string $resultsType |
||
713 | * |
||
714 | * @return mixed |
||
715 | * |
||
716 | * @throws \Exception |
||
717 | */ |
||
718 | public function scroll( |
||
736 | |||
737 | /** |
||
738 | * Clears scroll. |
||
739 | * |
||
740 | * @param string $scrollId |
||
741 | */ |
||
742 | public function clearScroll($scrollId) |
||
746 | |||
747 | /** |
||
748 | * Resolves type name by class name. |
||
749 | * |
||
750 | * @param string $className |
||
751 | * |
||
752 | * @return string |
||
753 | */ |
||
754 | private function resolveTypeName($className) |
||
762 | |||
763 | /** |
||
764 | * Starts and stops an event in the stopwatch |
||
765 | * |
||
766 | * @param string $action only 'start' and 'stop' |
||
767 | * @param string $name name of the event |
||
768 | */ |
||
769 | private function stopwatch($action, $name) |
||
775 | } |
||
776 |
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: