Complex classes like DocumentManager 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 DocumentManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class DocumentManager implements ObjectManager |
||
| 41 | { |
||
| 42 | public const CLIENT_TYPEMAP = ['root' => 'array', 'document' => 'array']; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The Doctrine MongoDB connection instance. |
||
| 46 | * |
||
| 47 | * @var Client |
||
| 48 | */ |
||
| 49 | private $client; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The used Configuration. |
||
| 53 | * |
||
| 54 | * @var Configuration |
||
| 55 | */ |
||
| 56 | private $config; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The metadata factory, used to retrieve the ODM metadata of document classes. |
||
| 60 | * |
||
| 61 | * @var ClassMetadataFactory |
||
| 62 | */ |
||
| 63 | private $metadataFactory; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The UnitOfWork used to coordinate object-level transactions. |
||
| 67 | * |
||
| 68 | * @var UnitOfWork |
||
| 69 | */ |
||
| 70 | private $unitOfWork; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The event manager that is the central point of the event system. |
||
| 74 | * |
||
| 75 | * @var EventManager |
||
| 76 | */ |
||
| 77 | private $eventManager; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The Hydrator factory instance. |
||
| 81 | * |
||
| 82 | * @var HydratorFactory |
||
| 83 | */ |
||
| 84 | private $hydratorFactory; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The Proxy factory instance. |
||
| 88 | * |
||
| 89 | * @var ProxyFactory |
||
| 90 | */ |
||
| 91 | private $proxyFactory; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The repository factory used to create dynamic repositories. |
||
| 95 | * |
||
| 96 | * @var RepositoryFactory |
||
| 97 | */ |
||
| 98 | private $repositoryFactory; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * SchemaManager instance |
||
| 102 | * |
||
| 103 | * @var SchemaManager |
||
| 104 | */ |
||
| 105 | private $schemaManager; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Array of cached document database instances that are lazily loaded. |
||
| 109 | * |
||
| 110 | * @var Database[] |
||
| 111 | */ |
||
| 112 | private $documentDatabases = []; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Array of cached document collection instances that are lazily loaded. |
||
| 116 | * |
||
| 117 | * @var Collection[] |
||
| 118 | */ |
||
| 119 | private $documentCollections = []; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Array of cached document bucket instances that are lazily loaded. |
||
| 123 | * |
||
| 124 | * @var Bucket[] |
||
| 125 | */ |
||
| 126 | private $documentBuckets = []; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Whether the DocumentManager is closed or not. |
||
| 130 | * |
||
| 131 | * @var bool |
||
| 132 | */ |
||
| 133 | private $closed = false; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Collection of query filters. |
||
| 137 | * |
||
| 138 | * @var FilterCollection |
||
| 139 | */ |
||
| 140 | private $filterCollection; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Creates a new Document that operates on the given Mongo connection |
||
| 144 | * and uses the given Configuration. |
||
| 145 | */ |
||
| 146 | 1636 | protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Gets the proxy factory used by the DocumentManager to create document proxies. |
||
| 188 | */ |
||
| 189 | 1 | public function getProxyFactory() : ProxyFactory |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Creates a new Document that operates on the given Mongo connection |
||
| 196 | * and uses the given Configuration. |
||
| 197 | */ |
||
| 198 | 1636 | public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) : DocumentManager |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Gets the EventManager used by the DocumentManager. |
||
| 205 | */ |
||
| 206 | 1699 | public function getEventManager() : EventManager |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Gets the MongoDB client instance that this DocumentManager wraps. |
||
| 213 | */ |
||
| 214 | 1636 | public function getClient() : Client |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Gets the metadata factory used to gather the metadata of classes. |
||
| 221 | * |
||
| 222 | * @return ClassMetadataFactory |
||
| 223 | */ |
||
| 224 | 1636 | public function getMetadataFactory() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 231 | * |
||
| 232 | * This method is a no-op for other objects. |
||
| 233 | * |
||
| 234 | * @param object $obj |
||
| 235 | */ |
||
| 236 | public function initializeObject($obj) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Gets the UnitOfWork used by the DocumentManager to coordinate operations. |
||
| 243 | */ |
||
| 244 | 1643 | public function getUnitOfWork() : UnitOfWork |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Gets the Hydrator factory used by the DocumentManager to generate and get hydrators |
||
| 251 | * for each type of document. |
||
| 252 | */ |
||
| 253 | 70 | public function getHydratorFactory() : HydratorFactory |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Returns SchemaManager, used to create/drop indexes/collections/databases. |
||
| 260 | */ |
||
| 261 | 28 | public function getSchemaManager() : SchemaManager |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Returns the metadata for a class. |
||
| 268 | * |
||
| 269 | * @internal Performance-sensitive method. |
||
| 270 | * |
||
| 271 | * @param string $className The class name. |
||
| 272 | * |
||
| 273 | * @return ClassMetadata |
||
| 274 | */ |
||
| 275 | 1371 | public function getClassMetadata($className) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Returns the MongoDB instance for a class. |
||
| 282 | */ |
||
| 283 | 1300 | public function getDocumentDatabase(string $className) : Database |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Gets the array of instantiated document database instances. |
||
| 302 | * |
||
| 303 | * @return Database[] |
||
| 304 | */ |
||
| 305 | public function getDocumentDatabases() : array |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns the collection instance for a class. |
||
| 312 | * |
||
| 313 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
| 314 | */ |
||
| 315 | 1303 | public function getDocumentCollection(string $className) : Collection |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Returns the bucket instance for a class. |
||
| 347 | * |
||
| 348 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
| 349 | */ |
||
| 350 | 16 | public function getDocumentBucket(string $className) : Bucket |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Gets the array of instantiated document collection instances. |
||
| 382 | * |
||
| 383 | * @return Collection[] |
||
| 384 | */ |
||
| 385 | public function getDocumentCollections() : array |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Create a new Query instance for a class. |
||
| 392 | * |
||
| 393 | * @param string[]|string|null $documentName (optional) an array of document names, the document name, or none |
||
| 394 | */ |
||
| 395 | 182 | public function createQueryBuilder($documentName = null) : Query\Builder |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Creates a new aggregation builder instance for a class. |
||
| 402 | */ |
||
| 403 | 41 | public function createAggregationBuilder(string $documentName) : Aggregation\Builder |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Tells the DocumentManager to make an instance managed and persistent. |
||
| 410 | * |
||
| 411 | * The document will be entered into the database at or before transaction |
||
| 412 | * commit or as a result of the flush operation. |
||
| 413 | * |
||
| 414 | * NOTE: The persist operation always considers documents that are not yet known to |
||
| 415 | * this DocumentManager as NEW. Do not pass detached documents to the persist operation. |
||
| 416 | * |
||
| 417 | * @param object $document The instance to make managed and persistent. |
||
| 418 | * |
||
| 419 | * @throws InvalidArgumentException When the given $document param is not an object. |
||
| 420 | */ |
||
| 421 | 615 | public function persist($document) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Removes a document instance. |
||
| 432 | * |
||
| 433 | * A removed document will be removed from the database at or before transaction commit |
||
| 434 | * or as a result of the flush operation. |
||
| 435 | * |
||
| 436 | * @param object $document The document instance to remove. |
||
| 437 | * |
||
| 438 | * @throws InvalidArgumentException When the $document param is not an object. |
||
| 439 | */ |
||
| 440 | 27 | public function remove($document) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Refreshes the persistent state of a document from the database, |
||
| 451 | * overriding any local changes that have not yet been persisted. |
||
| 452 | * |
||
| 453 | * @param object $document The document to refresh. |
||
| 454 | * |
||
| 455 | * @throws InvalidArgumentException When the given $document param is not an object. |
||
| 456 | */ |
||
| 457 | 26 | public function refresh($document) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Detaches a document from the DocumentManager, causing a managed document to |
||
| 468 | * become detached. Unflushed changes made to the document if any |
||
| 469 | * (including removal of the document), will not be synchronized to the database. |
||
| 470 | * Documents which previously referenced the detached document will continue to |
||
| 471 | * reference it. |
||
| 472 | * |
||
| 473 | * @param object $document The document to detach. |
||
| 474 | * |
||
| 475 | * @throws InvalidArgumentException When the $document param is not an object. |
||
| 476 | */ |
||
| 477 | 11 | public function detach($document) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Merges the state of a detached document into the persistence context |
||
| 487 | * of this DocumentManager and returns the managed copy of the document. |
||
| 488 | * The document passed to merge will not become associated/managed with this DocumentManager. |
||
| 489 | * |
||
| 490 | * @param object $document The detached document to merge into the persistence context. |
||
| 491 | * |
||
| 492 | * @return object The managed copy of the document. |
||
| 493 | * |
||
| 494 | * @throws LockException |
||
| 495 | * @throws InvalidArgumentException If the $document param is not an object. |
||
| 496 | */ |
||
| 497 | 14 | public function merge($document) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Acquire a lock on the given document. |
||
| 508 | * |
||
| 509 | * @throws InvalidArgumentException |
||
| 510 | * @throws LockException |
||
| 511 | */ |
||
| 512 | 8 | public function lock(object $document, int $lockMode, ?int $lockVersion = null) : void |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Releases a lock on the given document. |
||
| 519 | */ |
||
| 520 | 1 | public function unlock(object $document) : void |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Gets the repository for a document class. |
||
| 527 | * |
||
| 528 | * @param string $documentName The name of the Document. |
||
| 529 | * |
||
| 530 | * @return ObjectRepository The repository. |
||
| 531 | */ |
||
| 532 | 358 | public function getRepository($documentName) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Flushes all changes to objects that have been queued up to now to the database. |
||
| 539 | * This effectively synchronizes the in-memory state of managed objects with the |
||
| 540 | * database. |
||
| 541 | * |
||
| 542 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 543 | * |
||
| 544 | * @throws MongoDBException |
||
| 545 | */ |
||
| 546 | 587 | public function flush(array $options = []) |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Gets a reference to the document identified by the given type and identifier |
||
| 554 | * without actually loading it. |
||
| 555 | * |
||
| 556 | * If partial objects are allowed, this method will return a partial object that only |
||
| 557 | * has its identifier populated. Otherwise a proxy is returned that automatically |
||
| 558 | * loads itself on first access. |
||
| 559 | * |
||
| 560 | * @param string|object $identifier |
||
| 561 | */ |
||
| 562 | 134 | public function getReference(string $documentName, $identifier) : object |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Gets a partial reference to the document identified by the given type and identifier |
||
| 581 | * without actually loading it, if the document is not yet loaded. |
||
| 582 | * |
||
| 583 | * The returned reference may be a partial object if the document is not yet loaded/managed. |
||
| 584 | * If it is a partial object it will not initialize the rest of the document state on access. |
||
| 585 | * Thus you can only ever safely access the identifier of a document obtained through |
||
| 586 | * this method. |
||
| 587 | * |
||
| 588 | * The use-cases for partial references involve maintaining bidirectional associations |
||
| 589 | * without loading one side of the association or to update a document without loading it. |
||
| 590 | * Note, however, that in the latter case the original (persistent) document data will |
||
| 591 | * never be visible to the application (especially not event listeners) as it will |
||
| 592 | * never be loaded in the first place. |
||
| 593 | * |
||
| 594 | * @param mixed $identifier The document identifier. |
||
| 595 | */ |
||
| 596 | 1 | public function getPartialReference(string $documentName, $identifier) : object |
|
| 611 | |||
| 612 | /** |
||
| 613 | * Finds a Document by its identifier. |
||
| 614 | * |
||
| 615 | * This is just a convenient shortcut for getRepository($documentName)->find($id). |
||
| 616 | * |
||
| 617 | * @param string $documentName |
||
| 618 | * @param mixed $identifier |
||
| 619 | * @param int $lockMode |
||
| 620 | * @param int $lockVersion |
||
| 621 | * |
||
| 622 | * @return object $document |
||
| 623 | */ |
||
| 624 | 187 | public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Clears the DocumentManager. |
||
| 631 | * |
||
| 632 | * All documents that are currently managed by this DocumentManager become |
||
| 633 | * detached. |
||
| 634 | * |
||
| 635 | * @param string|null $documentName if given, only documents of this type will get detached |
||
| 636 | */ |
||
| 637 | 390 | public function clear($documentName = null) |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Closes the DocumentManager. All documents that are currently managed |
||
| 644 | * by this DocumentManager become detached. The DocumentManager may no longer |
||
| 645 | * be used after it is closed. |
||
| 646 | */ |
||
| 647 | 6 | public function close() |
|
| 652 | |||
| 653 | /** |
||
| 654 | * Determines whether a document instance is managed in this DocumentManager. |
||
| 655 | * |
||
| 656 | * @param object $document |
||
| 657 | * |
||
| 658 | * @return bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
||
| 659 | * |
||
| 660 | * @throws InvalidArgumentException When the $document param is not an object. |
||
| 661 | */ |
||
| 662 | 3 | public function contains($document) |
|
| 671 | |||
| 672 | /** |
||
| 673 | * Gets the Configuration used by the DocumentManager. |
||
| 674 | */ |
||
| 675 | 783 | public function getConfiguration() : Configuration |
|
| 679 | |||
| 680 | /** |
||
| 681 | * Returns a reference to the supplied document. |
||
| 682 | * |
||
| 683 | * @return mixed The reference for the document in question, according to the desired mapping |
||
| 684 | * |
||
| 685 | * @throws MappingException |
||
| 686 | * @throws RuntimeException |
||
| 687 | */ |
||
| 688 | 226 | public function createReference(object $document, array $referenceMapping) |
|
| 765 | |||
| 766 | /** |
||
| 767 | * Throws an exception if the DocumentManager is closed or currently not active. |
||
| 768 | * |
||
| 769 | * @throws MongoDBException If the DocumentManager is closed. |
||
| 770 | */ |
||
| 771 | 620 | private function errorIfClosed() : void |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Check if the Document manager is open or closed. |
||
| 780 | */ |
||
| 781 | 1 | public function isOpen() : bool |
|
| 785 | |||
| 786 | /** |
||
| 787 | * Gets the filter collection. |
||
| 788 | */ |
||
| 789 | 537 | public function getFilterCollection() : FilterCollection |
|
| 797 | |||
| 798 | 1636 | private function checkTypeMap() : void |
|
| 808 | } |
||
| 809 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: