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 | 2324 | protected function __construct(Connection $conn, Configuration $config, EventManager $eventManager) |
|
175 | |||
176 | /** |
||
177 | * {@inheritDoc} |
||
178 | */ |
||
179 | 1827 | public function getConnection() |
|
183 | |||
184 | /** |
||
185 | * Gets the metadata factory used to gather the metadata of classes. |
||
186 | * |
||
187 | * @return \Doctrine\ORM\Mapping\ClassMetadataFactory |
||
188 | */ |
||
189 | 2324 | public function getMetadataFactory() |
|
193 | |||
194 | /** |
||
195 | * {@inheritDoc} |
||
196 | */ |
||
197 | 17 | public function getExpressionBuilder() |
|
205 | |||
206 | /** |
||
207 | * {@inheritDoc} |
||
208 | */ |
||
209 | 2 | public function beginTransaction() |
|
213 | |||
214 | /** |
||
215 | * {@inheritDoc} |
||
216 | */ |
||
217 | 210 | public function getCache() |
|
221 | |||
222 | /** |
||
223 | * {@inheritDoc} |
||
224 | */ |
||
225 | 4 | public function transactional($func) |
|
247 | |||
248 | /** |
||
249 | * {@inheritDoc} |
||
250 | */ |
||
251 | 1 | public function commit() |
|
255 | |||
256 | /** |
||
257 | * {@inheritDoc} |
||
258 | */ |
||
259 | 1 | public function rollback() |
|
263 | |||
264 | /** |
||
265 | * Returns the ORM metadata descriptor for a class. |
||
266 | * |
||
267 | * The class name must be the fully-qualified class name without a leading backslash |
||
268 | * (as it is returned by get_class($obj)) or an aliased class name. |
||
269 | * |
||
270 | * Examples: |
||
271 | * MyProject\Domain\User |
||
272 | * sales:PriceRequest |
||
273 | * |
||
274 | * Internal note: Performance-sensitive method. |
||
275 | * |
||
276 | * @param string $className |
||
277 | * |
||
278 | * @return \Doctrine\ORM\Mapping\ClassMetadata |
||
279 | */ |
||
280 | 1881 | public function getClassMetadata($className) |
|
284 | |||
285 | /** |
||
286 | * {@inheritDoc} |
||
287 | */ |
||
288 | 921 | public function createQuery($dql = '') |
|
298 | |||
299 | /** |
||
300 | * {@inheritDoc} |
||
301 | */ |
||
302 | 1 | public function createNamedQuery($name) |
|
306 | |||
307 | /** |
||
308 | * {@inheritDoc} |
||
309 | */ |
||
310 | 21 | public function createNativeQuery($sql, ResultSetMapping $rsm) |
|
319 | |||
320 | /** |
||
321 | * {@inheritDoc} |
||
322 | */ |
||
323 | 1 | public function createNamedNativeQuery($name) |
|
329 | |||
330 | /** |
||
331 | * {@inheritDoc} |
||
332 | */ |
||
333 | 112 | public function createQueryBuilder() |
|
337 | |||
338 | /** |
||
339 | * Flushes all changes to objects that have been queued up to now to the database. |
||
340 | * This effectively synchronizes the in-memory state of managed objects with the |
||
341 | * database. |
||
342 | * |
||
343 | * If an entity is explicitly passed to this method only this entity and |
||
344 | * the cascade-persist semantics + scheduled inserts/removals are synchronized. |
||
345 | * |
||
346 | * @param null|object|array $entity |
||
347 | * |
||
348 | * @return void |
||
349 | * |
||
350 | * @throws \Doctrine\ORM\OptimisticLockException If a version check on an entity that |
||
351 | * makes use of optimistic locking fails. |
||
352 | * @throws ORMException |
||
353 | */ |
||
354 | 1009 | public function flush($entity = null) |
|
360 | |||
361 | /** |
||
362 | * Finds an Entity by its identifier. |
||
363 | * |
||
364 | * @param string $entityName The class name of the entity to find. |
||
365 | * @param mixed $id The identity of the entity to find. |
||
366 | * @param integer|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants |
||
367 | * or NULL if no specific lock mode should be used |
||
368 | * during the search. |
||
369 | * @param integer|null $lockVersion The version of the entity to find when using |
||
370 | * optimistic locking. |
||
371 | * |
||
372 | * @return object|null The entity instance or NULL if the entity can not be found. |
||
373 | * |
||
374 | * @throws OptimisticLockException |
||
375 | * @throws ORMInvalidArgumentException |
||
376 | * @throws TransactionRequiredException |
||
377 | * @throws ORMException |
||
378 | */ |
||
379 | 409 | public function find($entityName, $id, $lockMode = null, $lockVersion = null) |
|
466 | |||
467 | /** |
||
468 | * {@inheritDoc} |
||
469 | */ |
||
470 | 90 | public function getReference($entityName, $id) |
|
508 | |||
509 | /** |
||
510 | * {@inheritDoc} |
||
511 | */ |
||
512 | 4 | public function getPartialReference($entityName, $identifier) |
|
534 | |||
535 | /** |
||
536 | * Clears the EntityManager. All entities that are currently managed |
||
537 | * by this EntityManager become detached. |
||
538 | * |
||
539 | * @param string|null $entityName if given, only entities of this type will get detached |
||
540 | * |
||
541 | * @return void |
||
542 | */ |
||
543 | 1215 | public function clear($entityName = null) |
|
547 | |||
548 | /** |
||
549 | * {@inheritDoc} |
||
550 | */ |
||
551 | 19 | public function close() |
|
557 | |||
558 | /** |
||
559 | * Tells the EntityManager to make an instance managed and persistent. |
||
560 | * |
||
561 | * The entity will be entered into the database at or before transaction |
||
562 | * commit or as a result of the flush operation. |
||
563 | * |
||
564 | * NOTE: The persist operation always considers entities that are not yet known to |
||
565 | * this EntityManager as NEW. Do not pass detached entities to the persist operation. |
||
566 | * |
||
567 | * @param object $entity The instance to make managed and persistent. |
||
568 | * |
||
569 | * @return void |
||
570 | * |
||
571 | * @throws ORMInvalidArgumentException |
||
572 | * @throws ORMException |
||
573 | */ |
||
574 | 1003 | public function persist($entity) |
|
584 | |||
585 | /** |
||
586 | * Removes an entity instance. |
||
587 | * |
||
588 | * A removed entity will be removed from the database at or before transaction commit |
||
589 | * or as a result of the flush operation. |
||
590 | * |
||
591 | * @param object $entity The entity instance to remove. |
||
592 | * |
||
593 | * @return void |
||
594 | * |
||
595 | * @throws ORMInvalidArgumentException |
||
596 | * @throws ORMException |
||
597 | */ |
||
598 | 50 | public function remove($entity) |
|
608 | |||
609 | /** |
||
610 | * Refreshes the persistent state of an entity from the database, |
||
611 | * overriding any local changes that have not yet been persisted. |
||
612 | * |
||
613 | * @param object $entity The entity to refresh. |
||
614 | * |
||
615 | * @return void |
||
616 | * |
||
617 | * @throws ORMInvalidArgumentException |
||
618 | * @throws ORMException |
||
619 | */ |
||
620 | 18 | public function refresh($entity) |
|
630 | |||
631 | /** |
||
632 | * Detaches an entity from the EntityManager, causing a managed entity to |
||
633 | * become detached. Unflushed changes made to the entity if any |
||
634 | * (including removal of the entity), will not be synchronized to the database. |
||
635 | * Entities which previously referenced the detached entity will continue to |
||
636 | * reference it. |
||
637 | * |
||
638 | * @param object $entity The entity to detach. |
||
639 | * |
||
640 | * @return void |
||
641 | * |
||
642 | * @throws ORMInvalidArgumentException |
||
643 | */ |
||
644 | 13 | public function detach($entity) |
|
652 | |||
653 | /** |
||
654 | * Merges the state of a detached entity into the persistence context |
||
655 | * of this EntityManager and returns the managed copy of the entity. |
||
656 | * The entity passed to merge will not become associated/managed with this EntityManager. |
||
657 | * |
||
658 | * @param object $entity The detached entity to merge into the persistence context. |
||
659 | * |
||
660 | * @return object The managed copy of the entity. |
||
661 | * |
||
662 | * @throws ORMInvalidArgumentException |
||
663 | * @throws ORMException |
||
664 | */ |
||
665 | 42 | public function merge($entity) |
|
675 | |||
676 | /** |
||
677 | * {@inheritDoc} |
||
678 | * |
||
679 | * @todo Implementation need. This is necessary since $e2 = clone $e1; throws an E_FATAL when access anything on $e: |
||
680 | * Fatal error: Maximum function nesting level of '100' reached, aborting! |
||
681 | */ |
||
682 | public function copy($entity, $deep = false) |
||
686 | |||
687 | /** |
||
688 | * {@inheritDoc} |
||
689 | */ |
||
690 | 10 | public function lock($entity, $lockMode, $lockVersion = null) |
|
694 | |||
695 | /** |
||
696 | * Gets the repository for an entity class. |
||
697 | * |
||
698 | * @param string $entityName The name of the entity. |
||
699 | * |
||
700 | * @return \Doctrine\ORM\EntityRepository The repository class. |
||
701 | */ |
||
702 | 144 | public function getRepository($entityName) |
|
706 | |||
707 | /** |
||
708 | * Determines whether an entity instance is managed in this EntityManager. |
||
709 | * |
||
710 | * @param object $entity |
||
711 | * |
||
712 | * @return boolean TRUE if this EntityManager currently manages the given entity, FALSE otherwise. |
||
713 | */ |
||
714 | 22 | public function contains($entity) |
|
720 | |||
721 | /** |
||
722 | * {@inheritDoc} |
||
723 | */ |
||
724 | 2324 | public function getEventManager() |
|
728 | |||
729 | /** |
||
730 | * {@inheritDoc} |
||
731 | */ |
||
732 | 2324 | public function getConfiguration() |
|
736 | |||
737 | /** |
||
738 | * Throws an exception if the EntityManager is closed or currently not active. |
||
739 | * |
||
740 | * @return void |
||
741 | * |
||
742 | * @throws ORMException If the EntityManager is closed. |
||
743 | */ |
||
744 | 1022 | private function errorIfClosed() |
|
750 | |||
751 | /** |
||
752 | * {@inheritDoc} |
||
753 | */ |
||
754 | 1 | public function isOpen() |
|
758 | |||
759 | /** |
||
760 | * {@inheritDoc} |
||
761 | */ |
||
762 | 2324 | public function getUnitOfWork() |
|
766 | |||
767 | /** |
||
768 | * {@inheritDoc} |
||
769 | */ |
||
770 | public function getHydrator($hydrationMode) |
||
774 | |||
775 | /** |
||
776 | * {@inheritDoc} |
||
777 | */ |
||
778 | 889 | public function newHydrator($hydrationMode) |
|
804 | |||
805 | /** |
||
806 | * {@inheritDoc} |
||
807 | */ |
||
808 | 165 | public function getProxyFactory() |
|
812 | |||
813 | /** |
||
814 | * {@inheritDoc} |
||
815 | */ |
||
816 | public function initializeObject($obj) |
||
820 | |||
821 | /** |
||
822 | * Factory method to create EntityManager instances. |
||
823 | * |
||
824 | * @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
||
825 | * @param Configuration $config The Configuration instance to use. |
||
826 | * @param EventManager $eventManager The EventManager instance to use. |
||
827 | * |
||
828 | * @return EntityManager The created EntityManager. |
||
829 | * |
||
830 | * @throws \InvalidArgumentException |
||
831 | * @throws ORMException |
||
832 | */ |
||
833 | 1227 | public static function create($connection, Configuration $config, EventManager $eventManager = null) |
|
834 | { |
||
835 | 1227 | if ( ! $config->getMetadataDriverImpl()) { |
|
836 | throw ORMException::missingMappingDriverImpl(); |
||
837 | } |
||
838 | |||
839 | 1227 | $connection = static::createConnection($connection, $config, $eventManager); |
|
840 | |||
841 | 1227 | return new EntityManager($connection, $config, $connection->getEventManager()); |
|
842 | } |
||
843 | |||
844 | /** |
||
845 | * Factory method to create Connection instances. |
||
846 | * |
||
847 | * @param array|Connection $connection An array with the connection parameters or an existing Connection instance. |
||
848 | * @param Configuration $config The Configuration instance to use. |
||
849 | * @param EventManager $eventManager The EventManager instance to use. |
||
850 | * |
||
851 | * @return Connection |
||
852 | * |
||
853 | * @throws \InvalidArgumentException |
||
854 | * @throws ORMException |
||
855 | */ |
||
856 | 1227 | protected static function createConnection($connection, Configuration $config, EventManager $eventManager = null) |
|
857 | { |
||
858 | 1227 | if (is_array($connection)) { |
|
859 | return DriverManager::getConnection($connection, $config, $eventManager ?: new EventManager()); |
||
860 | } |
||
861 | |||
862 | 1227 | if ( ! $connection instanceof Connection) { |
|
863 | throw new \InvalidArgumentException("Invalid argument: " . $connection); |
||
864 | } |
||
865 | |||
866 | 1227 | if ($eventManager !== null && $connection->getEventManager() !== $eventManager) { |
|
867 | throw ORMException::mismatchedEventManager(); |
||
868 | } |
||
869 | |||
870 | 1227 | return $connection; |
|
871 | } |
||
872 | |||
873 | /** |
||
874 | * {@inheritDoc} |
||
875 | */ |
||
876 | 613 | public function getFilters() |
|
884 | |||
885 | /** |
||
886 | * {@inheritDoc} |
||
887 | */ |
||
888 | 35 | public function isFiltersStateClean() |
|
892 | |||
893 | /** |
||
894 | * {@inheritDoc} |
||
895 | */ |
||
896 | 732 | public function hasFilters() |
|
900 | } |
||
901 |
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: