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 |
||
31 | final class DoctrineOrmServiceProvider implements ServiceProviderInterface |
||
32 | { |
||
33 | /** |
||
34 | * Register ORM service. |
||
35 | * |
||
36 | * @param Container $container |
||
37 | */ |
||
38 | 3 | public function register(Container $container) |
|
61 | |||
62 | /** |
||
63 | * @param Container $container |
||
64 | * |
||
65 | * @return callable |
||
66 | */ |
||
67 | private function getOrmEmDefinition(Container $container): callable |
||
75 | |||
76 | /** |
||
77 | * @param Container $container |
||
78 | * |
||
79 | * @return callable |
||
80 | */ |
||
81 | private function getOrmApcuCacheFactoryDefinition(Container $container): callable |
||
87 | |||
88 | /** |
||
89 | * @param Container $container |
||
90 | * |
||
91 | * @return callable |
||
92 | */ |
||
93 | private function getOrmArrayCacheFactoryDefinition(Container $container): callable |
||
99 | |||
100 | /** |
||
101 | * @param Container $container |
||
102 | * |
||
103 | * @return callable |
||
104 | */ |
||
105 | private function getOrmEmConfigDefinition(Container $container): callable |
||
113 | |||
114 | /** |
||
115 | * @return array |
||
116 | */ |
||
117 | 3 | private function getOrmEmDefaultOptions(): array |
|
143 | |||
144 | /** |
||
145 | * @param Container $container |
||
146 | * |
||
147 | * @return callable |
||
148 | */ |
||
149 | private function getOrmEmsDefinition(Container $container): callable |
||
150 | { |
||
151 | 3 | return function () use ($container) { |
|
152 | 3 | $container['doctrine.orm.ems.options.initializer'](); |
|
153 | |||
154 | 3 | $ems = new Container(); |
|
155 | 3 | foreach ($container['doctrine.orm.ems.options'] as $name => $options) { |
|
156 | 3 | if ($container['doctrine.orm.ems.default'] === $name) { |
|
157 | // we use shortcuts here in case the default has been overridden |
||
158 | 3 | $config = $container['doctrine.orm.em.config']; |
|
159 | } else { |
||
160 | 1 | $config = $container['doctrine.orm.ems.config'][$name]; |
|
161 | } |
||
162 | |||
163 | 3 | $ems[$name] = function () use ($container, $options, $config) { |
|
164 | 3 | return EntityManager::create( |
|
165 | 3 | $container['doctrine.dbal.dbs'][$options['connection']], |
|
166 | 3 | $config, |
|
167 | 3 | $container['doctrine.dbal.dbs.event_manager'][$options['connection']] |
|
168 | ); |
||
169 | 3 | }; |
|
170 | } |
||
171 | |||
172 | 3 | return $ems; |
|
173 | 3 | }; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param Container $container |
||
178 | * |
||
179 | * @return callable |
||
180 | */ |
||
181 | private function getOrmEmsConfigServiceProvider(Container $container): callable |
||
182 | { |
||
183 | 3 | return function () use ($container) { |
|
184 | 3 | $container['doctrine.orm.ems.options.initializer'](); |
|
185 | |||
186 | 3 | $configs = new Container(); |
|
187 | 3 | foreach ($container['doctrine.orm.ems.options'] as $name => $options) { |
|
188 | 3 | $config = new Configuration(); |
|
189 | |||
190 | 3 | $config->setSQLLogger($container['doctrine.dbal.dbs.config'][$name]->getSQLLogger()); |
|
191 | |||
192 | 3 | $config->setQueryCacheImpl( |
|
193 | 3 | $container[sprintf('doctrine.orm.em.cache_factory.%s', $options['cache.query'])] |
|
194 | ); |
||
195 | 3 | $config->setHydrationCacheImpl( |
|
196 | 3 | $container[sprintf('doctrine.orm.em.cache_factory.%s', $options['cache.hydration'])] |
|
197 | ); |
||
198 | 3 | $config->setMetadataCacheImpl( |
|
199 | 3 | $container[sprintf('doctrine.orm.em.cache_factory.%s', $options['cache.metadata'])] |
|
200 | ); |
||
201 | 3 | $config->setResultCacheImpl($container['doctrine.dbal.dbs.config'][$name]->getResultCacheImpl()); |
|
202 | |||
203 | 3 | $config->setClassMetadataFactoryName($options['class_metadata.factory.name']); |
|
204 | |||
205 | 3 | $config->setCustomDatetimeFunctions($options['custom.datetime.functions']); |
|
206 | 3 | $config->setCustomHydrationModes($options['custom.hydration_modes']); |
|
207 | 3 | $config->setCustomNumericFunctions($options['custom.numeric.functions']); |
|
208 | 3 | $config->setCustomStringFunctions($options['custom.string.functions']); |
|
209 | |||
210 | 3 | $config->setEntityListenerResolver( |
|
211 | $container[ |
||
212 | 3 | sprintf('doctrine.orm.entity.listener_resolver.%s', $options['entity.listener_resolver']) |
|
213 | ] |
||
214 | ); |
||
215 | |||
216 | 3 | $config->setMetadataDriverImpl( |
|
217 | 3 | $container['doctrine.orm.mapping_driver_chain']($name, $config, (array) $options['mappings']) |
|
218 | ); |
||
219 | |||
220 | 3 | $config->setAutoGenerateProxyClasses($options['proxies.auto_generate']); |
|
221 | 3 | $config->setProxyDir($options['proxies.dir']); |
|
222 | 3 | $config->setProxyNamespace($options['proxies.namespace']); |
|
223 | |||
224 | 3 | $config->setDefaultQueryHints($options['query_hints']); |
|
225 | |||
226 | 3 | $config->setRepositoryFactory( |
|
227 | 3 | $container[sprintf('doctrine.orm.repository.factory.%s', $options['repository.factory'])] |
|
228 | ); |
||
229 | 3 | $config->setDefaultRepositoryClassName($options['repository.default.class']); |
|
230 | |||
231 | 3 | $this->assignSecondLevelCache($container, $config, $options); |
|
232 | |||
233 | 3 | $config->setNamingStrategy( |
|
234 | 3 | $container[sprintf('doctrine.orm.strategy.naming.%s', $options['strategy.naming'])] |
|
235 | ); |
||
236 | 3 | $config->setQuoteStrategy( |
|
237 | 3 | $container[sprintf('doctrine.orm.strategy.quote.%s', $options['strategy.quote'])] |
|
238 | ); |
||
239 | |||
240 | 3 | foreach ((array) $options['types'] as $typeName => $typeClass) { |
|
241 | 1 | if (Type::hasType($typeName)) { |
|
242 | 1 | Type::overrideType($typeName, $typeClass); |
|
243 | } else { |
||
244 | 1 | Type::addType($typeName, $typeClass); |
|
245 | } |
||
246 | } |
||
247 | |||
248 | 3 | $configs[$name] = $config; |
|
249 | } |
||
250 | |||
251 | 3 | return $configs; |
|
252 | 3 | }; |
|
253 | } |
||
254 | |||
255 | /** |
||
256 | * @param Container $container |
||
257 | * @param Configuration $config |
||
258 | * @param array $options |
||
259 | */ |
||
260 | 3 | private function assignSecondLevelCache(Container $container, Configuration $config, array $options) |
|
275 | |||
276 | /** |
||
277 | * @param Container $container |
||
278 | * |
||
279 | * @return callable |
||
280 | */ |
||
281 | View Code Duplication | private function getOrmEmsOptionsInitializerDefinition(Container $container): callable |
|
|
|||
282 | { |
||
283 | 3 | return $container->protect(function () use ($container) { |
|
284 | 3 | static $initialized = false; |
|
285 | |||
286 | 3 | if ($initialized) { |
|
287 | 3 | return; |
|
288 | } |
||
289 | |||
290 | 3 | $initialized = true; |
|
291 | |||
292 | 3 | if (!isset($container['doctrine.orm.ems.options'])) { |
|
293 | 2 | $container['doctrine.orm.ems.options'] = [ |
|
294 | 2 | 'default' => isset($container['doctrine.orm.em.options']) |
|
295 | 1 | ? $container['doctrine.orm.em.options'] : [], |
|
296 | ]; |
||
297 | } |
||
298 | |||
299 | 3 | $tmp = $container['doctrine.orm.ems.options']; |
|
300 | 3 | foreach ($tmp as $name => &$options) { |
|
301 | 3 | $options = array_replace($container['doctrine.orm.em.default_options'], $options); |
|
302 | |||
303 | 3 | if (!isset($container['doctrine.orm.ems.default'])) { |
|
304 | 3 | $container['doctrine.orm.ems.default'] = $name; |
|
305 | } |
||
306 | } |
||
307 | |||
308 | 3 | $container['doctrine.orm.ems.options'] = $tmp; |
|
309 | 3 | }); |
|
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param Container $container |
||
314 | * |
||
315 | * @return callable |
||
316 | */ |
||
317 | private function getOrmEntityListenerResolverDefinition(Container $container): callable |
||
323 | |||
324 | /** |
||
325 | * @param Container $container |
||
326 | * |
||
327 | * @return callable |
||
328 | */ |
||
329 | private function getOrmManagerRegistryDefintion(Container $container): callable |
||
335 | |||
336 | /** |
||
337 | * @param Container $container |
||
338 | * |
||
339 | * @return callable |
||
340 | */ |
||
341 | private function getOrmMappingDriverFactoryAnnotation(Container $container): callable |
||
347 | |||
348 | /** |
||
349 | * @param Container $container |
||
350 | * |
||
351 | * @return callable |
||
352 | */ |
||
353 | private function getOrmMappingDriverFactoryStaticPhp(Container $container): callable |
||
354 | { |
||
355 | 3 | return $container->protect(function (array $mapping, Configuration $config) { |
|
356 | 1 | return new StaticPHPDriver($mapping['path']); |
|
357 | 3 | }); |
|
358 | } |
||
359 | |||
360 | /** |
||
361 | * @param Container $container |
||
362 | * |
||
363 | * @return callable |
||
364 | */ |
||
365 | View Code Duplication | private function getOrmMappingDriverFactorySimpleYaml(Container $container): callable |
|
374 | |||
375 | /** |
||
376 | * @param Container $container |
||
377 | * |
||
378 | * @return callable |
||
379 | */ |
||
380 | View Code Duplication | private function getOrmMappingDriverFactorySimpleXml(Container $container): callable |
|
389 | |||
390 | /** |
||
391 | * @param Container $container |
||
392 | * |
||
393 | * @return callable |
||
394 | */ |
||
395 | private function getOrmMappingDriverFactoryYaml(Container $container): callable |
||
401 | |||
402 | /** |
||
403 | * @param Container $container |
||
404 | * |
||
405 | * @return callable |
||
406 | */ |
||
407 | private function getOrmMappingDriverFactoryXml(Container $container): callable |
||
413 | |||
414 | /** |
||
415 | * @param Container $container |
||
416 | * |
||
417 | * @return callable |
||
418 | */ |
||
419 | private function getOrmMappingDriverChainDefinition(Container $container): callable |
||
436 | |||
437 | /** |
||
438 | * @param Container $container |
||
439 | * |
||
440 | * @return callable |
||
441 | */ |
||
442 | private function getOrmRepositoryFactoryDefinition(Container $container): callable |
||
448 | |||
449 | /** |
||
450 | * @param Container $container |
||
451 | * |
||
452 | * @return callable |
||
453 | */ |
||
454 | private function getOrmNamingStrategyDefinition(Container $container): callable |
||
460 | |||
461 | /** |
||
462 | * @param Container $container |
||
463 | * |
||
464 | * @return callable |
||
465 | */ |
||
466 | private function getOrmQuoteStrategyDefinition(Container $container): callable |
||
472 | } |
||
473 |
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.