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 | 1580 | */ |
|
| 137 | private $filterCollection; |
||
| 138 | 1580 | ||
| 139 | 1580 | /** |
|
| 140 | 1580 | * Creates a new Document that operates on the given Mongo connection |
|
| 141 | * and uses the given Configuration. |
||
| 142 | 1580 | * |
|
| 143 | 1580 | */ |
|
| 144 | 1580 | protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
|
| 181 | 1 | ||
| 182 | /** |
||
| 183 | * Gets the proxy factory used by the DocumentManager to create document proxies. |
||
| 184 | * |
||
| 185 | * @return ProxyFactory |
||
| 186 | */ |
||
| 187 | public function getProxyFactory() |
||
| 191 | 1580 | ||
| 192 | /** |
||
| 193 | 1580 | * 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 | public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
||
| 203 | 1623 | ||
| 204 | /** |
||
| 205 | * Gets the EventManager used by the DocumentManager. |
||
| 206 | * |
||
| 207 | * @return EventManager |
||
| 208 | */ |
||
| 209 | public function getEventManager() |
||
| 213 | 1580 | ||
| 214 | /** |
||
| 215 | * Gets the MongoDB client instance that this DocumentManager wraps. |
||
| 216 | * |
||
| 217 | * @return Client |
||
| 218 | */ |
||
| 219 | public function getClient() |
||
| 223 | 1580 | ||
| 224 | /** |
||
| 225 | * Gets the metadata factory used to gather the metadata of classes. |
||
| 226 | * |
||
| 227 | * @return ClassMetadataFactory |
||
| 228 | */ |
||
| 229 | 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 | 1586 | ||
| 246 | /** |
||
| 247 | * Gets the UnitOfWork used by the DocumentManager to coordinate operations. |
||
| 248 | * |
||
| 249 | * @return UnitOfWork |
||
| 250 | */ |
||
| 251 | public function getUnitOfWork() |
||
| 255 | |||
| 256 | 66 | /** |
|
| 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 | public function getHydratorFactory() |
||
| 266 | 19 | ||
| 267 | /** |
||
| 268 | * Returns SchemaManager, used to create/drop indexes/collections/databases. |
||
| 269 | * |
||
| 270 | * @return SchemaManager |
||
| 271 | */ |
||
| 272 | public function getSchemaManager() |
||
| 276 | 1319 | ||
| 277 | /** |
||
| 278 | 1319 | * Returns the metadata for a class. |
|
| 279 | * |
||
| 280 | * @param string $className The class name. |
||
| 281 | * @return ClassMetadata |
||
| 282 | * @internal Performance-sensitive method. |
||
| 283 | */ |
||
| 284 | public function getClassMetadata($className) |
||
| 288 | |||
| 289 | 1253 | /** |
|
| 290 | * Returns the MongoDB instance for a class. |
||
| 291 | 1253 | * |
|
| 292 | 36 | * @param string $className The class name. |
|
| 293 | * @return Database |
||
| 294 | */ |
||
| 295 | 1249 | 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 | 1255 | ||
| 322 | /** |
||
| 323 | 1255 | * Returns the collection instance for a class. |
|
| 324 | * |
||
| 325 | 1255 | * @param string $className The class name. |
|
| 326 | 1255 | * @throws MongoDBException When the $className param is not mapped to a collection. |
|
| 327 | * @return Collection |
||
| 328 | 1255 | */ |
|
| 329 | public function getDocumentCollection($className) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns the bucket instance for a class. |
||
| 361 | * |
||
| 362 | 178 | * @param string $className The class name. |
|
| 363 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
| 364 | 178 | */ |
|
| 365 | public function getDocumentBucket(string $className): Bucket |
||
| 394 | |||
| 395 | 578 | /** |
|
| 396 | 577 | * Gets the array of instantiated document collection instances. |
|
| 397 | 573 | * |
|
| 398 | * @return Collection[] |
||
| 399 | */ |
||
| 400 | public function getDocumentCollections() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Create a new Query instance for a class. |
||
| 407 | * |
||
| 408 | 23 | * @param string $documentName The document class name. |
|
| 409 | * @return Query\Builder |
||
| 410 | 23 | */ |
|
| 411 | 1 | public function createQueryBuilder($documentName = null) |
|
| 415 | 21 | ||
| 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 | public function createAggregationBuilder($documentName) |
||
| 426 | 23 | ||
| 427 | 1 | /** |
|
| 428 | * Tells the DocumentManager to make an instance managed and persistent. |
||
| 429 | 22 | * |
|
| 430 | 21 | * The document will be entered into the database at or before transaction |
|
| 431 | 20 | * 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 | public function persist($document) |
||
| 447 | |||
| 448 | 10 | /** |
|
| 449 | 10 | * 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 | public function remove($document) |
||
| 465 | |||
| 466 | 13 | /** |
|
| 467 | 12 | * 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 | public function refresh($document) |
||
| 481 | |||
| 482 | /** |
||
| 483 | 8 | * Detaches a document from the DocumentManager, causing a managed document to |
|
| 484 | 5 | * 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 | 1 | 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 | 326 | * @throws LockException |
|
| 507 | * @throws \InvalidArgumentException If the $document param is not an object. |
||
| 508 | 326 | * @return object The managed copy of the document. |
|
| 509 | */ |
||
| 510 | public function merge($document) |
||
| 518 | |||
| 519 | 550 | /** |
|
| 520 | * Acquire a lock on the given document. |
||
| 521 | 550 | * |
|
| 522 | 549 | * @param object $document |
|
| 523 | 546 | * @param int $lockMode |
|
| 524 | * @param int $lockVersion |
||
| 525 | * @throws \InvalidArgumentException |
||
| 526 | */ |
||
| 527 | public function lock($document, $lockMode, $lockVersion = null) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Releases a lock on the given document. |
||
| 537 | 126 | * |
|
| 538 | * @param object $document |
||
| 539 | * @throws \InvalidArgumentException If the $document param is not an object. |
||
| 540 | 126 | */ |
|
| 541 | 126 | public function unlock($document) |
|
| 548 | 97 | ||
| 549 | 97 | /** |
|
| 550 | * Gets the repository for a document class. |
||
| 551 | 97 | * |
|
| 552 | * @param string $documentName The name of the Document. |
||
| 553 | * @return ObjectRepository The repository. |
||
| 554 | */ |
||
| 555 | 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 | public function flush(array $options = []) |
||
| 573 | 1 | ||
| 574 | /** |
||
| 575 | 1 | * Gets a reference to the document identified by the given type and identifier |
|
| 576 | 1 | * without actually loading it. |
|
| 577 | * |
||
| 578 | * If partial objects are allowed, this method will return a partial object that only |
||
| 579 | 1 | * has its identifier populated. Otherwise a proxy is returned that automatically |
|
| 580 | * loads itself on first access. |
||
| 581 | * |
||
| 582 | 1 | * @param string $documentName |
|
| 583 | 1 | * @param string|object $identifier |
|
| 584 | 1 | * @return mixed|object The document reference. |
|
| 585 | */ |
||
| 586 | 1 | public function getReference($documentName, $identifier) |
|
| 602 | 182 | ||
| 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 | 371 | * 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 | 371 | * never be visible to the application (especially not event listeners) as it will |
|
| 616 | 371 | * 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 | public function getPartialReference($documentName, $identifier) |
||
| 637 | |||
| 638 | 3 | /** |
|
| 639 | * Finds a Document by its identifier. |
||
| 640 | * |
||
| 641 | 3 | * This is just a convenient shortcut for getRepository($documentName)->find($id). |
|
| 642 | 3 | * |
|
| 643 | 3 | * @param string $documentName |
|
| 644 | * @param mixed $identifier |
||
| 645 | * @param int $lockMode |
||
| 646 | * @param int $lockVersion |
||
| 647 | * @return object $document |
||
| 648 | */ |
||
| 649 | public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) |
||
| 653 | 717 | ||
| 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 | public function clear($documentName = null) |
||
| 666 | 221 | ||
| 667 | /** |
||
| 668 | 221 | * 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 | 221 | public function close() |
|
| 677 | 1 | ||
| 678 | /** |
||
| 679 | * Determines whether a document instance is managed in this DocumentManager. |
||
| 680 | * |
||
| 681 | 220 | * @param object $document |
|
| 682 | 220 | * @throws \InvalidArgumentException When the $document param is not an object. |
|
| 683 | 220 | * @return bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
|
| 684 | */ |
||
| 685 | 46 | public function contains($document) |
|
| 694 | 19 | ||
| 695 | /** |
||
| 696 | * Gets the Configuration used by the DocumentManager. |
||
| 697 | * |
||
| 698 | 177 | * @return Configuration |
|
| 699 | 177 | */ |
|
| 700 | public function getConfiguration() |
||
| 704 | |||
| 705 | 17 | /** |
|
| 706 | 17 | * Returns a reference to the supplied document. |
|
| 707 | 17 | * |
|
| 708 | * @param object $document A document object |
||
| 709 | 17 | * @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 | 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 | private function errorIfClosed() |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Check if the Document manager is open or closed. |
||
| 811 | * |
||
| 812 | * @return bool |
||
| 813 | */ |
||
| 814 | public function isOpen() |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Gets the filter collection. |
||
| 821 | * |
||
| 822 | * @return FilterCollection The active filter collection. |
||
| 823 | */ |
||
| 824 | public function getFilterCollection() |
||
| 832 | } |
||
| 833 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.