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) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param Stopwatch $stopwatch |
||
| 172 | */ |
||
| 173 | public function setStopwatch(Stopwatch $stopwatch) |
||
| 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) |
||
| 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 = []) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Adds document to next flush. |
||
| 296 | * |
||
| 297 | * @param object $document |
||
| 298 | */ |
||
| 299 | public function persist($document) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Adds document for removal. |
||
| 309 | * |
||
| 310 | * @param object $document |
||
| 311 | */ |
||
| 312 | public function remove($document) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Flushes elasticsearch index. |
||
| 329 | * |
||
| 330 | * @param array $params |
||
| 331 | * |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | public function flush(array $params = []) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Refreshes elasticsearch index. |
||
| 341 | * |
||
| 342 | * @param array $params |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | public function refresh(array $params = []) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Inserts the current query container to the index, used for bulk queries execution. |
||
| 353 | * |
||
| 354 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
| 355 | * |
||
| 356 | * @return null|array |
||
| 357 | */ |
||
| 358 | public function commit(array $params = []) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Adds query to bulk queries container. |
||
| 401 | * |
||
| 402 | * @param string $operation One of: index, update, delete, create. |
||
| 403 | * @param string|array $type Elasticsearch type name. |
||
| 404 | * @param array $query DSL to execute. |
||
| 405 | * |
||
| 406 | * @throws \InvalidArgumentException |
||
| 407 | * |
||
| 408 | * @return null|array |
||
| 409 | */ |
||
| 410 | public function bulk($operation, $type, array $query) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Optional setter to change bulk query params. |
||
| 462 | * |
||
| 463 | * @param array $params Possible keys: |
||
| 464 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
| 465 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
| 466 | * ['replication'] = (enum) Explicitly set the replication type. |
||
| 467 | */ |
||
| 468 | public function setBulkParams(array $params) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Creates fresh elasticsearch index. |
||
| 475 | * |
||
| 476 | * @param bool $noMapping Determines if mapping should be included. |
||
| 477 | * |
||
| 478 | * @return array |
||
| 479 | */ |
||
| 480 | public function createIndex($noMapping = false) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Drops elasticsearch index. |
||
| 491 | */ |
||
| 492 | public function dropIndex() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Tries to drop and create fresh elasticsearch index. |
||
| 499 | * |
||
| 500 | * @param bool $noMapping Determines if mapping should be included. |
||
| 501 | * |
||
| 502 | * @return array |
||
| 503 | */ |
||
| 504 | public function dropAndCreateIndex($noMapping = false) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Checks if connection index is already created. |
||
| 517 | * |
||
| 518 | * @return bool |
||
| 519 | */ |
||
| 520 | public function indexExists() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Returns index name this connection is attached to. |
||
| 527 | * |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | public function getIndexName() |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Sets index name for this connection. |
||
| 537 | * |
||
| 538 | * @param string $name |
||
| 539 | */ |
||
| 540 | public function setIndexName($name) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Returns mappings of the index for this connection. |
||
| 547 | * |
||
| 548 | * @return array |
||
| 549 | */ |
||
| 550 | public function getIndexMappings() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Returns Elasticsearch version number. |
||
| 557 | * |
||
| 558 | * @return string |
||
| 559 | */ |
||
| 560 | public function getVersionNumber() |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Clears elasticsearch client cache. |
||
| 567 | */ |
||
| 568 | public function clearCache() |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns a single document by ID. Returns NULL if document was not found. |
||
| 575 | * |
||
| 576 | * @param string $className Document class name or Elasticsearch type name |
||
| 577 | * @param string $id Document ID to find |
||
| 578 | * @param string $routing Custom routing for the document |
||
| 579 | * |
||
| 580 | * @return object |
||
| 581 | */ |
||
| 582 | public function find($className, $id, $routing = null) |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Executes given search. |
||
| 607 | * |
||
| 608 | * @param array $types |
||
| 609 | * @param Search $search |
||
| 610 | * @param string $resultsType |
||
| 611 | * |
||
| 612 | * @return DocumentIterator|RawIterator|array |
||
| 613 | */ |
||
| 614 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Parses raw result. |
||
| 627 | * |
||
| 628 | * @param array $raw |
||
| 629 | * @param string $resultsType |
||
| 630 | * @param string $scrollDuration |
||
| 631 | * |
||
| 632 | * @return DocumentIterator|RawIterator|array |
||
| 633 | * |
||
| 634 | * @throws \Exception |
||
| 635 | */ |
||
| 636 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Normalizes response array. |
||
| 660 | * |
||
| 661 | * @param array $data |
||
| 662 | * |
||
| 663 | * @return array |
||
| 664 | */ |
||
| 665 | private function convertToNormalizedArray($data) |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Fetches next set of results. |
||
| 688 | * |
||
| 689 | * @param string $scrollId |
||
| 690 | * @param string $scrollDuration |
||
| 691 | * @param string $resultsType |
||
| 692 | * |
||
| 693 | * @return AbstractResultsIterator |
||
| 694 | * |
||
| 695 | * @throws \Exception |
||
| 696 | */ |
||
| 697 | public function scroll( |
||
| 706 | |||
| 707 | /** |
||
| 708 | * Clears scroll. |
||
| 709 | * |
||
| 710 | * @param string $scrollId |
||
| 711 | */ |
||
| 712 | public function clearScroll($scrollId) |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Resolves type name by class name. |
||
| 719 | * |
||
| 720 | * @param string $className |
||
| 721 | * |
||
| 722 | * @return string |
||
| 723 | */ |
||
| 724 | private function resolveTypeName($className) |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Starts and stops an event in the stopwatch |
||
| 735 | * |
||
| 736 | * @param string $action only 'start' and 'stop' |
||
| 737 | * @param string $name name of the event |
||
| 738 | */ |
||
| 739 | private function stopwatch($action, $name) |
||
| 745 | } |
||
| 746 |
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: