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 |
||
22 | class Configuration implements ConfigurationInterface |
||
23 | { |
||
24 | private $container; |
||
25 | |||
26 | 8 | public function __construct(ContainerBuilder $container) |
|
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | 8 | public function getConfigTreeBuilder() |
|
84 | |||
85 | /** |
||
86 | * @param ArrayNodeDefinition $root |
||
87 | */ |
||
88 | 8 | private function configsNode(ArrayNodeDefinition $root) |
|
89 | { |
||
90 | 8 | $container = $this->container; |
|
91 | 8 | $root->children() |
|
92 | 8 | ->arrayNode('configs') |
|
93 | 8 | ->addDefaultChildrenIfNoneSet('default') |
|
94 | 8 | ->useAttributeAsKey('name') |
|
95 | 8 | ->prototype('array') |
|
96 | 8 | ->fixXmlConfig('dir', 'dirs') |
|
97 | 8 | ->fixXmlConfig('excluded_dir') |
|
98 | 8 | ->fixXmlConfig('excluded_name') |
|
99 | 8 | ->fixXmlConfig('blacklist_domain') |
|
100 | 8 | ->fixXmlConfig('external_translations_dir') |
|
101 | 8 | ->fixXmlConfig('whitelist_domain') |
|
102 | 8 | ->children() |
|
103 | 8 | ->arrayNode('dirs') |
|
104 | 8 | ->info('Directories we should scan for translations') |
|
105 | 8 | ->prototype('scalar') |
|
106 | 8 | ->validate() |
|
107 | 8 | ->always(function ($value) use ($container) { |
|
108 | $value = str_replace(DIRECTORY_SEPARATOR, '/', $value); |
||
109 | |||
110 | if ('@' === $value[0]) { |
||
111 | View Code Duplication | if (false === $pos = strpos($value, '/')) { |
|
|
|||
112 | $bundleName = substr($value, 1); |
||
113 | } else { |
||
114 | $bundleName = substr($value, 1, $pos - 2); |
||
115 | } |
||
116 | |||
117 | $bundles = $container->getParameter('kernel.bundles'); |
||
118 | if (!isset($bundles[$bundleName])) { |
||
119 | throw new \Exception(sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, array_keys($bundles))); |
||
120 | } |
||
121 | |||
122 | $ref = new \ReflectionClass($bundles[$bundleName]); |
||
123 | $value = false === $pos ? dirname($ref->getFileName()) : dirname($ref->getFileName()).substr($value, $pos); |
||
124 | } |
||
125 | |||
126 | if (!is_dir($value)) { |
||
127 | throw new \Exception(sprintf('The directory "%s" does not exist.', $value)); |
||
128 | } |
||
129 | |||
130 | return $value; |
||
131 | 8 | }) |
|
132 | 8 | ->end() |
|
133 | 8 | ->end() |
|
134 | 8 | ->end() |
|
135 | 8 | ->arrayNode('excluded_dirs') |
|
136 | 8 | ->prototype('scalar')->end() |
|
137 | 8 | ->end() |
|
138 | 8 | ->arrayNode('excluded_names') |
|
139 | 8 | ->prototype('scalar')->end() |
|
140 | 8 | ->end() |
|
141 | 8 | ->arrayNode('external_translations_dirs') |
|
142 | 8 | ->prototype('scalar')->end() |
|
143 | 8 | ->end() |
|
144 | 8 | ->enumNode('output_format')->values(['php', 'yml', 'xlf', 'po'])->defaultValue('xlf')->end() |
|
145 | 8 | ->arrayNode('blacklist_domains') |
|
146 | 8 | ->prototype('scalar')->end() |
|
147 | 8 | ->end() |
|
148 | 8 | ->arrayNode('whitelist_domains') |
|
149 | 8 | ->prototype('scalar')->end() |
|
150 | 8 | ->end() |
|
151 | 8 | ->arrayNode('remote_storage') |
|
152 | 8 | ->info('Service ids with to classes that supports remote storage of translations.') |
|
153 | 8 | ->prototype('scalar')->end() |
|
154 | 8 | ->end() |
|
155 | 8 | ->arrayNode('local_storage') |
|
156 | 8 | ->defaultValue(['php_translation.local_file_storage.abstract']) |
|
157 | 8 | ->info('Service ids with to classes that supports local storage of translations.') |
|
158 | 8 | ->prototype('scalar')->end() |
|
159 | 8 | ->end() |
|
160 | 8 | ->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end() |
|
161 | 8 | ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent.")->end() |
|
162 | 8 | ->scalarNode('xliff_version')->info('The version of XLIFF XML you want to use (if dumping to this format).')->defaultValue('2.0')->end() |
|
163 | 8 | ->variableNode('local_file_storage_options') |
|
164 | 8 | ->info('Options passed to the local file storage\'s dumper.') |
|
165 | 8 | ->defaultValue([]) |
|
166 | 8 | ->validate() |
|
167 | 8 | ->ifTrue(function ($value) { |
|
168 | return !is_array($value); |
||
169 | 8 | }) |
|
170 | 8 | ->thenInvalid('"local_file_storage_options" must be an array.') |
|
171 | 8 | ->end() |
|
172 | 8 | ->end() |
|
173 | 8 | ->end() |
|
174 | 8 | ->end() |
|
175 | 8 | ->end() |
|
176 | 8 | ->end(); |
|
177 | 8 | } |
|
178 | |||
179 | 8 | private function addAutoTranslateNode(ArrayNodeDefinition $root) |
|
191 | |||
192 | 8 | private function addEditInPlaceNode(ArrayNodeDefinition $root) |
|
205 | |||
206 | 8 | private function addWebUINode(ArrayNodeDefinition $root) |
|
219 | } |
||
220 |
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.