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 |
||
| 16 | class SchemaManager |
||
| 17 | { |
||
| 18 | private const GRIDFS_FILE_COLLECTION_INDEX = ['files_id' => 1, 'n' => 1]; |
||
| 19 | |||
| 20 | private const GRIDFS_CHUNKS_COLLECTION_INDEX = ['filename' => 1, 'uploadDate' => 1]; |
||
| 21 | |||
| 22 | /** @var DocumentManager */ |
||
| 23 | protected $dm; |
||
| 24 | |||
| 25 | /** @var ClassMetadataFactory */ |
||
| 26 | protected $metadataFactory; |
||
| 27 | |||
| 28 | 1637 | public function __construct(DocumentManager $dm, ClassMetadataFactory $cmf) |
|
| 33 | |||
| 34 | /** |
||
| 35 | * Ensure indexes are created for all documents that can be loaded with the |
||
| 36 | * metadata factory. |
||
| 37 | * |
||
| 38 | * @param int $timeout Timeout (ms) for acknowledged index creation |
||
| 39 | */ |
||
| 40 | 1 | public function ensureIndexes($timeout = null) |
|
| 41 | { |
||
| 42 | 1 | foreach ($this->metadataFactory->getAllMetadata() as $class) { |
|
| 43 | 1 | if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) { |
|
| 44 | 1 | continue; |
|
| 45 | } |
||
| 46 | |||
| 47 | 1 | $this->ensureDocumentIndexes($class->name, $timeout); |
|
| 48 | } |
||
| 49 | 1 | } |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Ensure indexes exist for all mapped document classes. |
||
| 53 | * |
||
| 54 | * Indexes that exist in MongoDB but not the document metadata will be |
||
| 55 | * deleted. |
||
| 56 | * |
||
| 57 | * @param int $timeout Timeout (ms) for acknowledged index creation |
||
| 58 | */ |
||
| 59 | public function updateIndexes($timeout = null) |
||
| 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 | * @param string $documentName |
||
| 77 | * @param int $timeout Timeout (ms) for acknowledged index creation |
||
| 78 | * @throws \InvalidArgumentException |
||
| 79 | */ |
||
| 80 | 3 | public function updateDocumentIndexes($documentName, $timeout = null) |
|
| 81 | { |
||
| 82 | 3 | $class = $this->dm->getClassMetadata($documentName); |
|
| 83 | |||
| 84 | 3 | 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 | 3 | $documentIndexes = $this->getDocumentIndexes($documentName); |
|
| 89 | 3 | $collection = $this->dm->getDocumentCollection($documentName); |
|
| 90 | 3 | $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 | 3 | $self = $this; |
|
| 96 | $mongoIndexes = array_filter($mongoIndexes, function (IndexInfo $mongoIndex) use ($documentIndexes, $self) { |
||
| 97 | 1 | if ($mongoIndex['name'] === '_id_') { |
|
| 98 | return false; |
||
| 99 | } |
||
| 100 | |||
| 101 | 1 | foreach ($documentIndexes as $documentIndex) { |
|
| 102 | 1 | if ($self->isMongoIndexEquivalentToDocumentIndex($mongoIndex, $documentIndex)) { |
|
| 103 | 1 | return false; |
|
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | 1 | return true; |
|
| 108 | 3 | }); |
|
| 109 | |||
| 110 | // Delete indexes that do not exist in class metadata |
||
| 111 | 3 | foreach ($mongoIndexes as $mongoIndex) { |
|
| 112 | 1 | if (! isset($mongoIndex['name'])) { |
|
| 113 | continue; |
||
| 114 | } |
||
| 115 | |||
| 116 | 1 | $collection->dropIndex($mongoIndex['name']); |
|
| 117 | } |
||
| 118 | |||
| 119 | 3 | $this->ensureDocumentIndexes($documentName, $timeout); |
|
| 120 | 3 | } |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $documentName |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | 20 | public function getDocumentIndexes($documentName) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $documentName |
||
| 134 | * @param array $visited |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | 20 | private function doGetDocumentIndexes($documentName, array &$visited) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | 20 | private function prepareIndexes(ClassMetadata $class) |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Ensure the given document's indexes are created. |
||
| 229 | * |
||
| 230 | * @param string $documentName |
||
| 231 | * @param int $timeout Timeout (ms) for acknowledged index creation |
||
| 232 | * @throws \InvalidArgumentException |
||
| 233 | */ |
||
| 234 | 16 | public function ensureDocumentIndexes($documentName, $timeout = null) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Delete indexes for all documents that can be loaded with the |
||
| 265 | * metadata factory. |
||
| 266 | */ |
||
| 267 | 1 | public function deleteIndexes() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Delete the given document's indexes. |
||
| 280 | * |
||
| 281 | * @param string $documentName |
||
| 282 | * @throws \InvalidArgumentException |
||
| 283 | */ |
||
| 284 | 2 | public function deleteDocumentIndexes($documentName) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Create all the mapped document collections in the metadata factory. |
||
| 296 | */ |
||
| 297 | 1 | public function createCollections() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Create the document collection for a mapped class. |
||
| 309 | * |
||
| 310 | * @param string $documentName |
||
| 311 | * @throws \InvalidArgumentException |
||
| 312 | */ |
||
| 313 | 5 | public function createDocumentCollection($documentName) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Drop all the mapped document collections in the metadata factory. |
||
| 340 | */ |
||
| 341 | 1 | public function dropCollections() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Drop the document collection for a mapped class. |
||
| 354 | * |
||
| 355 | * @param string $documentName |
||
| 356 | * @throws \InvalidArgumentException |
||
| 357 | */ |
||
| 358 | 5 | public function dropDocumentCollection($documentName) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Drop all the mapped document databases in the metadata factory. |
||
| 376 | */ |
||
| 377 | 1 | public function dropDatabases() |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Drop the document database for a mapped class. |
||
| 390 | * |
||
| 391 | * @param string $documentName |
||
| 392 | * @throws \InvalidArgumentException |
||
| 393 | */ |
||
| 394 | 2 | public function dropDocumentDatabase($documentName) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Determine if an index returned by MongoCollection::getIndexInfo() can be |
||
| 406 | * considered equivalent to an index in class metadata. |
||
| 407 | * |
||
| 408 | * Indexes are considered different if: |
||
| 409 | * |
||
| 410 | * (a) Key/direction pairs differ or are not in the same order |
||
| 411 | * (b) Sparse or unique options differ |
||
| 412 | * (c) Mongo index is unique without dropDups and mapped index is unique |
||
| 413 | * with dropDups |
||
| 414 | * (d) Geospatial options differ (bits, max, min) |
||
| 415 | * (e) The partialFilterExpression differs |
||
| 416 | * |
||
| 417 | * Regarding (c), the inverse case is not a reason to delete and |
||
| 418 | * recreate the index, since dropDups only affects creation of |
||
| 419 | * the unique index. Additionally, the background option is only |
||
| 420 | * relevant to index creation and is not considered. |
||
| 421 | * |
||
| 422 | * @param array|IndexInfo $mongoIndex Mongo index data. |
||
| 423 | * @param array $documentIndex Document index data. |
||
| 424 | * @return bool True if the indexes are equivalent, otherwise false. |
||
| 425 | */ |
||
| 426 | 30 | public function isMongoIndexEquivalentToDocumentIndex($mongoIndex, $documentIndex) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Ensure collections are sharded for all documents that can be loaded with the |
||
| 472 | * metadata factory. |
||
| 473 | * |
||
| 474 | * @param array $indexOptions Options for `ensureIndex` command. It's performed on an existing collections |
||
| 475 | * |
||
| 476 | * @throws MongoDBException |
||
| 477 | */ |
||
| 478 | public function ensureSharding(array $indexOptions = []) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Ensure sharding for collection by document name. |
||
| 491 | * |
||
| 492 | * @param string $documentName |
||
| 493 | * @param array $indexOptions Options for `ensureIndex` command. It's performed on an existing collections. |
||
| 494 | * |
||
| 495 | * @throws MongoDBException |
||
| 496 | */ |
||
| 497 | 2 | public function ensureDocumentSharding($documentName, array $indexOptions = []) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Enable sharding for database which contains documents with given name. |
||
| 541 | * |
||
| 542 | * @param string $documentName |
||
| 543 | * |
||
| 544 | * @throws MongoDBException |
||
| 545 | */ |
||
| 546 | 2 | public function enableShardingForDbByDocumentName($documentName) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * @param string $documentName |
||
| 562 | * |
||
| 563 | * @return array |
||
| 564 | */ |
||
| 565 | 2 | private function runShardCollectionCommand($documentName) |
|
| 581 | |||
| 582 | 2 | private function ensureGridFSIndexes(ClassMetadata $class): void |
|
| 587 | |||
| 588 | 2 | private function ensureChunksIndex(ClassMetadata $class): void |
|
| 599 | |||
| 600 | 2 | private function ensureFilesIndex(ClassMetadata $class): void |
|
| 611 | } |
||
| 612 |
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.