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 Stopwatch |
||
| 99 | */ |
||
| 100 | private $stopwatch; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $name Manager name |
||
| 104 | * @param array $config Manager configuration |
||
| 105 | * @param Client $client |
||
| 106 | * @param array $indexSettings |
||
| 107 | * @param MetadataCollector $metadataCollector |
||
| 108 | * @param Converter $converter |
||
| 109 | */ |
||
| 110 | public function __construct( |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns Elasticsearch connection. |
||
| 128 | * |
||
| 129 | * @return Client |
||
| 130 | */ |
||
| 131 | public function getClient() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function getName() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public function getConfig() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param Stopwatch $stopwatch |
||
| 154 | */ |
||
| 155 | public function setStopwatch(Stopwatch $stopwatch) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns repository by document class. |
||
| 162 | * |
||
| 163 | * @param string $className FQCN or string in Bundle:Document format |
||
| 164 | * |
||
| 165 | * @return Repository |
||
| 166 | */ |
||
| 167 | public function getRepository($className) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return MetadataCollector |
||
| 187 | */ |
||
| 188 | public function getMetadataCollector() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return Converter |
||
| 195 | */ |
||
| 196 | public function getConverter() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function getCommitMode() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $commitMode |
||
| 211 | */ |
||
| 212 | public function setCommitMode($commitMode) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return int |
||
| 223 | */ |
||
| 224 | public function getBulkCommitSize() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param int $bulkCommitSize |
||
| 231 | */ |
||
| 232 | public function setBulkCommitSize($bulkCommitSize) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Creates a repository. |
||
| 239 | * |
||
| 240 | * @param string $className |
||
| 241 | * |
||
| 242 | * @return Repository |
||
| 243 | */ |
||
| 244 | private function createRepository($className) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Executes search query in the index. |
||
| 251 | * |
||
| 252 | * @param array $types List of types to search in. |
||
| 253 | * @param array $query Query to execute. |
||
| 254 | * @param array $queryStringParams Query parameters. |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | 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) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Adds document for removal. |
||
| 291 | * |
||
| 292 | * @param object $document |
||
| 293 | */ |
||
| 294 | public function remove($document) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Flushes elasticsearch index. |
||
| 311 | * |
||
| 312 | * @param array $params |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | public function flush(array $params = []) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Refreshes elasticsearch index. |
||
| 323 | * |
||
| 324 | * @param array $params |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | public function refresh(array $params = []) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Inserts the current query container to the index, used for bulk queries execution. |
||
| 335 | * |
||
| 336 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
| 337 | * |
||
| 338 | * @return null|array |
||
| 339 | */ |
||
| 340 | public function commit(array $params = []) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Adds query to bulk queries container. |
||
| 373 | * |
||
| 374 | * @param string $operation One of: index, update, delete, create. |
||
| 375 | * @param string|array $type Elasticsearch type name. |
||
| 376 | * @param array $query DSL to execute. |
||
| 377 | * |
||
| 378 | * @throws \InvalidArgumentException |
||
| 379 | * |
||
| 380 | * @return null|array |
||
| 381 | */ |
||
| 382 | public function bulk($operation, $type, array $query) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Optional setter to change bulk query params. |
||
| 426 | * |
||
| 427 | * @param array $params Possible keys: |
||
| 428 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
| 429 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
| 430 | * ['replication'] = (enum) Explicitly set the replication type. |
||
| 431 | */ |
||
| 432 | public function setBulkParams(array $params) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Creates fresh elasticsearch index. |
||
| 439 | * |
||
| 440 | * @param bool $noMapping Determines if mapping should be included. |
||
| 441 | * |
||
| 442 | * @return array |
||
| 443 | */ |
||
| 444 | public function createIndex($noMapping = false) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Drops elasticsearch index. |
||
| 455 | */ |
||
| 456 | public function dropIndex() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Tries to drop and create fresh elasticsearch index. |
||
| 463 | * |
||
| 464 | * @param bool $noMapping Determines if mapping should be included. |
||
| 465 | * |
||
| 466 | * @return array |
||
| 467 | */ |
||
| 468 | public function dropAndCreateIndex($noMapping = false) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Checks if connection index is already created. |
||
| 481 | * |
||
| 482 | * @return bool |
||
| 483 | */ |
||
| 484 | public function indexExists() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Returns index name this connection is attached to. |
||
| 491 | * |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | public function getIndexName() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Sets index name for this connection. |
||
| 501 | * |
||
| 502 | * @param string $name |
||
| 503 | */ |
||
| 504 | public function setIndexName($name) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Returns Elasticsearch version number. |
||
| 511 | * |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | public function getVersionNumber() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Clears elasticsearch client cache. |
||
| 521 | */ |
||
| 522 | public function clearCache() |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Returns a single document by ID. Returns NULL if document was not found. |
||
| 529 | * |
||
| 530 | * @param string $className Document class name or Elasticsearch type name |
||
| 531 | * @param string $id Document ID to find |
||
| 532 | * |
||
| 533 | * @return object |
||
| 534 | */ |
||
| 535 | public function find($className, $id) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Executes given search. |
||
| 556 | * |
||
| 557 | * @param array $types |
||
| 558 | * @param Search $search |
||
| 559 | * @param string $resultsType |
||
| 560 | * |
||
| 561 | * @return DocumentIterator|RawIterator|array |
||
| 562 | */ |
||
| 563 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Parses raw result. |
||
| 576 | * |
||
| 577 | * @param array $raw |
||
| 578 | * @param string $resultsType |
||
| 579 | * @param string $scrollDuration |
||
| 580 | * |
||
| 581 | * @return DocumentIterator|RawIterator|array |
||
| 582 | * |
||
| 583 | * @throws \Exception |
||
| 584 | */ |
||
| 585 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Normalizes response array. |
||
| 609 | * |
||
| 610 | * @param array $data |
||
| 611 | * |
||
| 612 | * @return array |
||
| 613 | */ |
||
| 614 | private function convertToNormalizedArray($data) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Fetches next set of results. |
||
| 637 | * |
||
| 638 | * @param string $scrollId |
||
| 639 | * @param string $scrollDuration |
||
| 640 | * @param string $resultsType |
||
| 641 | * |
||
| 642 | * @return AbstractResultsIterator |
||
| 643 | * |
||
| 644 | * @throws \Exception |
||
| 645 | */ |
||
| 646 | public function scroll( |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Clears scroll. |
||
| 658 | * |
||
| 659 | * @param string $scrollId |
||
| 660 | */ |
||
| 661 | public function clearScroll($scrollId) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Resolves type name by class name. |
||
| 668 | * |
||
| 669 | * @param string $className |
||
| 670 | * |
||
| 671 | * @return string |
||
| 672 | */ |
||
| 673 | private function resolveTypeName($className) |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Starts and stops an event in the stopwatch |
||
| 684 | * |
||
| 685 | * @param string $action only 'start' and 'stop' |
||
| 686 | * @param string $name name of the event |
||
| 687 | */ |
||
| 688 | private function stopwatch($action, $name) |
||
| 694 | } |
||
| 695 |
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: