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 | * @param string $name Manager name | ||
| 99 | * @param array $config Manager configuration | ||
| 100 | * @param Client $client | ||
| 101 | * @param array $indexSettings | ||
| 102 | * @param MetadataCollector $metadataCollector | ||
| 103 | * @param Converter $converter | ||
| 104 | */ | ||
| 105 | public function __construct( | ||
| 106 | $name, | ||
| 107 | array $config, | ||
| 108 | $client, | ||
| 109 | array $indexSettings, | ||
| 110 | $metadataCollector, | ||
| 111 | $converter | ||
| 112 |     ) { | ||
| 113 | $this->name = $name; | ||
| 114 | $this->config = $config; | ||
| 115 | $this->client = $client; | ||
| 116 | $this->indexSettings = $indexSettings; | ||
| 117 | $this->metadataCollector = $metadataCollector; | ||
| 118 | $this->converter = $converter; | ||
| 119 | } | ||
| 120 | |||
| 121 | /** | ||
| 122 | * Returns Elasticsearch connection. | ||
| 123 | * | ||
| 124 | * @return Client | ||
| 125 | */ | ||
| 126 | public function getClient() | ||
| 127 |     { | ||
| 128 | return $this->client; | ||
| 129 | } | ||
| 130 | |||
| 131 | /** | ||
| 132 | * @return string | ||
| 133 | */ | ||
| 134 | public function getName() | ||
| 135 |     { | ||
| 136 | return $this->name; | ||
| 137 | } | ||
| 138 | |||
| 139 | /** | ||
| 140 | * @return array | ||
| 141 | */ | ||
| 142 | public function getConfig() | ||
| 143 |     { | ||
| 144 | return $this->config; | ||
| 145 | } | ||
| 146 | |||
| 147 | /** | ||
| 148 | * Returns repository by document class. | ||
| 149 | * | ||
| 150 | * @param string $className FQCN or string in Bundle:Document format | ||
| 151 | * | ||
| 152 | * @return Repository | ||
| 153 | */ | ||
| 154 | public function getRepository($className) | ||
| 171 | |||
| 172 | /** | ||
| 173 | * @return MetadataCollector | ||
| 174 | */ | ||
| 175 | public function getMetadataCollector() | ||
| 176 |     { | ||
| 177 | return $this->metadataCollector; | ||
| 178 | } | ||
| 179 | |||
| 180 | /** | ||
| 181 | * @return Converter | ||
| 182 | */ | ||
| 183 | public function getConverter() | ||
| 184 |     { | ||
| 185 | return $this->converter; | ||
| 186 | } | ||
| 187 | |||
| 188 | /** | ||
| 189 | * @return string | ||
| 190 | */ | ||
| 191 | public function getCommitMode() | ||
| 192 |     { | ||
| 193 | return $this->commitMode; | ||
| 194 | } | ||
| 195 | |||
| 196 | /** | ||
| 197 | * @param string $commitMode | ||
| 198 | */ | ||
| 199 | public function setCommitMode($commitMode) | ||
| 207 | |||
| 208 | /** | ||
| 209 | * @return int | ||
| 210 | */ | ||
| 211 | public function getBulkCommitSize() | ||
| 212 |     { | ||
| 213 | return $this->bulkCommitSize; | ||
| 214 | } | ||
| 215 | |||
| 216 | /** | ||
| 217 | * @param int $bulkCommitSize | ||
| 218 | */ | ||
| 219 | public function setBulkCommitSize($bulkCommitSize) | ||
| 223 | |||
| 224 | /** | ||
| 225 | * Creates a repository. | ||
| 226 | * | ||
| 227 | * @param string $className | ||
| 228 | * | ||
| 229 | * @return Repository | ||
| 230 | */ | ||
| 231 | private function createRepository($className) | ||
| 235 | |||
| 236 | /** | ||
| 237 | * Executes search query in the index. | ||
| 238 | * | ||
| 239 | * @param array $types List of types to search in. | ||
| 240 | * @param array $query Query to execute. | ||
| 241 | * @param array $queryStringParams Query parameters. | ||
| 242 | * | ||
| 243 | * @return array | ||
| 244 | */ | ||
| 245 | public function search(array $types, array $query, array $queryStringParams = []) | ||
| 246 |     { | ||
| 247 | $params = []; | ||
| 248 | $params['index'] = $this->getIndexName(); | ||
| 249 |         $params['type'] = implode(',', $types); | ||
| 250 | $params['body'] = $query; | ||
| 251 | |||
| 252 |         if (!empty($queryStringParams)) { | ||
| 253 | $params = array_merge($queryStringParams, $params); | ||
| 254 | } | ||
| 255 | |||
| 256 | return $this->client->search($params); | ||
| 257 | } | ||
| 258 | |||
| 259 | /** | ||
| 260 | * Adds document to next flush. | ||
| 261 | * | ||
| 262 | * @param object $document | ||
| 263 | */ | ||
| 264 | public function persist($document) | ||
| 271 | |||
| 272 | /** | ||
| 273 | * Adds document for removal. | ||
| 274 | * | ||
| 275 | * @param object $document | ||
| 276 | */ | ||
| 277 | public function remove($document) | ||
| 291 | |||
| 292 | /** | ||
| 293 | * Flushes elasticsearch index. | ||
| 294 | * | ||
| 295 | * @param array $params | ||
| 296 | * | ||
| 297 | * @return array | ||
| 298 | */ | ||
| 299 | public function flush(array $params = []) | ||
| 300 |     { | ||
| 301 | return $this->client->indices()->flush($params); | ||
| 302 | } | ||
| 303 | |||
| 304 | /** | ||
| 305 | * Refreshes elasticsearch index. | ||
| 306 | * | ||
| 307 | * @param array $params | ||
| 308 | * | ||
| 309 | * @return array | ||
| 310 | */ | ||
| 311 | public function refresh(array $params = []) | ||
| 312 |     { | ||
| 313 | return $this->client->indices()->refresh($params); | ||
| 314 | } | ||
| 315 | |||
| 316 | /** | ||
| 317 | * Inserts the current query container to the index, used for bulk queries execution. | ||
| 318 | * | ||
| 319 | * @param array $params Parameters that will be passed to the flush or refresh queries. | ||
| 320 | * | ||
| 321 | * @return null|array | ||
| 322 | */ | ||
| 323 | public function commit(array $params = []) | ||
| 324 |     { | ||
| 325 |         if (!empty($this->bulkQueries)) { | ||
| 326 | $bulkQueries = array_merge($this->bulkQueries, $this->bulkParams); | ||
| 327 | |||
| 328 | $bulkResponse = $this->client->bulk($bulkQueries); | ||
| 329 | $this->bulkQueries = []; | ||
| 330 | $this->bulkCount = 0; | ||
| 331 | |||
| 332 |             switch ($this->getCommitMode()) { | ||
| 333 | case 'flush': | ||
| 334 | $this->flush($params); | ||
| 335 | break; | ||
| 336 | case 'refresh': | ||
| 337 | $this->refresh($params); | ||
| 338 | break; | ||
| 339 | } | ||
| 340 | |||
| 341 | return $bulkResponse; | ||
| 342 | } | ||
| 343 | |||
| 344 | return null; | ||
| 345 | } | ||
| 346 | |||
| 347 | /** | ||
| 348 | * Adds query to bulk queries container. | ||
| 349 | * | ||
| 350 | * @param string $operation One of: index, update, delete, create. | ||
| 351 | * @param string|array $type Elasticsearch type name. | ||
| 352 | * @param array $query DSL to execute. | ||
| 353 | * | ||
| 354 | * @throws \InvalidArgumentException | ||
| 355 | * | ||
| 356 | * @return null|array | ||
| 357 | */ | ||
| 358 | public function bulk($operation, $type, array $query) | ||
| 359 |     { | ||
| 360 |         if (!in_array($operation, ['index', 'create', 'update', 'delete'])) { | ||
| 361 |             throw new \InvalidArgumentException('Wrong bulk operation selected'); | ||
| 362 | } | ||
| 363 | |||
| 364 | $this->bulkQueries['body'][] = [ | ||
| 365 | $operation => array_filter( | ||
| 366 | [ | ||
| 367 | '_index' => $this->getIndexName(), | ||
| 368 | '_type' => $type, | ||
| 369 | '_id' => isset($query['_id']) ? $query['_id'] : null, | ||
| 370 | '_ttl' => isset($query['_ttl']) ? $query['_ttl'] : null, | ||
| 371 | '_parent' => isset($query['_parent']) ? $query['_parent'] : null, | ||
| 372 | ] | ||
| 373 | ), | ||
| 374 | ]; | ||
| 375 | unset($query['_id'], $query['_ttl'], $query['_parent']); | ||
| 376 | |||
| 377 |         switch ($operation) { | ||
| 378 | case 'index': | ||
| 379 | case 'create': | ||
| 380 | case 'update': | ||
| 381 | $this->bulkQueries['body'][] = $query; | ||
| 382 | break; | ||
| 383 | case 'delete': | ||
| 384 | // Body for delete operation is not needed to apply. | ||
| 385 | default: | ||
| 386 | // Do nothing. | ||
| 387 | break; | ||
| 388 | } | ||
| 389 | |||
| 390 | // We are using counter because there is to difficult to resolve this from bulkQueries array. | ||
| 391 | $this->bulkCount++; | ||
| 392 | |||
| 393 | $response = null; | ||
| 394 |         if ($this->bulkCommitSize === $this->bulkCount) { | ||
| 395 | $response = $this->commit(); | ||
| 396 | } | ||
| 397 | |||
| 398 | return $response; | ||
| 399 | } | ||
| 400 | |||
| 401 | /** | ||
| 402 | * Optional setter to change bulk query params. | ||
| 403 | * | ||
| 404 | * @param array $params Possible keys: | ||
| 405 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. | ||
| 406 | * ['refresh'] = (boolean) Refresh the index after performing the operation. | ||
| 407 | * ['replication'] = (enum) Explicitly set the replication type. | ||
| 408 | */ | ||
| 409 | public function setBulkParams(array $params) | ||
| 410 |     { | ||
| 411 | $this->bulkParams = $params; | ||
| 412 | } | ||
| 413 | |||
| 414 | /** | ||
| 415 | * Creates fresh elasticsearch index. | ||
| 416 | * | ||
| 417 | * @param bool $noMapping Determines if mapping should be included. | ||
| 418 | * | ||
| 419 | * @return array | ||
| 420 | */ | ||
| 421 | public function createIndex($noMapping = false) | ||
| 422 |     { | ||
| 423 |         if ($noMapping) { | ||
| 424 | unset($this->indexSettings['body']['mappings']); | ||
| 425 | } | ||
| 426 | |||
| 427 | return $this->getClient()->indices()->create($this->indexSettings); | ||
| 428 | } | ||
| 429 | |||
| 430 | /** | ||
| 431 | * Drops elasticsearch index. | ||
| 432 | */ | ||
| 433 | public function dropIndex() | ||
| 434 |     { | ||
| 435 | return $this->getClient()->indices()->delete(['index' => $this->getIndexName()]); | ||
| 436 | } | ||
| 437 | |||
| 438 | /** | ||
| 439 | * Tries to drop and create fresh elasticsearch index. | ||
| 440 | * | ||
| 441 | * @param bool $noMapping Determines if mapping should be included. | ||
| 442 | * | ||
| 443 | * @return array | ||
| 444 | */ | ||
| 445 | public function dropAndCreateIndex($noMapping = false) | ||
| 446 |     { | ||
| 447 |         try { | ||
| 448 | $this->dropIndex(); | ||
| 449 |         } catch (\Exception $e) { | ||
| 450 | // Do nothing, our target is to create new index. | ||
| 451 | } | ||
| 452 | |||
| 453 | return $this->createIndex($noMapping); | ||
| 454 | } | ||
| 455 | |||
| 456 | /** | ||
| 457 | * Checks if connection index is already created. | ||
| 458 | * | ||
| 459 | * @return bool | ||
| 460 | */ | ||
| 461 | public function indexExists() | ||
| 465 | |||
| 466 | /** | ||
| 467 | * Returns index name this connection is attached to. | ||
| 468 | * | ||
| 469 | * @return string | ||
| 470 | */ | ||
| 471 | public function getIndexName() | ||
| 475 | |||
| 476 | /** | ||
| 477 | * Sets index name for this connection. | ||
| 478 | * | ||
| 479 | * @param string $name | ||
| 480 | */ | ||
| 481 | public function setIndexName($name) | ||
| 485 | |||
| 486 | /** | ||
| 487 | * Returns Elasticsearch version number. | ||
| 488 | * | ||
| 489 | * @return string | ||
| 490 | */ | ||
| 491 | public function getVersionNumber() | ||
| 495 | |||
| 496 | /** | ||
| 497 | * Clears elasticsearch client cache. | ||
| 498 | */ | ||
| 499 | public function clearCache() | ||
| 503 | |||
| 504 | /** | ||
| 505 | * Returns a single document by ID. Returns NULL if document was not found. | ||
| 506 | * | ||
| 507 | * @param string $className Document class name or Elasticsearch type name | ||
| 508 | * @param string $id Document ID to find | ||
| 509 | * | ||
| 510 | * @return object | ||
| 511 | */ | ||
| 512 | public function find($className, $id) | ||
| 530 | |||
| 531 | /** | ||
| 532 | * Executes given search. | ||
| 533 | * | ||
| 534 | * @param array $types | ||
| 535 | * @param Search $search | ||
| 536 | * @param string $resultsType | ||
| 537 | * | ||
| 538 | * @return DocumentIterator|RawIterator|array | ||
| 539 | */ | ||
| 540 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) | ||
| 550 | |||
| 551 | /** | ||
| 552 | * Parses raw result. | ||
| 553 | * | ||
| 554 | * @param array $raw | ||
| 555 | * @param string $resultsType | ||
| 556 | * @param string $scrollDuration | ||
| 557 | * | ||
| 558 | * @return DocumentIterator|RawIterator|array | ||
| 559 | * | ||
| 560 | * @throws \Exception | ||
| 561 | */ | ||
| 562 | private function parseResult($raw, $resultsType, $scrollDuration = null) | ||
| 583 | |||
| 584 | /** | ||
| 585 | * Normalizes response array. | ||
| 586 | * | ||
| 587 | * @param array $data | ||
| 588 | * | ||
| 589 | * @return array | ||
| 590 | */ | ||
| 591 | private function convertToNormalizedArray($data) | ||
| 611 | |||
| 612 | /** | ||
| 613 | * Fetches next set of results. | ||
| 614 | * | ||
| 615 | * @param string $scrollId | ||
| 616 | * @param string $scrollDuration | ||
| 617 | * @param string $resultsType | ||
| 618 | * | ||
| 619 | * @return AbstractResultsIterator | ||
| 620 | * | ||
| 621 | * @throws \Exception | ||
| 622 | */ | ||
| 623 | public function scroll( | ||
| 632 | |||
| 633 | /** | ||
| 634 | * Clears scroll. | ||
| 635 | * | ||
| 636 | * @param string $scrollId | ||
| 637 | */ | ||
| 638 | public function clearScroll($scrollId) | ||
| 642 | |||
| 643 | /** | ||
| 644 | * Resolves type name by class name. | ||
| 645 | * | ||
| 646 | * @param string $className | ||
| 647 | * | ||
| 648 | * @return string | ||
| 649 | */ | ||
| 650 | private function resolveTypeName($className) | ||
| 658 | } | ||
| 659 | 
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: