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 |
||
| 42 | class DocumentManager implements ObjectManager |
||
| 43 | { |
||
| 44 | public const CLIENT_TYPEMAP = ['root' => 'array', 'document' => 'array']; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The Doctrine MongoDB connection instance. |
||
| 48 | * |
||
| 49 | * @var Client |
||
| 50 | */ |
||
| 51 | private $client; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The used Configuration. |
||
| 55 | * |
||
| 56 | * @var Configuration |
||
| 57 | */ |
||
| 58 | private $config; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The metadata factory, used to retrieve the ODM metadata of document classes. |
||
| 62 | * |
||
| 63 | * @var ClassMetadataFactory |
||
| 64 | */ |
||
| 65 | private $metadataFactory; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The UnitOfWork used to coordinate object-level transactions. |
||
| 69 | * |
||
| 70 | * @var UnitOfWork |
||
| 71 | */ |
||
| 72 | private $unitOfWork; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The event manager that is the central point of the event system. |
||
| 76 | * |
||
| 77 | * @var EventManager |
||
| 78 | */ |
||
| 79 | private $eventManager; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The Hydrator factory instance. |
||
| 83 | * |
||
| 84 | * @var HydratorFactory |
||
| 85 | */ |
||
| 86 | private $hydratorFactory; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The Proxy factory instance. |
||
| 90 | * |
||
| 91 | * @var ProxyFactory |
||
| 92 | */ |
||
| 93 | private $proxyFactory; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * The repository factory used to create dynamic repositories. |
||
| 97 | * |
||
| 98 | * @var RepositoryFactory |
||
| 99 | */ |
||
| 100 | private $repositoryFactory; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * SchemaManager instance |
||
| 104 | * |
||
| 105 | * @var SchemaManager |
||
| 106 | */ |
||
| 107 | private $schemaManager; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Array of cached document database instances that are lazily loaded. |
||
| 111 | * |
||
| 112 | * @var Database[] |
||
| 113 | */ |
||
| 114 | private $documentDatabases = []; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Array of cached document collection instances that are lazily loaded. |
||
| 118 | * |
||
| 119 | * @var Collection[] |
||
| 120 | */ |
||
| 121 | private $documentCollections = []; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Array of cached document bucket instances that are lazily loaded. |
||
| 125 | * |
||
| 126 | * @var Bucket[] |
||
| 127 | */ |
||
| 128 | private $documentBuckets = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Whether the DocumentManager is closed or not. |
||
| 132 | * |
||
| 133 | * @var bool |
||
| 134 | */ |
||
| 135 | private $closed = false; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Collection of query filters. |
||
| 139 | * |
||
| 140 | * @var FilterCollection |
||
| 141 | */ |
||
| 142 | private $filterCollection; |
||
| 143 | |||
| 144 | /** @var ClassNameResolver */ |
||
| 145 | private $classNameResolver; |
||
| 146 | 1636 | ||
| 147 | /** |
||
| 148 | 1636 | * Creates a new Document that operates on the given Mongo connection |
|
| 149 | 1636 | * and uses the given Configuration. |
|
| 150 | 1636 | */ |
|
| 151 | protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Gets the proxy factory used by the DocumentManager to create document proxies. |
||
| 189 | 1 | */ |
|
| 190 | public function getProxyFactory() : ProxyFactory |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Creates a new Document that operates on the given Mongo connection |
||
| 197 | * and uses the given Configuration. |
||
| 198 | 1636 | */ |
|
| 199 | public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) : DocumentManager |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Gets the EventManager used by the DocumentManager. |
||
| 206 | 1699 | */ |
|
| 207 | public function getEventManager() : EventManager |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Gets the MongoDB client instance that this DocumentManager wraps. |
||
| 214 | 1636 | */ |
|
| 215 | public function getClient() : Client |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Gets the metadata factory used to gather the metadata of classes. |
||
| 222 | * |
||
| 223 | * @return ClassMetadataFactory |
||
| 224 | 1636 | */ |
|
| 225 | public function getMetadataFactory() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 232 | * |
||
| 233 | * This method is a no-op for other objects. |
||
| 234 | * |
||
| 235 | * @param object $obj |
||
| 236 | */ |
||
| 237 | public function initializeObject($obj) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Gets the UnitOfWork used by the DocumentManager to coordinate operations. |
||
| 244 | 1643 | */ |
|
| 245 | public function getUnitOfWork() : UnitOfWork |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Gets the Hydrator factory used by the DocumentManager to generate and get hydrators |
||
| 252 | * for each type of document. |
||
| 253 | 70 | */ |
|
| 254 | public function getHydratorFactory() : HydratorFactory |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Returns SchemaManager, used to create/drop indexes/collections/databases. |
||
| 261 | 28 | */ |
|
| 262 | public function getSchemaManager() : SchemaManager |
||
| 266 | |||
| 267 | /** Returns the class name resolver which is used to resolve real class names for proxy objects. */ |
||
| 268 | public function getClassNameResolver() : ClassNameResolver |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Returns the metadata for a class. |
||
| 275 | 1371 | * |
|
| 276 | * @internal Performance-sensitive method. |
||
| 277 | 1371 | * |
|
| 278 | * @param string $className The class name. |
||
| 279 | */ |
||
| 280 | public function getClassMetadata($className) : ClassMetadata |
||
| 284 | |||
| 285 | 1300 | /** |
|
| 286 | * Returns the MongoDB instance for a class. |
||
| 287 | 1300 | */ |
|
| 288 | 46 | public function getDocumentDatabase(string $className) : Database |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Gets the array of instantiated document database instances. |
||
| 307 | * |
||
| 308 | * @return Database[] |
||
| 309 | */ |
||
| 310 | public function getDocumentDatabases() : array |
||
| 314 | |||
| 315 | 1303 | /** |
|
| 316 | * Returns the collection instance for a class. |
||
| 317 | 1303 | * |
|
| 318 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
| 319 | */ |
||
| 320 | 1303 | public function getDocumentCollection(string $className) : Collection |
|
| 349 | |||
| 350 | 16 | /** |
|
| 351 | * Returns the bucket instance for a class. |
||
| 352 | 16 | * |
|
| 353 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
| 354 | */ |
||
| 355 | 16 | public function getDocumentBucket(string $className) : Bucket |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Gets the array of instantiated document collection instances. |
||
| 387 | * |
||
| 388 | * @return Collection[] |
||
| 389 | */ |
||
| 390 | public function getDocumentCollections() : array |
||
| 394 | |||
| 395 | 182 | /** |
|
| 396 | * Create a new Query instance for a class. |
||
| 397 | 182 | * |
|
| 398 | * @param string[]|string|null $documentName (optional) an array of document names, the document name, or none |
||
| 399 | */ |
||
| 400 | public function createQueryBuilder($documentName = null) : Query\Builder |
||
| 404 | |||
| 405 | 41 | /** |
|
| 406 | * Creates a new aggregation builder instance for a class. |
||
| 407 | */ |
||
| 408 | public function createAggregationBuilder(string $documentName) : Aggregation\Builder |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Tells the DocumentManager to make an instance managed and persistent. |
||
| 415 | * |
||
| 416 | * The document will be entered into the database at or before transaction |
||
| 417 | * commit or as a result of the flush operation. |
||
| 418 | * |
||
| 419 | * NOTE: The persist operation always considers documents that are not yet known to |
||
| 420 | * this DocumentManager as NEW. Do not pass detached documents to the persist operation. |
||
| 421 | 615 | * |
|
| 422 | * @param object $document The instance to make managed and persistent. |
||
| 423 | 615 | * |
|
| 424 | 1 | * @throws InvalidArgumentException When the given $document param is not an object. |
|
| 425 | */ |
||
| 426 | 614 | public function persist($document) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Removes a document instance. |
||
| 437 | * |
||
| 438 | * A removed document will be removed from the database at or before transaction commit |
||
| 439 | * or as a result of the flush operation. |
||
| 440 | 27 | * |
|
| 441 | * @param object $document The document instance to remove. |
||
| 442 | 27 | * |
|
| 443 | 1 | * @throws InvalidArgumentException When the $document param is not an object. |
|
| 444 | */ |
||
| 445 | 26 | public function remove($document) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Refreshes the persistent state of a document from the database, |
||
| 456 | * overriding any local changes that have not yet been persisted. |
||
| 457 | 26 | * |
|
| 458 | * @param object $document The document to refresh. |
||
| 459 | 26 | * |
|
| 460 | 1 | * @throws InvalidArgumentException When the given $document param is not an object. |
|
| 461 | */ |
||
| 462 | 25 | public function refresh($document) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Detaches a document from the DocumentManager, causing a managed document to |
||
| 473 | * become detached. Unflushed changes made to the document if any |
||
| 474 | * (including removal of the document), will not be synchronized to the database. |
||
| 475 | * Documents which previously referenced the detached document will continue to |
||
| 476 | * reference it. |
||
| 477 | 11 | * |
|
| 478 | * @param object $document The document to detach. |
||
| 479 | 11 | * |
|
| 480 | 1 | * @throws InvalidArgumentException When the $document param is not an object. |
|
| 481 | */ |
||
| 482 | 10 | public function detach($document) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Merges the state of a detached document into the persistence context |
||
| 492 | * of this DocumentManager and returns the managed copy of the document. |
||
| 493 | * The document passed to merge will not become associated/managed with this DocumentManager. |
||
| 494 | * |
||
| 495 | * @param object $document The detached document to merge into the persistence context. |
||
| 496 | * |
||
| 497 | 14 | * @return object The managed copy of the document. |
|
| 498 | * |
||
| 499 | 14 | * @throws LockException |
|
| 500 | 1 | * @throws InvalidArgumentException If the $document param is not an object. |
|
| 501 | */ |
||
| 502 | 13 | public function merge($document) |
|
| 510 | |||
| 511 | /** |
||
| 512 | 8 | * Acquire a lock on the given document. |
|
| 513 | * |
||
| 514 | 8 | * @throws InvalidArgumentException |
|
| 515 | 5 | * @throws LockException |
|
| 516 | */ |
||
| 517 | public function lock(object $document, int $lockMode, ?int $lockVersion = null) : void |
||
| 521 | |||
| 522 | 1 | /** |
|
| 523 | 1 | * Releases a lock on the given document. |
|
| 524 | */ |
||
| 525 | public function unlock(object $document) : void |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Gets the repository for a document class. |
||
| 532 | 358 | * |
|
| 533 | * @param string $documentName The name of the Document. |
||
| 534 | 358 | * |
|
| 535 | * @return ObjectRepository The repository. |
||
| 536 | */ |
||
| 537 | public function getRepository($documentName) |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Flushes all changes to objects that have been queued up to now to the database. |
||
| 544 | * This effectively synchronizes the in-memory state of managed objects with the |
||
| 545 | * database. |
||
| 546 | 587 | * |
|
| 547 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 548 | 587 | * |
|
| 549 | 586 | * @throws MongoDBException |
|
| 550 | 583 | */ |
|
| 551 | public function flush(array $options = []) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Gets a reference to the document identified by the given type and identifier |
||
| 559 | * without actually loading it. |
||
| 560 | * |
||
| 561 | * If partial objects are allowed, this method will return a partial object that only |
||
| 562 | 134 | * has its identifier populated. Otherwise a proxy is returned that automatically |
|
| 563 | * loads itself on first access. |
||
| 564 | * |
||
| 565 | 134 | * @param string|object $identifier |
|
| 566 | 134 | */ |
|
| 567 | public function getReference(string $documentName, $identifier) : object |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Gets a partial reference to the document identified by the given type and identifier |
||
| 586 | * without actually loading it, if the document is not yet loaded. |
||
| 587 | * |
||
| 588 | * The returned reference may be a partial object if the document is not yet loaded/managed. |
||
| 589 | * If it is a partial object it will not initialize the rest of the document state on access. |
||
| 590 | * Thus you can only ever safely access the identifier of a document obtained through |
||
| 591 | * this method. |
||
| 592 | * |
||
| 593 | * The use-cases for partial references involve maintaining bidirectional associations |
||
| 594 | * without loading one side of the association or to update a document without loading it. |
||
| 595 | * Note, however, that in the latter case the original (persistent) document data will |
||
| 596 | 1 | * never be visible to the application (especially not event listeners) as it will |
|
| 597 | * never be loaded in the first place. |
||
| 598 | 1 | * |
|
| 599 | 1 | * @param mixed $identifier The document identifier. |
|
| 600 | */ |
||
| 601 | public function getPartialReference(string $documentName, $identifier) : object |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Finds a Document by its identifier. |
||
| 619 | * |
||
| 620 | * This is just a convenient shortcut for getRepository($documentName)->find($id). |
||
| 621 | * |
||
| 622 | * @param string $documentName |
||
| 623 | * @param mixed $identifier |
||
| 624 | 187 | * @param int $lockMode |
|
| 625 | * @param int $lockVersion |
||
| 626 | 187 | * |
|
| 627 | * @return object $document |
||
| 628 | */ |
||
| 629 | public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Clears the DocumentManager. |
||
| 636 | * |
||
| 637 | 390 | * All documents that are currently managed by this DocumentManager become |
|
| 638 | * detached. |
||
| 639 | 390 | * |
|
| 640 | 390 | * @param string|null $documentName if given, only documents of this type will get detached |
|
| 641 | */ |
||
| 642 | public function clear($documentName = null) |
||
| 646 | |||
| 647 | 6 | /** |
|
| 648 | * Closes the DocumentManager. All documents that are currently managed |
||
| 649 | 6 | * by this DocumentManager become detached. The DocumentManager may no longer |
|
| 650 | 6 | * be used after it is closed. |
|
| 651 | 6 | */ |
|
| 652 | public function close() |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Determines whether a document instance is managed in this DocumentManager. |
||
| 660 | * |
||
| 661 | * @param object $document |
||
| 662 | 3 | * |
|
| 663 | * @return bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
||
| 664 | 3 | * |
|
| 665 | * @throws InvalidArgumentException When the $document param is not an object. |
||
| 666 | */ |
||
| 667 | 3 | public function contains($document) |
|
| 676 | |||
| 677 | 783 | /** |
|
| 678 | * Gets the Configuration used by the DocumentManager. |
||
| 679 | */ |
||
| 680 | public function getConfiguration() : Configuration |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Returns a reference to the supplied document. |
||
| 687 | * |
||
| 688 | 226 | * @return mixed The reference for the document in question, according to the desired mapping |
|
| 689 | * |
||
| 690 | 226 | * @throws MappingException |
|
| 691 | 226 | * @throws RuntimeException |
|
| 692 | */ |
||
| 693 | 226 | public function createReference(object $document, array $referenceMapping) |
|
| 770 | |||
| 771 | 620 | /** |
|
| 772 | * Throws an exception if the DocumentManager is closed or currently not active. |
||
| 773 | 620 | * |
|
| 774 | 5 | * @throws MongoDBException If the DocumentManager is closed. |
|
| 775 | */ |
||
| 776 | 615 | private function errorIfClosed() : void |
|
| 782 | |||
| 783 | 1 | /** |
|
| 784 | * Check if the Document manager is open or closed. |
||
| 785 | */ |
||
| 786 | public function isOpen() : bool |
||
| 790 | |||
| 791 | 537 | /** |
|
| 792 | 537 | * Gets the filter collection. |
|
| 793 | */ |
||
| 794 | public function getFilterCollection() : FilterCollection |
||
| 802 | 1636 | ||
| 803 | 1636 | private function checkTypeMap() : void |
|
| 813 | } |
||
| 814 |
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: