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 |
||
| 38 | class DocumentManager implements ObjectManager |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * The Doctrine MongoDB connection instance. |
||
| 42 | * |
||
| 43 | * @var Client |
||
| 44 | */ |
||
| 45 | private $client; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The used Configuration. |
||
| 49 | * |
||
| 50 | * @var Configuration |
||
| 51 | */ |
||
| 52 | private $config; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The metadata factory, used to retrieve the ODM metadata of document classes. |
||
| 56 | * |
||
| 57 | * @var ClassMetadataFactory |
||
| 58 | */ |
||
| 59 | private $metadataFactory; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The UnitOfWork used to coordinate object-level transactions. |
||
| 63 | * |
||
| 64 | * @var UnitOfWork |
||
| 65 | */ |
||
| 66 | private $unitOfWork; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The event manager that is the central point of the event system. |
||
| 70 | * |
||
| 71 | * @var EventManager |
||
| 72 | */ |
||
| 73 | private $eventManager; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The Hydrator factory instance. |
||
| 77 | * |
||
| 78 | * @var HydratorFactory |
||
| 79 | */ |
||
| 80 | private $hydratorFactory; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The Proxy factory instance. |
||
| 84 | * |
||
| 85 | * @var ProxyFactory |
||
| 86 | */ |
||
| 87 | private $proxyFactory; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The repository factory used to create dynamic repositories. |
||
| 91 | * |
||
| 92 | * @var RepositoryFactory |
||
| 93 | */ |
||
| 94 | private $repositoryFactory; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * SchemaManager instance |
||
| 98 | * |
||
| 99 | * @var SchemaManager |
||
| 100 | */ |
||
| 101 | private $schemaManager; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Array of cached document database instances that are lazily loaded. |
||
| 105 | * |
||
| 106 | * @var Database[] |
||
| 107 | */ |
||
| 108 | private $documentDatabases = []; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Array of cached document collection instances that are lazily loaded. |
||
| 112 | * |
||
| 113 | * @var Collection[] |
||
| 114 | */ |
||
| 115 | private $documentCollections = []; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Whether the DocumentManager is closed or not. |
||
| 119 | * |
||
| 120 | * @var bool |
||
| 121 | */ |
||
| 122 | private $closed = false; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Collection of query filters. |
||
| 126 | * |
||
| 127 | * @var FilterCollection |
||
| 128 | */ |
||
| 129 | private $filterCollection; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Creates a new Document that operates on the given Mongo connection |
||
| 133 | * and uses the given Configuration. |
||
| 134 | * |
||
| 135 | */ |
||
| 136 | 1584 | protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Gets the proxy factory used by the DocumentManager to create document proxies. |
||
| 176 | * |
||
| 177 | * @return ProxyFactory |
||
| 178 | */ |
||
| 179 | 1 | public function getProxyFactory() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Creates a new Document that operates on the given Mongo connection |
||
| 186 | * and uses the given Configuration. |
||
| 187 | * |
||
| 188 | * @static |
||
| 189 | * @return DocumentManager |
||
| 190 | */ |
||
| 191 | 1584 | public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Gets the EventManager used by the DocumentManager. |
||
| 198 | * |
||
| 199 | * @return EventManager |
||
| 200 | */ |
||
| 201 | 1627 | public function getEventManager() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Gets the MongoDB client instance that this DocumentManager wraps. |
||
| 208 | * |
||
| 209 | * @return Client |
||
| 210 | */ |
||
| 211 | 1584 | public function getClient() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Gets the metadata factory used to gather the metadata of classes. |
||
| 218 | * |
||
| 219 | * @return ClassMetadataFactory |
||
| 220 | */ |
||
| 221 | 1584 | public function getMetadataFactory() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 228 | * |
||
| 229 | * This method is a no-op for other objects. |
||
| 230 | * |
||
| 231 | * @param object $obj |
||
| 232 | */ |
||
| 233 | public function initializeObject($obj) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Gets the UnitOfWork used by the DocumentManager to coordinate operations. |
||
| 240 | * |
||
| 241 | * @return UnitOfWork |
||
| 242 | */ |
||
| 243 | 1590 | public function getUnitOfWork() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Gets the Hydrator factory used by the DocumentManager to generate and get hydrators |
||
| 250 | * for each type of document. |
||
| 251 | * |
||
| 252 | * @return HydratorFactory |
||
| 253 | */ |
||
| 254 | 66 | public function getHydratorFactory() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Returns SchemaManager, used to create/drop indexes/collections/databases. |
||
| 261 | * |
||
| 262 | * @return SchemaManager |
||
| 263 | */ |
||
| 264 | 19 | public function getSchemaManager() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Returns the metadata for a class. |
||
| 271 | * |
||
| 272 | * @param string $className The class name. |
||
| 273 | * @return ClassMetadata |
||
| 274 | * @internal Performance-sensitive method. |
||
| 275 | */ |
||
| 276 | 1323 | public function getClassMetadata($className) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Returns the MongoDB instance for a class. |
||
| 283 | * |
||
| 284 | * @param string $className The class name. |
||
| 285 | * @return Database |
||
| 286 | */ |
||
| 287 | 1257 | public function getDocumentDatabase($className) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Gets the array of instantiated document database instances. |
||
| 307 | * |
||
| 308 | * @return Database[] |
||
| 309 | */ |
||
| 310 | public function getDocumentDatabases() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Returns the MongoCollection instance for a class. |
||
| 317 | * |
||
| 318 | * @param string $className The class name. |
||
| 319 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
| 320 | * @return Collection |
||
| 321 | 1259 | */ |
|
| 322 | public function getDocumentCollection($className) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Gets the array of instantiated document collection instances. |
||
| 350 | * |
||
| 351 | * @return Collection[] |
||
| 352 | */ |
||
| 353 | public function getDocumentCollections() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Create a new Query instance for a class. |
||
| 360 | * |
||
| 361 | * @param string $documentName The document class name. |
||
| 362 | 180 | * @return Query\Builder |
|
| 363 | */ |
||
| 364 | 180 | public function createQueryBuilder($documentName = null) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Creates a new aggregation builder instance for a class. |
||
| 371 | * |
||
| 372 | * @param string $documentName The document class name. |
||
| 373 | 41 | * @return Aggregation\Builder |
|
| 374 | */ |
||
| 375 | 41 | public function createAggregationBuilder($documentName) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Tells the DocumentManager to make an instance managed and persistent. |
||
| 382 | * |
||
| 383 | * The document will be entered into the database at or before transaction |
||
| 384 | * commit or as a result of the flush operation. |
||
| 385 | * |
||
| 386 | * NOTE: The persist operation always considers documents that are not yet known to |
||
| 387 | * this DocumentManager as NEW. Do not pass detached documents to the persist operation. |
||
| 388 | * |
||
| 389 | * @param object $document The instance to make managed and persistent. |
||
| 390 | 582 | * @throws \InvalidArgumentException When the given $document param is not an object. |
|
| 391 | */ |
||
| 392 | 582 | public function persist($document) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Removes a document instance. |
||
| 403 | * |
||
| 404 | * A removed document will be removed from the database at or before transaction commit |
||
| 405 | * or as a result of the flush operation. |
||
| 406 | * |
||
| 407 | * @param object $document The document instance to remove. |
||
| 408 | 25 | * @throws \InvalidArgumentException When the $document param is not an object. |
|
| 409 | */ |
||
| 410 | 25 | public function remove($document) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Refreshes the persistent state of a document from the database, |
||
| 421 | * overriding any local changes that have not yet been persisted. |
||
| 422 | * |
||
| 423 | * @param object $document The document to refresh. |
||
| 424 | 23 | * @throws \InvalidArgumentException When the given $document param is not an object. |
|
| 425 | */ |
||
| 426 | 23 | public function refresh($document) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Detaches a document from the DocumentManager, causing a managed document to |
||
| 437 | * become detached. Unflushed changes made to the document if any |
||
| 438 | * (including removal of the document), will not be synchronized to the database. |
||
| 439 | * Documents which previously referenced the detached document will continue to |
||
| 440 | * reference it. |
||
| 441 | * |
||
| 442 | * @param object $document The document to detach. |
||
| 443 | 11 | * @throws \InvalidArgumentException When the $document param is not an object. |
|
| 444 | */ |
||
| 445 | 11 | public function detach($document) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Merges the state of a detached document into the persistence context |
||
| 455 | * of this DocumentManager and returns the managed copy of the document. |
||
| 456 | * The document passed to merge will not become associated/managed with this DocumentManager. |
||
| 457 | * |
||
| 458 | * @param object $document The detached document to merge into the persistence context. |
||
| 459 | * @throws LockException |
||
| 460 | * @throws \InvalidArgumentException If the $document param is not an object. |
||
| 461 | 14 | * @return object The managed copy of the document. |
|
| 462 | */ |
||
| 463 | 14 | public function merge($document) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Acquire a lock on the given document. |
||
| 474 | * |
||
| 475 | * @param object $document |
||
| 476 | * @param int $lockMode |
||
| 477 | * @param int $lockVersion |
||
| 478 | 8 | * @throws \InvalidArgumentException |
|
| 479 | */ |
||
| 480 | 8 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Releases a lock on the given document. |
||
| 490 | * |
||
| 491 | * @param object $document |
||
| 492 | 1 | * @throws \InvalidArgumentException If the $document param is not an object. |
|
| 493 | */ |
||
| 494 | 1 | public function unlock($document) |
|
| 501 | |||
| 502 | /** |
||
| 503 | * Gets the repository for a document class. |
||
| 504 | * |
||
| 505 | * @param string $documentName The name of the Document. |
||
| 506 | 328 | * @return ObjectRepository The repository. |
|
| 507 | */ |
||
| 508 | 328 | public function getRepository($documentName) |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Flushes all changes to objects that have been queued up to now to the database. |
||
| 515 | * This effectively synchronizes the in-memory state of managed objects with the |
||
| 516 | * database. |
||
| 517 | * |
||
| 518 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 519 | 553 | * @throws \InvalidArgumentException |
|
| 520 | */ |
||
| 521 | 553 | public function flush(array $options = []) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Gets a reference to the document identified by the given type and identifier |
||
| 529 | * without actually loading it. |
||
| 530 | * |
||
| 531 | * If partial objects are allowed, this method will return a partial object that only |
||
| 532 | * has its identifier populated. Otherwise a proxy is returned that automatically |
||
| 533 | * loads itself on first access. |
||
| 534 | * |
||
| 535 | * @param string $documentName |
||
| 536 | * @param string|object $identifier |
||
| 537 | 126 | * @return mixed|object The document reference. |
|
| 538 | */ |
||
| 539 | public function getReference($documentName, $identifier) |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Gets a partial reference to the document identified by the given type and identifier |
||
| 558 | * without actually loading it, if the document is not yet loaded. |
||
| 559 | * |
||
| 560 | * The returned reference may be a partial object if the document is not yet loaded/managed. |
||
| 561 | * If it is a partial object it will not initialize the rest of the document state on access. |
||
| 562 | * Thus you can only ever safely access the identifier of a document obtained through |
||
| 563 | * this method. |
||
| 564 | * |
||
| 565 | * The use-cases for partial references involve maintaining bidirectional associations |
||
| 566 | * without loading one side of the association or to update a document without loading it. |
||
| 567 | * Note, however, that in the latter case the original (persistent) document data will |
||
| 568 | * never be visible to the application (especially not event listeners) as it will |
||
| 569 | * never be loaded in the first place. |
||
| 570 | * |
||
| 571 | * @param string $documentName The name of the document type. |
||
| 572 | * @param mixed $identifier The document identifier. |
||
| 573 | 1 | * @return object The (partial) document reference. |
|
| 574 | */ |
||
| 575 | 1 | public function getPartialReference($documentName, $identifier) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Finds a Document by its identifier. |
||
| 594 | * |
||
| 595 | * This is just a convenient shortcut for getRepository($documentName)->find($id). |
||
| 596 | * |
||
| 597 | * @param string $documentName |
||
| 598 | * @param mixed $identifier |
||
| 599 | * @param int $lockMode |
||
| 600 | 182 | * @param int $lockVersion |
|
| 601 | * @return object $document |
||
| 602 | 182 | */ |
|
| 603 | public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) |
||
| 612 | |||
| 613 | 371 | /** |
|
| 614 | * Clears the DocumentManager. |
||
| 615 | 371 | * |
|
| 616 | 371 | * All documents that are currently managed by this DocumentManager become |
|
| 617 | * detached. |
||
| 618 | * |
||
| 619 | * @param string|null $documentName if given, only documents of this type will get detached |
||
| 620 | */ |
||
| 621 | public function clear($documentName = null) |
||
| 625 | 6 | ||
| 626 | 6 | /** |
|
| 627 | 6 | * Closes the DocumentManager. All documents that are currently managed |
|
| 628 | * by this DocumentManager become detached. The DocumentManager may no longer |
||
| 629 | * be used after it is closed. |
||
| 630 | */ |
||
| 631 | public function close() |
||
| 636 | 3 | ||
| 637 | /** |
||
| 638 | 3 | * Determines whether a document instance is managed in this DocumentManager. |
|
| 639 | * |
||
| 640 | * @param object $document |
||
| 641 | 3 | * @throws \InvalidArgumentException When the $document param is not an object. |
|
| 642 | 3 | * @return bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
|
| 643 | 3 | */ |
|
| 644 | public function contains($document) |
||
| 653 | 721 | ||
| 654 | /** |
||
| 655 | * Gets the Configuration used by the DocumentManager. |
||
| 656 | * |
||
| 657 | * @return Configuration |
||
| 658 | */ |
||
| 659 | public function getConfiguration() |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Returns a reference to the supplied document. |
||
| 666 | 224 | * |
|
| 667 | * @param object $document A document object |
||
| 668 | 224 | * @param array $referenceMapping Mapping for the field that references the document |
|
| 669 | * |
||
| 670 | * @throws \InvalidArgumentException |
||
| 671 | * @throws MappingException |
||
| 672 | 224 | * @return mixed The reference for the document in question, according to the desired mapping |
|
| 673 | 224 | */ |
|
| 674 | public function createReference($document, array $referenceMapping) |
||
| 755 | 586 | ||
| 756 | 5 | /** |
|
| 757 | * Throws an exception if the DocumentManager is closed or currently not active. |
||
| 758 | 581 | * |
|
| 759 | * @throws MongoDBException If the DocumentManager is closed. |
||
| 760 | */ |
||
| 761 | private function errorIfClosed() |
||
| 767 | 1 | ||
| 768 | /** |
||
| 769 | * Check if the Document manager is open or closed. |
||
| 770 | * |
||
| 771 | * @return bool |
||
| 772 | */ |
||
| 773 | public function isOpen() |
||
| 777 | 507 | ||
| 778 | 507 | /** |
|
| 779 | * Gets the filter collection. |
||
| 780 | * |
||
| 781 | 507 | * @return FilterCollection The active filter collection. |
|
| 782 | */ |
||
| 783 | public function getFilterCollection() |
||
| 791 | } |
||
| 792 |
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.