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 |
||
17 | class ManagerRegistry extends AbstractManagerRegistry |
||
18 | { |
||
19 | use EventManagerAwareTrait; |
||
20 | |||
21 | /** |
||
22 | * Имя менеджера |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | const NAME = 'defaultManagerRegistry'; |
||
27 | |||
28 | /** |
||
29 | * Контекст вызова метода getService |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | const OBJECT_MANAGER_CONTEXT = 'objectManagerContext'; |
||
34 | |||
35 | /** |
||
36 | * Контекст вызова метода getService |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | const CONNECTION_CONTEXT = 'connectionContext'; |
||
41 | |||
42 | /** |
||
43 | * Контекст вызова метода getService |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $serviceContext; |
||
48 | |||
49 | /** |
||
50 | * Ключем является имя ObjectManager'a, а значением объект ObjectManager'a |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $objectManagerByName = []; |
||
55 | |||
56 | /** |
||
57 | * Ключем является имя соеденения, а значением объект соеденения к базе данных |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $connectionByName = []; |
||
62 | |||
63 | /** |
||
64 | * Прототип для создания объекта события, бросаемого когда нужно получить ресурс |
||
65 | * |
||
66 | * @var ManagerRegistryResourceEventInterface |
||
67 | */ |
||
68 | protected $managerRegistryResourceEventPrototype; |
||
69 | |||
70 | /** |
||
71 | * Возвращает прототип для создания объекта события, бросаемого когда нужно получить ресурс |
||
72 | * |
||
73 | * @return ManagerRegistryResourceEventInterface |
||
74 | */ |
||
75 | public function getManagerRegistryResourceEventPrototype() |
||
83 | |||
84 | /** |
||
85 | * Устанавливает прототип для создания объекта события, бросаемого когда нужно получить ресурс |
||
86 | * |
||
87 | * @param ManagerRegistryResourceEventInterface $managerRegistryResourceEventPrototype |
||
88 | * |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function setManagerRegistryResourceEventPrototype(ManagerRegistryResourceEventInterface $managerRegistryResourceEventPrototype) |
||
97 | |||
98 | /** |
||
99 | * @inheritdoc |
||
100 | * |
||
101 | * @param null $name |
||
102 | * |
||
103 | * @return mixed |
||
104 | */ |
||
105 | View Code Duplication | public function getConnection($name = null) |
|
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | View Code Duplication | public function getConnections() |
|
123 | |||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | View Code Duplication | public function getManager($name = null) |
|
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | View Code Duplication | public function getManagerForClass($class) |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | View Code Duplication | public function getManagers() |
|
157 | |||
158 | |||
159 | /** |
||
160 | * @inheritdoc |
||
161 | * |
||
162 | * @param string $name |
||
163 | * |
||
164 | * @return mixed |
||
165 | * @throws \Nnx\Doctrine\ManagerRegistry\Exception\ConnectionNotFoundException |
||
166 | * @throws \Nnx\Doctrine\ManagerRegistry\Exception\ObjectManagerNotFoundException |
||
167 | * @throws \Nnx\Doctrine\ManagerRegistry\Exception\RuntimeException |
||
168 | */ |
||
169 | protected function getService($name) |
||
182 | |||
183 | /** |
||
184 | * Получает ObjectManager по имени |
||
185 | * |
||
186 | * @param $name |
||
187 | * |
||
188 | * @return ObjectManager |
||
189 | * @throws \Nnx\Doctrine\ManagerRegistry\Exception\ObjectManagerNotFoundException |
||
190 | */ |
||
191 | View Code Duplication | protected function getObjectManagerByName($name) |
|
218 | |||
219 | |||
220 | /** |
||
221 | * Получает соеденение по имени |
||
222 | * |
||
223 | * @param $name |
||
224 | * |
||
225 | * @return mixed |
||
226 | * @throws \Nnx\Doctrine\ManagerRegistry\Exception\ConnectionNotFoundException |
||
227 | */ |
||
228 | View Code Duplication | protected function getConnectionByName($name) |
|
255 | |||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | public function resetManager($name = null) |
||
266 | |||
267 | /** |
||
268 | * @param string $name |
||
269 | * |
||
270 | * @return mixed |
||
271 | * @throws \Nnx\Doctrine\ManagerRegistry\Exception\ObjectManagerNotFoundException |
||
272 | */ |
||
273 | protected function resetService($name) |
||
279 | |||
280 | /** |
||
281 | * @inheritdoc |
||
282 | * |
||
283 | * @param string $alias |
||
284 | * |
||
285 | * @return mixed |
||
286 | */ |
||
287 | public function getAliasNamespace($alias) |
||
291 | } |
||
292 |
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.