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) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Adds document for removal. |
||
| 303 | * |
||
| 304 | * @param object $document |
||
| 305 | */ |
||
| 306 | public function remove($document) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Flushes elasticsearch index. |
||
| 323 | * |
||
| 324 | * @param array $params |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | public function flush(array $params = []) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Refreshes elasticsearch index. |
||
| 335 | * |
||
| 336 | * @param array $params |
||
| 337 | * |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | public function refresh(array $params = []) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Gets Id field of the document |
||
| 347 | * |
||
| 348 | * @param object $document |
||
| 349 | * |
||
| 350 | * @return mixed |
||
| 351 | */ |
||
| 352 | public function getIdFieldInfo($document) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Adds ids to documents |
||
| 376 | * |
||
| 377 | * @param array $bulkQueries |
||
| 378 | * |
||
| 379 | * @param array $bulkResponse |
||
| 380 | */ |
||
| 381 | public function addIds(array $bulkQueries, $bulkResponse = []) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Inserts the current query container to the index, used for bulk queries execution. |
||
| 415 | * |
||
| 416 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
| 417 | * |
||
| 418 | * @return null|array |
||
| 419 | */ |
||
| 420 | public function commit(array $params = []) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Adds query to bulk queries container. |
||
| 448 | * |
||
| 449 | * @param string $operation One of: index, update, delete, create. |
||
| 450 | * @param string|array $type Elasticsearch type name. |
||
| 451 | * @param array $query DSL to execute. |
||
| 452 | * |
||
| 453 | * @throws \InvalidArgumentException |
||
| 454 | */ |
||
| 455 | public function bulk($operation, $type, array $query) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Optional setter to change bulk query params. |
||
| 500 | * |
||
| 501 | * @param array $params Possible keys: |
||
| 502 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
| 503 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
| 504 | * ['replication'] = (enum) Explicitly set the replication type. |
||
| 505 | */ |
||
| 506 | public function setBulkParams(array $params) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Creates fresh elasticsearch index. |
||
| 513 | * |
||
| 514 | * @param bool $noMapping Determines if mapping should be included. |
||
| 515 | * |
||
| 516 | * @return array |
||
| 517 | */ |
||
| 518 | public function createIndex($noMapping = false) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Drops elasticsearch index. |
||
| 531 | */ |
||
| 532 | 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) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Checks if connection index is already created. |
||
| 559 | * |
||
| 560 | * @return bool |
||
| 561 | */ |
||
| 562 | public function indexExists() |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Returns index name this connection is attached to. |
||
| 569 | * |
||
| 570 | * @return string |
||
| 571 | */ |
||
| 572 | public function getIndexName() |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Sets index name for this connection. |
||
| 579 | * |
||
| 580 | * @param string $name |
||
| 581 | */ |
||
| 582 | public function setIndexName($name) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Returns Elasticsearch version number. |
||
| 589 | * |
||
| 590 | * @return string |
||
| 591 | */ |
||
| 592 | public function getVersionNumber() |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Clears elasticsearch client cache. |
||
| 599 | */ |
||
| 600 | public function clearCache() |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Set connection to read only state. |
||
| 609 | * |
||
| 610 | * @param bool $readOnly |
||
| 611 | */ |
||
| 612 | public function setReadOnly($readOnly) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Checks if connection is read only. |
||
| 619 | * |
||
| 620 | * @param string $message Error message. |
||
| 621 | * |
||
| 622 | * @throws Forbidden403Exception |
||
| 623 | */ |
||
| 624 | public function isReadOnly($message = '') |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Returns a single document by ID. Returns NULL if document was not found. |
||
| 633 | * |
||
| 634 | * @param string $className Document class name or Elasticsearch type name |
||
| 635 | * @param string $id Document ID to find |
||
| 636 | * |
||
| 637 | * @return object |
||
| 638 | */ |
||
| 639 | public function find($className, $id) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Executes given search. |
||
| 660 | * |
||
| 661 | * @param array $types |
||
| 662 | * @param Search $search |
||
| 663 | * @param string $resultsType |
||
| 664 | * |
||
| 665 | * @return DocumentIterator|RawIterator|array |
||
| 666 | */ |
||
| 667 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Parses raw result. |
||
| 680 | * |
||
| 681 | * @param array $raw |
||
| 682 | * @param string $resultsType |
||
| 683 | * @param string $scrollDuration |
||
| 684 | * |
||
| 685 | * @return DocumentIterator|RawIterator|array |
||
| 686 | * |
||
| 687 | * @throws \Exception |
||
| 688 | */ |
||
| 689 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Normalizes response array. |
||
| 713 | * |
||
| 714 | * @param array $data |
||
| 715 | * |
||
| 716 | * @return array |
||
| 717 | */ |
||
| 718 | private function convertToNormalizedArray($data) |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Fetches next set of results. |
||
| 741 | * |
||
| 742 | * @param string $scrollId |
||
| 743 | * @param string $scrollDuration |
||
| 744 | * @param string $resultsType |
||
| 745 | * |
||
| 746 | * @return AbstractResultsIterator |
||
| 747 | * |
||
| 748 | * @throws \Exception |
||
| 749 | */ |
||
| 750 | public function scroll( |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Clears scroll. |
||
| 762 | * |
||
| 763 | * @param string $scrollId |
||
| 764 | */ |
||
| 765 | public function clearScroll($scrollId) |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Resolves type name by class name. |
||
| 772 | * |
||
| 773 | * @param string $className |
||
| 774 | * |
||
| 775 | * @return string |
||
| 776 | */ |
||
| 777 | private function resolveTypeName($className) |
||
| 785 | } |
||
| 786 |
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: