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:
1 | <?php |
||
9 | class ArrayHydrator |
||
10 | { |
||
11 | /** |
||
12 | * @var EntityManager |
||
13 | */ |
||
14 | protected $entityManager; |
||
15 | |||
16 | /** |
||
17 | * @var bool |
||
18 | */ |
||
19 | protected $hydrateAssociationReferences = true; |
||
20 | |||
21 | /** |
||
22 | * @param EntityManager $entityManager |
||
23 | */ |
||
24 | public function __construct(EntityManager $entityManager) |
||
28 | |||
29 | /** |
||
30 | * @param $entity |
||
31 | * @param array $data |
||
32 | * @return mixed|object |
||
33 | * @throws Exception |
||
34 | */ |
||
35 | public function hydrate($entity, array $data) |
||
48 | |||
49 | /** |
||
50 | * @param boolean $hydrateAssociationReferences |
||
51 | */ |
||
52 | public function setHydrateAssociationReferences($hydrateAssociationReferences) |
||
56 | |||
57 | /** |
||
58 | * @param $entity |
||
59 | * @param $data |
||
60 | * @return object |
||
61 | */ |
||
62 | protected function hydrateProperties($entity, $data) |
||
89 | |||
90 | /** |
||
91 | * @param $entity |
||
92 | * @param $data |
||
93 | * @return mixed |
||
94 | */ |
||
95 | protected function hydrateAssociations($entity, $data) |
||
112 | |||
113 | /** |
||
114 | * @param $entity |
||
115 | * @param $propertyName |
||
116 | * @param $mapping |
||
117 | * @param $value |
||
118 | * @return mixed |
||
119 | */ |
||
120 | protected function hydrateToOneAssociation($entity, $propertyName, $mapping, $value) |
||
131 | |||
132 | /** |
||
133 | * @param $entity |
||
134 | * @param $propertyName |
||
135 | * @param $mapping |
||
136 | * @param $value |
||
137 | * @return mixed |
||
138 | */ |
||
139 | protected function hydrateToManyAssociation($entity, $propertyName, $mapping, $value) |
||
158 | |||
159 | /** |
||
160 | * @param $entity |
||
161 | * @param $propertyName |
||
162 | * @param $value |
||
163 | * @param null $reflectionObject |
||
164 | * @return mixed |
||
165 | */ |
||
166 | protected function setProperty($entity, $propertyName, $value, $reflectionObject = null) |
||
174 | |||
175 | /** |
||
176 | * @param $className |
||
177 | * @param $id |
||
178 | * @return bool|\Doctrine\Common\Proxy\Proxy|null|object |
||
179 | * @throws \Doctrine\ORM\ORMException |
||
180 | * @throws \Doctrine\ORM\OptimisticLockException |
||
181 | * @throws \Doctrine\ORM\TransactionRequiredException |
||
182 | */ |
||
183 | protected function fetchAssociationEntity($className, $id) |
||
191 | } |
||
192 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.