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 | * @var EventDispatcherInterface |
||
99 | */ |
||
100 | private $eventDispatcher; |
||
101 | |||
102 | /** |
||
103 | * @var Stopwatch |
||
104 | */ |
||
105 | private $stopwatch; |
||
106 | |||
107 | /** |
||
108 | * @param string $name Manager name |
||
109 | * @param array $config Manager configuration |
||
110 | * @param Client $client |
||
111 | * @param array $indexSettings |
||
112 | * @param MetadataCollector $metadataCollector |
||
113 | * @param Converter $converter |
||
114 | */ |
||
115 | public function __construct( |
||
130 | |||
131 | /** |
||
132 | * Returns Elasticsearch connection. |
||
133 | * |
||
134 | * @return Client |
||
135 | */ |
||
136 | public function getClient() |
||
140 | |||
141 | /** |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getName() |
||
148 | |||
149 | /** |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getConfig() |
||
156 | |||
157 | /** |
||
158 | * @param EventDispatcherInterface $eventDispatcher |
||
159 | */ |
||
160 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
164 | |||
165 | /** |
||
166 | * @param Stopwatch $stopwatch |
||
167 | */ |
||
168 | public function setStopwatch(Stopwatch $stopwatch) |
||
172 | |||
173 | /** |
||
174 | * Returns repository by document class. |
||
175 | * |
||
176 | * @param string $className FQCN or string in Bundle:Document format |
||
177 | * |
||
178 | * @return Repository |
||
179 | */ |
||
180 | public function getRepository($className) |
||
207 | |||
208 | /** |
||
209 | * @return MetadataCollector |
||
210 | */ |
||
211 | public function getMetadataCollector() |
||
215 | |||
216 | /** |
||
217 | * @return Converter |
||
218 | */ |
||
219 | public function getConverter() |
||
223 | |||
224 | /** |
||
225 | * @return string |
||
226 | */ |
||
227 | public function getCommitMode() |
||
231 | |||
232 | /** |
||
233 | * @param string $commitMode |
||
234 | */ |
||
235 | public function setCommitMode($commitMode) |
||
243 | |||
244 | /** |
||
245 | * @return int |
||
246 | */ |
||
247 | public function getBulkCommitSize() |
||
251 | |||
252 | /** |
||
253 | * @param int $bulkCommitSize |
||
254 | */ |
||
255 | public function setBulkCommitSize($bulkCommitSize) |
||
259 | |||
260 | /** |
||
261 | * Creates a repository. |
||
262 | * |
||
263 | * @param string $className |
||
264 | * |
||
265 | * @return Repository |
||
266 | */ |
||
267 | private function createRepository($className) |
||
271 | |||
272 | /** |
||
273 | * Executes search query in the index. |
||
274 | * |
||
275 | * @param array $types List of types to search in. |
||
276 | * @param array $query Query to execute. |
||
277 | * @param array $queryStringParams Query parameters. |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | public function search(array $types, array $query, array $queryStringParams = []) |
||
307 | |||
308 | /** |
||
309 | * Execute search queries using multisearch api |
||
310 | * $body - is array of requests described in elastic Multi Search API |
||
311 | * |
||
312 | * @param $body |
||
313 | * @return array |
||
314 | */ |
||
315 | public function msearch(array $body) |
||
325 | |||
326 | /** |
||
327 | * Adds document to next flush. |
||
328 | * |
||
329 | * @param object $document |
||
330 | */ |
||
331 | public function persist($document) |
||
338 | |||
339 | /** |
||
340 | * Adds document for removal. |
||
341 | * |
||
342 | * @param object $document |
||
343 | */ |
||
344 | public function remove($document) |
||
358 | |||
359 | /** |
||
360 | * Flushes elasticsearch index. |
||
361 | * |
||
362 | * @param array $params |
||
363 | * |
||
364 | * @return array |
||
365 | */ |
||
366 | public function flush(array $params = []) |
||
370 | |||
371 | /** |
||
372 | * Refreshes elasticsearch index. |
||
373 | * |
||
374 | * @param array $params |
||
375 | * |
||
376 | * @return array |
||
377 | */ |
||
378 | public function refresh(array $params = []) |
||
382 | |||
383 | /** |
||
384 | * Inserts the current query container to the index, used for bulk queries execution. |
||
385 | * |
||
386 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
387 | * |
||
388 | * @return null|array |
||
389 | * |
||
390 | * @throws BulkWithErrorsException |
||
391 | */ |
||
392 | public function commit(array $params = []) |
||
441 | |||
442 | /** |
||
443 | * Adds query to bulk queries container. |
||
444 | * |
||
445 | * @param string $operation One of: index, update, delete, create. |
||
446 | * @param string|array $type Elasticsearch type name. |
||
447 | * @param array $query DSL to execute. |
||
448 | * |
||
449 | * @throws \InvalidArgumentException |
||
450 | * |
||
451 | * @return null|array |
||
452 | */ |
||
453 | public function bulk($operation, $type, array $query) |
||
501 | |||
502 | /** |
||
503 | * Optional setter to change bulk query params. |
||
504 | * |
||
505 | * @param array $params Possible keys: |
||
506 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
507 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
508 | * ['replication'] = (enum) Explicitly set the replication type. |
||
509 | */ |
||
510 | public function setBulkParams(array $params) |
||
514 | |||
515 | /** |
||
516 | * Creates fresh elasticsearch index. |
||
517 | * |
||
518 | * @param bool $noMapping Determines if mapping should be included. |
||
519 | * |
||
520 | * @return array |
||
521 | */ |
||
522 | public function createIndex($noMapping = false) |
||
530 | |||
531 | /** |
||
532 | * Drops elasticsearch index. |
||
533 | */ |
||
534 | public function dropIndex() |
||
538 | |||
539 | /** |
||
540 | * Tries to drop and create fresh elasticsearch index. |
||
541 | * |
||
542 | * @param bool $noMapping Determines if mapping should be included. |
||
543 | * |
||
544 | * @return array |
||
545 | */ |
||
546 | public function dropAndCreateIndex($noMapping = false) |
||
558 | |||
559 | /** |
||
560 | * Checks if connection index is already created. |
||
561 | * |
||
562 | * @return bool |
||
563 | */ |
||
564 | public function indexExists() |
||
568 | |||
569 | /** |
||
570 | * Returns index name this connection is attached to. |
||
571 | * |
||
572 | * @return string |
||
573 | */ |
||
574 | public function getIndexName() |
||
578 | |||
579 | /** |
||
580 | * Sets index name for this connection. |
||
581 | * |
||
582 | * @param string $name |
||
583 | */ |
||
584 | public function setIndexName($name) |
||
588 | |||
589 | /** |
||
590 | * Returns mappings of the index for this connection. |
||
591 | * |
||
592 | * @return array |
||
593 | */ |
||
594 | public function getIndexMappings() |
||
598 | |||
599 | /** |
||
600 | * Returns Elasticsearch version number. |
||
601 | * |
||
602 | * @return string |
||
603 | */ |
||
604 | public function getVersionNumber() |
||
608 | |||
609 | /** |
||
610 | * Clears elasticsearch client cache. |
||
611 | */ |
||
612 | public function clearCache() |
||
616 | |||
617 | /** |
||
618 | * Returns a single document by ID. Returns NULL if document was not found. |
||
619 | * |
||
620 | * @param string $className Document class name or Elasticsearch type name |
||
621 | * @param string $id Document ID to find |
||
622 | * @param string $routing Custom routing for the document |
||
623 | * |
||
624 | * @return object |
||
625 | */ |
||
626 | public function find($className, $id, $routing = null) |
||
648 | |||
649 | /** |
||
650 | * Fetches next set of results. |
||
651 | * |
||
652 | * @param string $scrollId |
||
653 | * @param string $scrollDuration |
||
654 | * |
||
655 | * @return mixed |
||
656 | * |
||
657 | * @throws \Exception |
||
658 | */ |
||
659 | public function scroll( |
||
667 | |||
668 | /** |
||
669 | * Clears scroll. |
||
670 | * |
||
671 | * @param string $scrollId |
||
672 | */ |
||
673 | public function clearScroll($scrollId) |
||
677 | |||
678 | /** |
||
679 | * Calls "Get Settings API" in Elasticsearch and will return you the currently configured settings. |
||
680 | * |
||
681 | * return array |
||
682 | */ |
||
683 | public function getSettings() |
||
687 | |||
688 | /** |
||
689 | * Gets Elasticsearch aliases information. |
||
690 | * @param $params |
||
691 | * |
||
692 | * @return array |
||
693 | */ |
||
694 | public function getAliases($params = []) |
||
698 | |||
699 | /** |
||
700 | * Resolves type name by class name. |
||
701 | * |
||
702 | * @param string $className |
||
703 | * |
||
704 | * @return string |
||
705 | */ |
||
706 | private function resolveTypeName($className) |
||
714 | |||
715 | /** |
||
716 | * Starts and stops an event in the stopwatch |
||
717 | * |
||
718 | * @param string $action only 'start' and 'stop' |
||
719 | * @param string $name name of the event |
||
720 | */ |
||
721 | private function stopwatch($action, $name) |
||
727 | } |
||
728 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: