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 ObjectManager[] |
||
31 | */ |
||
32 | protected $managers; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $defaultManagerName; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $proxyInterfaceName; |
||
43 | |||
44 | /** |
||
45 | * @param Container $container |
||
46 | * @param string $proxyInterfaceName |
||
47 | */ |
||
48 | public function __construct(Container $container, $proxyInterfaceName = 'Doctrine\ODM\MongoDB\Proxy\Proxy') |
||
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | public function getDefaultConnectionName() |
||
63 | |||
64 | /** |
||
65 | * @param string|null $name |
||
66 | * @return Connection |
||
67 | * @throws \InvalidArgumentException |
||
68 | */ |
||
69 | View Code Duplication | public function getConnection($name = null) |
|
83 | |||
84 | /** |
||
85 | * @return array |
||
86 | */ |
||
87 | public function getConnections() |
||
101 | |||
102 | /** |
||
103 | * @return array |
||
104 | */ |
||
105 | public function getConnectionNames() |
||
115 | |||
116 | protected function loadConnections() |
||
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getDefaultManagerName() |
||
133 | |||
134 | /** |
||
135 | * @param null $name |
||
136 | * @return ObjectManager |
||
137 | * @throws \InvalidArgumentException |
||
138 | */ |
||
139 | View Code Duplication | public function getManager($name = null) |
|
153 | |||
154 | /** |
||
155 | * @return array |
||
156 | */ |
||
157 | public function getManagers() |
||
171 | |||
172 | /** |
||
173 | * @return array |
||
174 | */ |
||
175 | public function getManagerNames() |
||
185 | |||
186 | /** |
||
187 | * @param null $name |
||
188 | * @return void |
||
189 | * @throws \InvalidArgumentException |
||
190 | */ |
||
191 | View Code Duplication | public function resetManager($name = null) |
|
205 | |||
206 | protected function loadManagers() |
||
213 | |||
214 | /** |
||
215 | * @param string $alias |
||
216 | * @return string |
||
217 | * @throws MongoDBException |
||
218 | */ |
||
219 | public function getAliasNamespace($alias) |
||
229 | |||
230 | /** |
||
231 | * @param string $persistentObject |
||
232 | * @param null $persistentManagerName |
||
233 | * @return ObjectRepository |
||
234 | */ |
||
235 | public function getRepository($persistentObject, $persistentManagerName = null) |
||
239 | |||
240 | /** |
||
241 | * @param string $class |
||
242 | * @return ObjectManager|null |
||
243 | */ |
||
244 | public function getManagerForClass($class) |
||
257 | } |
||
258 |
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.