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 |
||
46 | class DocumentManager implements ObjectManager |
||
47 | { |
||
48 | public const CLIENT_TYPEMAP = ['root' => 'array', 'document' => 'array']; |
||
49 | |||
50 | /** |
||
51 | * The Doctrine MongoDB connection instance. |
||
52 | * |
||
53 | * @var Client |
||
54 | */ |
||
55 | private $client; |
||
56 | |||
57 | /** |
||
58 | * The used Configuration. |
||
59 | * |
||
60 | * @var Configuration |
||
61 | */ |
||
62 | private $config; |
||
63 | |||
64 | /** |
||
65 | * The metadata factory, used to retrieve the ODM metadata of document classes. |
||
66 | * |
||
67 | * @var ClassMetadataFactory |
||
68 | */ |
||
69 | private $metadataFactory; |
||
70 | |||
71 | /** |
||
72 | * The UnitOfWork used to coordinate object-level transactions. |
||
73 | * |
||
74 | * @var UnitOfWork |
||
75 | */ |
||
76 | private $unitOfWork; |
||
77 | |||
78 | /** |
||
79 | * The event manager that is the central point of the event system. |
||
80 | * |
||
81 | * @var EventManager |
||
82 | */ |
||
83 | private $eventManager; |
||
84 | |||
85 | /** |
||
86 | * The Hydrator factory instance. |
||
87 | * |
||
88 | * @var HydratorFactory |
||
89 | */ |
||
90 | private $hydratorFactory; |
||
91 | |||
92 | /** |
||
93 | * The Proxy factory instance. |
||
94 | * |
||
95 | * @var ProxyFactory |
||
96 | */ |
||
97 | private $proxyFactory; |
||
98 | |||
99 | /** |
||
100 | * The repository factory used to create dynamic repositories. |
||
101 | * |
||
102 | * @var RepositoryFactory |
||
103 | */ |
||
104 | private $repositoryFactory; |
||
105 | |||
106 | /** |
||
107 | * SchemaManager instance |
||
108 | * |
||
109 | * @var SchemaManager |
||
110 | */ |
||
111 | private $schemaManager; |
||
112 | |||
113 | /** |
||
114 | * Array of cached document database instances that are lazily loaded. |
||
115 | * |
||
116 | * @var Database[] |
||
117 | */ |
||
118 | private $documentDatabases = []; |
||
119 | |||
120 | /** |
||
121 | * Array of cached document collection instances that are lazily loaded. |
||
122 | * |
||
123 | * @var Collection[] |
||
124 | */ |
||
125 | private $documentCollections = []; |
||
126 | |||
127 | /** |
||
128 | * Array of cached document bucket instances that are lazily loaded. |
||
129 | * |
||
130 | * @var Bucket[] |
||
131 | */ |
||
132 | private $documentBuckets = []; |
||
133 | |||
134 | /** |
||
135 | * Whether the DocumentManager is closed or not. |
||
136 | * |
||
137 | * @var bool |
||
138 | */ |
||
139 | private $closed = false; |
||
140 | |||
141 | /** |
||
142 | * Collection of query filters. |
||
143 | * |
||
144 | * @var FilterCollection |
||
145 | */ |
||
146 | private $filterCollection; |
||
147 | |||
148 | /** @var ClassNameResolver */ |
||
149 | private $classNameResolver; |
||
150 | |||
151 | /** |
||
152 | * Creates a new Document that operates on the given Mongo connection |
||
153 | * and uses the given Configuration. |
||
154 | */ |
||
155 | 1773 | protected function __construct(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) |
|
156 | { |
||
157 | 1773 | $this->config = $config ?: new Configuration(); |
|
158 | 1773 | $this->eventManager = $eventManager ?: new EventManager(); |
|
159 | 1773 | $this->client = $client ?: new Client('mongodb://127.0.0.1', [], ['typeMap' => self::CLIENT_TYPEMAP]); |
|
160 | |||
161 | 1773 | $this->checkTypeMap(); |
|
162 | |||
163 | 1773 | $metadataFactoryClassName = $this->config->getClassMetadataFactoryName(); |
|
164 | 1773 | $this->metadataFactory = new $metadataFactoryClassName(); |
|
165 | 1773 | $this->metadataFactory->setDocumentManager($this); |
|
166 | 1773 | $this->metadataFactory->setConfiguration($this->config); |
|
167 | |||
168 | 1773 | $cacheDriver = $this->config->getMetadataCacheImpl(); |
|
169 | 1773 | if ($cacheDriver) { |
|
170 | $this->metadataFactory->setCacheDriver($cacheDriver); |
||
171 | } |
||
172 | |||
173 | 1773 | $hydratorDir = $this->config->getHydratorDir(); |
|
174 | 1773 | $hydratorNs = $this->config->getHydratorNamespace(); |
|
175 | 1773 | $this->hydratorFactory = new HydratorFactory( |
|
176 | 1773 | $this, |
|
177 | 1773 | $this->eventManager, |
|
178 | 1773 | $hydratorDir, |
|
179 | 1773 | $hydratorNs, |
|
180 | 1773 | $this->config->getAutoGenerateHydratorClasses() |
|
181 | ); |
||
182 | |||
183 | 1773 | $this->unitOfWork = new UnitOfWork($this, $this->eventManager, $this->hydratorFactory); |
|
184 | 1773 | $this->hydratorFactory->setUnitOfWork($this->unitOfWork); |
|
185 | 1773 | $this->schemaManager = new SchemaManager($this, $this->metadataFactory); |
|
186 | 1773 | $this->proxyFactory = new StaticProxyFactory($this); |
|
187 | 1773 | $this->repositoryFactory = $this->config->getRepositoryFactory(); |
|
188 | 1773 | $this->classNameResolver = new CachingClassNameResolver(new ProxyManagerClassNameResolver($this->config)); |
|
189 | 1773 | } |
|
190 | |||
191 | /** |
||
192 | * Gets the proxy factory used by the DocumentManager to create document proxies. |
||
193 | */ |
||
194 | 2 | public function getProxyFactory() : ProxyFactory |
|
195 | { |
||
196 | 2 | return $this->proxyFactory; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * Creates a new Document that operates on the given Mongo connection |
||
201 | * and uses the given Configuration. |
||
202 | */ |
||
203 | 1773 | public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) : DocumentManager |
|
204 | { |
||
205 | 1773 | return new static($client, $config, $eventManager); |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Gets the EventManager used by the DocumentManager. |
||
210 | */ |
||
211 | 1773 | public function getEventManager() : EventManager |
|
212 | { |
||
213 | 1773 | return $this->eventManager; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * Gets the MongoDB client instance that this DocumentManager wraps. |
||
218 | */ |
||
219 | 1662 | public function getClient() : Client |
|
223 | |||
224 | /** |
||
225 | * Gets the metadata factory used to gather the metadata of classes. |
||
226 | * |
||
227 | * @return ClassMetadataFactory |
||
228 | */ |
||
229 | 120 | 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 | 1773 | public function getUnitOfWork() : UnitOfWork |
|
250 | { |
||
251 | 1773 | return $this->unitOfWork; |
|
252 | } |
||
253 | |||
254 | /** |
||
255 | * Gets the Hydrator factory used by the DocumentManager to generate and get hydrators |
||
256 | * for each type of document. |
||
257 | */ |
||
258 | 54 | public function getHydratorFactory() : HydratorFactory |
|
259 | { |
||
260 | 54 | return $this->hydratorFactory; |
|
261 | } |
||
262 | |||
263 | /** |
||
264 | * Returns SchemaManager, used to create/drop indexes/collections/databases. |
||
265 | */ |
||
266 | 141 | public function getSchemaManager() : SchemaManager |
|
267 | { |
||
268 | 141 | return $this->schemaManager; |
|
269 | } |
||
270 | |||
271 | /** Returns the class name resolver which is used to resolve real class names for proxy objects. */ |
||
272 | 1548 | public function getClassNameResolver() : ClassNameResolver |
|
273 | { |
||
274 | 1548 | return $this->classNameResolver; |
|
275 | } |
||
276 | |||
277 | /** |
||
278 | * Returns the metadata for a class. |
||
279 | * |
||
280 | * @internal Performance-sensitive method. |
||
281 | * |
||
282 | * @param string $className The class name. |
||
283 | */ |
||
284 | 1488 | public function getClassMetadata($className) : ClassMetadata |
|
288 | |||
289 | /** |
||
290 | * Returns the MongoDB instance for a class. |
||
291 | */ |
||
292 | 1411 | public function getDocumentDatabase(string $className) : Database |
|
293 | { |
||
294 | 1411 | $className = $this->classNameResolver->getRealClass($className); |
|
295 | |||
296 | 1411 | if (isset($this->documentDatabases[$className])) { |
|
297 | 50 | return $this->documentDatabases[$className]; |
|
298 | } |
||
299 | |||
300 | 1411 | $metadata = $this->metadataFactory->getMetadataFor($className); |
|
301 | 1411 | assert($metadata instanceof ClassMetadata); |
|
302 | |||
303 | 1411 | $db = $metadata->getDatabase(); |
|
304 | 1411 | $db = $db ?: $this->config->getDefaultDB(); |
|
305 | 1411 | $db = $db ?: 'doctrine'; |
|
306 | 1411 | $this->documentDatabases[$className] = $this->client->selectDatabase($db); |
|
307 | |||
308 | 1411 | return $this->documentDatabases[$className]; |
|
309 | } |
||
310 | |||
311 | /** |
||
312 | * Gets the array of instantiated document database instances. |
||
313 | * |
||
314 | * @return Database[] |
||
315 | */ |
||
316 | public function getDocumentDatabases() : array |
||
317 | { |
||
318 | return $this->documentDatabases; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Returns the collection instance for a class. |
||
323 | * |
||
324 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
325 | */ |
||
326 | 1387 | public function getDocumentCollection(string $className) : Collection |
|
327 | { |
||
328 | 1387 | $className = $this->classNameResolver->getRealClass($className); |
|
329 | |||
330 | /** @var ClassMetadata $metadata */ |
||
331 | 1387 | $metadata = $this->metadataFactory->getMetadataFor($className); |
|
332 | 1387 | assert($metadata instanceof ClassMetadata); |
|
333 | 1387 | if ($metadata->isFile) { |
|
334 | 33 | return $this->getDocumentBucket($className)->getFilesCollection(); |
|
335 | } |
||
336 | |||
337 | 1370 | $collectionName = $metadata->getCollection(); |
|
338 | |||
339 | 1370 | if (! $collectionName) { |
|
340 | throw MongoDBException::documentNotMappedToCollection($className); |
||
341 | } |
||
342 | |||
343 | 1370 | if (! isset($this->documentCollections[$className])) { |
|
344 | 1370 | $db = $this->getDocumentDatabase($className); |
|
345 | |||
346 | 1370 | $options = []; |
|
347 | 1370 | if ($metadata->readPreference !== null) { |
|
348 | 3 | $options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags); |
|
349 | } |
||
350 | |||
351 | 1370 | $this->documentCollections[$className] = $db->selectCollection($collectionName, $options); |
|
352 | } |
||
353 | |||
354 | 1370 | return $this->documentCollections[$className]; |
|
355 | } |
||
356 | |||
357 | /** |
||
358 | * Returns the bucket instance for a class. |
||
359 | * |
||
360 | * @throws MongoDBException When the $className param is not mapped to a collection. |
||
361 | */ |
||
362 | 33 | public function getDocumentBucket(string $className) : Bucket |
|
391 | |||
392 | /** |
||
393 | * Gets the array of instantiated document collection instances. |
||
394 | * |
||
395 | * @return Collection[] |
||
396 | */ |
||
397 | public function getDocumentCollections() : array |
||
401 | |||
402 | /** |
||
403 | * Create a new Query instance for a class. |
||
404 | * |
||
405 | * @param string[]|string|null $documentName (optional) an array of document names, the document name, or none |
||
406 | */ |
||
407 | 181 | public function createQueryBuilder($documentName = null) : Query\Builder |
|
411 | |||
412 | /** |
||
413 | * Creates a new aggregation builder instance for a class. |
||
414 | */ |
||
415 | 42 | public function createAggregationBuilder(string $documentName) : Aggregation\Builder |
|
419 | |||
420 | /** |
||
421 | * Tells the DocumentManager to make an instance managed and persistent. |
||
422 | * |
||
423 | * The document will be entered into the database at or before transaction |
||
424 | * commit or as a result of the flush operation. |
||
425 | * |
||
426 | * NOTE: The persist operation always considers documents that are not yet known to |
||
427 | * this DocumentManager as NEW. Do not pass detached documents to the persist operation. |
||
428 | * |
||
429 | * @param object $document The instance to make managed and persistent. |
||
430 | * |
||
431 | * @throws InvalidArgumentException When the given $document param is not an object. |
||
432 | */ |
||
433 | 639 | public function persist($document) |
|
441 | |||
442 | /** |
||
443 | * Removes a document instance. |
||
444 | * |
||
445 | * A removed document will be removed from the database at or before transaction commit |
||
446 | * or as a result of the flush operation. |
||
447 | * |
||
448 | * @param object $document The document instance to remove. |
||
449 | * |
||
450 | * @throws InvalidArgumentException When the $document param is not an object. |
||
451 | */ |
||
452 | 27 | public function remove($document) |
|
460 | |||
461 | /** |
||
462 | * Refreshes the persistent state of a document from the database, |
||
463 | * overriding any local changes that have not yet been persisted. |
||
464 | * |
||
465 | * @param object $document The document to refresh. |
||
466 | * |
||
467 | * @throws InvalidArgumentException When the given $document param is not an object. |
||
468 | */ |
||
469 | 26 | public function refresh($document) |
|
477 | |||
478 | /** |
||
479 | * Detaches a document from the DocumentManager, causing a managed document to |
||
480 | * become detached. Unflushed changes made to the document if any |
||
481 | * (including removal of the document), will not be synchronized to the database. |
||
482 | * Documents which previously referenced the detached document will continue to |
||
483 | * reference it. |
||
484 | * |
||
485 | * @param object $document The document to detach. |
||
486 | * |
||
487 | * @throws InvalidArgumentException When the $document param is not an object. |
||
488 | */ |
||
489 | 11 | public function detach($document) |
|
496 | |||
497 | /** |
||
498 | * Merges the state of a detached document into the persistence context |
||
499 | * of this DocumentManager and returns the managed copy of the document. |
||
500 | * The document passed to merge will not become associated/managed with this DocumentManager. |
||
501 | * |
||
502 | * @param object $document The detached document to merge into the persistence context. |
||
503 | * |
||
504 | * @return object The managed copy of the document. |
||
505 | * |
||
506 | * @throws LockException |
||
507 | * @throws InvalidArgumentException If the $document param is not an object. |
||
508 | */ |
||
509 | 13 | public function merge($document) |
|
518 | |||
519 | /** |
||
520 | * Acquire a lock on the given document. |
||
521 | * |
||
522 | * @throws InvalidArgumentException |
||
523 | * @throws LockException |
||
524 | */ |
||
525 | 8 | public function lock(object $document, int $lockMode, ?int $lockVersion = null) : void |
|
529 | |||
530 | /** |
||
531 | * Releases a lock on the given document. |
||
532 | */ |
||
533 | 1 | public function unlock(object $document) : void |
|
537 | |||
538 | /** |
||
539 | * Gets the repository for a document class. |
||
540 | * |
||
541 | * @param string $documentName The name of the Document. |
||
542 | * |
||
543 | * @return ObjectRepository The repository. |
||
544 | */ |
||
545 | 355 | public function getRepository($documentName) |
|
549 | |||
550 | /** |
||
551 | * Flushes all changes to objects that have been queued up to now to the database. |
||
552 | * This effectively synchronizes the in-memory state of managed objects with the |
||
553 | * database. |
||
554 | * |
||
555 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
556 | * |
||
557 | * @throws MongoDBException |
||
558 | */ |
||
559 | 600 | public function flush(array $options = []) |
|
564 | |||
565 | /** |
||
566 | * Gets a reference to the document identified by the given type and identifier |
||
567 | * without actually loading it. |
||
568 | * |
||
569 | * If partial objects are allowed, this method will return a partial object that only |
||
570 | * has its identifier populated. Otherwise a proxy is returned that automatically |
||
571 | * loads itself on first access. |
||
572 | * |
||
573 | * @param mixed $identifier |
||
574 | */ |
||
575 | 129 | public function getReference(string $documentName, $identifier) : object |
|
591 | |||
592 | /** |
||
593 | * Gets a partial reference to the document identified by the given type and identifier |
||
594 | * without actually loading it, if the document is not yet loaded. |
||
595 | * |
||
596 | * The returned reference may be a partial object if the document is not yet loaded/managed. |
||
597 | * If it is a partial object it will not initialize the rest of the document state on access. |
||
598 | * Thus you can only ever safely access the identifier of a document obtained through |
||
599 | * this method. |
||
600 | * |
||
601 | * The use-cases for partial references involve maintaining bidirectional associations |
||
602 | * without loading one side of the association or to update a document without loading it. |
||
603 | * Note, however, that in the latter case the original (persistent) document data will |
||
604 | * never be visible to the application (especially not event listeners) as it will |
||
605 | * never be loaded in the first place. |
||
606 | * |
||
607 | * @param mixed $identifier The document identifier. |
||
608 | */ |
||
609 | 1 | public function getPartialReference(string $documentName, $identifier) : object |
|
625 | |||
626 | /** |
||
627 | * Finds a Document by its identifier. |
||
628 | * |
||
629 | * This is just a convenient shortcut for getRepository($documentName)->find($id). |
||
630 | * |
||
631 | * @param string $documentName |
||
632 | * @param mixed $identifier |
||
633 | * @param int $lockMode |
||
634 | * @param int $lockVersion |
||
635 | */ |
||
636 | 175 | public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) : ?object |
|
645 | |||
646 | /** |
||
647 | * Clears the DocumentManager. |
||
648 | * |
||
649 | * All documents that are currently managed by this DocumentManager become |
||
650 | * detached. |
||
651 | * |
||
652 | * @param string|null $documentName if given, only documents of this type will get detached |
||
653 | */ |
||
654 | 380 | public function clear($documentName = null) |
|
658 | |||
659 | /** |
||
660 | * Closes the DocumentManager. All documents that are currently managed |
||
661 | * by this DocumentManager become detached. The DocumentManager may no longer |
||
662 | * be used after it is closed. |
||
663 | */ |
||
664 | 6 | public function close() |
|
669 | |||
670 | /** |
||
671 | * Determines whether a document instance is managed in this DocumentManager. |
||
672 | * |
||
673 | * @param object $document |
||
674 | * |
||
675 | * @return bool TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
||
676 | * |
||
677 | * @throws InvalidArgumentException When the $document param is not an object. |
||
678 | */ |
||
679 | 3 | public function contains($document) |
|
689 | |||
690 | /** |
||
691 | * Gets the Configuration used by the DocumentManager. |
||
692 | */ |
||
693 | 1773 | public function getConfiguration() : Configuration |
|
697 | |||
698 | /** |
||
699 | * Returns a reference to the supplied document. |
||
700 | * |
||
701 | * @return mixed The reference for the document in question, according to the desired mapping |
||
702 | * |
||
703 | * @throws MappingException |
||
704 | * @throws RuntimeException |
||
705 | */ |
||
706 | 239 | public function createReference(object $document, array $referenceMapping) |
|
750 | |||
751 | /** |
||
752 | * Build discriminator portion of reference for specified reference mapping and class metadata. |
||
753 | * |
||
754 | * @param array $referenceMapping Mappings of reference for which discriminator data is created. |
||
755 | * @param ClassMetadata $class Metadata of reference document class. |
||
756 | * |
||
757 | * @return array with next structure [{discriminator field} => {discriminator value}] |
||
758 | * |
||
759 | * @throws MappingException When discriminator map is present and reference class in not registered in it. |
||
760 | */ |
||
761 | 216 | private function getDiscriminatorData(array $referenceMapping, ClassMetadata $class) : array |
|
801 | |||
802 | /** |
||
803 | * Throws an exception if the DocumentManager is closed or currently not active. |
||
804 | * |
||
805 | * @throws MongoDBException If the DocumentManager is closed. |
||
806 | */ |
||
807 | 644 | private function errorIfClosed() : void |
|
813 | |||
814 | /** |
||
815 | * Check if the Document manager is open or closed. |
||
816 | */ |
||
817 | 1 | public function isOpen() : bool |
|
821 | |||
822 | /** |
||
823 | * Gets the filter collection. |
||
824 | */ |
||
825 | 533 | public function getFilterCollection() : FilterCollection |
|
833 | |||
834 | 1773 | private function checkTypeMap() : void |
|
844 | } |
||
845 |
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.