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 EntityMetadataFactory 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 EntityMetadataFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class EntityMetadataFactory extends AbstractClassMetadataFactory |
||
14 | { |
||
15 | /** @var EntityManager */ |
||
16 | private $manager; |
||
17 | /** @var MappingDriver */ |
||
18 | private $driver; |
||
19 | |||
20 | /** @var string[] */ |
||
21 | private $aliases = []; |
||
22 | |||
23 | public function registerAlias($namespaceAlias, $namespace) |
||
31 | |||
32 | /** |
||
33 | * @param EntityManager $manager |
||
34 | */ |
||
35 | 28 | public function setEntityManager($manager) |
|
39 | |||
40 | /** |
||
41 | * {@inheritDoc} |
||
42 | */ |
||
43 | 28 | protected function loadMetadata($name) |
|
50 | |||
51 | /** {@inheritdoc} */ |
||
52 | 28 | protected function initialize() |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | * @throws MappingException |
||
61 | */ |
||
62 | protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
||
70 | |||
71 | /** {@inheritdoc} */ |
||
72 | 28 | protected function wakeupReflection(ClassMetadata $class, ReflectionService $reflService) |
|
81 | |||
82 | /** |
||
83 | * Initializes Reflection after ClassMetadata was constructed. |
||
84 | * |
||
85 | * @param ClassMetadata $class |
||
86 | * @param ReflectionService $reflService |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | 28 | protected function initializeReflection(ClassMetadata $class, ReflectionService $reflService) |
|
99 | |||
100 | /** |
||
101 | * Checks whether the class metadata is an entity. |
||
102 | * |
||
103 | * This method should return false for mapped superclasses or embedded classes. |
||
104 | * |
||
105 | * @param ClassMetadata $class |
||
106 | * |
||
107 | * @return boolean |
||
108 | */ |
||
109 | 28 | protected function isEntity(ClassMetadata $class) |
|
113 | |||
114 | /** {@inheritdoc} */ |
||
115 | 28 | protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents) |
|
157 | |||
158 | /** |
||
159 | * Returns the mapping driver implementation. |
||
160 | * |
||
161 | * @return \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver |
||
162 | */ |
||
163 | 28 | protected function getDriver() |
|
167 | |||
168 | /** |
||
169 | * Creates a new ClassMetadata instance for the given class name. |
||
170 | * |
||
171 | * @param string $className |
||
172 | * |
||
173 | * @return ClassMetadata |
||
174 | */ |
||
175 | 28 | protected function newClassMetadataInstance($className) |
|
179 | |||
180 | /** |
||
181 | * Populates the discriminator value of the given metadata (if not set) by iterating over discriminator |
||
182 | * map classes and looking for a fitting one. |
||
183 | * |
||
184 | * @param EntityMetadata $metadata |
||
185 | * |
||
186 | * @return void |
||
187 | * |
||
188 | * @throws MappingException |
||
189 | */ |
||
190 | 28 | private function resolveDiscriminatorValue(EntityMetadata $metadata) |
|
191 | { |
||
192 | 28 | if ($metadata->discriminatorValue |
|
193 | 4 | || !$metadata->discriminatorMap |
|
194 | 4 | || $metadata->isMappedSuperclass |
|
195 | 4 | || !$metadata->reflClass |
|
196 | 4 | || $metadata->reflClass->isAbstract() |
|
197 | 28 | ) { |
|
198 | 28 | return; |
|
199 | } |
||
200 | // minor optimization: avoid loading related metadata when not needed |
||
201 | foreach ($metadata->discriminatorMap as $discriminatorValue => $discriminatorClass) { |
||
202 | if ($discriminatorClass === $metadata->name) { |
||
203 | $metadata->discriminatorValue = $discriminatorValue; |
||
204 | |||
205 | return; |
||
206 | } |
||
207 | } |
||
208 | // iterate over discriminator mappings and resolve actual referenced classes according to existing metadata |
||
209 | foreach ($metadata->discriminatorMap as $discriminatorValue => $discriminatorClass) { |
||
210 | if ($metadata->name === $this->getMetadataFor($discriminatorClass)->getName()) { |
||
211 | $metadata->discriminatorValue = $discriminatorValue; |
||
212 | |||
213 | return; |
||
214 | } |
||
215 | } |
||
216 | |||
217 | throw MappingException::mappedClassNotPartOfDiscriminatorMap($metadata->name, $metadata->rootEntityName); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Adds a default discriminator map if no one is given |
||
222 | * |
||
223 | * If an entity is of any inheritance type and does not contain a |
||
224 | * discriminator map, then the map is generated automatically. This process |
||
225 | * is expensive computation wise. |
||
226 | * |
||
227 | * The automatically generated discriminator map contains the lowercase short name of |
||
228 | * each class as key. |
||
229 | * |
||
230 | * @param EntityMetadata $class |
||
231 | * |
||
232 | * @throws MappingException |
||
233 | */ |
||
234 | 28 | private function addDefaultDiscriminatorMap(EntityMetadata $class) |
|
254 | |||
255 | /** |
||
256 | * Gets the lower-case short name of a class. |
||
257 | * |
||
258 | * @param string $className |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | 28 | private function getShortName($className) |
|
271 | |||
272 | /** |
||
273 | * Adds inherited fields to the subclass mapping. |
||
274 | * |
||
275 | * @param EntityMetadata $subClass |
||
276 | * @param EntityMetadata $parentClass |
||
277 | * |
||
278 | * @return void |
||
279 | */ |
||
280 | 14 | private function addInheritedFields(EntityMetadata $subClass, EntityMetadata $parentClass) |
|
295 | |||
296 | /** |
||
297 | * Adds inherited association mappings to the subclass mapping. |
||
298 | * |
||
299 | * @param EntityMetadata $subClass |
||
300 | * @param EntityMetadata $parentClass |
||
301 | * |
||
302 | * @return void |
||
303 | * |
||
304 | * @throws MappingException |
||
305 | */ |
||
306 | 14 | private function addInheritedRelations(EntityMetadata $subClass, EntityMetadata $parentClass) |
|
318 | } |
||
319 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.