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 | /** @var DocumentManager */ |
||
| 29 | protected $dm; |
||
| 30 | |||
| 31 | /** @var ClassMetadataFactory */ |
||
| 32 | protected $metadataFactory; |
||
| 33 | |||
| 34 | 1755 | public function __construct(DocumentManager $dm, ClassMetadataFactory $cmf) |
|
| 35 | { |
||
| 36 | 1755 | $this->dm = $dm; |
|
| 37 | 1755 | $this->metadataFactory = $cmf; |
|
| 38 | 1755 | } |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Ensure indexes are created for all documents that can be loaded with the |
||
| 42 | * metadata factory. |
||
| 43 | */ |
||
| 44 | 4 | public function ensureIndexes(?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 45 | { |
||
| 46 | 4 | foreach ($this->metadataFactory->getAllMetadata() as $class) { |
|
| 47 | 4 | if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) { |
|
| 48 | 4 | continue; |
|
| 49 | } |
||
| 50 | |||
| 51 | 4 | $this->ensureDocumentIndexes($class->name, $maxTimeMs, $writeConcern); |
|
| 52 | } |
||
| 53 | 4 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Ensure indexes exist for all mapped document classes. |
||
| 57 | * |
||
| 58 | * Indexes that exist in MongoDB but not the document metadata will be |
||
| 59 | * deleted. |
||
| 60 | */ |
||
| 61 | public function updateIndexes(?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
||
| 62 | { |
||
| 63 | foreach ($this->metadataFactory->getAllMetadata() as $class) { |
||
| 64 | if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) { |
||
| 65 | continue; |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->updateDocumentIndexes($class->name, $maxTimeMs, $writeConcern); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Ensure indexes exist for the mapped document class. |
||
| 74 | * |
||
| 75 | * Indexes that exist in MongoDB but not the document metadata will be |
||
| 76 | * deleted. |
||
| 77 | * |
||
| 78 | * @throws InvalidArgumentException |
||
| 79 | */ |
||
| 80 | 9 | public function updateDocumentIndexes(string $documentName, ?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 81 | { |
||
| 82 | 9 | $class = $this->dm->getClassMetadata($documentName); |
|
| 83 | |||
| 84 | 9 | if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) { |
|
| 85 | throw new InvalidArgumentException('Cannot update document indexes for mapped super classes, embedded documents or aggregation result documents.'); |
||
| 86 | } |
||
| 87 | |||
| 88 | 9 | $documentIndexes = $this->getDocumentIndexes($documentName); |
|
| 89 | 9 | $collection = $this->dm->getDocumentCollection($documentName); |
|
| 90 | 9 | $mongoIndexes = iterator_to_array($collection->listIndexes()); |
|
| 91 | |||
| 92 | /* Determine which Mongo indexes should be deleted. Exclude the ID index |
||
| 93 | * and those that are equivalent to any in the class metadata. |
||
| 94 | */ |
||
| 95 | 9 | $self = $this; |
|
| 96 | $mongoIndexes = array_filter($mongoIndexes, static function (IndexInfo $mongoIndex) use ($documentIndexes, $self) { |
||
| 97 | 4 | if ($mongoIndex['name'] === '_id_') { |
|
| 98 | return false; |
||
| 99 | } |
||
| 100 | |||
| 101 | 4 | foreach ($documentIndexes as $documentIndex) { |
|
| 102 | 4 | if ($self->isMongoIndexEquivalentToDocumentIndex($mongoIndex, $documentIndex)) { |
|
| 103 | 4 | return false; |
|
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | 4 | return true; |
|
| 108 | 9 | }); |
|
| 109 | |||
| 110 | // Delete indexes that do not exist in class metadata |
||
| 111 | 9 | foreach ($mongoIndexes as $mongoIndex) { |
|
| 112 | 4 | if (! isset($mongoIndex['name'])) { |
|
| 113 | continue; |
||
| 114 | } |
||
| 115 | |||
| 116 | 4 | $collection->dropIndex($mongoIndex['name'], $this->getWriteOptions($maxTimeMs, $writeConcern)); |
|
| 117 | } |
||
| 118 | |||
| 119 | 9 | $this->ensureDocumentIndexes($documentName, $maxTimeMs, $writeConcern); |
|
| 120 | 9 | } |
|
| 121 | |||
| 122 | 40 | public function getDocumentIndexes(string $documentName) : array |
|
| 127 | |||
| 128 | 40 | private function doGetDocumentIndexes(string $documentName, array &$visited) : array |
|
| 186 | |||
| 187 | 40 | private function prepareIndexes(ClassMetadata $class) : array |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Ensure the given document's indexes are created. |
||
| 217 | * |
||
| 218 | * @throws InvalidArgumentException |
||
| 219 | */ |
||
| 220 | 36 | public function ensureDocumentIndexes(string $documentName, ?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Delete indexes for all documents that can be loaded with the |
||
| 244 | * metadata factory. |
||
| 245 | */ |
||
| 246 | 4 | public function deleteIndexes(?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Delete the given document's indexes. |
||
| 259 | * |
||
| 260 | * @throws InvalidArgumentException |
||
| 261 | */ |
||
| 262 | 8 | public function deleteDocumentIndexes(string $documentName, ?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Create all the mapped document collections in the metadata factory. |
||
| 274 | */ |
||
| 275 | 4 | public function createCollections(?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Create the document collection for a mapped class. |
||
| 287 | * |
||
| 288 | * @throws InvalidArgumentException |
||
| 289 | */ |
||
| 290 | 14 | public function createDocumentCollection(string $documentName, ?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Drop all the mapped document collections in the metadata factory. |
||
| 321 | */ |
||
| 322 | 4 | public function dropCollections(?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Drop the document collection for a mapped class. |
||
| 335 | * |
||
| 336 | * @throws InvalidArgumentException |
||
| 337 | */ |
||
| 338 | 14 | public function dropDocumentCollection(string $documentName, ?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Drop all the mapped document databases in the metadata factory. |
||
| 358 | */ |
||
| 359 | 4 | public function dropDatabases(?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Drop the document database for a mapped class. |
||
| 372 | * |
||
| 373 | * @throws InvalidArgumentException |
||
| 374 | */ |
||
| 375 | 8 | public function dropDocumentDatabase(string $documentName, ?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Determine if an index returned by MongoCollection::getIndexInfo() can be |
||
| 387 | * considered equivalent to an index in class metadata. |
||
| 388 | * |
||
| 389 | * Indexes are considered different if: |
||
| 390 | * |
||
| 391 | * (a) Key/direction pairs differ or are not in the same order |
||
| 392 | * (b) Sparse or unique options differ |
||
| 393 | * (c) Geospatial options differ (bits, max, min) |
||
| 394 | * (d) The partialFilterExpression differs |
||
| 395 | * |
||
| 396 | * The background option is only relevant to index creation and is not |
||
| 397 | * considered. |
||
| 398 | * |
||
| 399 | * @param array|IndexInfo $mongoIndex Mongo index data. |
||
| 400 | */ |
||
| 401 | 46 | public function isMongoIndexEquivalentToDocumentIndex($mongoIndex, array $documentIndex) : bool |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Determine if the keys for a MongoDB index can be considered equivalent to |
||
| 455 | * those for an index in class metadata. |
||
| 456 | * |
||
| 457 | * @param array|IndexInfo $mongoIndex Mongo index data. |
||
| 458 | */ |
||
| 459 | 46 | private function isEquivalentIndexKeys($mongoIndex, array $documentIndex) : bool |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Determine if the text index weights for a MongoDB index can be considered |
||
| 486 | * equivalent to those for an index in class metadata. |
||
| 487 | * |
||
| 488 | * @param array|IndexInfo $mongoIndex Mongo index data. |
||
| 489 | */ |
||
| 490 | 14 | private function isEquivalentTextIndexWeights($mongoIndex, array $documentIndex) : bool |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Ensure collections are sharded for all documents that can be loaded with the |
||
| 518 | * metadata factory. |
||
| 519 | * |
||
| 520 | * @throws MongoDBException |
||
| 521 | */ |
||
| 522 | public function ensureSharding() : void |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Ensure sharding for collection by document name. |
||
| 535 | * |
||
| 536 | * @throws MongoDBException |
||
| 537 | */ |
||
| 538 | 11 | public function ensureDocumentSharding(string $documentName) : void |
|
| 557 | |||
| 558 | /** |
||
| 559 | * Enable sharding for database which contains documents with given name. |
||
| 560 | * |
||
| 561 | * @throws MongoDBException |
||
| 562 | */ |
||
| 563 | 11 | public function enableShardingForDbByDocumentName(string $documentName) : void |
|
| 579 | |||
| 580 | 11 | private function runShardCollectionCommand(string $documentName) : array |
|
| 594 | |||
| 595 | 8 | private function ensureGridFSIndexes(ClassMetadata $class, ?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 600 | |||
| 601 | 8 | private function ensureChunksIndex(ClassMetadata $class, ?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 615 | |||
| 616 | 8 | private function ensureFilesIndex(ClassMetadata $class, ?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) : void |
|
| 627 | |||
| 628 | 11 | private function collectionIsSharded(string $documentName) : bool |
|
| 641 | |||
| 642 | 78 | private function getWriteOptions(?int $maxTimeMs = null, ?WriteConcern $writeConcern = null, array $options = []) : array |
|
| 656 | } |
||
| 657 |
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.