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 |
||
19 | class KunstmaanMediaExtension extends Extension implements PrependExtensionInterface |
||
20 | { |
||
21 | /** |
||
22 | * Loads configuration |
||
23 | * |
||
24 | * @param array $configs Configuration |
||
25 | * @param ContainerBuilder $container Container |
||
26 | */ |
||
27 | 4 | public function load(array $configs, ContainerBuilder $container) |
|
28 | { |
||
29 | 4 | $configuration = new Configuration(); |
|
30 | 4 | $config = $this->processConfiguration($configuration, $configs); |
|
31 | |||
32 | 4 | $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
33 | |||
34 | 4 | $container->setParameter( |
|
35 | 4 | 'twig.form.resources', |
|
36 | 4 | array_merge( |
|
37 | 4 | $container->hasParameter('twig.form.resources') ? $container->getParameter('twig.form.resources') : [], |
|
38 | 4 | ['@KunstmaanMedia/Form/formWidgets.html.twig'] |
|
39 | ) |
||
40 | ); |
||
41 | 4 | $container->setParameter('kunstmaan_media.soundcloud_api_key', $config['soundcloud_api_key']); |
|
42 | 4 | $container->setParameter('kunstmaan_media.remote_video', $config['remote_video']); |
|
43 | 4 | $container->setParameter('kunstmaan_media.enable_pdf_preview', $config['enable_pdf_preview']); |
|
44 | 4 | $container->setParameter('kunstmaan_media.blacklisted_extensions', $config['blacklisted_extensions']); |
|
45 | 4 | $container->setParameter('kunstmaan_media.web_root', $config['web_root']); |
|
46 | 4 | $container->setParameter('kunstmaan_media.full_media_path', $config['web_root'] . '%kunstmaan_media.media_path%'); |
|
47 | |||
48 | 4 | $loader->load('services.yml'); |
|
49 | 4 | $loader->load('handlers.yml'); |
|
50 | |||
51 | 4 | if ($config['enable_pdf_preview'] === true) { |
|
52 | 1 | $loader->load('pdf_preview.yml'); |
|
53 | } |
||
54 | |||
55 | 4 | $container->setParameter('liip_imagine.filter.loader.background.class', 'Kunstmaan\MediaBundle\Helper\Imagine\BackgroundFilterLoader'); |
|
56 | |||
57 | 4 | $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
58 | 4 | $loader->load('imagine.xml'); |
|
59 | |||
60 | 4 | $container->setAlias('liip_imagine.controller', 'Kunstmaan\MediaBundle\Helper\Imagine\ImagineController')->setPublic(true); |
|
61 | 4 | $container->setAlias('Liip\ImagineBundle\Controller\ImagineController', 'Kunstmaan\MediaBundle\Helper\Imagine\ImagineController')->setPublic(true); |
|
62 | 4 | $container->setAlias('liip_imagine.cache.resolver.prototype.web_path', 'Kunstmaan\MediaBundle\Helper\Imagine\WebPathResolver'); |
|
63 | 4 | $container->setAlias('liip_imagine.cache.manager', 'Kunstmaan\MediaBundle\Helper\Imagine\CacheManager')->setPublic(true); |
|
64 | 4 | $container->setAlias('liip_imagine.filter.loader.background', 'kunstmaan_media.imagine.filter.loader.background')->setPublic(true); |
|
65 | |||
66 | 4 | $this->addAvairyApiKeyParameter($container, $config); |
|
67 | |||
68 | 4 | if (!$container->hasDefinition('mime_types')) { |
|
69 | 4 | $mimeTypes = new Definition(MimeTypes::class); |
|
70 | 4 | $mimeTypes->setPublic(true); |
|
71 | 4 | $container->setDefinition('mime_types', $mimeTypes); |
|
72 | } |
||
73 | 4 | } |
|
74 | |||
75 | 4 | public function prepend(ContainerBuilder $container) |
|
76 | { |
||
77 | 4 | if (!$container->hasParameter('kunstmaan_media.upload_dir')) { |
|
78 | 4 | $container->setParameter('kunstmaan_media.upload_dir', '/uploads/media/'); |
|
79 | } |
||
80 | |||
81 | 4 | $twigConfig = []; |
|
82 | 4 | $twigConfig['globals']['upload_dir'] = $container->getParameter('kunstmaan_media.upload_dir'); |
|
83 | 4 | $twigConfig['globals']['mediabundleisactive'] = true; |
|
84 | 4 | $twigConfig['globals']['mediamanager'] = '@kunstmaan_media.media_manager'; |
|
85 | 4 | $container->prependExtensionConfig('twig', $twigConfig); |
|
86 | |||
87 | 4 | $liipConfig = Yaml::parse(file_get_contents(__DIR__ . '/../Resources/config/imagine_filters.yml')); |
|
88 | 4 | $container->prependExtensionConfig('liip_imagine', $liipConfig['liip_imagine']); |
|
89 | |||
90 | 4 | $defaultLocale = $container->hasParameter('kunstmaan_admin.default_locale') ? $container->getParameter('kunstmaan_admin.default_locale') : 'en'; |
|
91 | $stofDoctrineExtensionsConfig = [ |
||
92 | 4 | 'default_locale' => $defaultLocale, |
|
93 | 'translation_fallback' => true, |
||
94 | 'orm' => [ |
||
95 | 'default' => [ |
||
96 | 'translatable' => true, |
||
97 | ], |
||
98 | ], |
||
99 | ]; |
||
100 | |||
101 | 4 | $container->prependExtensionConfig('stof_doctrine_extensions', $stofDoctrineExtensionsConfig); |
|
102 | |||
103 | $doctrineGedmoEntityConfig = [ |
||
104 | 'orm' => [ |
||
105 | 'mappings' => [ |
||
106 | 'gedmo_translatable' => [ |
||
107 | 4 | 'type' => 'annotation', |
|
108 | 'prefix' => 'Gedmo\Translatable\Entity', |
||
109 | 'dir' => '%kernel.project_dir%/vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity', |
||
110 | 'alias' => 'GedmoTranslatable', |
||
111 | 'is_bundle' => false, |
||
112 | ], |
||
113 | ], |
||
114 | ], |
||
115 | ]; |
||
116 | |||
117 | 4 | $container->prependExtensionConfig('doctrine', $doctrineGedmoEntityConfig); |
|
118 | |||
119 | 4 | $configs = $container->getExtensionConfig($this->getAlias()); |
|
120 | 4 | $this->processConfiguration(new Configuration(), $configs); |
|
121 | 4 | } |
|
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | 4 | public function getAlias() |
|
130 | |||
131 | 4 | View Code Duplication | private function addAvairyApiKeyParameter(ContainerBuilder $container, array $config) |
|
|||
132 | { |
||
145 | } |
||
146 |
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.