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() |
|
89 | |||
90 | 8 | private function configsNode(ArrayNodeDefinition $root) |
|
91 | { |
||
92 | 8 | $container = $this->container; |
|
93 | 8 | $root->children() |
|
94 | 8 | ->arrayNode('configs') |
|
95 | 8 | ->addDefaultChildrenIfNoneSet('default') |
|
96 | 8 | ->useAttributeAsKey('name') |
|
97 | 8 | ->prototype('array') |
|
98 | 8 | ->fixXmlConfig('dir', 'dirs') |
|
99 | 8 | ->fixXmlConfig('excluded_dir') |
|
100 | 8 | ->fixXmlConfig('excluded_name') |
|
101 | 8 | ->fixXmlConfig('blacklist_domain') |
|
102 | 8 | ->fixXmlConfig('external_translations_dir') |
|
103 | 8 | ->fixXmlConfig('whitelist_domain') |
|
104 | 8 | ->children() |
|
105 | 8 | ->arrayNode('dirs') |
|
106 | 8 | ->info('Directories we should scan for translations') |
|
107 | 8 | ->prototype('scalar') |
|
108 | 8 | ->validate() |
|
109 | ->always(function ($value) use ($container) { |
||
110 | $value = \str_replace(\DIRECTORY_SEPARATOR, '/', $value); |
||
111 | |||
112 | if ('@' === $value[0]) { |
||
113 | View Code Duplication | if (false === $pos = \strpos($value, '/')) { |
|
114 | $bundleName = \substr($value, 1); |
||
115 | } else { |
||
116 | $bundleName = \substr($value, 1, $pos - 2); |
||
117 | } |
||
118 | |||
119 | $bundles = $container->getParameter('kernel.bundles'); |
||
120 | if (!isset($bundles[$bundleName])) { |
||
121 | throw new \Exception(\sprintf('The bundle "%s" does not exist. Available bundles: %s', $bundleName, \array_keys($bundles))); |
||
122 | } |
||
123 | |||
124 | $ref = new \ReflectionClass($bundles[$bundleName]); |
||
125 | $value = false === $pos ? \dirname($ref->getFileName()) : \dirname($ref->getFileName()).\substr($value, $pos); |
||
126 | } |
||
127 | |||
128 | if (!\is_dir($value)) { |
||
129 | throw new \Exception(\sprintf('The directory "%s" does not exist.', $value)); |
||
130 | } |
||
131 | |||
132 | return $value; |
||
133 | 8 | }) |
|
134 | 8 | ->end() |
|
135 | 8 | ->end() |
|
136 | 8 | ->end() |
|
137 | 8 | ->arrayNode('excluded_dirs') |
|
138 | 8 | ->prototype('scalar')->end() |
|
139 | 8 | ->end() |
|
140 | 8 | ->arrayNode('excluded_names') |
|
141 | 8 | ->prototype('scalar')->end() |
|
142 | 8 | ->end() |
|
143 | 8 | ->arrayNode('external_translations_dirs') |
|
144 | 8 | ->prototype('scalar')->end() |
|
145 | 8 | ->end() |
|
146 | 8 | ->scalarNode('output_format')->defaultValue('xlf')->end() |
|
147 | 8 | ->arrayNode('blacklist_domains') |
|
148 | 8 | ->prototype('scalar')->end() |
|
149 | 8 | ->end() |
|
150 | 8 | ->arrayNode('whitelist_domains') |
|
151 | 8 | ->prototype('scalar')->end() |
|
152 | 8 | ->end() |
|
153 | 8 | ->arrayNode('remote_storage') |
|
154 | 8 | ->info('Service ids with to classes that supports remote storage of translations.') |
|
155 | 8 | ->prototype('scalar')->end() |
|
156 | 8 | ->end() |
|
157 | 8 | ->arrayNode('local_storage') |
|
158 | 8 | ->defaultValue(['php_translation.local_file_storage.abstract']) |
|
159 | 8 | ->info('Service ids with to classes that supports local storage of translations.') |
|
160 | 8 | ->prototype('scalar')->end() |
|
161 | 8 | ->end() |
|
162 | 8 | ->scalarNode('output_dir')->cannotBeEmpty()->defaultValue('%kernel.root_dir%/Resources/translations')->end() |
|
163 | 8 | ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent.")->end() |
|
164 | 8 | ->scalarNode('xliff_version')->info('The version of XLIFF XML you want to use (if dumping to this format).')->defaultValue('2.0')->end() |
|
165 | 8 | ->variableNode('local_file_storage_options') |
|
166 | 8 | ->info('Options passed to the local file storage\'s dumper.') |
|
167 | 8 | ->defaultValue([]) |
|
168 | 8 | ->validate() |
|
169 | ->ifTrue(function ($value) { |
||
170 | return !\is_array($value); |
||
171 | 8 | }) |
|
172 | 8 | ->thenInvalid('"local_file_storage_options" must be an array.') |
|
173 | 8 | ->end() |
|
174 | 8 | ->end() |
|
175 | 8 | ->end() |
|
176 | 8 | ->end() |
|
177 | 8 | ->end() |
|
178 | 8 | ->end(); |
|
179 | 8 | } |
|
180 | |||
181 | 8 | private function addAutoTranslateNode(ArrayNodeDefinition $root) |
|
193 | |||
194 | 8 | private function addEditInPlaceNode(ArrayNodeDefinition $root) |
|
207 | |||
208 | 8 | private function addWebUINode(ArrayNodeDefinition $root) |
|
221 | } |
||
222 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.