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 | * Whether the DocumentManager is closed or not. |
||
120 | * |
||
121 | * @var bool |
||
122 | */ |
||
123 | private $closed = false; |
||
124 | |||
125 | /** |
||
126 | * Collection of query filters. |
||
127 | * |
||
128 | * @var FilterCollection |
||
129 | */ |
||
130 | private $filterCollection; |
||
131 | |||
132 | /** |
||
133 | * Creates a new Document that operates on the given Mongo connection |
||
134 | * and uses the given Configuration. |
||
135 | * |
||
136 | 1584 | */ |
|
137 | protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
||
174 | |||
175 | /** |
||
176 | * Gets the proxy factory used by the DocumentManager to create document proxies. |
||
177 | * |
||
178 | * @return ProxyFactory |
||
179 | 1 | */ |
|
180 | public function getProxyFactory() |
||
184 | |||
185 | /** |
||
186 | * Creates a new Document that operates on the given Mongo connection |
||
187 | * and uses the given Configuration. |
||
188 | * |
||
189 | * @static |
||
190 | * @return DocumentManager |
||
191 | 1584 | */ |
|
192 | public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
||
196 | |||
197 | /** |
||
198 | * Gets the EventManager used by the DocumentManager. |
||
199 | * |
||
200 | * @return EventManager |
||
201 | 1627 | */ |
|
202 | public function getEventManager() |
||
206 | |||
207 | /** |
||
208 | * Gets the MongoDB client instance that this DocumentManager wraps. |
||
209 | * |
||
210 | * @return Client |
||
211 | 1584 | */ |
|
212 | public function getClient() |
||
216 | |||
217 | /** |
||
218 | * Gets the metadata factory used to gather the metadata of classes. |
||
219 | * |
||
220 | * @return ClassMetadataFactory |
||
221 | 1584 | */ |
|
222 | public function getMetadataFactory() |
||
226 | |||
227 | /** |
||
228 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
229 | * |
||
230 | * This method is a no-op for other objects. |
||
231 | * |
||
232 | * @param object $obj |
||
233 | */ |
||
234 | public function initializeObject($obj) |
||
238 | |||
239 | /** |
||
240 | * Gets the UnitOfWork used by the DocumentManager to coordinate operations. |
||
241 | * |
||
242 | * @return UnitOfWork |
||
243 | 1590 | */ |
|
244 | public function getUnitOfWork() |
||
248 | |||
249 | /** |
||
250 | * Gets the Hydrator factory used by the DocumentManager to generate and get hydrators |
||
251 | * for each type of document. |
||
252 | * |
||
253 | * @return HydratorFactory |
||
254 | 66 | */ |
|
255 | public function getHydratorFactory() |
||
259 | |||
260 | /** |
||
261 | * Returns SchemaManager, used to create/drop indexes/collections/databases. |
||
262 | * |
||
263 | * @return SchemaManager |
||
264 | 19 | */ |
|
265 | public function getSchemaManager() |
||
269 | |||
270 | /** |
||
271 | * Returns the metadata for a class. |
||
272 | * |
||
273 | * @param string $className The class name. |
||
274 | * @return ClassMetadata |
||
275 | * @internal Performance-sensitive method. |
||
276 | 1323 | */ |
|
277 | public function getClassMetadata($className) |
||
281 | |||
282 | /** |
||
283 | * Returns the MongoDB instance for a class. |
||
284 | * |
||
285 | * @param string $className The class name. |
||
286 | * @return Database |
||
287 | 1257 | */ |
|
288 | public function getDocumentDatabase($className) |
||
305 | |||
306 | /** |
||
307 | * Gets the array of instantiated document database instances. |
||
308 | * |
||
309 | * @return Database[] |
||
310 | */ |
||
311 | public function getDocumentDatabases() |
||
315 | |||
316 | /** |
||
317 | * Returns the MongoCollection instance for a class. |
||
318 | * |
||
319 | * @param string $className The class name. |
||
320 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
321 | 1259 | * @return Collection |
|
322 | */ |
||
323 | 1259 | public function getDocumentCollection($className) |
|
348 | |||
349 | /** |
||
350 | * Gets the array of instantiated document collection instances. |
||
351 | * |
||
352 | * @return Collection[] |
||
353 | */ |
||
354 | public function getDocumentCollections() |
||
358 | |||
359 | /** |
||
360 | * Create a new Query instance for a class. |
||
361 | * |
||
362 | 180 | * @param string $documentName The document class name. |
|
363 | * @return Query\Builder |
||
364 | 180 | */ |
|
365 | public function createQueryBuilder($documentName = null) |
||
369 | |||
370 | /** |
||
371 | * Creates a new aggregation builder instance for a class. |
||
372 | * |
||
373 | 41 | * @param string $documentName The document class name. |
|
374 | * @return Aggregation\Builder |
||
375 | 41 | */ |
|
376 | public function createAggregationBuilder($documentName) |
||
380 | |||
381 | /** |
||
382 | * Tells the DocumentManager to make an instance managed and persistent. |
||
383 | * |
||
384 | * The document will be entered into the database at or before transaction |
||
385 | * commit or as a result of the flush operation. |
||
386 | * |
||
387 | * NOTE: The persist operation always considers documents that are not yet known to |
||
388 | * this DocumentManager as NEW. Do not pass detached documents to the persist operation. |
||
389 | * |
||
390 | 582 | * @param object $document The instance to make managed and persistent. |
|
391 | * @throws \InvalidArgumentException When the given $document param is not an object. |
||
392 | 582 | */ |
|
393 | 1 | public function persist($document) |
|
401 | |||
402 | /** |
||
403 | * Removes a document instance. |
||
404 | * |
||
405 | * A removed document will be removed from the database at or before transaction commit |
||
406 | * or as a result of the flush operation. |
||
407 | * |
||
408 | 25 | * @param object $document The document instance to remove. |
|
409 | * @throws \InvalidArgumentException When the $document param is not an object. |
||
410 | 25 | */ |
|
411 | 1 | public function remove($document) |
|
419 | |||
420 | /** |
||
421 | * Refreshes the persistent state of a document from the database, |
||
422 | * overriding any local changes that have not yet been persisted. |
||
423 | * |
||
424 | 23 | * @param object $document The document to refresh. |
|
425 | * @throws \InvalidArgumentException When the given $document param is not an object. |
||
426 | 23 | */ |
|
427 | 1 | public function refresh($document) |
|
435 | |||
436 | /** |
||
437 | * Detaches a document from the DocumentManager, causing a managed document to |
||
438 | * become detached. Unflushed changes made to the document if any |
||
439 | * (including removal of the document), will not be synchronized to the database. |
||
440 | * Documents which previously referenced the detached document will continue to |
||
441 | * reference it. |
||
442 | * |
||
443 | 11 | * @param object $document The document to detach. |
|
444 | * @throws \InvalidArgumentException When the $document param is not an object. |
||
445 | 11 | */ |
|
446 | 1 | public function detach($document) |
|
453 | |||
454 | /** |
||
455 | * Merges the state of a detached document into the persistence context |
||
456 | * of this DocumentManager and returns the managed copy of the document. |
||
457 | * The document passed to merge will not become associated/managed with this DocumentManager. |
||
458 | * |
||
459 | * @param object $document The detached document to merge into the persistence context. |
||
460 | * @throws LockException |
||
461 | 14 | * @throws \InvalidArgumentException If the $document param is not an object. |
|
462 | * @return object The managed copy of the document. |
||
463 | 14 | */ |
|
464 | 1 | public function merge($document) |
|
472 | |||
473 | /** |
||
474 | * Acquire a lock on the given document. |
||
475 | * |
||
476 | * @param object $document |
||
477 | * @param int $lockMode |
||
478 | 8 | * @param int $lockVersion |
|
479 | * @throws \InvalidArgumentException |
||
480 | 8 | */ |
|
481 | public function lock($document, $lockMode, $lockVersion = null) |
||
488 | |||
489 | /** |
||
490 | * Releases a lock on the given document. |
||
491 | * |
||
492 | 1 | * @param object $document |
|
493 | * @throws \InvalidArgumentException If the $document param is not an object. |
||
494 | 1 | */ |
|
495 | public function unlock($document) |
||
502 | |||
503 | /** |
||
504 | * Gets the repository for a document class. |
||
505 | * |
||
506 | 328 | * @param string $documentName The name of the Document. |
|
507 | * @return ObjectRepository The repository. |
||
508 | 328 | */ |
|
509 | public function getRepository($documentName) |
||
513 | |||
514 | /** |
||
515 | * Flushes all changes to objects that have been queued up to now to the database. |
||
516 | * This effectively synchronizes the in-memory state of managed objects with the |
||
517 | * database. |
||
518 | * |
||
519 | 553 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
|
520 | * @throws \InvalidArgumentException |
||
521 | 553 | */ |
|
522 | 552 | public function flush(array $options = []) |
|
527 | |||
528 | /** |
||
529 | * Gets a reference to the document identified by the given type and identifier |
||
530 | * without actually loading it. |
||
531 | * |
||
532 | * If partial objects are allowed, this method will return a partial object that only |
||
533 | * has its identifier populated. Otherwise a proxy is returned that automatically |
||
534 | * loads itself on first access. |
||
535 | * |
||
536 | * @param string $documentName |
||
537 | 126 | * @param string|object $identifier |
|
538 | * @return mixed|object The document reference. |
||
539 | */ |
||
540 | 126 | public function getReference($documentName, $identifier) |
|
556 | |||
557 | /** |
||
558 | * Gets a partial reference to the document identified by the given type and identifier |
||
559 | * without actually loading it, if the document is not yet loaded. |
||
560 | * |
||
561 | * The returned reference may be a partial object if the document is not yet loaded/managed. |
||
562 | * If it is a partial object it will not initialize the rest of the document state on access. |
||
563 | * Thus you can only ever safely access the identifier of a document obtained through |
||
564 | * this method. |
||
565 | * |
||
566 | * The use-cases for partial references involve maintaining bidirectional associations |
||
567 | * without loading one side of the association or to update a document without loading it. |
||
568 | * Note, however, that in the latter case the original (persistent) document data will |
||
569 | * never be visible to the application (especially not event listeners) as it will |
||
570 | * never be loaded in the first place. |
||
571 | * |
||
572 | * @param string $documentName The name of the document type. |
||
573 | 1 | * @param mixed $identifier The document identifier. |
|
574 | * @return object The (partial) document reference. |
||
575 | 1 | */ |
|
576 | 1 | public function getPartialReference($documentName, $identifier) |
|
592 | |||
593 | /** |
||
594 | * Finds a Document by its identifier. |
||
595 | * |
||
596 | * This is just a convenient shortcut for getRepository($documentName)->find($id). |
||
597 | * |
||
598 | * @param string $documentName |
||
599 | * @param mixed $identifier |
||
600 | 182 | * @param int $lockMode |
|
601 | * @param int $lockVersion |
||
602 | 182 | * @return object $document |
|
603 | */ |
||
604 | public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) |
||
613 | 371 | ||
614 | /** |
||
615 | 371 | * Clears the DocumentManager. |
|
616 | 371 | * |
|
617 | * All documents that are currently managed by this DocumentManager become |
||
618 | * detached. |
||
619 | * |
||
620 | * @param string|null $documentName if given, only documents of this type will get detached |
||
621 | */ |
||
622 | public function clear($documentName = null) |
||
626 | 6 | ||
627 | 6 | /** |
|
628 | * Closes the DocumentManager. All documents that are currently managed |
||
629 | * by this DocumentManager become detached. The DocumentManager may no longer |
||
630 | * be used after it is closed. |
||
631 | */ |
||
632 | public function close() |
||
637 | |||
638 | 3 | /** |
|
639 | * Determines whether a document instance is managed in this DocumentManager. |
||
640 | * |
||
641 | 3 | * @param object $document |
|
642 | 3 | * @throws \InvalidArgumentException When the $document param is not an object. |
|
643 | 3 | * @return bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
|
644 | */ |
||
645 | public function contains($document) |
||
654 | |||
655 | /** |
||
656 | * Gets the Configuration used by the DocumentManager. |
||
657 | * |
||
658 | * @return Configuration |
||
659 | */ |
||
660 | public function getConfiguration() |
||
664 | |||
665 | /** |
||
666 | 224 | * Returns a reference to the supplied document. |
|
667 | * |
||
668 | 224 | * @param object $document A document object |
|
669 | * @param array $referenceMapping Mapping for the field that references the document |
||
670 | * |
||
671 | * @throws \InvalidArgumentException |
||
672 | 224 | * @throws MappingException |
|
673 | 224 | * @return mixed The reference for the document in question, according to the desired mapping |
|
674 | */ |
||
675 | 224 | public function createReference($document, array $referenceMapping) |
|
756 | 5 | ||
757 | /** |
||
758 | 581 | * Throws an exception if the DocumentManager is closed or currently not active. |
|
759 | * |
||
760 | * @throws MongoDBException If the DocumentManager is closed. |
||
761 | */ |
||
762 | private function errorIfClosed() |
||
768 | |||
769 | /** |
||
770 | * Check if the Document manager is open or closed. |
||
771 | * |
||
772 | * @return bool |
||
773 | */ |
||
774 | public function isOpen() |
||
778 | 507 | ||
779 | /** |
||
780 | * Gets the filter collection. |
||
781 | 507 | * |
|
782 | * @return FilterCollection The active filter collection. |
||
783 | */ |
||
784 | public function getFilterCollection() |
||
792 | } |
||
793 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.