Complex classes like EntityManager 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 EntityManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
64 | /* final */class EntityManager implements EntityManagerInterface |
||
65 | { |
||
66 | /** |
||
67 | * The used Configuration. |
||
68 | * |
||
69 | * @var \Doctrine\ORM\Configuration |
||
70 | */ |
||
71 | private $config; |
||
72 | |||
73 | /** |
||
74 | * The database connection used by the EntityManager. |
||
75 | * |
||
76 | * @var \Doctrine\DBAL\Connection |
||
77 | */ |
||
78 | private $conn; |
||
79 | |||
80 | /** |
||
81 | * The metadata factory, used to retrieve the ORM metadata of entity classes. |
||
82 | * |
||
83 | * @var \Doctrine\ORM\Mapping\ClassMetadataFactory |
||
84 | */ |
||
85 | private $metadataFactory; |
||
86 | |||
87 | /** |
||
88 | * The UnitOfWork used to coordinate object-level transactions. |
||
89 | * |
||
90 | * @var \Doctrine\ORM\UnitOfWork |
||
91 | */ |
||
92 | private $unitOfWork; |
||
93 | |||
94 | /** |
||
95 | * The event manager that is the central point of the event system. |
||
96 | * |
||
97 | * @var \Doctrine\Common\EventManager |
||
98 | */ |
||
99 | private $eventManager; |
||
100 | |||
101 | /** |
||
102 | * The proxy factory used to create dynamic proxies. |
||
103 | * |
||
104 | * @var \Doctrine\ORM\Proxy\ProxyFactory |
||
105 | */ |
||
106 | private $proxyFactory; |
||
107 | |||
108 | /** |
||
109 | * The repository factory used to create dynamic repositories. |
||
110 | * |
||
111 | * @var \Doctrine\ORM\Repository\RepositoryFactory |
||
112 | */ |
||
113 | private $repositoryFactory; |
||
114 | |||
115 | /** |
||
116 | * The expression builder instance used to generate query expressions. |
||
117 | * |
||
118 | * @var \Doctrine\ORM\Query\Expr |
||
119 | */ |
||
120 | private $expressionBuilder; |
||
121 | |||
122 | /** |
||
123 | * Whether the EntityManager is closed or not. |
||
124 | * |
||
125 | * @var bool |
||
126 | */ |
||
127 | private $closed = false; |
||
128 | |||
129 | /** |
||
130 | * Collection of query filters. |
||
131 | * |
||
132 | * @var \Doctrine\ORM\Query\FilterCollection |
||
133 | */ |
||
134 | private $filterCollection; |
||
135 | |||
136 | /** |
||
137 | * @var \Doctrine\ORM\Cache The second level cache regions API. |
||
138 | */ |
||
139 | private $cache; |
||
140 | |||
141 | /** |
||
142 | * Creates a new EntityManager that operates on the given database connection |
||
143 | * and uses the given Configuration and EventManager implementations. |
||
144 | * |
||
145 | * @param \Doctrine\DBAL\Connection $conn |
||
146 | * @param \Doctrine\ORM\Configuration $config |
||
147 | * @param \Doctrine\Common\EventManager $eventManager |
||
148 | */ |
||
149 | 2360 | protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager) |
|
150 | { |
||
151 | 2360 | $this->conn = $conn; |
|
152 | 2360 | $this->config = $config; |
|
153 | 2360 | $this->eventManager = $eventManager; |
|
154 | |||
155 | 2360 | $metadataFactoryClassName = $config->getClassMetadataFactoryName(); |
|
156 | |||
157 | 2360 | $this->metadataFactory = new $metadataFactoryClassName; |
|
158 | 2360 | $this->metadataFactory->setEntityManager($this); |
|
159 | 2360 | $this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl()); |
|
160 | |||
161 | 2360 | $this->repositoryFactory = $config->getRepositoryFactory(); |
|
162 | 2360 | $this->unitOfWork = new UnitOfWork($this); |
|
163 | 2360 | $this->proxyFactory = new ProxyFactory( |
|
164 | $this, |
||
165 | 2360 | $config->getProxyDir(), |
|
166 | 2360 | $config->getProxyNamespace(), |
|
167 | 2360 | $config->getAutoGenerateProxyClasses() |
|
168 | ); |
||
169 | |||
170 | 2360 | if ($config->isSecondLevelCacheEnabled()) { |
|
171 | 280 | $cacheConfig = $config->getSecondLevelCacheConfiguration(); |
|
172 | 280 | $cacheFactory = $cacheConfig->getCacheFactory(); |
|
173 | 280 | $this->cache = $cacheFactory->createCache($this); |
|
174 | } |
||
175 | 2360 | } |
|
176 | |||
177 | /** |
||
178 | * {@inheritDoc} |
||
179 | */ |
||
180 | 1848 | public function getConnection() |
|
184 | |||
185 | /** |
||
186 | * Gets the metadata factory used to gather the metadata of classes. |
||
187 | * |
||
188 | * @return \Doctrine\ORM\Mapping\ClassMetadataFactory |
||
189 | */ |
||
190 | 2360 | public function getMetadataFactory() |
|
194 | |||
195 | /** |
||
196 | * {@inheritDoc} |
||
197 | */ |
||
198 | 17 | public function getExpressionBuilder() |
|
206 | |||
207 | /** |
||
208 | * {@inheritDoc} |
||
209 | */ |
||
210 | 2 | public function beginTransaction() |
|
214 | |||
215 | /** |
||
216 | * {@inheritDoc} |
||
217 | */ |
||
218 | 214 | public function getCache() |
|
222 | |||
223 | /** |
||
224 | * {@inheritDoc} |
||
225 | */ |
||
226 | 4 | public function transactional($func) |
|
248 | |||
249 | /** |
||
250 | * {@inheritDoc} |
||
251 | */ |
||
252 | 1 | public function commit() |
|
256 | |||
257 | /** |
||
258 | * {@inheritDoc} |
||
259 | */ |
||
260 | 1 | public function rollback() |
|
264 | |||
265 | /** |
||
266 | * Returns the ORM metadata descriptor for a class. |
||
267 | * |
||
268 | * The class name must be the fully-qualified class name without a leading backslash |
||
269 | * (as it is returned by get_class($obj)) or an aliased class name. |
||
270 | * |
||
271 | * Examples: |
||
272 | * MyProject\Domain\User |
||
273 | * sales:PriceRequest |
||
274 | * |
||
275 | * Internal note: Performance-sensitive method. |
||
276 | * |
||
277 | * @param string $className |
||
278 | * |
||
279 | * @return \Doctrine\ORM\Mapping\ClassMetadata |
||
280 | */ |
||
281 | 1914 | public function getClassMetadata($className) |
|
285 | |||
286 | /** |
||
287 | * {@inheritDoc} |
||
288 | */ |
||
289 | 928 | public function createQuery($dql = '') |
|
299 | |||
300 | /** |
||
301 | * {@inheritDoc} |
||
302 | */ |
||
303 | 1 | public function createNamedQuery($name) |
|
307 | |||
308 | /** |
||
309 | * {@inheritDoc} |
||
310 | */ |
||
311 | 21 | public function createNativeQuery($sql, ResultSetMapping $rsm) |
|
320 | |||
321 | /** |
||
322 | * {@inheritDoc} |
||
323 | */ |
||
324 | 1 | public function createNamedNativeQuery($name) |
|
330 | |||
331 | /** |
||
332 | * {@inheritDoc} |
||
333 | */ |
||
334 | 114 | public function createQueryBuilder() |
|
338 | |||
339 | /** |
||
340 | * Flushes all changes to objects that have been queued up to now to the database. |
||
341 | * This effectively synchronizes the in-memory state of managed objects with the |
||
342 | * database. |
||
343 | * |
||
344 | * If an entity is explicitly passed to this method only this entity and |
||
345 | * the cascade-persist semantics + scheduled inserts/removals are synchronized. |
||
346 | * |
||
347 | * @param null|object|array $entity |
||
348 | * |
||
349 | * @return void |
||
350 | * |
||
351 | * @throws \Doctrine\ORM\OptimisticLockException If a version check on an entity that |
||
352 | * makes use of optimistic locking fails. |
||
353 | * @throws ORMException |
||
354 | */ |
||
355 | 1021 | public function flush($entity = null) |
|
361 | |||
362 | /** |
||
363 | * Finds an Entity by its identifier. |
||
364 | * |
||
365 | * @param string $entityName The class name of the entity to find. |
||
366 | * @param mixed $id The identity of the entity to find. |
||
367 | * @param integer|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants |
||
368 | * or NULL if no specific lock mode should be used |
||
369 | * during the search. |
||
370 | * @param integer|null $lockVersion The version of the entity to find when using |
||
371 | * optimistic locking. |
||
372 | * |
||
373 | * @return object|null The entity instance or NULL if the entity can not be found. |
||
374 | * |
||
375 | * @throws OptimisticLockException |
||
376 | * @throws ORMInvalidArgumentException |
||
377 | * @throws TransactionRequiredException |
||
378 | * @throws ORMException |
||
379 | */ |
||
380 | 414 | public function find($entityName, $id, $lockMode = null, $lockVersion = null) |
|
381 | { |
||
382 | 414 | $class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\')); |
|
383 | |||
384 | 414 | if ( ! is_array($id)) { |
|
385 | 370 | if ($class->isIdentifierComposite) { |
|
|
|||
386 | throw ORMInvalidArgumentException::invalidCompositeIdentifier(); |
||
387 | } |
||
388 | |||
389 | 370 | $id = array($class->identifier[0] => $id); |
|
390 | } |
||
391 | |||
392 | 414 | foreach ($id as $i => $value) { |
|
393 | 414 | if (is_object($value) && $this->metadataFactory->hasMetadataFor(ClassUtils::getClass($value))) { |
|
394 | 6 | $id[$i] = $this->unitOfWork->getSingleIdentifierValue($value); |
|
395 | |||
396 | 6 | if ($id[$i] === null) { |
|
397 | 414 | throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); |
|
398 | } |
||
399 | } |
||
400 | } |
||
401 | |||
402 | 413 | $sortedId = array(); |
|
403 | |||
404 | 413 | foreach ($class->identifier as $identifier) { |
|
405 | 413 | if ( ! isset($id[$identifier])) { |
|
406 | 1 | throw ORMException::missingIdentifierField($class->name, $identifier); |
|
407 | } |
||
408 | |||
409 | 412 | $sortedId[$identifier] = $id[$identifier]; |
|
410 | 412 | unset($id[$identifier]); |
|
411 | } |
||
412 | |||
413 | 412 | if ($id) { |
|
414 | 1 | throw ORMException::unrecognizedIdentifierFields($class->name, array_keys($id)); |
|
415 | } |
||
416 | |||
417 | 411 | $unitOfWork = $this->getUnitOfWork(); |
|
418 | |||
419 | // Check identity map first |
||
420 | 411 | if (($entity = $unitOfWork->tryGetById($sortedId, $class->rootEntityName)) !== false) { |
|
421 | 42 | if ( ! ($entity instanceof $class->name)) { |
|
422 | 1 | return null; |
|
423 | } |
||
424 | |||
425 | switch (true) { |
||
426 | 41 | case LockMode::OPTIMISTIC === $lockMode: |
|
427 | 1 | $this->lock($entity, $lockMode, $lockVersion); |
|
428 | break; |
||
429 | |||
430 | 41 | case LockMode::NONE === $lockMode: |
|
431 | 41 | case LockMode::PESSIMISTIC_READ === $lockMode: |
|
432 | 41 | case LockMode::PESSIMISTIC_WRITE === $lockMode: |
|
433 | $persister = $unitOfWork->getEntityPersister($class->name); |
||
434 | $persister->refresh($sortedId, $entity, $lockMode); |
||
435 | break; |
||
436 | } |
||
437 | |||
438 | 41 | return $entity; // Hit! |
|
439 | } |
||
440 | |||
441 | 395 | $persister = $unitOfWork->getEntityPersister($class->name); |
|
442 | |||
443 | switch (true) { |
||
444 | 395 | case LockMode::OPTIMISTIC === $lockMode: |
|
445 | 1 | if ( ! $class->isVersioned) { |
|
446 | 1 | throw OptimisticLockException::notVersioned($class->name); |
|
447 | } |
||
448 | |||
449 | $entity = $persister->load($sortedId); |
||
450 | |||
451 | $unitOfWork->lock($entity, $lockMode, $lockVersion); |
||
452 | |||
453 | return $entity; |
||
454 | |||
455 | 394 | case LockMode::PESSIMISTIC_READ === $lockMode: |
|
456 | 393 | case LockMode::PESSIMISTIC_WRITE === $lockMode: |
|
457 | 2 | if ( ! $this->getConnection()->isTransactionActive()) { |
|
458 | 2 | throw TransactionRequiredException::transactionRequired(); |
|
459 | } |
||
460 | |||
461 | return $persister->load($sortedId, null, null, array(), $lockMode); |
||
462 | |||
463 | default: |
||
464 | 392 | return $persister->loadById($sortedId); |
|
465 | } |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * {@inheritDoc} |
||
470 | */ |
||
471 | 92 | public function getReference($entityName, $id) |
|
509 | |||
510 | /** |
||
511 | * {@inheritDoc} |
||
512 | */ |
||
513 | 4 | public function getPartialReference($entityName, $identifier) |
|
535 | |||
536 | /** |
||
537 | * Clears the EntityManager. All entities that are currently managed |
||
538 | * by this EntityManager become detached. |
||
539 | * |
||
540 | * @param string|null $entityName if given, only entities of this type will get detached |
||
541 | * |
||
542 | * @return void |
||
543 | * |
||
544 | * @throws ORMInvalidArgumentException if a non-null non-string value is given |
||
545 | * @throws \Doctrine\Common\Persistence\Mapping\MappingException if a $entityName is given, but that entity is not |
||
546 | * found in the mappings |
||
547 | */ |
||
548 | 1232 | public function clear($entityName = null) |
|
560 | |||
561 | /** |
||
562 | * {@inheritDoc} |
||
563 | */ |
||
564 | 19 | public function close() |
|
570 | |||
571 | /** |
||
572 | * Tells the EntityManager to make an instance managed and persistent. |
||
573 | * |
||
574 | * The entity will be entered into the database at or before transaction |
||
575 | * commit or as a result of the flush operation. |
||
576 | * |
||
577 | * NOTE: The persist operation always considers entities that are not yet known to |
||
578 | * this EntityManager as NEW. Do not pass detached entities to the persist operation. |
||
579 | * |
||
580 | * @param object $entity The instance to make managed and persistent. |
||
581 | * |
||
582 | * @return void |
||
583 | * |
||
584 | * @throws ORMInvalidArgumentException |
||
585 | * @throws ORMException |
||
586 | */ |
||
587 | 1017 | public function persist($entity) |
|
597 | |||
598 | /** |
||
599 | * Removes an entity instance. |
||
600 | * |
||
601 | * A removed entity will be removed from the database at or before transaction commit |
||
602 | * or as a result of the flush operation. |
||
603 | * |
||
604 | * @param object $entity The entity instance to remove. |
||
605 | * |
||
606 | * @return void |
||
607 | * |
||
608 | * @throws ORMInvalidArgumentException |
||
609 | * @throws ORMException |
||
610 | */ |
||
611 | 51 | public function remove($entity) |
|
621 | |||
622 | /** |
||
623 | * Refreshes the persistent state of an entity from the database, |
||
624 | * overriding any local changes that have not yet been persisted. |
||
625 | * |
||
626 | * @param object $entity The entity to refresh. |
||
627 | * |
||
628 | * @return void |
||
629 | * |
||
630 | * @throws ORMInvalidArgumentException |
||
631 | * @throws ORMException |
||
632 | */ |
||
633 | 19 | public function refresh($entity) |
|
643 | |||
644 | /** |
||
645 | * Detaches an entity from the EntityManager, causing a managed entity to |
||
646 | * become detached. Unflushed changes made to the entity if any |
||
647 | * (including removal of the entity), will not be synchronized to the database. |
||
648 | * Entities which previously referenced the detached entity will continue to |
||
649 | * reference it. |
||
650 | * |
||
651 | * @param object $entity The entity to detach. |
||
652 | * |
||
653 | * @return void |
||
654 | * |
||
655 | * @throws ORMInvalidArgumentException |
||
656 | */ |
||
657 | 13 | public function detach($entity) |
|
665 | |||
666 | /** |
||
667 | * Merges the state of a detached entity into the persistence context |
||
668 | * of this EntityManager and returns the managed copy of the entity. |
||
669 | * The entity passed to merge will not become associated/managed with this EntityManager. |
||
670 | * |
||
671 | * @param object $entity The detached entity to merge into the persistence context. |
||
672 | * |
||
673 | * @return object The managed copy of the entity. |
||
674 | * |
||
675 | * @throws ORMInvalidArgumentException |
||
676 | * @throws ORMException |
||
677 | */ |
||
678 | 42 | public function merge($entity) |
|
688 | |||
689 | /** |
||
690 | * {@inheritDoc} |
||
691 | * |
||
692 | * @todo Implementation need. This is necessary since $e2 = clone $e1; throws an E_FATAL when access anything on $e: |
||
693 | * Fatal error: Maximum function nesting level of '100' reached, aborting! |
||
694 | */ |
||
695 | public function copy($entity, $deep = false) |
||
699 | |||
700 | /** |
||
701 | * {@inheritDoc} |
||
702 | */ |
||
703 | 10 | public function lock($entity, $lockMode, $lockVersion = null) |
|
707 | |||
708 | /** |
||
709 | * Gets the repository for an entity class. |
||
710 | * |
||
711 | * @param string $entityName The name of the entity. |
||
712 | * |
||
713 | * @return \Doctrine\ORM\EntityRepository The repository class. |
||
714 | */ |
||
715 | 147 | public function getRepository($entityName) |
|
719 | |||
720 | /** |
||
721 | * Determines whether an entity instance is managed in this EntityManager. |
||
722 | * |
||
723 | * @param object $entity |
||
724 | * |
||
725 | * @return boolean TRUE if this EntityManager currently manages the given entity, FALSE otherwise. |
||
726 | */ |
||
727 | 24 | public function contains($entity) |
|
733 | |||
734 | /** |
||
735 | * {@inheritDoc} |
||
736 | */ |
||
737 | 2360 | public function getEventManager() |
|
741 | |||
742 | /** |
||
743 | * {@inheritDoc} |
||
744 | */ |
||
745 | 2360 | public function getConfiguration() |
|
749 | |||
750 | /** |
||
751 | * Throws an exception if the EntityManager is closed or currently not active. |
||
752 | * |
||
753 | * @return void |
||
754 | * |
||
755 | * @throws ORMException If the EntityManager is closed. |
||
756 | */ |
||
757 | 1036 | private function errorIfClosed() |
|
763 | |||
764 | /** |
||
765 | * {@inheritDoc} |
||
766 | */ |
||
767 | 1 | public function isOpen() |
|
771 | |||
772 | /** |
||
773 | * {@inheritDoc} |
||
774 | */ |
||
775 | 2360 | public function getUnitOfWork() |
|
779 | |||
780 | /** |
||
781 | * {@inheritDoc} |
||
782 | */ |
||
783 | public function getHydrator($hydrationMode) |
||
787 | |||
788 | /** |
||
789 | * {@inheritDoc} |
||
790 | */ |
||
791 | 891 | public function newHydrator($hydrationMode) |
|
817 | |||
818 | /** |
||
819 | * {@inheritDoc} |
||
820 | */ |
||
821 | 167 | public function getProxyFactory() |
|
825 | |||
826 | /** |
||
827 | * {@inheritDoc} |
||
828 | */ |
||
829 | public function initializeObject($obj) |
||
833 | |||
834 | /** |
||
835 | * Factory method to create EntityManager instances. |
||
836 | * |
||
837 | * @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
||
838 | * @param Configuration $config The Configuration instance to use. |
||
839 | * @param EventManager $eventManager The EventManager instance to use. |
||
840 | * |
||
841 | * @return EntityManager The created EntityManager. |
||
842 | * |
||
843 | * @throws \InvalidArgumentException |
||
844 | * @throws ORMException |
||
845 | */ |
||
846 | 1241 | public static function create($connection, Configuration $config, EventManager $eventManager = null) |
|
856 | |||
857 | /** |
||
858 | * Factory method to create Connection instances. |
||
859 | * |
||
860 | * @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
||
861 | * @param Configuration $config The Configuration instance to use. |
||
862 | * @param EventManager $eventManager The EventManager instance to use. |
||
863 | * |
||
864 | * @return Connection |
||
865 | * |
||
866 | * @throws \InvalidArgumentException |
||
867 | * @throws ORMException |
||
868 | */ |
||
869 | 1241 | protected static function createConnection($connection, Configuration $config, EventManager $eventManager = null) |
|
891 | |||
892 | /** |
||
893 | * {@inheritDoc} |
||
894 | */ |
||
895 | 612 | public function getFilters() |
|
903 | |||
904 | /** |
||
905 | * {@inheritDoc} |
||
906 | */ |
||
907 | 39 | public function isFiltersStateClean() |
|
911 | |||
912 | /** |
||
913 | * {@inheritDoc} |
||
914 | */ |
||
915 | 739 | public function hasFilters() |
|
919 | } |
||
920 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: