Complex classes like SchemaManager 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 SchemaManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class SchemaManager |
||
| 19 | { |
||
| 20 | private const GRIDFS_FILE_COLLECTION_INDEX = ['files_id' => 1, 'n' => 1]; |
||
| 21 | |||
| 22 | private const GRIDFS_CHUNKS_COLLECTION_INDEX = ['filename' => 1, 'uploadDate' => 1]; |
||
| 23 | |||
| 24 | private const CODE_SHARDING_ALREADY_INITIALIZED = 23; |
||
| 25 | |||
| 26 | /** @var DocumentManager */ |
||
| 27 | protected $dm; |
||
| 28 | |||
| 29 | /** @var ClassMetadataFactory */ |
||
| 30 | protected $metadataFactory; |
||
| 31 | |||
| 32 | 1668 | public function __construct(DocumentManager $dm, ClassMetadataFactory $cmf) |
|
| 33 | { |
||
| 34 | 1668 | $this->dm = $dm; |
|
| 35 | 1668 | $this->metadataFactory = $cmf; |
|
| 36 | 1668 | } |
|
| 37 | |||
| 38 | /** |
||
| 39 | * Ensure indexes are created for all documents that can be loaded with the |
||
| 40 | * metadata factory. |
||
| 41 | */ |
||
| 42 | 1 | public function ensureIndexes(?int $timeout = null): void |
|
| 43 | { |
||
| 44 | 1 | foreach ($this->metadataFactory->getAllMetadata() as $class) { |
|
| 45 | 1 | if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) { |
|
| 46 | 1 | continue; |
|
| 47 | } |
||
| 48 | |||
| 49 | 1 | $this->ensureDocumentIndexes($class->name, $timeout); |
|
| 50 | } |
||
| 51 | 1 | } |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Ensure indexes exist for all mapped document classes. |
||
| 55 | * |
||
| 56 | * Indexes that exist in MongoDB but not the document metadata will be |
||
| 57 | * deleted. |
||
| 58 | */ |
||
| 59 | public function updateIndexes(?int $timeout = null): void |
||
| 60 | { |
||
| 61 | foreach ($this->metadataFactory->getAllMetadata() as $class) { |
||
| 62 | if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) { |
||
| 63 | continue; |
||
| 64 | } |
||
| 65 | |||
| 66 | $this->updateDocumentIndexes($class->name, $timeout); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Ensure indexes exist for the mapped document class. |
||
| 72 | * |
||
| 73 | * Indexes that exist in MongoDB but not the document metadata will be |
||
| 74 | * deleted. |
||
| 75 | * |
||
| 76 | * @throws \InvalidArgumentException |
||
| 77 | */ |
||
| 78 | 3 | public function updateDocumentIndexes(string $documentName, ?int $timeout = null): void |
|
| 79 | { |
||
| 80 | 3 | $class = $this->dm->getClassMetadata($documentName); |
|
| 81 | |||
| 82 | 3 | if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) { |
|
| 83 | throw new \InvalidArgumentException('Cannot update document indexes for mapped super classes, embedded documents or aggregation result documents.'); |
||
| 84 | } |
||
| 85 | |||
| 86 | 3 | $documentIndexes = $this->getDocumentIndexes($documentName); |
|
| 87 | 3 | $collection = $this->dm->getDocumentCollection($documentName); |
|
| 88 | 3 | $mongoIndexes = iterator_to_array($collection->listIndexes()); |
|
| 89 | |||
| 90 | /* Determine which Mongo indexes should be deleted. Exclude the ID index |
||
| 91 | * and those that are equivalent to any in the class metadata. |
||
| 92 | */ |
||
| 93 | 3 | $self = $this; |
|
| 94 | $mongoIndexes = array_filter($mongoIndexes, function (IndexInfo $mongoIndex) use ($documentIndexes, $self) { |
||
| 95 | 1 | if ($mongoIndex['name'] === '_id_') { |
|
| 96 | return false; |
||
| 97 | } |
||
| 98 | |||
| 99 | 1 | foreach ($documentIndexes as $documentIndex) { |
|
| 100 | 1 | if ($self->isMongoIndexEquivalentToDocumentIndex($mongoIndex, $documentIndex)) { |
|
| 101 | 1 | return false; |
|
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | 1 | return true; |
|
| 106 | 3 | }); |
|
| 107 | |||
| 108 | // Delete indexes that do not exist in class metadata |
||
| 109 | 3 | foreach ($mongoIndexes as $mongoIndex) { |
|
| 110 | 1 | if (! isset($mongoIndex['name'])) { |
|
| 111 | continue; |
||
| 112 | } |
||
| 113 | |||
| 114 | 1 | $collection->dropIndex($mongoIndex['name']); |
|
| 115 | } |
||
| 116 | |||
| 117 | 3 | $this->ensureDocumentIndexes($documentName, $timeout); |
|
| 118 | 3 | } |
|
| 119 | |||
| 120 | 23 | public function getDocumentIndexes(string $documentName): array |
|
| 125 | |||
| 126 | 23 | private function doGetDocumentIndexes(string $documentName, array &$visited): array |
|
| 127 | { |
||
| 128 | 23 | if (isset($visited[$documentName])) { |
|
| 129 | 1 | return []; |
|
| 184 | |||
| 185 | 23 | private function prepareIndexes(ClassMetadata $class): array |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Ensure the given document's indexes are created. |
||
| 215 | * |
||
| 216 | * @throws \InvalidArgumentException |
||
| 217 | */ |
||
| 218 | 19 | public function ensureDocumentIndexes(string $documentName, ?int $timeoutMs = null): void |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Delete indexes for all documents that can be loaded with the |
||
| 249 | * metadata factory. |
||
| 250 | */ |
||
| 251 | 1 | public function deleteIndexes(): void |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Delete the given document's indexes. |
||
| 264 | * |
||
| 265 | * @throws \InvalidArgumentException |
||
| 266 | */ |
||
| 267 | 2 | public function deleteDocumentIndexes(string $documentName): void |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Create all the mapped document collections in the metadata factory. |
||
| 279 | */ |
||
| 280 | 1 | public function createCollections(): void |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Create the document collection for a mapped class. |
||
| 292 | * |
||
| 293 | * @throws \InvalidArgumentException |
||
| 294 | */ |
||
| 295 | 5 | public function createDocumentCollection(string $documentName): void |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Drop all the mapped document collections in the metadata factory. |
||
| 322 | */ |
||
| 323 | 1 | public function dropCollections(): void |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Drop the document collection for a mapped class. |
||
| 336 | * |
||
| 337 | * @throws \InvalidArgumentException |
||
| 338 | */ |
||
| 339 | 5 | public function dropDocumentCollection(string $documentName): void |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Drop all the mapped document databases in the metadata factory. |
||
| 357 | */ |
||
| 358 | 1 | public function dropDatabases(): void |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Drop the document database for a mapped class. |
||
| 371 | * |
||
| 372 | * @throws \InvalidArgumentException |
||
| 373 | */ |
||
| 374 | 2 | public function dropDocumentDatabase(string $documentName): void |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Determine if an index returned by MongoCollection::getIndexInfo() can be |
||
| 386 | * considered equivalent to an index in class metadata. |
||
| 387 | * |
||
| 388 | * Indexes are considered different if: |
||
| 389 | * |
||
| 390 | * (a) Key/direction pairs differ or are not in the same order |
||
| 391 | * (b) Sparse or unique options differ |
||
| 392 | * (c) Mongo index is unique without dropDups and mapped index is unique |
||
| 393 | * with dropDups |
||
| 394 | * (d) Geospatial options differ (bits, max, min) |
||
| 395 | * (e) The partialFilterExpression differs |
||
| 396 | * |
||
| 397 | * Regarding (c), the inverse case is not a reason to delete and |
||
| 398 | * recreate the index, since dropDups only affects creation of |
||
| 399 | * the unique index. Additionally, the background option is only |
||
| 400 | * relevant to index creation and is not considered. |
||
| 401 | * |
||
| 402 | * @param array|IndexInfo $mongoIndex Mongo index data. |
||
| 403 | */ |
||
| 404 | 47 | public function isMongoIndexEquivalentToDocumentIndex($mongoIndex, array $documentIndex): bool |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Determine if the keys for a MongoDB index can be considered equivalent to |
||
| 463 | * those for an index in class metadata. |
||
| 464 | * |
||
| 465 | * @param array|IndexInfo $mongoIndex Mongo index data. |
||
| 466 | */ |
||
| 467 | 47 | private function isEquivalentIndexKeys($mongoIndex, array $documentIndex): bool |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Determine if the text index weights for a MongoDB index can be considered |
||
| 494 | * equivalent to those for an index in class metadata. |
||
| 495 | * |
||
| 496 | * @param array|IndexInfo $mongoIndex Mongo index data. |
||
| 497 | */ |
||
| 498 | 14 | private function isEquivalentTextIndexWeights($mongoIndex, array $documentIndex): bool |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Ensure collections are sharded for all documents that can be loaded with the |
||
| 526 | * metadata factory. |
||
| 527 | * |
||
| 528 | * @throws MongoDBException |
||
| 529 | */ |
||
| 530 | public function ensureSharding(): void |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Ensure sharding for collection by document name. |
||
| 543 | * |
||
| 544 | * @throws MongoDBException |
||
| 545 | */ |
||
| 546 | 5 | public function ensureDocumentSharding(string $documentName): void |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Enable sharding for database which contains documents with given name. |
||
| 568 | * |
||
| 569 | * @throws MongoDBException |
||
| 570 | */ |
||
| 571 | 5 | public function enableShardingForDbByDocumentName(string $documentName): void |
|
| 587 | |||
| 588 | 5 | private function runShardCollectionCommand(string $documentName): array |
|
| 604 | |||
| 605 | 2 | private function ensureGridFSIndexes(ClassMetadata $class): void |
|
| 610 | |||
| 611 | 2 | private function ensureChunksIndex(ClassMetadata $class): void |
|
| 622 | |||
| 623 | 2 | private function ensureFilesIndex(ClassMetadata $class): void |
|
| 634 | |||
| 635 | 5 | private function collectionIsSharded(string $documentName): bool |
|
| 648 | } |
||
| 649 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.