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 |
||
| 32 | class Manager |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var string Manager name |
||
| 36 | */ |
||
| 37 | private $name; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array Manager configuration |
||
| 41 | */ |
||
| 42 | private $config = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Client |
||
| 46 | */ |
||
| 47 | private $client; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Converter |
||
| 51 | */ |
||
| 52 | private $converter; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array Container for bulk queries |
||
| 56 | */ |
||
| 57 | private $bulkQueries = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array Holder for consistency, refresh and replication parameters |
||
| 61 | */ |
||
| 62 | private $bulkParams = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $indexSettings; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var MetadataCollector |
||
| 71 | */ |
||
| 72 | private $metadataCollector; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * After commit to make data available the refresh or flush operation is needed |
||
| 76 | * so one of those methods has to be defined, the default is refresh. |
||
| 77 | * |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | private $commitMode = 'refresh'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The size that defines after how much document inserts call commit function. |
||
| 84 | * |
||
| 85 | * @var int |
||
| 86 | */ |
||
| 87 | private $bulkCommitSize = 100; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Container to count how many documents was passed to the bulk query. |
||
| 91 | * |
||
| 92 | * @var int |
||
| 93 | */ |
||
| 94 | private $bulkCount = 0; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var Repository[] Repository local cache |
||
| 98 | */ |
||
| 99 | private $repositories; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var EventDispatcherInterface |
||
| 103 | */ |
||
| 104 | private $eventDispatcher; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $name Manager name |
||
| 108 | * @param array $config Manager configuration |
||
| 109 | * @param Client $client |
||
| 110 | * @param array $indexSettings |
||
| 111 | * @param MetadataCollector $metadataCollector |
||
| 112 | * @param Converter $converter |
||
| 113 | */ |
||
| 114 | public function __construct( |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns Elasticsearch connection. |
||
| 132 | * |
||
| 133 | * @return Client |
||
| 134 | */ |
||
| 135 | public function getClient() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function getName() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function getConfig() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param EventDispatcherInterface $eventDispatcher |
||
| 158 | */ |
||
| 159 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Returns repository by document class. |
||
| 166 | * |
||
| 167 | * @param string $className FQCN or string in Bundle:Document format |
||
| 168 | * |
||
| 169 | * @return Repository |
||
| 170 | */ |
||
| 171 | public function getRepository($className) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return MetadataCollector |
||
| 191 | */ |
||
| 192 | public function getMetadataCollector() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return Converter |
||
| 199 | */ |
||
| 200 | public function getConverter() |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getCommitMode() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $commitMode |
||
| 215 | */ |
||
| 216 | public function setCommitMode($commitMode) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return int |
||
| 227 | */ |
||
| 228 | public function getBulkCommitSize() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param int $bulkCommitSize |
||
| 235 | */ |
||
| 236 | public function setBulkCommitSize($bulkCommitSize) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Creates a repository. |
||
| 243 | * |
||
| 244 | * @param string $className |
||
| 245 | * |
||
| 246 | * @return Repository |
||
| 247 | */ |
||
| 248 | private function createRepository($className) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Executes search query in the index. |
||
| 255 | * |
||
| 256 | * @param array $types List of types to search in. |
||
| 257 | * @param array $query Query to execute. |
||
| 258 | * @param array $queryStringParams Query parameters. |
||
| 259 | * |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function search(array $types, array $query, array $queryStringParams = []) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Adds document to next flush. |
||
| 278 | * |
||
| 279 | * @param object $document |
||
| 280 | */ |
||
| 281 | public function persist($document) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Adds document for removal. |
||
| 293 | * |
||
| 294 | * @param object $document |
||
| 295 | */ |
||
| 296 | public function remove($document) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Flushes elasticsearch index. |
||
| 313 | * |
||
| 314 | * @param array $params |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function flush(array $params = []) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Refreshes elasticsearch index. |
||
| 325 | * |
||
| 326 | * @param array $params |
||
| 327 | * |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | public function refresh(array $params = []) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Inserts the current query container to the index, used for bulk queries execution. |
||
| 337 | * |
||
| 338 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
| 339 | * |
||
| 340 | * @return null|array |
||
| 341 | */ |
||
| 342 | public function commit(array $params = []) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Adds query to bulk queries container. |
||
| 378 | * |
||
| 379 | * @param string $operation One of: index, update, delete, create. |
||
| 380 | * @param string|array $type Elasticsearch type name. |
||
| 381 | * @param array $query DSL to execute. |
||
| 382 | * |
||
| 383 | * @throws \InvalidArgumentException |
||
| 384 | * |
||
| 385 | * @return null|array |
||
| 386 | */ |
||
| 387 | public function bulk($operation, $type, array $query) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Optional setter to change bulk query params. |
||
| 437 | * |
||
| 438 | * @param array $params Possible keys: |
||
| 439 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
| 440 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
| 441 | * ['replication'] = (enum) Explicitly set the replication type. |
||
| 442 | */ |
||
| 443 | public function setBulkParams(array $params) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Creates fresh elasticsearch index. |
||
| 450 | * |
||
| 451 | * @param bool $noMapping Determines if mapping should be included. |
||
| 452 | * |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | public function createIndex($noMapping = false) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Drops elasticsearch index. |
||
| 466 | */ |
||
| 467 | public function dropIndex() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Tries to drop and create fresh elasticsearch index. |
||
| 474 | * |
||
| 475 | * @param bool $noMapping Determines if mapping should be included. |
||
| 476 | * |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | public function dropAndCreateIndex($noMapping = false) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Checks if connection index is already created. |
||
| 492 | * |
||
| 493 | * @return bool |
||
| 494 | */ |
||
| 495 | public function indexExists() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Returns index name this connection is attached to. |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | public function getIndexName() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Sets index name for this connection. |
||
| 512 | * |
||
| 513 | * @param string $name |
||
| 514 | */ |
||
| 515 | public function setIndexName($name) |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Returns Elasticsearch version number. |
||
| 522 | * |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | public function getVersionNumber() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Clears elasticsearch client cache. |
||
| 532 | */ |
||
| 533 | public function clearCache() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Returns a single document by ID. Returns NULL if document was not found. |
||
| 540 | * |
||
| 541 | * @param string $className Document class name or Elasticsearch type name |
||
| 542 | * @param string $id Document ID to find |
||
| 543 | * |
||
| 544 | * @return object |
||
| 545 | */ |
||
| 546 | public function find($className, $id) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Executes given search. |
||
| 567 | * |
||
| 568 | * @param array $types |
||
| 569 | * @param Search $search |
||
| 570 | * @param string $resultsType |
||
| 571 | * |
||
| 572 | * @return DocumentIterator|RawIterator|array |
||
| 573 | */ |
||
| 574 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Parses raw result. |
||
| 587 | * |
||
| 588 | * @param array $raw |
||
| 589 | * @param string $resultsType |
||
| 590 | * @param string $scrollDuration |
||
| 591 | * |
||
| 592 | * @return DocumentIterator|RawIterator|array |
||
| 593 | * |
||
| 594 | * @throws \Exception |
||
| 595 | */ |
||
| 596 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Normalizes response array. |
||
| 620 | * |
||
| 621 | * @param array $data |
||
| 622 | * |
||
| 623 | * @return array |
||
| 624 | */ |
||
| 625 | private function convertToNormalizedArray($data) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Fetches next set of results. |
||
| 648 | * |
||
| 649 | * @param string $scrollId |
||
| 650 | * @param string $scrollDuration |
||
| 651 | * @param string $resultsType |
||
| 652 | * |
||
| 653 | * @return AbstractResultsIterator |
||
| 654 | * |
||
| 655 | * @throws \Exception |
||
| 656 | */ |
||
| 657 | public function scroll( |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Clears scroll. |
||
| 669 | * |
||
| 670 | * @param string $scrollId |
||
| 671 | */ |
||
| 672 | public function clearScroll($scrollId) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Resolves type name by class name. |
||
| 679 | * |
||
| 680 | * @param string $className |
||
| 681 | * |
||
| 682 | * @return string |
||
| 683 | */ |
||
| 684 | private function resolveTypeName($className) |
||
| 692 | } |
||
| 693 |
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: