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 EntityManager implements ApiEntityManager |
||
14 | { |
||
15 | /** @var EntityMetadataFactory */ |
||
16 | private $metadataFactory; |
||
17 | /** @var Configuration */ |
||
18 | private $configuration; |
||
19 | /** @var ObjectRepository[] */ |
||
20 | private $repositories = []; |
||
21 | /** @var UnitOfWork */ |
||
22 | private $unitOfWork; |
||
23 | /** @var ProxyFactory */ |
||
24 | private $proxyFactory; |
||
25 | |||
26 | /** |
||
27 | * EntityManager constructor. |
||
28 | * |
||
29 | * @param Configuration $configuration |
||
30 | */ |
||
31 | 20 | public function __construct(Configuration $configuration) |
|
41 | |||
42 | /** {@inheritdoc} */ |
||
43 | 20 | public function getConfiguration() |
|
47 | |||
48 | /** {@inheritdoc} */ |
||
49 | 10 | public function find($className, $id) |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | * @return ApiMetadata |
||
65 | */ |
||
66 | 20 | public function getClassMetadata($className) |
|
70 | |||
71 | /** {@inheritdoc} */ |
||
72 | 20 | public function getMetadataFactory() |
|
76 | |||
77 | /** {@inheritdoc} */ |
||
78 | 20 | public function getUnitOfWork() |
|
82 | |||
83 | /** {@inheritdoc} */ |
||
84 | 5 | public function persist($object) |
|
92 | |||
93 | /** {@inheritdoc} */ |
||
94 | 1 | public function remove($object) |
|
99 | |||
100 | /** {@inheritdoc} */ |
||
101 | public function merge($object) |
||
102 | { |
||
103 | throw new \BadMethodCallException('Merge is not supported'); |
||
104 | } |
||
105 | |||
106 | /** {@inheritdoc} */ |
||
107 | public function clear($objectName = null) |
||
111 | |||
112 | /** {@inheritdoc} */ |
||
113 | public function detach($object) |
||
117 | |||
118 | /** {@inheritdoc} */ |
||
119 | public function refresh($object) |
||
123 | |||
124 | /** {@inheritdoc} */ |
||
125 | 13 | public function getRepository($className) |
|
137 | |||
138 | /** {@inheritdoc} */ |
||
139 | 5 | public function flush($entity = null) |
|
143 | |||
144 | /** {@inheritdoc} */ |
||
145 | public function initializeObject($obj) |
||
153 | |||
154 | /** {@inheritdoc} */ |
||
155 | public function contains($object) |
||
159 | |||
160 | /** |
||
161 | * Gets a reference to the entity identified by the given type and identifier |
||
162 | * without actually loading it, if the entity is not yet loaded. |
||
163 | * |
||
164 | * @param string $entityName The name of the entity type. |
||
165 | * @param mixed $id The entity identifier. |
||
166 | * |
||
167 | * @return object The entity reference. |
||
168 | */ |
||
169 | 6 | public function getReference($entityName, $id) |
|
184 | |||
185 | /** {@inheritdoc} */ |
||
186 | 4 | public function getProxyFactory() |
|
190 | } |
||
191 |
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.