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 | * The size that defines after how much document inserts call commit function. |
||
| 85 | * |
||
| 86 | * @var int |
||
| 87 | */ |
||
| 88 | private $bulkCommitSize = 100; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Container to count how many documents was passed to the bulk query. |
||
| 92 | * |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | private $bulkCount = 0; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var Repository[] Repository local cache |
||
| 99 | */ |
||
| 100 | private $repositories; |
||
| 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( |
||
| 111 | $name, |
||
| 112 | array $config, |
||
| 113 | $client, |
||
| 114 | array $indexSettings, |
||
| 115 | $metadataCollector, |
||
| 116 | $converter |
||
| 117 | ) { |
||
| 118 | $this->name = $name; |
||
| 119 | $this->config = $config; |
||
| 120 | $this->client = $client; |
||
| 121 | $this->indexSettings = $indexSettings; |
||
| 122 | $this->metadataCollector = $metadataCollector; |
||
| 123 | $this->converter = $converter; |
||
| 124 | |||
| 125 | $this->setReadOnly($config['readonly']); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns Elasticsearch connection. |
||
| 130 | * |
||
| 131 | * @return Client |
||
| 132 | */ |
||
| 133 | public function getClient() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function getName() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public function getConfig() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns repository by document class. |
||
| 156 | * |
||
| 157 | * @param string $className FQCN or string in Bundle:Document format |
||
| 158 | * |
||
| 159 | * @return Repository |
||
| 160 | */ |
||
| 161 | public function getRepository($className) |
||
| 162 | { |
||
| 163 | if (!is_string($className)) { |
||
| 164 | throw new \InvalidArgumentException('Document class must be a string.'); |
||
| 165 | } |
||
| 166 | |||
| 167 | $namespace = $this->getMetadataCollector()->getClassName($className); |
||
| 168 | |||
| 169 | if (isset($this->repositories[$namespace])) { |
||
| 170 | return $this->repositories[$namespace]; |
||
| 171 | } |
||
| 172 | |||
| 173 | $repository = $this->createRepository($namespace); |
||
| 174 | $this->repositories[$namespace] = $repository; |
||
| 175 | |||
| 176 | return $repository; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return MetadataCollector |
||
| 181 | */ |
||
| 182 | public function getMetadataCollector() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return Converter |
||
| 189 | */ |
||
| 190 | public function getConverter() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getCommitMode() |
||
| 199 | { |
||
| 200 | return $this->commitMode; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $commitMode |
||
| 205 | */ |
||
| 206 | public function setCommitMode($commitMode) |
||
| 207 | { |
||
| 208 | if ($commitMode === 'refresh' || $commitMode === 'flush' || $commitMode === 'none') { |
||
| 209 | $this->commitMode = $commitMode; |
||
| 210 | } else { |
||
| 211 | throw new \LogicException('The commit method must be either refresh, flush or none.'); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return int |
||
| 217 | */ |
||
| 218 | public function getBulkCommitSize() |
||
| 219 | { |
||
| 220 | return $this->bulkCommitSize; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param int $bulkCommitSize |
||
| 225 | */ |
||
| 226 | public function setBulkCommitSize($bulkCommitSize) |
||
| 227 | { |
||
| 228 | $this->bulkCommitSize = $bulkCommitSize; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Creates a repository. |
||
| 233 | * |
||
| 234 | * @param string $className |
||
| 235 | * |
||
| 236 | * @return Repository |
||
| 237 | */ |
||
| 238 | private function createRepository($className) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Executes search query in the index. |
||
| 245 | * |
||
| 246 | * @param array $types List of types to search in. |
||
| 247 | * @param array $query Query to execute. |
||
| 248 | * @param array $queryStringParams Query parameters. |
||
| 249 | * |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | public function search(array $types, array $query, array $queryStringParams = []) |
||
| 253 | { |
||
| 254 | $params = []; |
||
| 255 | $params['index'] = $this->getIndexName(); |
||
| 256 | $params['type'] = implode(',', $types); |
||
| 257 | $params['body'] = $query; |
||
| 258 | |||
| 259 | if (!empty($queryStringParams)) { |
||
| 260 | $params = array_merge($queryStringParams, $params); |
||
| 261 | } |
||
| 262 | |||
| 263 | return $this->client->search($params); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Adds document to next flush. |
||
| 268 | * |
||
| 269 | * @param object $document |
||
| 270 | */ |
||
| 271 | public function persist($document) |
||
| 272 | { |
||
| 273 | $documentArray = $this->converter->convertToArray($document); |
||
| 274 | $type = $this->getMetadataCollector()->getDocumentType(get_class($document)); |
||
| 275 | |||
| 276 | $this->bulk('index', $type, $documentArray); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Adds document for removal. |
||
| 281 | * |
||
| 282 | * @param object $document |
||
| 283 | */ |
||
| 284 | public function remove($document) |
||
| 285 | { |
||
| 286 | $data = $this->converter->convertToArray($document, [], ['_id']); |
||
| 287 | |||
| 288 | if (!isset($data['_id'])) { |
||
| 289 | throw new \LogicException( |
||
| 290 | 'In order to use remove() method document class must have property with @Id annotation.' |
||
| 291 | ); |
||
| 292 | } |
||
| 293 | |||
| 294 | $type = $this->getMetadataCollector()->getDocumentType(get_class($document)); |
||
| 295 | |||
| 296 | $this->bulk('delete', $type, ['_id' => $data['_id']]); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Flushes elasticsearch index. |
||
| 301 | * |
||
| 302 | * @param array $params |
||
| 303 | * |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | public function flush(array $params = []) |
||
| 307 | { |
||
| 308 | return $this->client->indices()->flush($params); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Refreshes elasticsearch index. |
||
| 313 | * |
||
| 314 | * @param array $params |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | public function refresh(array $params = []) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Inserts the current query container to the index, used for bulk queries execution. |
||
| 325 | * |
||
| 326 | * @param array $params Parameters that will be passed to the flush or refresh queries. |
||
| 327 | * |
||
| 328 | * @return null|array |
||
| 329 | */ |
||
| 330 | public function commit(array $params = []) |
||
| 331 | { |
||
| 332 | $this->isReadOnly('Commit'); |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Adds query to bulk queries container. |
||
| 357 | * |
||
| 358 | * @param string $operation One of: index, update, delete, create. |
||
| 359 | * @param string|array $type Elasticsearch type name. |
||
| 360 | * @param array $query DSL to execute. |
||
| 361 | * |
||
| 362 | * @throws \InvalidArgumentException |
||
| 363 | */ |
||
| 364 | public function bulk($operation, $type, array $query) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Optional setter to change bulk query params. |
||
| 409 | * |
||
| 410 | * @param array $params Possible keys: |
||
| 411 | * ['consistency'] = (enum) Explicit write consistency setting for the operation. |
||
| 412 | * ['refresh'] = (boolean) Refresh the index after performing the operation. |
||
| 413 | * ['replication'] = (enum) Explicitly set the replication type. |
||
| 414 | */ |
||
| 415 | public function setBulkParams(array $params) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Creates fresh elasticsearch index. |
||
| 422 | * |
||
| 423 | * @param bool $noMapping Determines if mapping should be included. |
||
| 424 | * |
||
| 425 | * @return array |
||
| 426 | */ |
||
| 427 | public function createIndex($noMapping = false) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Drops elasticsearch index. |
||
| 440 | */ |
||
| 441 | public function dropIndex() |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Tries to drop and create fresh elasticsearch index. |
||
| 450 | * |
||
| 451 | * @param bool $noMapping Determines if mapping should be included. |
||
| 452 | * |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | public function dropAndCreateIndex($noMapping = false) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Checks if connection index is already created. |
||
| 468 | * |
||
| 469 | * @return bool |
||
| 470 | */ |
||
| 471 | public function indexExists() |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Returns index name this connection is attached to. |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function getIndexName() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Sets index name for this connection. |
||
| 488 | * |
||
| 489 | * @param string $name |
||
| 490 | */ |
||
| 491 | public function setIndexName($name) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Returns Elasticsearch version number. |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | public function getVersionNumber() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Clears elasticsearch client cache. |
||
| 508 | */ |
||
| 509 | public function clearCache() |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Set connection to read only state. |
||
| 518 | * |
||
| 519 | * @param bool $readOnly |
||
| 520 | */ |
||
| 521 | public function setReadOnly($readOnly) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Checks if connection is read only. |
||
| 528 | * |
||
| 529 | * @param string $message Error message. |
||
| 530 | * |
||
| 531 | * @throws Forbidden403Exception |
||
| 532 | */ |
||
| 533 | public function isReadOnly($message = '') |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Returns a single document by ID. Returns NULL if document was not found. |
||
| 542 | * |
||
| 543 | * @param string $className Document class name or Elasticsearch type name |
||
| 544 | * @param string $id Document ID to find |
||
| 545 | * |
||
| 546 | * @return object |
||
| 547 | */ |
||
| 548 | public function find($className, $id) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Executes given search. |
||
| 569 | * |
||
| 570 | * @param array $types |
||
| 571 | * @param Search $search |
||
| 572 | * @param string $resultsType |
||
| 573 | * |
||
| 574 | * @return DocumentIterator|RawIterator|array |
||
| 575 | */ |
||
| 576 | public function execute($types, Search $search, $resultsType = Result::RESULTS_OBJECT) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Parses raw result. |
||
| 589 | * |
||
| 590 | * @param array $raw |
||
| 591 | * @param string $resultsType |
||
| 592 | * @param string $scrollDuration |
||
| 593 | * |
||
| 594 | * @return DocumentIterator|RawIterator|array |
||
| 595 | * |
||
| 596 | * @throws \Exception |
||
| 597 | */ |
||
| 598 | private function parseResult($raw, $resultsType, $scrollDuration = null) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Normalizes response array. |
||
| 622 | * |
||
| 623 | * @param array $data |
||
| 624 | * |
||
| 625 | * @return array |
||
| 626 | */ |
||
| 627 | private function convertToNormalizedArray($data) |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Fetches next set of results. |
||
| 650 | * |
||
| 651 | * @param string $scrollId |
||
| 652 | * @param string $scrollDuration |
||
| 653 | * @param string $resultsType |
||
| 654 | * |
||
| 655 | * @return AbstractResultsIterator |
||
| 656 | * |
||
| 657 | * @throws \Exception |
||
| 658 | */ |
||
| 659 | public function scroll( |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Clears scroll. |
||
| 671 | * |
||
| 672 | * @param string $scrollId |
||
| 673 | */ |
||
| 674 | public function clearScroll($scrollId) |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Resolves type name by class name. |
||
| 681 | * |
||
| 682 | * @param string $className |
||
| 683 | * |
||
| 684 | * @return string |
||
| 685 | */ |
||
| 686 | private function resolveTypeName($className) |
||
| 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: