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 |
||
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 | 18 | public function setEntityManager($manager) |
|
39 | |||
40 | /** {@inheritdoc} */ |
||
41 | 18 | protected function initialize() |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | * @throws MappingException |
||
50 | */ |
||
51 | protected function getFqcnFromAlias($namespaceAlias, $simpleClassName) |
||
59 | |||
60 | /** {@inheritdoc} */ |
||
61 | 18 | protected function wakeupReflection(ClassMetadata $class, ReflectionService $reflService) |
|
70 | |||
71 | /** |
||
72 | * Initializes Reflection after ClassMetadata was constructed. |
||
73 | * |
||
74 | * @param ClassMetadata $class |
||
75 | * @param ReflectionService $reflService |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | 18 | protected function initializeReflection(ClassMetadata $class, ReflectionService $reflService) |
|
88 | |||
89 | /** |
||
90 | * Checks whether the class metadata is an entity. |
||
91 | * |
||
92 | * This method should return false for mapped superclasses or embedded classes. |
||
93 | * |
||
94 | * @param ClassMetadata $class |
||
95 | * |
||
96 | * @return boolean |
||
97 | */ |
||
98 | 18 | protected function isEntity(ClassMetadata $class) |
|
102 | |||
103 | /** |
||
104 | * Actually loads the metadata from the underlying metadata. |
||
105 | * |
||
106 | * @param EntityMetadata $class |
||
107 | * @param EntityMetadata|null $parent |
||
108 | * @param bool $rootEntityFound |
||
109 | * @param array $nonSuperclassParents All parent class names |
||
110 | * that are not marked as mapped superclasses. |
||
111 | * |
||
112 | * @return void |
||
113 | * @throws MappingException |
||
114 | */ |
||
115 | 18 | protected function doLoadMetadata($class, $parent, $rootEntityFound, array $nonSuperclassParents) |
|
140 | |||
141 | /** |
||
142 | * Returns the mapping driver implementation. |
||
143 | * |
||
144 | * @return \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver |
||
145 | */ |
||
146 | 18 | protected function getDriver() |
|
150 | |||
151 | /** |
||
152 | * Creates a new ClassMetadata instance for the given class name. |
||
153 | * |
||
154 | * @param string $className |
||
155 | * |
||
156 | * @return ClassMetadata |
||
157 | */ |
||
158 | 18 | protected function newClassMetadataInstance($className) |
|
162 | |||
163 | /** |
||
164 | * Adds inherited fields to the subclass mapping. |
||
165 | * |
||
166 | * @param EntityMetadata $subClass |
||
167 | * @param EntityMetadata $parentClass |
||
168 | * |
||
169 | * @return void |
||
170 | */ |
||
171 | 5 | private function addInheritedFields(EntityMetadata $subClass, EntityMetadata $parentClass) |
|
186 | |||
187 | /** |
||
188 | * Adds inherited association mappings to the subclass mapping. |
||
189 | * |
||
190 | * @param EntityMetadata $subClass |
||
191 | * @param EntityMetadata $parentClass |
||
192 | * |
||
193 | * @return void |
||
194 | * |
||
195 | * @throws MappingException |
||
196 | */ |
||
197 | 5 | private function addInheritedRelations(EntityMetadata $subClass, EntityMetadata $parentClass) |
|
209 | } |
||
210 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.