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 |
||
| 39 | class DocumentManager implements ObjectManager |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * The Doctrine MongoDB connection instance. |
||
| 43 | * |
||
| 44 | * @var Client |
||
| 45 | */ |
||
| 46 | private $client; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The used Configuration. |
||
| 50 | * |
||
| 51 | * @var Configuration |
||
| 52 | */ |
||
| 53 | private $config; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The metadata factory, used to retrieve the ODM metadata of document classes. |
||
| 57 | * |
||
| 58 | * @var ClassMetadataFactory |
||
| 59 | */ |
||
| 60 | private $metadataFactory; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The UnitOfWork used to coordinate object-level transactions. |
||
| 64 | * |
||
| 65 | * @var UnitOfWork |
||
| 66 | */ |
||
| 67 | private $unitOfWork; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The event manager that is the central point of the event system. |
||
| 71 | * |
||
| 72 | * @var EventManager |
||
| 73 | */ |
||
| 74 | private $eventManager; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The Hydrator factory instance. |
||
| 78 | * |
||
| 79 | * @var HydratorFactory |
||
| 80 | */ |
||
| 81 | private $hydratorFactory; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * The Proxy factory instance. |
||
| 85 | * |
||
| 86 | * @var ProxyFactory |
||
| 87 | */ |
||
| 88 | private $proxyFactory; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The repository factory used to create dynamic repositories. |
||
| 92 | * |
||
| 93 | * @var RepositoryFactory |
||
| 94 | */ |
||
| 95 | private $repositoryFactory; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * SchemaManager instance |
||
| 99 | * |
||
| 100 | * @var SchemaManager |
||
| 101 | */ |
||
| 102 | private $schemaManager; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Array of cached document database instances that are lazily loaded. |
||
| 106 | * |
||
| 107 | * @var Database[] |
||
| 108 | */ |
||
| 109 | private $documentDatabases = []; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Array of cached document collection instances that are lazily loaded. |
||
| 113 | * |
||
| 114 | * @var Collection[] |
||
| 115 | */ |
||
| 116 | private $documentCollections = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Array of cached document bucket instances that are lazily loaded. |
||
| 120 | * |
||
| 121 | * @var Bucket[] |
||
| 122 | */ |
||
| 123 | private $documentBuckets = []; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Whether the DocumentManager is closed or not. |
||
| 127 | * |
||
| 128 | * @var bool |
||
| 129 | */ |
||
| 130 | private $closed = false; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Collection of query filters. |
||
| 134 | * |
||
| 135 | * @var FilterCollection |
||
| 136 | */ |
||
| 137 | private $filterCollection; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Creates a new Document that operates on the given Mongo connection |
||
| 141 | * and uses the given Configuration. |
||
| 142 | * |
||
| 143 | */ |
||
| 144 | 1602 | protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Gets the proxy factory used by the DocumentManager to create document proxies. |
||
| 184 | * |
||
| 185 | * @return ProxyFactory |
||
| 186 | */ |
||
| 187 | 1 | public function getProxyFactory() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Creates a new Document that operates on the given Mongo connection |
||
| 194 | * and uses the given Configuration. |
||
| 195 | * |
||
| 196 | * @static |
||
| 197 | * @return DocumentManager |
||
| 198 | */ |
||
| 199 | 1602 | public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Gets the EventManager used by the DocumentManager. |
||
| 206 | * |
||
| 207 | * @return EventManager |
||
| 208 | */ |
||
| 209 | 1665 | public function getEventManager() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Gets the MongoDB client instance that this DocumentManager wraps. |
||
| 216 | * |
||
| 217 | * @return Client |
||
| 218 | */ |
||
| 219 | 1602 | public function getClient() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Gets the metadata factory used to gather the metadata of classes. |
||
| 226 | * |
||
| 227 | * @return ClassMetadataFactory |
||
| 228 | */ |
||
| 229 | 1602 | public function getMetadataFactory() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 236 | * |
||
| 237 | * This method is a no-op for other objects. |
||
| 238 | * |
||
| 239 | * @param object $obj |
||
| 240 | */ |
||
| 241 | public function initializeObject($obj) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Gets the UnitOfWork used by the DocumentManager to coordinate operations. |
||
| 248 | * |
||
| 249 | * @return UnitOfWork |
||
| 250 | */ |
||
| 251 | 1609 | public function getUnitOfWork() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Gets the Hydrator factory used by the DocumentManager to generate and get hydrators |
||
| 258 | * for each type of document. |
||
| 259 | * |
||
| 260 | * @return HydratorFactory |
||
| 261 | */ |
||
| 262 | 69 | public function getHydratorFactory() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Returns SchemaManager, used to create/drop indexes/collections/databases. |
||
| 269 | * |
||
| 270 | * @return SchemaManager |
||
| 271 | */ |
||
| 272 | 19 | public function getSchemaManager() |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Returns the metadata for a class. |
||
| 279 | * |
||
| 280 | * @param string $className The class name. |
||
| 281 | * @return ClassMetadata |
||
| 282 | * @internal Performance-sensitive method. |
||
| 283 | */ |
||
| 284 | 1340 | public function getClassMetadata($className) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Returns the MongoDB instance for a class. |
||
| 291 | * |
||
| 292 | * @param string $className The class name. |
||
| 293 | * @return Database |
||
| 294 | */ |
||
| 295 | 1269 | public function getDocumentDatabase($className) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Gets the array of instantiated document database instances. |
||
| 314 | * |
||
| 315 | * @return Database[] |
||
| 316 | */ |
||
| 317 | public function getDocumentDatabases() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns the collection instance for a class. |
||
| 324 | * |
||
| 325 | * @param string $className The class name. |
||
| 326 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
| 327 | * @return Collection |
||
| 328 | */ |
||
| 329 | 1272 | public function getDocumentCollection($className) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Returns the bucket instance for a class. |
||
| 361 | * |
||
| 362 | * @param string $className The class name. |
||
| 363 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
| 364 | */ |
||
| 365 | 16 | public function getDocumentBucket(string $className): Bucket |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Gets the array of instantiated document collection instances. |
||
| 397 | * |
||
| 398 | * @return Collection[] |
||
| 399 | */ |
||
| 400 | public function getDocumentCollections() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Create a new Query instance for a class. |
||
| 407 | * |
||
| 408 | * @param string $documentName The document class name. |
||
| 409 | * @return Query\Builder |
||
| 410 | */ |
||
| 411 | 182 | public function createQueryBuilder($documentName = null) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Creates a new aggregation builder instance for a class. |
||
| 418 | * |
||
| 419 | * @param string $documentName The document class name. |
||
| 420 | * @return Aggregation\Builder |
||
| 421 | */ |
||
| 422 | 41 | public function createAggregationBuilder($documentName) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Tells the DocumentManager to make an instance managed and persistent. |
||
| 429 | * |
||
| 430 | * The document will be entered into the database at or before transaction |
||
| 431 | * commit or as a result of the flush operation. |
||
| 432 | * |
||
| 433 | * NOTE: The persist operation always considers documents that are not yet known to |
||
| 434 | * this DocumentManager as NEW. Do not pass detached documents to the persist operation. |
||
| 435 | * |
||
| 436 | * @param object $document The instance to make managed and persistent. |
||
| 437 | * @throws \InvalidArgumentException When the given $document param is not an object. |
||
| 438 | */ |
||
| 439 | 585 | public function persist($document) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Removes a document instance. |
||
| 450 | * |
||
| 451 | * A removed document will be removed from the database at or before transaction commit |
||
| 452 | * or as a result of the flush operation. |
||
| 453 | * |
||
| 454 | * @param object $document The document instance to remove. |
||
| 455 | * @throws \InvalidArgumentException When the $document param is not an object. |
||
| 456 | */ |
||
| 457 | 26 | public function remove($document) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Refreshes the persistent state of a document from the database, |
||
| 468 | * overriding any local changes that have not yet been persisted. |
||
| 469 | * |
||
| 470 | * @param object $document The document to refresh. |
||
| 471 | * @throws \InvalidArgumentException When the given $document param is not an object. |
||
| 472 | */ |
||
| 473 | 25 | public function refresh($document) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Detaches a document from the DocumentManager, causing a managed document to |
||
| 484 | * become detached. Unflushed changes made to the document if any |
||
| 485 | * (including removal of the document), will not be synchronized to the database. |
||
| 486 | * Documents which previously referenced the detached document will continue to |
||
| 487 | * reference it. |
||
| 488 | * |
||
| 489 | * @param object $document The document to detach. |
||
| 490 | * @throws \InvalidArgumentException When the $document param is not an object. |
||
| 491 | */ |
||
| 492 | 11 | public function detach($document) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Merges the state of a detached document into the persistence context |
||
| 502 | * of this DocumentManager and returns the managed copy of the document. |
||
| 503 | * The document passed to merge will not become associated/managed with this DocumentManager. |
||
| 504 | * |
||
| 505 | * @param object $document The detached document to merge into the persistence context. |
||
| 506 | * @throws LockException |
||
| 507 | * @throws \InvalidArgumentException If the $document param is not an object. |
||
| 508 | * @return object The managed copy of the document. |
||
| 509 | */ |
||
| 510 | 14 | public function merge($document) |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Acquire a lock on the given document. |
||
| 521 | * |
||
| 522 | * @param object $document |
||
| 523 | * @param int $lockMode |
||
| 524 | * @param int $lockVersion |
||
| 525 | * @throws \InvalidArgumentException |
||
| 526 | */ |
||
| 527 | 8 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Releases a lock on the given document. |
||
| 537 | * |
||
| 538 | * @param object $document |
||
| 539 | * @throws \InvalidArgumentException If the $document param is not an object. |
||
| 540 | */ |
||
| 541 | 1 | public function unlock($document) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Gets the repository for a document class. |
||
| 551 | * |
||
| 552 | * @param string $documentName The name of the Document. |
||
| 553 | * @return ObjectRepository The repository. |
||
| 554 | */ |
||
| 555 | 337 | public function getRepository($documentName) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Flushes all changes to objects that have been queued up to now to the database. |
||
| 562 | * This effectively synchronizes the in-memory state of managed objects with the |
||
| 563 | * database. |
||
| 564 | * |
||
| 565 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 566 | * @throws \InvalidArgumentException |
||
| 567 | */ |
||
| 568 | 557 | public function flush(array $options = []) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Gets a reference to the document identified by the given type and identifier |
||
| 576 | * without actually loading it. |
||
| 577 | * |
||
| 578 | * If partial objects are allowed, this method will return a partial object that only |
||
| 579 | * has its identifier populated. Otherwise a proxy is returned that automatically |
||
| 580 | * loads itself on first access. |
||
| 581 | * |
||
| 582 | * @param string $documentName |
||
| 583 | * @param string|object $identifier |
||
| 584 | * @return mixed|object The document reference. |
||
| 585 | */ |
||
| 586 | 133 | public function getReference($documentName, $identifier) |
|
| 602 | |||
| 603 | /** |
||
| 604 | * Gets a partial reference to the document identified by the given type and identifier |
||
| 605 | * without actually loading it, if the document is not yet loaded. |
||
| 606 | * |
||
| 607 | * The returned reference may be a partial object if the document is not yet loaded/managed. |
||
| 608 | * If it is a partial object it will not initialize the rest of the document state on access. |
||
| 609 | * Thus you can only ever safely access the identifier of a document obtained through |
||
| 610 | * this method. |
||
| 611 | * |
||
| 612 | * The use-cases for partial references involve maintaining bidirectional associations |
||
| 613 | * without loading one side of the association or to update a document without loading it. |
||
| 614 | * Note, however, that in the latter case the original (persistent) document data will |
||
| 615 | * never be visible to the application (especially not event listeners) as it will |
||
| 616 | * never be loaded in the first place. |
||
| 617 | * |
||
| 618 | * @param string $documentName The name of the document type. |
||
| 619 | * @param mixed $identifier The document identifier. |
||
| 620 | * @return object The (partial) document reference. |
||
| 621 | */ |
||
| 622 | 1 | public function getPartialReference($documentName, $identifier) |
|
| 637 | |||
| 638 | /** |
||
| 639 | * Finds a Document by its identifier. |
||
| 640 | * |
||
| 641 | * This is just a convenient shortcut for getRepository($documentName)->find($id). |
||
| 642 | * |
||
| 643 | * @param string $documentName |
||
| 644 | * @param mixed $identifier |
||
| 645 | * @param int $lockMode |
||
| 646 | * @param int $lockVersion |
||
| 647 | * @return object $document |
||
| 648 | */ |
||
| 649 | 182 | public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Clears the DocumentManager. |
||
| 656 | * |
||
| 657 | * All documents that are currently managed by this DocumentManager become |
||
| 658 | * detached. |
||
| 659 | * |
||
| 660 | * @param string|null $documentName if given, only documents of this type will get detached |
||
| 661 | */ |
||
| 662 | 372 | public function clear($documentName = null) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * Closes the DocumentManager. All documents that are currently managed |
||
| 669 | * by this DocumentManager become detached. The DocumentManager may no longer |
||
| 670 | * be used after it is closed. |
||
| 671 | */ |
||
| 672 | 6 | public function close() |
|
| 677 | |||
| 678 | /** |
||
| 679 | * Determines whether a document instance is managed in this DocumentManager. |
||
| 680 | * |
||
| 681 | * @param object $document |
||
| 682 | * @throws \InvalidArgumentException When the $document param is not an object. |
||
| 683 | * @return bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
||
| 684 | */ |
||
| 685 | 3 | public function contains($document) |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Gets the Configuration used by the DocumentManager. |
||
| 697 | * |
||
| 698 | * @return Configuration |
||
| 699 | */ |
||
| 700 | 753 | public function getConfiguration() |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Returns a reference to the supplied document. |
||
| 707 | * |
||
| 708 | * @param object $document A document object |
||
| 709 | * @param array $referenceMapping Mapping for the field that references the document |
||
| 710 | * |
||
| 711 | * @throws \InvalidArgumentException |
||
| 712 | * @throws MappingException |
||
| 713 | * @return mixed The reference for the document in question, according to the desired mapping |
||
| 714 | */ |
||
| 715 | 225 | public function createReference($document, array $referenceMapping) |
|
| 796 | |||
| 797 | /** |
||
| 798 | * Throws an exception if the DocumentManager is closed or currently not active. |
||
| 799 | * |
||
| 800 | * @throws MongoDBException If the DocumentManager is closed. |
||
| 801 | */ |
||
| 802 | 590 | private function errorIfClosed() |
|
| 808 | |||
| 809 | /** |
||
| 810 | * Check if the Document manager is open or closed. |
||
| 811 | * |
||
| 812 | * @return bool |
||
| 813 | */ |
||
| 814 | 1 | public function isOpen() |
|
| 818 | |||
| 819 | /** |
||
| 820 | * Gets the filter collection. |
||
| 821 | * |
||
| 822 | * @return FilterCollection The active filter collection. |
||
| 823 | */ |
||
| 824 | 517 | public function getFilterCollection() |
|
| 832 | } |
||
| 833 |
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: