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 bool |
||
| 52 | */ |
||
| 53 | private $readOnly; |
||
| 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 | * Reference to the persisted objects that need to be given an ID that returns from |
||
| 85 | * elasticsearch |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private $persistedObjects = []; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The size that defines after how much document inserts call commit function. |
||
| 93 | * |
||
| 94 | * @var int |
||
| 95 | */ |
||
| 96 | private $bulkCommitSize = 100; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Container to count how many documents was passed to the bulk query. |
||
| 100 | * |
||
| 101 | * @var int |
||
| 102 | */ |
||
| 103 | private $bulkCount = 0; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var Repository[] Repository local cache |
||
| 107 | */ |
||
| 108 | private $repositories; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $name Manager name |
||
| 112 | * @param array $config Manager configuration |
||
| 113 | * @param Client $client |
||
| 114 | * @param array $indexSettings |
||
| 115 | * @param MetadataCollector $metadataCollector |
||
| 116 | * @param Converter $converter |
||
| 117 | */ |
||
| 118 | 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 | * Returns repository by document class. |
||
| 164 | * |
||
| 165 | * @param string $className FQCN or string in Bundle:Document format |
||
| 166 | * |
||
| 167 | * @return Repository |
||
| 168 | */ |
||
| 169 | public function getRepository($className) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return MetadataCollector |
||
| 189 | */ |
||
| 190 | public function getMetadataCollector() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return Converter |
||
| 197 | */ |
||
| 198 | public function getConverter() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getCommitMode() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $commitMode |
||
| 213 | */ |
||
| 214 | public function setCommitMode($commitMode) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @return int |
||
| 225 | */ |
||
| 226 | public function getBulkCommitSize() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param int $bulkCommitSize |
||
| 233 | */ |
||
| 234 | public function setBulkCommitSize($bulkCommitSize) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Creates a repository. |
||
| 241 | * |
||
| 242 | * @param string $className |
||
| 243 | * |
||
| 244 | * @return Repository |
||
| 245 | */ |
||
| 246 | private function createRepository($className) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Executes search query in the index. |
||
| 253 | * |
||
| 254 | * @param array $types List of types to search in. |
||
| 255 | * @param array $query Query to execute. |
||
| 256 | * @param array $queryStringParams Query parameters. |
||
| 257 | * |
||
| 258 | * @return array |
||
| 259 | */ |
||
| 260 | public function search(array $types, array $query, array $queryStringParams = []) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Adds document to next flush. |
||
| 276 | * |
||
| 277 | * @param object $document |
||
| 278 | */ |
||
| 279 | public function persist($document) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Adds document for removal. |
||
| 292 | * |
||
| 293 | * @param object $document |
||
| 294 | */ |
||
| 295 | public function remove($document) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Flushes elasticsearch index. |
||
| 312 | * |
||
| 313 | * @param array $params |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | public function flush(array $params = []) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Refreshes elasticsearch index. |
||
| 324 | * |
||
| 325 | * @param array $params |
||
| 326 | * |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public function refresh(array $params = []) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Adds ids to documents |
||
| 336 | * |
||
| 337 | * @param array $bulkQueries |
||
| 338 | * |
||
| 339 | * @param array $bulkResponse |
||
| 340 | */ |
||
| 341 | public function addIds(array $bulkQueries, $bulkResponse = []) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Inserts the current query container to the index, used for bulk queries execution. |
||
| 374 | * |
||
| 375 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
| 376 | * |
||
| 377 | * @return null|array |
||
| 378 | */ |
||
| 379 | public function commit(array $params = []) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Adds query to bulk queries container. |
||
| 407 | * |
||
| 408 | * @param string $operation One of: index, update, delete, create. |
||
| 409 | * @param string|array $type Elasticsearch type name. |
||
| 410 | * @param array $query DSL to execute. |
||
| 411 | * |
||
| 412 | * @throws \InvalidArgumentException |
||
| 413 | */ |
||
| 414 | public function bulk($operation, $type, array $query) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Optional setter to change bulk query params. |
||
| 459 | * |
||
| 460 | * @param array $params Possible keys: |
||
| 461 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
| 462 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
| 463 | * ['replication'] = (enum) Explicitly set the replication type. |
||
| 464 | */ |
||
| 465 | public function setBulkParams(array $params) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Creates fresh elasticsearch index. |
||
| 472 | * |
||
| 473 | * @param bool $noMapping Determines if mapping should be included. |
||
| 474 | * |
||
| 475 | * @return array |
||
| 476 | */ |
||
| 477 | public function createIndex($noMapping = false) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Drops elasticsearch index. |
||
| 490 | */ |
||
| 491 | public function dropIndex() |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Tries to drop and create fresh elasticsearch index. |
||
| 500 | * |
||
| 501 | * @param bool $noMapping Determines if mapping should be included. |
||
| 502 | * |
||
| 503 | * @return array |
||
| 504 | */ |
||
| 505 | public function dropAndCreateIndex($noMapping = false) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Checks if connection index is already created. |
||
| 518 | * |
||
| 519 | * @return bool |
||
| 520 | */ |
||
| 521 | public function indexExists() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Returns index name this connection is attached to. |
||
| 528 | * |
||
| 529 | * @return string |
||
| 530 | */ |
||
| 531 | public function getIndexName() |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Sets index name for this connection. |
||
| 538 | * |
||
| 539 | * @param string $name |
||
| 540 | */ |
||
| 541 | public function setIndexName($name) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns Elasticsearch version number. |
||
| 548 | * |
||
| 549 | * @return string |
||
| 550 | */ |
||
| 551 | public function getVersionNumber() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Clears elasticsearch client cache. |
||
| 558 | */ |
||
| 559 | public function clearCache() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Set connection to read only state. |
||
| 568 | * |
||
| 569 | * @param bool $readOnly |
||
| 570 | */ |
||
| 571 | public function setReadOnly($readOnly) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Checks if connection is read only. |
||
| 578 | * |
||
| 579 | * @param string $message Error message. |
||
| 580 | * |
||
| 581 | * @throws Forbidden403Exception |
||
| 582 | */ |
||
| 583 | public function isReadOnly($message = '') |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Returns a single document by ID. Returns NULL if document was not found. |
||
| 592 | * |
||
| 593 | * @param string $className Document class name or Elasticsearch type name |
||
| 594 | * @param string $id Document ID to find |
||
| 595 | * |
||
| 596 | * @return object |
||
| 597 | */ |
||
| 598 | public function find($className, $id) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Executes given search. |
||
| 619 | * |
||
| 620 | * @param array $types |
||
| 621 | * @param Search $search |
||
| 622 | * @param string $resultsType |
||
| 623 | * |
||
| 624 | * @return DocumentIterator|RawIterator|array |
||
| 625 | */ |
||
| 626 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Parses raw result. |
||
| 639 | * |
||
| 640 | * @param array $raw |
||
| 641 | * @param string $resultsType |
||
| 642 | * @param string $scrollDuration |
||
| 643 | * |
||
| 644 | * @return DocumentIterator|RawIterator|array |
||
| 645 | * |
||
| 646 | * @throws \Exception |
||
| 647 | */ |
||
| 648 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Normalizes response array. |
||
| 672 | * |
||
| 673 | * @param array $data |
||
| 674 | * |
||
| 675 | * @return array |
||
| 676 | */ |
||
| 677 | private function convertToNormalizedArray($data) |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Fetches next set of results. |
||
| 700 | * |
||
| 701 | * @param string $scrollId |
||
| 702 | * @param string $scrollDuration |
||
| 703 | * @param string $resultsType |
||
| 704 | * |
||
| 705 | * @return AbstractResultsIterator |
||
| 706 | * |
||
| 707 | * @throws \Exception |
||
| 708 | */ |
||
| 709 | public function scroll( |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Clears scroll. |
||
| 721 | * |
||
| 722 | * @param string $scrollId |
||
| 723 | */ |
||
| 724 | public function clearScroll($scrollId) |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Resolves type name by class name. |
||
| 731 | * |
||
| 732 | * @param string $className |
||
| 733 | * |
||
| 734 | * @return string |
||
| 735 | */ |
||
| 736 | private function resolveTypeName($className) |
||
| 744 | } |
||
| 745 |
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: