Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
30 | class DocumentManager implements ObjectManager |
||
31 | { |
||
32 | /** |
||
33 | * The Doctrine MongoDB connection instance. |
||
34 | * |
||
35 | * @var Client |
||
36 | */ |
||
37 | private $client; |
||
38 | |||
39 | /** |
||
40 | * The used Configuration. |
||
41 | * |
||
42 | * @var \Doctrine\ODM\MongoDB\Configuration |
||
43 | */ |
||
44 | private $config; |
||
45 | |||
46 | /** |
||
47 | * The metadata factory, used to retrieve the ODM metadata of document classes. |
||
48 | * |
||
49 | * @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory |
||
50 | */ |
||
51 | private $metadataFactory; |
||
52 | |||
53 | /** |
||
54 | * The UnitOfWork used to coordinate object-level transactions. |
||
55 | * |
||
56 | * @var UnitOfWork |
||
57 | */ |
||
58 | private $unitOfWork; |
||
59 | |||
60 | /** |
||
61 | * The event manager that is the central point of the event system. |
||
62 | * |
||
63 | * @var \Doctrine\Common\EventManager |
||
64 | */ |
||
65 | private $eventManager; |
||
66 | |||
67 | /** |
||
68 | * The Hydrator factory instance. |
||
69 | * |
||
70 | * @var HydratorFactory |
||
71 | */ |
||
72 | private $hydratorFactory; |
||
73 | |||
74 | /** |
||
75 | * The Proxy factory instance. |
||
76 | * |
||
77 | * @var ProxyFactory |
||
78 | */ |
||
79 | private $proxyFactory; |
||
80 | |||
81 | /** |
||
82 | * The repository factory used to create dynamic repositories. |
||
83 | * |
||
84 | * @var RepositoryFactory |
||
85 | */ |
||
86 | private $repositoryFactory; |
||
87 | |||
88 | /** |
||
89 | * SchemaManager instance |
||
90 | * |
||
91 | * @var SchemaManager |
||
92 | */ |
||
93 | private $schemaManager; |
||
94 | |||
95 | /** |
||
96 | * Array of cached document database instances that are lazily loaded. |
||
97 | * |
||
98 | * @var Database[] |
||
99 | */ |
||
100 | private $documentDatabases = array(); |
||
101 | |||
102 | /** |
||
103 | * Array of cached document collection instances that are lazily loaded. |
||
104 | * |
||
105 | * @var Collection[] |
||
106 | */ |
||
107 | private $documentCollections = array(); |
||
108 | |||
109 | /** |
||
110 | * Whether the DocumentManager is closed or not. |
||
111 | * |
||
112 | * @var bool |
||
113 | */ |
||
114 | private $closed = false; |
||
115 | |||
116 | /** |
||
117 | * Collection of query filters. |
||
118 | * |
||
119 | * @var \Doctrine\ODM\MongoDB\Query\FilterCollection |
||
120 | */ |
||
121 | private $filterCollection; |
||
122 | |||
123 | /** |
||
124 | * Creates a new Document that operates on the given Mongo connection |
||
125 | * and uses the given Configuration. |
||
126 | * |
||
127 | * @param Client|null $client |
||
128 | * @param Configuration|null $config |
||
129 | * @param \Doctrine\Common\EventManager|null $eventManager |
||
130 | */ |
||
131 | 1576 | protected function __construct(Client $client = null, Configuration $config = null, EventManager $eventManager = null) |
|
165 | |||
166 | /** |
||
167 | * Gets the proxy factory used by the DocumentManager to create document proxies. |
||
168 | * |
||
169 | * @return ProxyFactory |
||
170 | */ |
||
171 | 1 | public function getProxyFactory() |
|
175 | |||
176 | /** |
||
177 | * Creates a new Document that operates on the given Mongo connection |
||
178 | * and uses the given Configuration. |
||
179 | * |
||
180 | * @static |
||
181 | * @param Client|null $client |
||
182 | * @param Configuration|null $config |
||
183 | * @param \Doctrine\Common\EventManager|null $eventManager |
||
184 | * @return DocumentManager |
||
185 | */ |
||
186 | 1576 | public static function create(Client $client = null, Configuration $config = null, EventManager $eventManager = null) |
|
190 | |||
191 | /** |
||
192 | * Gets the EventManager used by the DocumentManager. |
||
193 | * |
||
194 | * @return \Doctrine\Common\EventManager |
||
195 | */ |
||
196 | 1619 | public function getEventManager() |
|
200 | |||
201 | /** |
||
202 | * Gets the MongoDB client instance that this DocumentManager wraps. |
||
203 | * |
||
204 | * @return Client |
||
205 | */ |
||
206 | 1576 | public function getClient() |
|
210 | |||
211 | /** |
||
212 | * Gets the metadata factory used to gather the metadata of classes. |
||
213 | * |
||
214 | * @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory |
||
215 | */ |
||
216 | 1576 | public function getMetadataFactory() |
|
220 | |||
221 | /** |
||
222 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
223 | * |
||
224 | * This method is a no-op for other objects. |
||
225 | * |
||
226 | * @param object $obj |
||
227 | */ |
||
228 | public function initializeObject($obj) |
||
232 | |||
233 | /** |
||
234 | * Gets the UnitOfWork used by the DocumentManager to coordinate operations. |
||
235 | * |
||
236 | * @return UnitOfWork |
||
237 | */ |
||
238 | 1582 | public function getUnitOfWork() |
|
242 | |||
243 | /** |
||
244 | * Gets the Hydrator factory used by the DocumentManager to generate and get hydrators |
||
245 | * for each type of document. |
||
246 | * |
||
247 | * @return HydratorFactory |
||
248 | */ |
||
249 | 66 | public function getHydratorFactory() |
|
253 | |||
254 | /** |
||
255 | * Returns SchemaManager, used to create/drop indexes/collections/databases. |
||
256 | * |
||
257 | * @return \Doctrine\ODM\MongoDB\SchemaManager |
||
258 | */ |
||
259 | 18 | public function getSchemaManager() |
|
263 | |||
264 | /** |
||
265 | * Returns the metadata for a class. |
||
266 | * |
||
267 | * @param string $className The class name. |
||
268 | * @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
||
269 | * @internal Performance-sensitive method. |
||
270 | */ |
||
271 | 1318 | public function getClassMetadata($className) |
|
275 | |||
276 | /** |
||
277 | * Returns the MongoDB instance for a class. |
||
278 | * |
||
279 | * @param string $className The class name. |
||
280 | * @return Database |
||
281 | */ |
||
282 | 1252 | public function getDocumentDatabase($className) |
|
298 | |||
299 | /** |
||
300 | * Gets the array of instantiated document database instances. |
||
301 | * |
||
302 | * @return Database[] |
||
303 | */ |
||
304 | public function getDocumentDatabases() |
||
308 | |||
309 | /** |
||
310 | * Returns the MongoCollection instance for a class. |
||
311 | * |
||
312 | * @param string $className The class name. |
||
313 | * @throws MongoDBException When the $className param is not mapped to a collection |
||
314 | * @return Collection |
||
315 | */ |
||
316 | 1254 | public function getDocumentCollection($className) |
|
340 | |||
341 | /** |
||
342 | * Gets the array of instantiated document collection instances. |
||
343 | * |
||
344 | * @return Collection[] |
||
345 | */ |
||
346 | public function getDocumentCollections() |
||
350 | |||
351 | /** |
||
352 | * Create a new Query instance for a class. |
||
353 | * |
||
354 | * @param string $documentName The document class name. |
||
355 | * @return Query\Builder |
||
356 | */ |
||
357 | 178 | public function createQueryBuilder($documentName = null) |
|
361 | |||
362 | /** |
||
363 | * Creates a new aggregation builder instance for a class. |
||
364 | * |
||
365 | * @param string $documentName The document class name. |
||
366 | * @return Aggregation\Builder |
||
367 | */ |
||
368 | 41 | public function createAggregationBuilder($documentName) |
|
372 | |||
373 | /** |
||
374 | * Tells the DocumentManager to make an instance managed and persistent. |
||
375 | * |
||
376 | * The document will be entered into the database at or before transaction |
||
377 | * commit or as a result of the flush operation. |
||
378 | * |
||
379 | * NOTE: The persist operation always considers documents that are not yet known to |
||
380 | * this DocumentManager as NEW. Do not pass detached documents to the persist operation. |
||
381 | * |
||
382 | * @param object $document The instance to make managed and persistent. |
||
383 | * @throws \InvalidArgumentException When the given $document param is not an object |
||
384 | */ |
||
385 | 579 | View Code Duplication | public function persist($document) |
393 | |||
394 | /** |
||
395 | * Removes a document instance. |
||
396 | * |
||
397 | * A removed document will be removed from the database at or before transaction commit |
||
398 | * or as a result of the flush operation. |
||
399 | * |
||
400 | * @param object $document The document instance to remove. |
||
401 | * @throws \InvalidArgumentException when the $document param is not an object |
||
402 | */ |
||
403 | 23 | View Code Duplication | public function remove($document) |
411 | |||
412 | /** |
||
413 | * Refreshes the persistent state of a document from the database, |
||
414 | * overriding any local changes that have not yet been persisted. |
||
415 | * |
||
416 | * @param object $document The document to refresh. |
||
417 | * @throws \InvalidArgumentException When the given $document param is not an object |
||
418 | */ |
||
419 | 23 | View Code Duplication | public function refresh($document) |
427 | |||
428 | /** |
||
429 | * Detaches a document from the DocumentManager, causing a managed document to |
||
430 | * become detached. Unflushed changes made to the document if any |
||
431 | * (including removal of the document), will not be synchronized to the database. |
||
432 | * Documents which previously referenced the detached document will continue to |
||
433 | * reference it. |
||
434 | * |
||
435 | * @param object $document The document to detach. |
||
436 | * @throws \InvalidArgumentException when the $document param is not an object |
||
437 | */ |
||
438 | 11 | public function detach($document) |
|
445 | |||
446 | /** |
||
447 | * Merges the state of a detached document into the persistence context |
||
448 | * of this DocumentManager and returns the managed copy of the document. |
||
449 | * The document passed to merge will not become associated/managed with this DocumentManager. |
||
450 | * |
||
451 | * @param object $document The detached document to merge into the persistence context. |
||
452 | * @throws LockException |
||
453 | * @throws \InvalidArgumentException if the $document param is not an object |
||
454 | * @return object The managed copy of the document. |
||
455 | */ |
||
456 | 14 | View Code Duplication | public function merge($document) |
464 | |||
465 | /** |
||
466 | * Acquire a lock on the given document. |
||
467 | * |
||
468 | * @param object $document |
||
469 | * @param int $lockMode |
||
470 | * @param int $lockVersion |
||
471 | * @throws \InvalidArgumentException |
||
472 | */ |
||
473 | 8 | public function lock($document, $lockMode, $lockVersion = null) |
|
480 | |||
481 | /** |
||
482 | * Releases a lock on the given document. |
||
483 | * |
||
484 | * @param object $document |
||
485 | * @throws \InvalidArgumentException if the $document param is not an object |
||
486 | */ |
||
487 | 1 | public function unlock($document) |
|
494 | |||
495 | /** |
||
496 | * Gets the repository for a document class. |
||
497 | * |
||
498 | * @param string $documentName The name of the Document. |
||
499 | * @return ObjectRepository The repository. |
||
500 | */ |
||
501 | 325 | public function getRepository($documentName) |
|
505 | |||
506 | /** |
||
507 | * Flushes all changes to objects that have been queued up to now to the database. |
||
508 | * This effectively synchronizes the in-memory state of managed objects with the |
||
509 | * database. |
||
510 | * |
||
511 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
512 | * @throws \InvalidArgumentException |
||
513 | */ |
||
514 | 550 | public function flush(array $options = array()) |
|
519 | |||
520 | /** |
||
521 | * Gets a reference to the document identified by the given type and identifier |
||
522 | * without actually loading it. |
||
523 | * |
||
524 | * If partial objects are allowed, this method will return a partial object that only |
||
525 | * has its identifier populated. Otherwise a proxy is returned that automatically |
||
526 | * loads itself on first access. |
||
527 | * |
||
528 | * @param string $documentName |
||
529 | * @param string|object $identifier |
||
530 | * @return mixed|object The document reference. |
||
531 | */ |
||
532 | 125 | public function getReference($documentName, $identifier) |
|
547 | |||
548 | /** |
||
549 | * Gets a partial reference to the document identified by the given type and identifier |
||
550 | * without actually loading it, if the document is not yet loaded. |
||
551 | * |
||
552 | * The returned reference may be a partial object if the document is not yet loaded/managed. |
||
553 | * If it is a partial object it will not initialize the rest of the document state on access. |
||
554 | * Thus you can only ever safely access the identifier of a document obtained through |
||
555 | * this method. |
||
556 | * |
||
557 | * The use-cases for partial references involve maintaining bidirectional associations |
||
558 | * without loading one side of the association or to update a document without loading it. |
||
559 | * Note, however, that in the latter case the original (persistent) document data will |
||
560 | * never be visible to the application (especially not event listeners) as it will |
||
561 | * never be loaded in the first place. |
||
562 | * |
||
563 | * @param string $documentName The name of the document type. |
||
564 | * @param mixed $identifier The document identifier. |
||
565 | * @return object The (partial) document reference. |
||
566 | */ |
||
567 | 1 | public function getPartialReference($documentName, $identifier) |
|
581 | |||
582 | /** |
||
583 | * Finds a Document by its identifier. |
||
584 | * |
||
585 | * This is just a convenient shortcut for getRepository($documentName)->find($id). |
||
586 | * |
||
587 | * @param string $documentName |
||
588 | * @param mixed $identifier |
||
589 | * @param int $lockMode |
||
590 | * @param int $lockVersion |
||
591 | * @return object $document |
||
592 | */ |
||
593 | 181 | public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null) |
|
597 | |||
598 | /** |
||
599 | * Clears the DocumentManager. |
||
600 | * |
||
601 | * All documents that are currently managed by this DocumentManager become |
||
602 | * detached. |
||
603 | * |
||
604 | * @param string|null $documentName if given, only documents of this type will get detached |
||
605 | */ |
||
606 | 371 | public function clear($documentName = null) |
|
610 | |||
611 | /** |
||
612 | * Closes the DocumentManager. All documents that are currently managed |
||
613 | * by this DocumentManager become detached. The DocumentManager may no longer |
||
614 | * be used after it is closed. |
||
615 | */ |
||
616 | 6 | public function close() |
|
621 | |||
622 | /** |
||
623 | * Determines whether a document instance is managed in this DocumentManager. |
||
624 | * |
||
625 | * @param object $document |
||
626 | * @throws \InvalidArgumentException When the $document param is not an object |
||
627 | * @return boolean TRUE if this DocumentManager currently manages the given document, FALSE otherwise. |
||
628 | */ |
||
629 | 3 | public function contains($document) |
|
638 | |||
639 | /** |
||
640 | * Gets the Configuration used by the DocumentManager. |
||
641 | * |
||
642 | * @return Configuration |
||
643 | */ |
||
644 | 717 | public function getConfiguration() |
|
648 | |||
649 | /** |
||
650 | * Returns a reference to the supplied document. |
||
651 | * |
||
652 | * @param object $document A document object |
||
653 | * @param array $referenceMapping Mapping for the field that references the document |
||
654 | * |
||
655 | * @throws \InvalidArgumentException |
||
656 | * @throws MappingException |
||
657 | * @return mixed The reference for the document in question, according to the desired mapping |
||
658 | */ |
||
659 | 220 | public function createReference($document, array $referenceMapping) |
|
741 | |||
742 | /** |
||
743 | * Throws an exception if the DocumentManager is closed or currently not active. |
||
744 | * |
||
745 | * @throws MongoDBException If the DocumentManager is closed. |
||
746 | */ |
||
747 | 583 | private function errorIfClosed() |
|
753 | |||
754 | /** |
||
755 | * Check if the Document manager is open or closed. |
||
756 | * |
||
757 | * @return bool |
||
758 | */ |
||
759 | 1 | public function isOpen() |
|
763 | |||
764 | /** |
||
765 | * Gets the filter collection. |
||
766 | * |
||
767 | * @return \Doctrine\ODM\MongoDB\Query\FilterCollection The active filter collection. |
||
768 | */ |
||
769 | 502 | public function getFilterCollection() |
|
777 | } |
||
778 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.