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) | |
| 147 | |||
| 148 | /** | ||
| 149 | * Returns the mapping driver implementation. | ||
| 150 | * | ||
| 151 | * @return \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver | ||
| 152 | */ | ||
| 153 | 28 | protected function getDriver() | |
| 157 | |||
| 158 | /** | ||
| 159 | * Creates a new ClassMetadata instance for the given class name. | ||
| 160 | * | ||
| 161 | * @param string $className | ||
| 162 | * | ||
| 163 | * @return ClassMetadata | ||
| 164 | */ | ||
| 165 | 28 | protected function newClassMetadataInstance($className) | |
| 169 | |||
| 170 | /** | ||
| 171 | * Populates the discriminator value of the given metadata (if not set) by iterating over discriminator | ||
| 172 | * map classes and looking for a fitting one. | ||
| 173 | * | ||
| 174 | * @param EntityMetadata $metadata | ||
| 175 | * | ||
| 176 | * @return void | ||
| 177 | * | ||
| 178 | * @throws MappingException | ||
| 179 | */ | ||
| 180 | 28 | private function resolveDiscriminatorValue(EntityMetadata $metadata) | |
| 209 | |||
| 210 | /** | ||
| 211 | * Adds a default discriminator map if no one is given | ||
| 212 | * | ||
| 213 | * If an entity is of any inheritance type and does not contain a | ||
| 214 | * discriminator map, then the map is generated automatically. This process | ||
| 215 | * is expensive computation wise. | ||
| 216 | * | ||
| 217 | * The automatically generated discriminator map contains the lowercase short name of | ||
| 218 | * each class as key. | ||
| 219 | * | ||
| 220 | * @param EntityMetadata $class | ||
| 221 | * | ||
| 222 | * @throws MappingException | ||
| 223 | */ | ||
| 224 | 28 | private function addDefaultDiscriminatorMap(EntityMetadata $class) | |
| 244 | |||
| 245 | /** | ||
| 246 | * Gets the lower-case short name of a class. | ||
| 247 | * | ||
| 248 | * @param string $className | ||
| 249 | * | ||
| 250 | * @return string | ||
| 251 | */ | ||
| 252 | 28 | private function getShortName($className) | |
| 261 | |||
| 262 | /** | ||
| 263 | * Adds inherited fields to the subclass mapping. | ||
| 264 | * | ||
| 265 | * @param EntityMetadata $subClass | ||
| 266 | * @param EntityMetadata $parentClass | ||
| 267 | * | ||
| 268 | * @return void | ||
| 269 | */ | ||
| 270 | 15 | private function addInheritedFields(EntityMetadata $subClass, EntityMetadata $parentClass) | |
| 285 | |||
| 286 | /** | ||
| 287 | * Adds inherited association mappings to the subclass mapping. | ||
| 288 | * | ||
| 289 | * @param EntityMetadata $subClass | ||
| 290 | * @param EntityMetadata $parentClass | ||
| 291 | * | ||
| 292 | * @return void | ||
| 293 | * | ||
| 294 | * @throws MappingException | ||
| 295 | */ | ||
| 296 | 15 | private function addInheritedRelations(EntityMetadata $subClass, EntityMetadata $parentClass) | |
| 308 | } | ||
| 309 | 
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.