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 |
||
12 | class ManagerRegistry implements ManagerRegistryInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var Container |
||
16 | */ |
||
17 | protected $container; |
||
18 | |||
19 | /** |
||
20 | * @var Connection[] |
||
21 | */ |
||
22 | protected $connections; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $defaultConnectionName; |
||
28 | |||
29 | /** |
||
30 | * @var EntityManager[] |
||
31 | */ |
||
32 | protected $originalManagers; |
||
33 | |||
34 | /** |
||
35 | * @var EntityManager[] |
||
36 | */ |
||
37 | protected $resetManagers = []; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $defaultManagerName; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $proxyInterfaceName; |
||
48 | |||
49 | /** |
||
50 | * @param Container $container |
||
51 | * @param string $proxyInterfaceName |
||
52 | */ |
||
53 | public function __construct(Container $container, $proxyInterfaceName = 'Doctrine\ORM\Proxy\Proxy') |
||
58 | |||
59 | /** |
||
60 | * @return string |
||
61 | */ |
||
62 | public function getDefaultConnectionName() |
||
68 | |||
69 | /** |
||
70 | * @param string|null $name |
||
71 | * @return Connection |
||
72 | * @throws \InvalidArgumentException |
||
73 | */ |
||
74 | public function getConnection($name = null) |
||
86 | |||
87 | /** |
||
88 | * @return Connection[] |
||
89 | */ |
||
90 | public function getConnections() |
||
104 | |||
105 | /** |
||
106 | * @return array |
||
107 | */ |
||
108 | public function getConnectionNames() |
||
118 | |||
119 | protected function loadConnections() |
||
126 | |||
127 | /** |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getDefaultManagerName() |
||
136 | |||
137 | /** |
||
138 | * @param string|null $name |
||
139 | * @return EntityManager |
||
140 | */ |
||
141 | View Code Duplication | public function getManager($name = null) |
|
148 | |||
149 | /** |
||
150 | * @param string|null $name |
||
151 | * @return string |
||
152 | */ |
||
153 | protected function validateManagerName($name) |
||
161 | |||
162 | /** |
||
163 | * @param array $data |
||
164 | * @param string $default |
||
165 | * @param string|null $name |
||
166 | * @return string |
||
167 | * @throws \InvalidArgumentException |
||
168 | */ |
||
169 | protected function validateName($data, $name, $default) |
||
181 | |||
182 | /** |
||
183 | * @return EntityManager[] |
||
184 | */ |
||
185 | public function getManagers() |
||
199 | |||
200 | /** |
||
201 | * @return array |
||
202 | */ |
||
203 | public function getManagerNames() |
||
213 | |||
214 | /** |
||
215 | * @param string|null $name |
||
216 | * @return void |
||
217 | */ |
||
218 | View Code Duplication | public function resetManager($name = null) |
|
225 | |||
226 | protected function loadManagers() |
||
233 | |||
234 | /** |
||
235 | * @param string $alias |
||
236 | * @return string |
||
237 | * @throws ORMException |
||
238 | */ |
||
239 | public function getAliasNamespace($alias) |
||
250 | |||
251 | /** |
||
252 | * @param string $persistentObject |
||
253 | * @param null $persistentManagerName |
||
254 | * @return EntityRepository |
||
255 | */ |
||
256 | public function getRepository($persistentObject, $persistentManagerName = null) |
||
260 | |||
261 | /** |
||
262 | * @param string $class |
||
263 | * @return EntityManager|null |
||
264 | */ |
||
265 | public function getManagerForClass($class) |
||
278 | } |
||
279 |
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.