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) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return MetadataCollector |
||
| 200 | */ |
||
| 201 | public function getMetadataCollector() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return Converter |
||
| 208 | */ |
||
| 209 | public function getConverter() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getCommitMode() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $commitMode |
||
| 224 | */ |
||
| 225 | public function setCommitMode($commitMode) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @return int |
||
| 236 | */ |
||
| 237 | public function getBulkCommitSize() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param int $bulkCommitSize |
||
| 244 | */ |
||
| 245 | public function setBulkCommitSize($bulkCommitSize) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Creates a repository. |
||
| 252 | * |
||
| 253 | * @param string $className |
||
| 254 | * |
||
| 255 | * @return Repository |
||
| 256 | */ |
||
| 257 | private function createRepository($className) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Executes search query in the index. |
||
| 264 | * |
||
| 265 | * @param array $types List of types to search in. |
||
| 266 | * @param array $query Query to execute. |
||
| 267 | * @param array $queryStringParams Query parameters. |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | public function search(array $types, array $query, array $queryStringParams = []) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Execute search queries using multisearch api |
||
| 295 | * $body - is array of requests described in elastic Multi Search API |
||
| 296 | * |
||
| 297 | * @param $body |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | public function msearch(array $body) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Adds document to next flush. |
||
| 313 | * |
||
| 314 | * @param object $document |
||
| 315 | */ |
||
| 316 | public function persist($document) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Adds document for removal. |
||
| 326 | * |
||
| 327 | * @param object $document |
||
| 328 | */ |
||
| 329 | public function remove($document) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Flushes elasticsearch index. |
||
| 346 | * |
||
| 347 | * @param array $params |
||
| 348 | * |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | public function flush(array $params = []) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Refreshes elasticsearch index. |
||
| 358 | * |
||
| 359 | * @param array $params |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | */ |
||
| 363 | public function refresh(array $params = []) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Inserts the current query container to the index, used for bulk queries execution. |
||
| 370 | * |
||
| 371 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
| 372 | * |
||
| 373 | * @return null|array |
||
| 374 | * |
||
| 375 | * @throws BulkWithErrorsException |
||
| 376 | */ |
||
| 377 | public function commit(array $params = []) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Adds query to bulk queries container. |
||
| 429 | * |
||
| 430 | * @param string $operation One of: index, update, delete, create. |
||
| 431 | * @param string|array $type Elasticsearch type name. |
||
| 432 | * @param array $query DSL to execute. |
||
| 433 | * |
||
| 434 | * @throws \InvalidArgumentException |
||
| 435 | * |
||
| 436 | * @return null|array |
||
| 437 | */ |
||
| 438 | public function bulk($operation, $type, array $query) |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Optional setter to change bulk query params. |
||
| 489 | * |
||
| 490 | * @param array $params Possible keys: |
||
| 491 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
| 492 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
| 493 | * ['replication'] = (enum) Explicitly set the replication type. |
||
| 494 | */ |
||
| 495 | public function setBulkParams(array $params) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Creates fresh elasticsearch index. |
||
| 502 | * |
||
| 503 | * @param bool $noMapping Determines if mapping should be included. |
||
| 504 | * |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | public function createIndex($noMapping = false) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Drops elasticsearch index. |
||
| 518 | */ |
||
| 519 | public function dropIndex() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Tries to drop and create fresh elasticsearch index. |
||
| 526 | * |
||
| 527 | * @param bool $noMapping Determines if mapping should be included. |
||
| 528 | * |
||
| 529 | * @return array |
||
| 530 | */ |
||
| 531 | public function dropAndCreateIndex($noMapping = false) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Checks if connection index is already created. |
||
| 544 | * |
||
| 545 | * @return bool |
||
| 546 | */ |
||
| 547 | public function indexExists() |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Returns index name this connection is attached to. |
||
| 554 | * |
||
| 555 | * @return string |
||
| 556 | */ |
||
| 557 | public function getIndexName() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Sets index name for this connection. |
||
| 564 | * |
||
| 565 | * @param string $name |
||
| 566 | */ |
||
| 567 | public function setIndexName($name) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Returns mappings of the index for this connection. |
||
| 574 | * |
||
| 575 | * @return array |
||
| 576 | */ |
||
| 577 | public function getIndexMappings() |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Returns Elasticsearch version number. |
||
| 584 | * |
||
| 585 | * @return string |
||
| 586 | */ |
||
| 587 | public function getVersionNumber() |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Clears elasticsearch client cache. |
||
| 594 | */ |
||
| 595 | public function clearCache() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Returns a single document by ID. Returns NULL if document was not found. |
||
| 602 | * |
||
| 603 | * @param string $className Document class name or Elasticsearch type name |
||
| 604 | * @param string $id Document ID to find |
||
| 605 | * @param string $routing Custom routing for the document |
||
| 606 | * |
||
| 607 | * @return object |
||
| 608 | */ |
||
| 609 | public function find($className, $id, $routing = null) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Fetches next set of results. |
||
| 634 | * |
||
| 635 | * @param string $scrollId |
||
| 636 | * @param string $scrollDuration |
||
| 637 | * |
||
| 638 | * @return mixed |
||
| 639 | * |
||
| 640 | * @throws \Exception |
||
| 641 | */ |
||
| 642 | public function scroll( |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Clears scroll. |
||
| 653 | * |
||
| 654 | * @param string $scrollId |
||
| 655 | */ |
||
| 656 | public function clearScroll($scrollId) |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Calls "Get Settings API" in Elasticsearch and will return you the currently configured settings. |
||
| 663 | * |
||
| 664 | * return array |
||
| 665 | */ |
||
| 666 | public function getSettings() |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Gets Elasticsearch aliases information. |
||
| 673 | * @param $params |
||
| 674 | * |
||
| 675 | * @return array |
||
| 676 | */ |
||
| 677 | public function getAliases($params = []) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Resolves type name by class name. |
||
| 684 | * |
||
| 685 | * @param string $className |
||
| 686 | * |
||
| 687 | * @return string |
||
| 688 | */ |
||
| 689 | private function resolveTypeName($className) |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Starts and stops an event in the stopwatch |
||
| 700 | * |
||
| 701 | * @param string $action only 'start' and 'stop' |
||
| 702 | * @param string $name name of the event |
||
| 703 | */ |
||
| 704 | private function stopwatch($action, $name) |
||
| 710 | } |
||
| 711 |
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: