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 |
||
| 20 | class SchemaManager |
||
| 21 | { |
||
| 22 | private const GRIDFS_FILE_COLLECTION_INDEX = ['files_id' => 1, 'n' => 1]; |
||
| 23 | |||
| 24 | private const GRIDFS_CHUNKS_COLLECTION_INDEX = ['filename' => 1, 'uploadDate' => 1]; |
||
| 25 | |||
| 26 | private const CODE_SHARDING_ALREADY_INITIALIZED = 23; |
||
| 27 | |||
| 28 | private const ALLOWED_MISSING_INDEX_OPTIONS = [ |
||
| 29 | 'partialFilterExpression', |
||
| 30 | 'sparse', |
||
| 31 | 'unique', |
||
| 32 | 'weights', |
||
| 33 | 'default_language', |
||
| 34 | 'language_override', |
||
| 35 | 'textIndexVersion', |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** @var DocumentManager */ |
||
| 39 | protected $dm; |
||
| 40 | |||
| 41 | /** @var ClassMetadataFactory */ |
||
| 42 | protected $metadataFactory; |
||
| 43 | |||
| 44 | 1709 | public function __construct(DocumentManager $dm, ClassMetadataFactory $cmf) |
|
| 45 | { |
||
| 46 | 1709 | $this->dm = $dm; |
|
| 47 | 1709 | $this->metadataFactory = $cmf; |
|
| 48 | 1709 | } |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Ensure indexes are created for all documents that can be loaded with the |
||
| 52 | * metadata factory. |
||
| 53 | */ |
||
| 54 | 1 | public function ensureIndexes(?int $timeout = null) : void |
|
| 55 | { |
||
| 56 | 1 | foreach ($this->metadataFactory->getAllMetadata() as $class) { |
|
| 57 | 1 | if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) { |
|
| 58 | 1 | continue; |
|
| 59 | } |
||
| 60 | |||
| 61 | 1 | $this->ensureDocumentIndexes($class->name, $timeout); |
|
| 62 | } |
||
| 63 | 1 | } |
|
| 64 | |||
| 65 | /** |
||
| 66 | * Ensure indexes exist for all mapped document classes. |
||
| 67 | * |
||
| 68 | * Indexes that exist in MongoDB but not the document metadata will be |
||
| 69 | * deleted. |
||
| 70 | */ |
||
| 71 | public function updateIndexes(?int $timeout = null) : void |
||
| 72 | { |
||
| 73 | foreach ($this->metadataFactory->getAllMetadata() as $class) { |
||
| 74 | if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) { |
||
| 75 | continue; |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->updateDocumentIndexes($class->name, $timeout); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Ensure indexes exist for the mapped document class. |
||
| 84 | * |
||
| 85 | * Indexes that exist in MongoDB but not the document metadata will be |
||
| 86 | * deleted. |
||
| 87 | * |
||
| 88 | * @throws InvalidArgumentException |
||
| 89 | */ |
||
| 90 | 3 | public function updateDocumentIndexes(string $documentName, ?int $timeout = null) : void |
|
| 91 | { |
||
| 92 | 3 | $class = $this->dm->getClassMetadata($documentName); |
|
| 93 | |||
| 94 | 3 | if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) { |
|
| 95 | throw new InvalidArgumentException('Cannot update document indexes for mapped super classes, embedded documents or aggregation result documents.'); |
||
| 96 | } |
||
| 97 | |||
| 98 | 3 | $documentIndexes = $this->getDocumentIndexes($documentName); |
|
| 99 | 3 | $collection = $this->dm->getDocumentCollection($documentName); |
|
| 100 | 3 | $mongoIndexes = iterator_to_array($collection->listIndexes()); |
|
| 101 | |||
| 102 | /* Determine which Mongo indexes should be deleted. Exclude the ID index |
||
| 103 | * and those that are equivalent to any in the class metadata. |
||
| 104 | */ |
||
| 105 | 3 | $self = $this; |
|
| 106 | $mongoIndexes = array_filter($mongoIndexes, static function (IndexInfo $mongoIndex) use ($documentIndexes, $self) { |
||
| 107 | 1 | if ($mongoIndex['name'] === '_id_') { |
|
| 108 | return false; |
||
| 109 | } |
||
| 110 | |||
| 111 | 1 | foreach ($documentIndexes as $documentIndex) { |
|
| 112 | 1 | if ($self->isMongoIndexEquivalentToDocumentIndex($mongoIndex, $documentIndex)) { |
|
| 113 | 1 | return false; |
|
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | 1 | return true; |
|
| 118 | 3 | }); |
|
| 119 | |||
| 120 | // Delete indexes that do not exist in class metadata |
||
| 121 | 3 | foreach ($mongoIndexes as $mongoIndex) { |
|
| 122 | 1 | if (! isset($mongoIndex['name'])) { |
|
| 123 | continue; |
||
| 124 | } |
||
| 125 | |||
| 126 | 1 | $collection->dropIndex($mongoIndex['name']); |
|
| 127 | } |
||
| 128 | |||
| 129 | 3 | $this->ensureDocumentIndexes($documentName, $timeout); |
|
| 130 | 3 | } |
|
| 131 | |||
| 132 | 23 | public function getDocumentIndexes(string $documentName) : array |
|
| 133 | { |
||
| 134 | 23 | $visited = []; |
|
| 135 | 23 | return $this->doGetDocumentIndexes($documentName, $visited); |
|
| 136 | } |
||
| 137 | |||
| 138 | 23 | private function doGetDocumentIndexes(string $documentName, array &$visited) : array |
|
| 196 | |||
| 197 | 23 | private function prepareIndexes(ClassMetadata $class) : array |
|
| 198 | { |
||
| 199 | 23 | $persister = $this->dm->getUnitOfWork()->getDocumentPersister($class->name); |
|
| 200 | 23 | $indexes = $class->getIndexes(); |
|
| 201 | 23 | $newIndexes = []; |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Ensure the given document's indexes are created. |
||
| 227 | * |
||
| 228 | * @throws InvalidArgumentException |
||
| 229 | */ |
||
| 230 | 19 | public function ensureDocumentIndexes(string $documentName, ?int $timeoutMs = null) : void |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Delete indexes for all documents that can be loaded with the |
||
| 261 | * metadata factory. |
||
| 262 | */ |
||
| 263 | 1 | public function deleteIndexes() : void |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Delete the given document's indexes. |
||
| 276 | * |
||
| 277 | * @throws InvalidArgumentException |
||
| 278 | */ |
||
| 279 | 2 | public function deleteDocumentIndexes(string $documentName) : void |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Create all the mapped document collections in the metadata factory. |
||
| 291 | */ |
||
| 292 | 1 | public function createCollections() : void |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Create the document collection for a mapped class. |
||
| 304 | * |
||
| 305 | * @throws InvalidArgumentException |
||
| 306 | */ |
||
| 307 | 5 | public function createDocumentCollection(string $documentName) : void |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Drop all the mapped document collections in the metadata factory. |
||
| 334 | */ |
||
| 335 | 1 | public function dropCollections() : void |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Drop the document collection for a mapped class. |
||
| 348 | * |
||
| 349 | * @throws InvalidArgumentException |
||
| 350 | */ |
||
| 351 | 5 | public function dropDocumentCollection(string $documentName) : void |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Drop all the mapped document databases in the metadata factory. |
||
| 369 | */ |
||
| 370 | 1 | public function dropDatabases() : void |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Drop the document database for a mapped class. |
||
| 383 | * |
||
| 384 | * @throws InvalidArgumentException |
||
| 385 | */ |
||
| 386 | 2 | public function dropDocumentDatabase(string $documentName) : void |
|
| 395 | |||
| 396 | 44 | public function isMongoIndexEquivalentToDocumentIndex(IndexInfo $mongoIndex, array $documentIndex) : bool |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Determine if the keys for a MongoDB index can be considered equivalent to |
||
| 403 | * those for an index in class metadata. |
||
| 404 | */ |
||
| 405 | 44 | private function isEquivalentIndexKeys(IndexInfo $mongoIndex, array $documentIndex) : bool |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Determine if an index returned by MongoCollection::getIndexInfo() can be |
||
| 432 | * considered equivalent to an index in class metadata based on options. |
||
| 433 | * |
||
| 434 | * Indexes are considered different if: |
||
| 435 | * |
||
| 436 | * (a) Key/direction pairs differ or are not in the same order |
||
| 437 | * (b) Sparse or unique options differ |
||
| 438 | * (c) Geospatial options differ (bits, max, min) |
||
| 439 | * (d) The partialFilterExpression differs |
||
| 440 | */ |
||
| 441 | 40 | private function isEquivalentIndexOptions(IndexInfo $mongoIndex, array $documentIndex) : bool |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Checks if any index options are missing. |
||
| 494 | * |
||
| 495 | * Options added to the ALLOWED_MISSING_INDEX_OPTIONS constant are ignored |
||
| 496 | * and are expected to be checked later |
||
| 497 | */ |
||
| 498 | 40 | private function indexOptionsAreMissing(array $mongoIndexOptions, array $documentIndexOptions) : bool |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Determine if the text index weights for a MongoDB index can be considered |
||
| 509 | * equivalent to those for an index in class metadata. |
||
| 510 | */ |
||
| 511 | 14 | private function isEquivalentTextIndexWeights(IndexInfo $mongoIndex, array $documentIndex) : bool |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Ensure collections are sharded for all documents that can be loaded with the |
||
| 539 | * metadata factory. |
||
| 540 | * |
||
| 541 | * @throws MongoDBException |
||
| 542 | */ |
||
| 543 | public function ensureSharding() : void |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Ensure sharding for collection by document name. |
||
| 556 | * |
||
| 557 | * @throws MongoDBException |
||
| 558 | */ |
||
| 559 | 11 | public function ensureDocumentSharding(string $documentName) : void |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Enable sharding for database which contains documents with given name. |
||
| 581 | * |
||
| 582 | * @throws MongoDBException |
||
| 583 | */ |
||
| 584 | 11 | public function enableShardingForDbByDocumentName(string $documentName) : void |
|
| 600 | |||
| 601 | 11 | private function runShardCollectionCommand(string $documentName) : array |
|
| 615 | |||
| 616 | 2 | private function ensureGridFSIndexes(ClassMetadata $class) : void |
|
| 621 | |||
| 622 | 2 | private function ensureChunksIndex(ClassMetadata $class) : void |
|
| 633 | |||
| 634 | 2 | private function ensureFilesIndex(ClassMetadata $class) : void |
|
| 645 | |||
| 646 | 11 | private function collectionIsSharded(string $documentName) : bool |
|
| 659 | } |
||
| 660 |
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.