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 |
||
29 | final class Configuration implements ConfigurationInterface |
||
30 | { |
||
31 | /** |
||
32 | * Generates the configuration tree. |
||
33 | * |
||
34 | * @return TreeBuilder |
||
35 | */ |
||
36 | 47 | public function getConfigTreeBuilder() |
|
37 | { |
||
38 | 47 | $treeBuilder = new TreeBuilder(); |
|
39 | 47 | $rootNode = $treeBuilder->root('fos_rest', 'array'); |
|
40 | |||
41 | $rootNode |
||
42 | 47 | ->children() |
|
43 | 47 | ->scalarNode('disable_csrf_role')->defaultNull()->end() |
|
44 | 47 | ->arrayNode('access_denied_listener') |
|
45 | 47 | ->canBeEnabled() |
|
46 | 47 | ->beforeNormalization() |
|
47 | View Code Duplication | ->ifArray()->then(function ($v) { if (!empty($v) && empty($v['formats'])) { |
|
|
|||
48 | unset($v['enabled']); |
||
49 | $v = ['enabled' => true, 'formats' => $v]; |
||
50 | } |
||
51 | |||
52 | 47 | return $v; }) |
|
53 | 47 | ->end() |
|
54 | 47 | ->fixXmlConfig('format', 'formats') |
|
55 | 47 | ->children() |
|
56 | 47 | ->scalarNode('service')->defaultNull()->end() |
|
57 | 47 | ->arrayNode('formats') |
|
58 | 47 | ->useAttributeAsKey('name') |
|
59 | 47 | ->prototype('boolean')->end() |
|
60 | 47 | ->end() |
|
61 | 47 | ->end() |
|
62 | 47 | ->end() |
|
63 | 47 | ->scalarNode('unauthorized_challenge')->defaultNull()->end() |
|
64 | 47 | ->arrayNode('param_fetcher_listener') |
|
65 | 47 | ->beforeNormalization() |
|
66 | 47 | ->ifString() |
|
67 | View Code Duplication | ->then(function ($v) { return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v]; }) |
|
68 | 47 | ->end() |
|
69 | 47 | ->canBeEnabled() |
|
70 | 47 | ->children() |
|
71 | 47 | ->booleanNode('enabled')->defaultFalse()->end() |
|
72 | 47 | ->booleanNode('force')->defaultFalse()->end() |
|
73 | 47 | ->scalarNode('service')->defaultNull()->end() |
|
74 | 47 | ->end() |
|
75 | 47 | ->end() |
|
76 | 47 | ->scalarNode('cache_dir')->cannotBeEmpty()->defaultValue('%kernel.cache_dir%/fos_rest')->end() |
|
77 | 47 | ->arrayNode('allowed_methods_listener') |
|
78 | 47 | ->canBeEnabled() |
|
79 | 47 | ->children() |
|
80 | 47 | ->scalarNode('service')->defaultNull()->end() |
|
81 | 47 | ->end() |
|
82 | 47 | ->end() |
|
83 | 47 | ->arrayNode('routing_loader') |
|
84 | 47 | ->addDefaultsIfNotSet() |
|
85 | 47 | ->children() |
|
86 | 47 | ->scalarNode('default_format')->defaultNull()->end() |
|
87 | 47 | ->scalarNode('include_format')->defaultTrue()->end() |
|
88 | 47 | ->end() |
|
89 | 47 | ->end() |
|
90 | 47 | ->arrayNode('body_converter') |
|
91 | 47 | ->addDefaultsIfNotSet() |
|
92 | 47 | ->children() |
|
93 | 47 | ->scalarNode('enabled')->defaultFalse()->end() |
|
94 | 47 | ->scalarNode('validate')->defaultFalse()->end() |
|
95 | 47 | ->scalarNode('validation_errors_argument')->defaultValue('validationErrors')->end() |
|
96 | 47 | ->end() |
|
97 | 47 | ->end() |
|
98 | 47 | ->arrayNode('service') |
|
99 | 47 | ->addDefaultsIfNotSet() |
|
100 | 47 | ->children() |
|
101 | 47 | ->scalarNode('router')->defaultValue('router')->end() |
|
102 | 47 | ->scalarNode('templating')->defaultValue('templating')->end() |
|
103 | 47 | ->scalarNode('serializer')->defaultNull()->end() |
|
104 | 47 | ->scalarNode('view_handler')->defaultValue('fos_rest.view_handler.default')->end() |
|
105 | 47 | ->scalarNode('exception_handler')->defaultValue('fos_rest.view.exception_wrapper_handler')->end() |
|
106 | 47 | ->scalarNode('inflector')->defaultValue('fos_rest.inflector.doctrine')->end() |
|
107 | 47 | ->scalarNode('validator')->defaultValue('validator')->end() |
|
108 | 47 | ->end() |
|
109 | 47 | ->end() |
|
110 | 47 | ->arrayNode('serializer') |
|
111 | 47 | ->addDefaultsIfNotSet() |
|
112 | 47 | ->children() |
|
113 | 47 | ->scalarNode('version')->defaultNull()->end() |
|
114 | 47 | ->arrayNode('groups') |
|
115 | 47 | ->prototype('scalar')->end() |
|
116 | 47 | ->end() |
|
117 | 47 | ->booleanNode('serialize_null')->defaultFalse()->end() |
|
118 | 47 | ->end() |
|
119 | 47 | ->end() |
|
120 | 47 | ->arrayNode('zone') |
|
121 | 47 | ->cannotBeOverwritten() |
|
122 | 47 | ->prototype('array') |
|
123 | 47 | ->fixXmlConfig('ip') |
|
124 | 47 | ->children() |
|
125 | 47 | ->scalarNode('path') |
|
126 | 47 | ->defaultNull() |
|
127 | 47 | ->info('use the urldecoded format') |
|
128 | 47 | ->example('^/path to resource/') |
|
129 | 47 | ->end() |
|
130 | 47 | ->scalarNode('host')->defaultNull()->end() |
|
131 | 47 | ->arrayNode('methods') |
|
132 | ->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end() |
||
133 | 47 | ->prototype('scalar')->end() |
|
134 | 47 | ->end() |
|
135 | 47 | ->arrayNode('ips') |
|
136 | ->beforeNormalization()->ifString()->then(function ($v) { return array($v); })->end() |
||
137 | 47 | ->prototype('scalar')->end() |
|
138 | 47 | ->end() |
|
139 | 47 | ->end() |
|
140 | 47 | ->end() |
|
141 | 47 | ->end() |
|
142 | 47 | ->end(); |
|
143 | |||
144 | 47 | $this->addViewSection($rootNode); |
|
145 | 47 | $this->addExceptionSection($rootNode); |
|
146 | 47 | $this->addBodyListenerSection($rootNode); |
|
147 | 47 | $this->addFormatListenerSection($rootNode); |
|
148 | 47 | $this->addVersioningSection($rootNode); |
|
149 | |||
150 | 47 | return $treeBuilder; |
|
151 | } |
||
152 | |||
153 | 47 | private function addViewSection(ArrayNodeDefinition $rootNode) |
|
154 | { |
||
155 | $rootNode |
||
156 | 47 | ->children() |
|
157 | 47 | ->arrayNode('view') |
|
158 | 47 | ->fixXmlConfig('format', 'formats') |
|
159 | 47 | ->fixXmlConfig('mime_type', 'mime_types') |
|
160 | 47 | ->fixXmlConfig('templating_format', 'templating_formats') |
|
161 | 47 | ->fixXmlConfig('force_redirect', 'force_redirects') |
|
162 | 47 | ->addDefaultsIfNotSet() |
|
163 | 47 | ->children() |
|
164 | 47 | ->scalarNode('default_engine')->defaultValue('twig')->end() |
|
165 | 47 | ->arrayNode('force_redirects') |
|
166 | 47 | ->useAttributeAsKey('name') |
|
167 | 47 | ->defaultValue(['html' => true]) |
|
168 | 47 | ->prototype('boolean')->end() |
|
169 | 47 | ->end() |
|
170 | 47 | ->arrayNode('mime_types') |
|
171 | 47 | ->canBeEnabled() |
|
172 | 47 | ->beforeNormalization() |
|
173 | View Code Duplication | ->ifArray()->then(function ($v) { |
|
174 | 1 | if (!empty($v) && empty($v['formats'])) { |
|
175 | 1 | unset($v['enabled']); |
|
176 | 1 | $v = ['enabled' => true, 'formats' => $v]; |
|
177 | 1 | } |
|
178 | |||
179 | 1 | return $v; |
|
180 | 47 | }) |
|
181 | 47 | ->end() |
|
182 | 47 | ->fixXmlConfig('format', 'formats') |
|
183 | 47 | ->children() |
|
184 | 47 | ->scalarNode('service')->defaultNull()->end() |
|
185 | 47 | ->arrayNode('formats') |
|
186 | 47 | ->useAttributeAsKey('name') |
|
187 | 47 | ->prototype('variable')->end() |
|
188 | 47 | ->end() |
|
189 | 47 | ->end() |
|
190 | 47 | ->end() |
|
191 | 47 | ->arrayNode('formats') |
|
192 | 47 | ->useAttributeAsKey('name') |
|
193 | 47 | ->defaultValue(['json' => true, 'xml' => true]) |
|
194 | 47 | ->prototype('boolean')->end() |
|
195 | 47 | ->end() |
|
196 | 47 | ->arrayNode('templating_formats') |
|
197 | 47 | ->useAttributeAsKey('name') |
|
198 | 47 | ->defaultValue(['html' => true]) |
|
199 | 47 | ->prototype('boolean')->end() |
|
200 | 47 | ->end() |
|
201 | 47 | ->arrayNode('view_response_listener') |
|
202 | 47 | ->beforeNormalization() |
|
203 | 47 | ->ifString() |
|
204 | View Code Duplication | ->then(function ($v) { return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v]; }) |
|
205 | 47 | ->end() |
|
206 | 47 | ->canBeEnabled() |
|
207 | 47 | ->children() |
|
208 | 47 | ->booleanNode('enabled')->defaultFalse()->end() |
|
209 | 47 | ->booleanNode('force')->defaultFalse()->end() |
|
210 | 47 | ->scalarNode('service')->defaultNull()->end() |
|
211 | 47 | ->end() |
|
212 | 47 | ->end() |
|
213 | 47 | ->scalarNode('failed_validation')->defaultValue(Response::HTTP_BAD_REQUEST)->end() |
|
214 | 47 | ->scalarNode('empty_content')->defaultValue(Response::HTTP_NO_CONTENT)->end() |
|
215 | 47 | ->scalarNode('exception_wrapper_handler')->defaultNull()->end() |
|
216 | 47 | ->booleanNode('serialize_null')->defaultFalse()->end() |
|
217 | 47 | ->arrayNode('jsonp_handler') |
|
218 | 47 | ->canBeUnset() |
|
219 | 47 | ->children() |
|
220 | 47 | ->scalarNode('callback_param')->defaultValue('callback')->end() |
|
221 | 47 | ->scalarNode('mime_type')->defaultValue('application/javascript+jsonp')->end() |
|
222 | 47 | ->end() |
|
223 | 47 | ->end() |
|
224 | 47 | ->end() |
|
225 | 47 | ->end() |
|
226 | 47 | ->end(); |
|
227 | 47 | } |
|
228 | |||
229 | 47 | private function addBodyListenerSection(ArrayNodeDefinition $rootNode) |
|
263 | |||
264 | 47 | private function addFormatListenerSection(ArrayNodeDefinition $rootNode) |
|
265 | { |
||
266 | $rootNode |
||
267 | 47 | ->children() |
|
268 | 47 | ->arrayNode('format_listener') |
|
269 | 47 | ->fixXmlConfig('rule', 'rules') |
|
270 | 47 | ->addDefaultsIfNotSet() |
|
271 | 47 | ->canBeUnset() |
|
272 | 47 | ->beforeNormalization() |
|
273 | ->ifTrue(function ($v) { |
||
274 | // check if we got an assoc array in rules |
||
275 | 7 | return isset($v['rules']) |
|
276 | 7 | && is_array($v['rules']) |
|
277 | 7 | && array_keys($v['rules']) !== range(0, count($v['rules']) - 1); |
|
278 | 47 | }) |
|
279 | ->then(function ($v) { |
||
280 | 1 | $v['rules'] = [$v['rules']]; |
|
281 | |||
282 | 1 | return $v; |
|
283 | 47 | }) |
|
284 | 47 | ->end() |
|
285 | 47 | ->canBeEnabled() |
|
286 | 47 | ->validate() |
|
287 | ->ifTrue(function ($v) { return empty($v['rules']) && !empty($v['media_type']['enabled']); }) |
||
288 | 47 | ->thenInvalid('To enable the "media_type" setting, a "rules" setting must also needs to be defined for the "format_listener"') |
|
289 | 47 | ->end() |
|
290 | 47 | ->children() |
|
291 | 47 | ->scalarNode('service')->defaultNull()->end() |
|
292 | 47 | ->arrayNode('rules') |
|
293 | 47 | ->cannotBeOverwritten() |
|
294 | 47 | ->prototype('array') |
|
295 | 47 | ->fixXmlConfig('priority', 'priorities') |
|
296 | 47 | ->children() |
|
297 | 47 | ->scalarNode('path')->defaultNull()->info('URL path info')->end() |
|
298 | 47 | ->scalarNode('host')->defaultNull()->info('URL host name')->end() |
|
299 | 47 | ->variableNode('methods')->defaultNull()->info('Method for URL')->end() |
|
300 | 47 | ->booleanNode('stop')->defaultFalse()->end() |
|
301 | 47 | ->booleanNode('prefer_extension')->defaultTrue()->end() |
|
302 | 47 | ->scalarNode('fallback_format')->defaultValue('html')->end() |
|
303 | 47 | ->scalarNode('exception_fallback_format')->defaultNull()->end() |
|
304 | 47 | ->arrayNode('priorities') |
|
305 | ->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end() |
||
306 | 47 | ->prototype('scalar')->end() |
|
307 | 47 | ->end() |
|
308 | 47 | ->end() |
|
309 | 47 | ->end() |
|
310 | 47 | ->end() |
|
311 | 47 | ->end() |
|
312 | 47 | ->end() |
|
313 | 47 | ->end(); |
|
314 | 47 | } |
|
315 | |||
316 | 47 | private function addVersioningSection(ArrayNodeDefinition $rootNode) |
|
365 | |||
366 | 47 | private function addExceptionSection(ArrayNodeDefinition $rootNode) |
|
393 | } |
||
394 |
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.