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 | |||
147 | /** |
||
148 | * Creates a new Document that operates on the given Mongo connection |
||
149 | * and uses the given Configuration. |
||
150 | */ |
||
151 | 1634 | protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
|
152 | { |
||
153 | 1634 | $this->config = $config ?: new Configuration(); |
|
154 | 1634 | $this->eventManager = $eventManager ?: new EventManager(); |
|
155 | 1634 | $this->client = $client ?: new Client('mongodb://127.0.0.1', [], ['typeMap' => self::CLIENT_TYPEMAP]); |
|
156 | |||
157 | 1634 | $this->checkTypeMap(); |
|
158 | |||
159 | 1634 | $metadataFactoryClassName = $this->config->getClassMetadataFactoryName(); |
|
160 | 1634 | $this->metadataFactory = new $metadataFactoryClassName(); |
|
161 | 1634 | $this->metadataFactory->setDocumentManager($this); |
|
162 | 1634 | $this->metadataFactory->setConfiguration($this->config); |
|
163 | |||
164 | 1634 | $cacheDriver = $this->config->getMetadataCacheImpl(); |
|
165 | 1634 | if ($cacheDriver) { |
|
166 | $this->metadataFactory->setCacheDriver($cacheDriver); |
||
167 | } |
||
168 | |||
169 | 1634 | $hydratorDir = $this->config->getHydratorDir(); |
|
170 | 1634 | $hydratorNs = $this->config->getHydratorNamespace(); |
|
171 | 1634 | $this->hydratorFactory = new HydratorFactory( |
|
172 | 1634 | $this, |
|
173 | 1634 | $this->eventManager, |
|
174 | 1634 | $hydratorDir, |
|
175 | 1634 | $hydratorNs, |
|
176 | 1634 | $this->config->getAutoGenerateHydratorClasses() |
|
177 | ); |
||
178 | |||
179 | 1634 | $this->unitOfWork = new UnitOfWork($this, $this->eventManager, $this->hydratorFactory); |
|
180 | 1634 | $this->hydratorFactory->setUnitOfWork($this->unitOfWork); |
|
181 | 1634 | $this->schemaManager = new SchemaManager($this, $this->metadataFactory); |
|
182 | 1634 | $this->proxyFactory = new StaticProxyFactory($this); |
|
183 | 1634 | $this->repositoryFactory = $this->config->getRepositoryFactory(); |
|
184 | 1634 | $this->classNameResolver = new ClassNameResolver($this->config); |
|
185 | 1634 | } |
|
186 | |||
187 | /** |
||
188 | * Gets the proxy factory used by the DocumentManager to create document proxies. |
||
189 | */ |
||
190 | 1 | public function getProxyFactory() : ProxyFactory |
|
191 | { |
||
192 | 1 | return $this->proxyFactory; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Creates a new Document that operates on the given Mongo connection |
||
197 | * and uses the given Configuration. |
||
198 | */ |
||
199 | 1634 | public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) : DocumentManager |
|
200 | { |
||
201 | 1634 | return new static($client, $config, $eventManager); |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * Gets the EventManager used by the DocumentManager. |
||
206 | */ |
||
207 | 1697 | public function getEventManager() : EventManager |
|
208 | { |
||
209 | 1697 | return $this->eventManager; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Gets the MongoDB client instance that this DocumentManager wraps. |
||
214 | */ |
||
215 | 1634 | public function getClient() : Client |
|
216 | { |
||
217 | 1634 | return $this->client; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Gets the metadata factory used to gather the metadata of classes. |
||
222 | * |
||
223 | * @return ClassMetadataFactory |
||
224 | */ |
||
225 | 5 | public function getMetadataFactory() |
|
226 | { |
||
227 | 5 | return $this->metadataFactory; |
|
228 | } |
||
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) |
||
238 | { |
||
239 | $this->unitOfWork->initializeObject($obj); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Gets the UnitOfWork used by the DocumentManager to coordinate operations. |
||
244 | */ |
||
245 | 1641 | public function getUnitOfWork() : UnitOfWork |
|
246 | { |
||
247 | 1641 | return $this->unitOfWork; |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * Gets the Hydrator factory used by the DocumentManager to generate and get hydrators |
||
252 | * for each type of document. |
||
253 | */ |
||
254 | 70 | public function getHydratorFactory() : HydratorFactory |
|
258 | |||
259 | /** |
||
260 | * Returns SchemaManager, used to create/drop indexes/collections/databases. |
||
261 | */ |
||
262 | 28 | public function getSchemaManager() : SchemaManager |
|
263 | { |
||
264 | 28 | return $this->schemaManager; |
|
265 | } |
||
266 | |||
267 | /** Returns the class name resolver which is used to resolve real class names for proxy objects. */ |
||
268 | 1433 | public function getClassNameResolver() : ClassNameResolver |
|
272 | |||
273 | /** |
||
274 | * Returns the metadata for a class. |
||
275 | * |
||
276 | * @internal Performance-sensitive method. |
||
277 | * |
||
278 | * @param string $className The class name. |
||
279 | */ |
||
280 | 1370 | public function getClassMetadata($className) : ClassMetadata |
|
284 | |||
285 | /** |
||
286 | * Returns the MongoDB instance for a class. |
||
287 | */ |
||
288 | 1299 | 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 | /** |
||
316 | * Returns the collection instance for a class. |
||
317 | * |
||
318 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
319 | */ |
||
320 | 1302 | public function getDocumentCollection(string $className) : Collection |
|
349 | |||
350 | /** |
||
351 | * Returns the bucket instance for a class. |
||
352 | * |
||
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 | /** |
||
396 | * Create a new Query instance for a class. |
||
397 | * |
||
398 | * @param string[]|string|null $documentName (optional) an array of document names, the document name, or none |
||
399 | */ |
||
400 | 182 | public function createQueryBuilder($documentName = null) : Query\Builder |
|
404 | |||
405 | /** |
||
406 | * Creates a new aggregation builder instance for a class. |
||
407 | */ |
||
408 | 41 | 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 | * |
||
422 | * @param object $document The instance to make managed and persistent. |
||
423 | * |
||
424 | * @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 | * |
||
441 | * @param object $document The document instance to remove. |
||
442 | * |
||
443 | * @throws InvalidArgumentException When the $document param is not an object. |
||
444 | */ |
||
445 | 27 | 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 | * |
||
458 | * @param object $document The document to refresh. |
||
459 | * |
||
460 | * @throws InvalidArgumentException When the given $document param is not an object. |
||
461 | */ |
||
462 | 26 | 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 | * |
||
478 | * @param object $document The document to detach. |
||
479 | * |
||
480 | * @throws InvalidArgumentException When the $document param is not an object. |
||
481 | */ |
||
482 | 11 | 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 | * @return object The managed copy of the document. |
||
498 | * |
||
499 | * @throws LockException |
||
500 | * @throws InvalidArgumentException If the $document param is not an object. |
||
501 | */ |
||
502 | 13 | public function merge($document) |
|
510 | |||
511 | /** |
||
512 | * Acquire a lock on the given document. |
||
513 | * |
||
514 | * @throws InvalidArgumentException |
||
515 | * @throws LockException |
||
516 | */ |
||
517 | 8 | public function lock(object $document, int $lockMode, ?int $lockVersion = null) : void |
|
521 | |||
522 | /** |
||
523 | * Releases a lock on the given document. |
||
524 | */ |
||
525 | 1 | public function unlock(object $document) : void |
|
529 | |||
530 | /** |
||
531 | * Gets the repository for a document class. |
||
532 | * |
||
533 | * @param string $documentName The name of the Document. |
||
534 | * |
||
535 | * @return ObjectRepository The repository. |
||
536 | */ |
||
537 | 357 | 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 | * |
||
547 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
548 | * |
||
549 | * @throws MongoDBException |
||
550 | */ |
||
551 | 586 | 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 | * has its identifier populated. Otherwise a proxy is returned that automatically |
||
563 | * loads itself on first access. |
||
564 | * |
||
565 | * @param string|object $identifier |
||
566 | */ |
||
567 | 133 | 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 | * never be visible to the application (especially not event listeners) as it will |
||
597 | * never be loaded in the first place. |
||
598 | * |
||
599 | * @param mixed $identifier The document identifier. |
||
600 | */ |
||
601 | 1 | 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 | * @param int $lockMode |
||
625 | * @param int $lockVersion |
||
626 | * |
||
627 | * @return object $document |
||
628 | */ |
||
629 | 186 | public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) |
|
633 | |||
634 | /** |
||
635 | * Clears the DocumentManager. |
||
636 | * |
||
637 | * All documents that are currently managed by this DocumentManager become |
||
638 | * detached. |
||
639 | * |
||
640 | * @param string|null $documentName if given, only documents of this type will get detached |
||
641 | */ |
||
642 | 389 | public function clear($documentName = null) |
|
646 | |||
647 | /** |
||
648 | * Closes the DocumentManager. All documents that are currently managed |
||
649 | * by this DocumentManager become detached. The DocumentManager may no longer |
||
650 | * be used after it is closed. |
||
651 | */ |
||
652 | 6 | public function close() |
|
657 | |||
658 | /** |
||
659 | * Determines whether a document instance is managed in this DocumentManager. |
||
660 | * |
||
661 | * @param object $document |
||
662 | * |
||
663 | * @return bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
||
664 | * |
||
665 | * @throws InvalidArgumentException When the $document param is not an object. |
||
666 | */ |
||
667 | 3 | public function contains($document) |
|
676 | |||
677 | /** |
||
678 | * Gets the Configuration used by the DocumentManager. |
||
679 | */ |
||
680 | 1697 | public function getConfiguration() : Configuration |
|
684 | |||
685 | /** |
||
686 | * Returns a reference to the supplied document. |
||
687 | * |
||
688 | * @return mixed The reference for the document in question, according to the desired mapping |
||
689 | * |
||
690 | * @throws MappingException |
||
691 | * @throws RuntimeException |
||
692 | */ |
||
693 | 225 | public function createReference(object $document, array $referenceMapping) |
|
770 | |||
771 | /** |
||
772 | * Throws an exception if the DocumentManager is closed or currently not active. |
||
773 | * |
||
774 | * @throws MongoDBException If the DocumentManager is closed. |
||
775 | */ |
||
776 | 619 | private function errorIfClosed() : void |
|
782 | |||
783 | /** |
||
784 | * Check if the Document manager is open or closed. |
||
785 | */ |
||
786 | 1 | public function isOpen() : bool |
|
790 | |||
791 | /** |
||
792 | * Gets the filter collection. |
||
793 | */ |
||
794 | 536 | public function getFilterCollection() : FilterCollection |
|
802 | |||
803 | 1634 | private function checkTypeMap() : void |
|
813 | } |
||
814 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: