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 |
||
63 | /* final */class EntityManager implements EntityManagerInterface |
||
64 | { |
||
65 | /** |
||
66 | * The used Configuration. |
||
67 | * |
||
68 | * @var \Doctrine\ORM\Configuration |
||
69 | */ |
||
70 | private $config; |
||
71 | |||
72 | /** |
||
73 | * The database connection used by the EntityManager. |
||
74 | * |
||
75 | * @var \Doctrine\DBAL\Connection |
||
76 | */ |
||
77 | private $conn; |
||
78 | |||
79 | /** |
||
80 | * The metadata factory, used to retrieve the ORM metadata of entity classes. |
||
81 | * |
||
82 | * @var \Doctrine\ORM\Mapping\ClassMetadataFactory |
||
83 | */ |
||
84 | private $metadataFactory; |
||
85 | |||
86 | /** |
||
87 | * The UnitOfWork used to coordinate object-level transactions. |
||
88 | * |
||
89 | * @var \Doctrine\ORM\UnitOfWork |
||
90 | */ |
||
91 | private $unitOfWork; |
||
92 | |||
93 | /** |
||
94 | * The event manager that is the central point of the event system. |
||
95 | * |
||
96 | * @var \Doctrine\Common\EventManager |
||
97 | */ |
||
98 | private $eventManager; |
||
99 | |||
100 | /** |
||
101 | * The proxy factory used to create dynamic proxies. |
||
102 | * |
||
103 | * @var \Doctrine\ORM\Proxy\ProxyFactory |
||
104 | */ |
||
105 | private $proxyFactory; |
||
106 | |||
107 | /** |
||
108 | * The repository factory used to create dynamic repositories. |
||
109 | * |
||
110 | * @var \Doctrine\ORM\Repository\RepositoryFactory |
||
111 | */ |
||
112 | private $repositoryFactory; |
||
113 | |||
114 | /** |
||
115 | * The expression builder instance used to generate query expressions. |
||
116 | * |
||
117 | * @var \Doctrine\ORM\Query\Expr |
||
118 | */ |
||
119 | private $expressionBuilder; |
||
120 | |||
121 | /** |
||
122 | * Whether the EntityManager is closed or not. |
||
123 | * |
||
124 | * @var bool |
||
125 | */ |
||
126 | private $closed = false; |
||
127 | |||
128 | /** |
||
129 | * Collection of query filters. |
||
130 | * |
||
131 | * @var \Doctrine\ORM\Query\FilterCollection |
||
132 | */ |
||
133 | private $filterCollection; |
||
134 | |||
135 | /** |
||
136 | * @var \Doctrine\ORM\Cache The second level cache regions API. |
||
137 | */ |
||
138 | private $cache; |
||
139 | |||
140 | /** |
||
141 | * Creates a new EntityManager that operates on the given database connection |
||
142 | * and uses the given Configuration and EventManager implementations. |
||
143 | * |
||
144 | * @param \Doctrine\DBAL\Connection $conn |
||
145 | * @param \Doctrine\ORM\Configuration $config |
||
146 | * @param \Doctrine\Common\EventManager $eventManager |
||
147 | */ |
||
148 | 2347 | protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager) |
|
176 | |||
177 | /** |
||
178 | * {@inheritDoc} |
||
179 | */ |
||
180 | 1839 | 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 | 2347 | 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 | 212 | 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 | 1904 | public function getClassMetadata($className) |
|
285 | |||
286 | /** |
||
287 | * {@inheritDoc} |
||
288 | */ |
||
289 | 925 | 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 | 113 | 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 | 1016 | 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 | 411 | public function find($entityName, $id, $lockMode = null, $lockVersion = null) |
|
467 | |||
468 | /** |
||
469 | * {@inheritDoc} |
||
470 | */ |
||
471 | 91 | 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 | 1223 | public function clear($entityName = null) |
|
548 | |||
549 | /** |
||
550 | * {@inheritDoc} |
||
551 | */ |
||
552 | 19 | public function close() |
|
558 | |||
559 | /** |
||
560 | * Tells the EntityManager to make an instance managed and persistent. |
||
561 | * |
||
562 | * The entity will be entered into the database at or before transaction |
||
563 | * commit or as a result of the flush operation. |
||
564 | * |
||
565 | * NOTE: The persist operation always considers entities that are not yet known to |
||
566 | * this EntityManager as NEW. Do not pass detached entities to the persist operation. |
||
567 | * |
||
568 | * @param object $entity The instance to make managed and persistent. |
||
569 | * |
||
570 | * @return void |
||
571 | * |
||
572 | * @throws ORMInvalidArgumentException |
||
573 | * @throws ORMException |
||
574 | */ |
||
575 | 1010 | public function persist($entity) |
|
585 | |||
586 | /** |
||
587 | * Removes an entity instance. |
||
588 | * |
||
589 | * A removed entity will be removed from the database at or before transaction commit |
||
590 | * or as a result of the flush operation. |
||
591 | * |
||
592 | * @param object $entity The entity instance to remove. |
||
593 | * |
||
594 | * @return void |
||
595 | * |
||
596 | * @throws ORMInvalidArgumentException |
||
597 | * @throws ORMException |
||
598 | */ |
||
599 | 51 | public function remove($entity) |
|
609 | |||
610 | /** |
||
611 | * Refreshes the persistent state of an entity from the database, |
||
612 | * overriding any local changes that have not yet been persisted. |
||
613 | * |
||
614 | * @param object $entity The entity to refresh. |
||
615 | * |
||
616 | * @return void |
||
617 | * |
||
618 | * @throws ORMInvalidArgumentException |
||
619 | * @throws ORMException |
||
620 | */ |
||
621 | 19 | public function refresh($entity) |
|
631 | |||
632 | /** |
||
633 | * Detaches an entity from the EntityManager, causing a managed entity to |
||
634 | * become detached. Unflushed changes made to the entity if any |
||
635 | * (including removal of the entity), will not be synchronized to the database. |
||
636 | * Entities which previously referenced the detached entity will continue to |
||
637 | * reference it. |
||
638 | * |
||
639 | * @param object $entity The entity to detach. |
||
640 | * |
||
641 | * @return void |
||
642 | * |
||
643 | * @throws ORMInvalidArgumentException |
||
644 | */ |
||
645 | 13 | public function detach($entity) |
|
653 | |||
654 | /** |
||
655 | * Merges the state of a detached entity into the persistence context |
||
656 | * of this EntityManager and returns the managed copy of the entity. |
||
657 | * The entity passed to merge will not become associated/managed with this EntityManager. |
||
658 | * |
||
659 | * @param object $entity The detached entity to merge into the persistence context. |
||
660 | * |
||
661 | * @return object The managed copy of the entity. |
||
662 | * |
||
663 | * @throws ORMInvalidArgumentException |
||
664 | * @throws ORMException |
||
665 | */ |
||
666 | 42 | public function merge($entity) |
|
676 | |||
677 | /** |
||
678 | * {@inheritDoc} |
||
679 | * |
||
680 | * @todo Implementation need. This is necessary since $e2 = clone $e1; throws an E_FATAL when access anything on $e: |
||
681 | * Fatal error: Maximum function nesting level of '100' reached, aborting! |
||
682 | */ |
||
683 | public function copy($entity, $deep = false) |
||
687 | |||
688 | /** |
||
689 | * {@inheritDoc} |
||
690 | */ |
||
691 | 10 | public function lock($entity, $lockMode, $lockVersion = null) |
|
695 | |||
696 | /** |
||
697 | * Gets the repository for an entity class. |
||
698 | * |
||
699 | * @param string $entityName The name of the entity. |
||
700 | * |
||
701 | * @return \Doctrine\ORM\EntityRepository The repository class. |
||
702 | */ |
||
703 | 147 | public function getRepository($entityName) |
|
707 | |||
708 | /** |
||
709 | * Determines whether an entity instance is managed in this EntityManager. |
||
710 | * |
||
711 | * @param object $entity |
||
712 | * |
||
713 | * @return boolean TRUE if this EntityManager currently manages the given entity, FALSE otherwise. |
||
714 | */ |
||
715 | 22 | public function contains($entity) |
|
721 | |||
722 | /** |
||
723 | * {@inheritDoc} |
||
724 | */ |
||
725 | 2347 | public function getEventManager() |
|
729 | |||
730 | /** |
||
731 | * {@inheritDoc} |
||
732 | */ |
||
733 | 2347 | public function getConfiguration() |
|
737 | |||
738 | /** |
||
739 | * Throws an exception if the EntityManager is closed or currently not active. |
||
740 | * |
||
741 | * @return void |
||
742 | * |
||
743 | * @throws ORMException If the EntityManager is closed. |
||
744 | */ |
||
745 | 1029 | private function errorIfClosed() |
|
751 | |||
752 | /** |
||
753 | * {@inheritDoc} |
||
754 | */ |
||
755 | 1 | public function isOpen() |
|
759 | |||
760 | /** |
||
761 | * {@inheritDoc} |
||
762 | */ |
||
763 | 2347 | public function getUnitOfWork() |
|
767 | |||
768 | /** |
||
769 | * {@inheritDoc} |
||
770 | */ |
||
771 | public function getHydrator($hydrationMode) |
||
775 | |||
776 | /** |
||
777 | * {@inheritDoc} |
||
778 | */ |
||
779 | 885 | public function newHydrator($hydrationMode) |
|
805 | |||
806 | /** |
||
807 | * {@inheritDoc} |
||
808 | */ |
||
809 | 165 | public function getProxyFactory() |
|
813 | |||
814 | /** |
||
815 | * {@inheritDoc} |
||
816 | */ |
||
817 | public function initializeObject($obj) |
||
821 | |||
822 | /** |
||
823 | * Factory method to create EntityManager instances. |
||
824 | * |
||
825 | * @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
||
826 | * @param Configuration $config The Configuration instance to use. |
||
827 | * @param EventManager $eventManager The EventManager instance to use. |
||
828 | * |
||
829 | * @return EntityManager The created EntityManager. |
||
830 | * |
||
831 | * @throws \InvalidArgumentException |
||
832 | * @throws ORMException |
||
833 | */ |
||
834 | 1235 | public static function create($connection, Configuration $config, EventManager $eventManager = null) |
|
844 | |||
845 | /** |
||
846 | * Factory method to create Connection instances. |
||
847 | * |
||
848 | * @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
||
849 | * @param Configuration $config The Configuration instance to use. |
||
850 | * @param EventManager $eventManager The EventManager instance to use. |
||
851 | * |
||
852 | * @return Connection |
||
853 | * |
||
854 | * @throws \InvalidArgumentException |
||
855 | * @throws ORMException |
||
856 | */ |
||
857 | 1235 | protected static function createConnection($connection, Configuration $config, EventManager $eventManager = null) |
|
873 | |||
874 | /** |
||
875 | * {@inheritDoc} |
||
876 | */ |
||
877 | 608 | public function getFilters() |
|
885 | |||
886 | /** |
||
887 | * {@inheritDoc} |
||
888 | */ |
||
889 | 37 | public function isFiltersStateClean() |
|
893 | |||
894 | /** |
||
895 | * {@inheritDoc} |
||
896 | */ |
||
897 | 736 | public function hasFilters() |
|
901 | } |
||
902 |
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: