Completed
Pull Request — 2.x (#2204)
by Christian
04:11
created

Configuration::addExceptionSection()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 111

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 85
CRAP Score 6.0004

Importance

Changes 0
Metric Value
dl 0
loc 111
ccs 85
cts 87
cp 0.977
rs 7.3777
c 0
b 0
f 0
cc 6
nc 1
nop 1
crap 6.0004

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\DependencyInjection;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
16
use Symfony\Component\Config\Definition\ConfigurationInterface;
17
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
use Symfony\Component\Serializer\Encoder\XmlEncoder;
21
22
/**
23
 * This class contains the configuration information for the bundle.
24
 *
25
 * This information is solely responsible for how the different configuration
26
 * sections are normalized, and merged.
27
 *
28
 * @author Lukas Kahwe Smith <[email protected]>
29
 *
30
 * @internal
31
 */
32
final class Configuration implements ConfigurationInterface
33
{
34
    /**
35
     * Default debug mode value.
36
     *
37
     * @var bool
38
     */
39
    private $debug;
40
41 83
    public function __construct(bool $debug)
42
    {
43 83
        $this->debug = $debug;
44 83
    }
45
46 82
    public function getConfigTreeBuilder(): TreeBuilder
47
    {
48 82
        $treeBuilder = new TreeBuilder('fos_rest');
49
50 82
        if (method_exists($treeBuilder, 'getRootNode')) {
51 82
            $rootNode = $treeBuilder->getRootNode();
52
        } else {
53
            $rootNode = $treeBuilder->root('fos_rest');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

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.

Loading history...
54
        }
55
56
        $rootNode
57 82
            ->children()
58 82
                ->scalarNode('disable_csrf_role')->defaultNull()->end()
59 82
                ->arrayNode('access_denied_listener')
60 82
                    ->canBeEnabled()
61 82
                    ->beforeNormalization()
62 View Code Duplication
                        ->ifArray()->then(function ($v) {
63
                            if (!empty($v) && empty($v['formats'])) {
64
                                unset($v['enabled']);
65
                                $v = ['enabled' => true, 'formats' => $v];
66
                            }
67
68
                            return $v;
69 82
                        })
70 82
                    ->end()
71 82
                    ->fixXmlConfig('format', 'formats')
72 82
                    ->children()
73 82
                        ->scalarNode('service')->defaultNull()->end()
74 82
                        ->arrayNode('formats')
75 82
                            ->useAttributeAsKey('name')
76 82
                            ->prototype('boolean')->end()
77 82
                        ->end()
78 82
                    ->end()
79 82
                ->end()
80 82
                ->scalarNode('unauthorized_challenge')->defaultNull()->end()
81 82
                ->arrayNode('param_fetcher_listener')
82 82
                    ->beforeNormalization()
83 82
                        ->ifString()
84 View Code Duplication
                        ->then(function ($v) {
85 1
                            return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v];
86 82
                        })
87 82
                    ->end()
88 82
                    ->canBeEnabled()
89 82
                    ->children()
90 82
                        ->booleanNode('force')->defaultFalse()->end()
91 82
                        ->scalarNode('service')->defaultNull()->end()
92 82
                    ->end()
93 82
                ->end()
94 82
                ->scalarNode('cache_dir')->cannotBeEmpty()->defaultValue('%kernel.cache_dir%/fos_rest')->end()
95 82
                ->arrayNode('allowed_methods_listener')
96 82
                    ->canBeEnabled()
97 82
                    ->children()
98 82
                        ->scalarNode('service')->defaultNull()->end()
99 82
                    ->end()
100 82
                ->end()
101 82
                ->arrayNode('routing_loader')
102 82
                    ->addDefaultsIfNotSet()
103 82
                    ->beforeNormalization()
104
                        ->ifTrue(function ($v) { return isset($v['enabled']) && false !== $v['enabled']; })
105
                        ->then(function ($v) {
106
                            @trigger_error('Enabling the route generation feature is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
107
108
                            return $v;
109 82
                        })
110 82
                    ->end()
111 82
                    ->beforeNormalization()
112
                        ->ifTrue(function ($v) { return is_bool($v); })
113
                        ->then(function ($v) {
114
                            return [
115 65
                                'enabled' => $v,
116
                            ];
117 82
                        })
118 82
                    ->end()
119 82
                    ->children()
120 82
                        ->booleanNode('enabled')
121
                            ->defaultValue(function () {
122 6
                                @trigger_error('Enabling the route generation feature is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
123
124 6
                                return true;
125 82
                            })
126 82
                        ->end()
127 82
                        ->scalarNode('default_format')->defaultNull()->end()
128 82
                        ->scalarNode('prefix_methods')->defaultTrue()->end()
129 82
                        ->scalarNode('include_format')->defaultTrue()->end()
130 82
                    ->end()
131 82
                ->end()
132 82
                ->arrayNode('body_converter')
133 82
                    ->canBeEnabled()
134 82
                    ->children()
135 82
                        ->scalarNode('validate')
136 82
                            ->defaultFalse()
137 82
                            ->beforeNormalization()
138 82
                                ->ifTrue()
139
                                ->then(function ($value) {
140 3
                                    if (!class_exists(OptionsResolver::class)) {
141
                                        throw new InvalidConfigurationException("'body_converter.validate: true' requires OptionsResolver component installation ( composer require symfony/options-resolver )");
142
                                    }
143
144 3
                                    return $value;
145 82
                                })
146 82
                            ->end()
147 82
                        ->end()
148 82
                        ->scalarNode('validation_errors_argument')->defaultValue('validationErrors')->end()
149 82
                    ->end()
150 82
                ->end()
151 82
                ->arrayNode('service')
152 82
                    ->addDefaultsIfNotSet()
153 82
                    ->children()
154 82
                        ->scalarNode('router')->defaultValue('router')->setDeprecated('The "%path%.%node%" configuration key has been deprecated in FOSRestBundle 2.8.')->end()
155 82
                        ->scalarNode('templating')
156
                            ->defaultValue(static function () {
157 5
                                @trigger_error('Not setting the "fos_rest.service.templating" configuration option to "null" is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
158
159 5
                                return 'templating';
160 82
                            })
161 82
                            ->validate()
162
                                ->ifTrue(static function ($v) { return $v; })
163
                                ->then(static function ($v) {
164
                                    @trigger_error('Not setting the "fos_rest.service.templating" configuration option to "null" is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
165
166
                                    return $v;
167 82
                                })
168 82
                            ->end()
169 82
                        ->end()
170 82
                        ->scalarNode('serializer')->defaultNull()->end()
171 82
                        ->scalarNode('view_handler')->defaultValue('fos_rest.view_handler.default')->end()
172 82
                        ->scalarNode('inflector')
173 82
                            ->defaultValue('fos_rest.inflector.doctrine')
174 82
                            ->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
175 82
                        ->end()
176 82
                        ->scalarNode('validator')->defaultValue('validator')->end()
177 82
                    ->end()
178 82
                ->end()
179 82
                ->arrayNode('serializer')
180 82
                    ->addDefaultsIfNotSet()
181 82
                    ->children()
182 82
                        ->scalarNode('version')->defaultNull()->end()
183 82
                        ->arrayNode('groups')
184 82
                            ->prototype('scalar')->end()
185 82
                        ->end()
186 82
                        ->booleanNode('serialize_null')->defaultFalse()->end()
187 82
                    ->end()
188 82
                ->end()
189 82
                ->arrayNode('zone')
190 82
                    ->cannotBeOverwritten()
191 82
                    ->prototype('array')
192 82
                    ->fixXmlConfig('ip')
193 82
                    ->children()
194 82
                        ->scalarNode('path')
195 82
                            ->defaultNull()
196 82
                            ->info('use the urldecoded format')
197 82
                            ->example('^/path to resource/')
198 82
                        ->end()
199 82
                        ->scalarNode('host')->defaultNull()->end()
200 82
                        ->arrayNode('methods')
201
                            ->beforeNormalization()->ifString()->then(function ($v) {
202
                                return preg_split('/\s*,\s*/', $v);
203 82
                            })->end()
204 82
                            ->prototype('scalar')->end()
205 82
                        ->end()
206 82
                        ->arrayNode('ips')
207
                            ->beforeNormalization()->ifString()->then(function ($v) {
208 1
                                return array($v);
209 82
                            })->end()
210 82
                            ->prototype('scalar')->end()
211 82
                        ->end()
212 82
                    ->end()
213 82
                ->end()
214 82
            ->end()
215 82
        ->end();
216
217 82
        $this->addViewSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
218 82
        $this->addExceptionSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
219 82
        $this->addBodyListenerSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
220 82
        $this->addFormatListenerSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
221 82
        $this->addVersioningSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
222
223 82
        return $treeBuilder;
224
    }
225
226 82
    private function addViewSection(ArrayNodeDefinition $rootNode)
227
    {
228
        $rootNode
229 82
            ->children()
230 82
                ->arrayNode('view')
231 82
                    ->fixXmlConfig('format', 'formats')
232 82
                    ->fixXmlConfig('mime_type', 'mime_types')
233 82
                    ->fixXmlConfig('templating_format', 'templating_formats')
234 82
                    ->fixXmlConfig('force_redirect', 'force_redirects')
235 82
                    ->addDefaultsIfNotSet()
236 82
                    ->children()
237 82
                        ->scalarNode('default_engine')
238
                            ->defaultValue(static function () {
239 5
                                @trigger_error('Not setting the "fos_rest.view.default_engine" configuration option to "null" is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
240
241 5
                                return 'twig';
242 82
                            })
243 82
                            ->validate()
244
                                ->ifTrue(static function ($v) { return $v; })
245
                                ->then(static function ($v) {
246
                                    @trigger_error('Not setting the "fos_rest.view.default_engine" configuration option to "null" is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
247
248
                                    return $v;
249 82
                                })
250 82
                            ->end()
251 82
                        ->end()
252 82
                        ->arrayNode('force_redirects')
253 82
                            ->useAttributeAsKey('name')
254 82
                            ->defaultValue(['html' => true])
255 82
                            ->prototype('boolean')->end()
256 82
                        ->end()
257 82
                        ->arrayNode('mime_types')
258 82
                            ->canBeEnabled()
259 82
                            ->beforeNormalization()
260 View Code Duplication
                                ->ifArray()->then(function ($v) {
261 1
                                    if (!empty($v) && empty($v['formats'])) {
262 1
                                        unset($v['enabled']);
263 1
                                        $v = ['enabled' => true, 'formats' => $v];
264
                                    }
265
266 1
                                    return $v;
267 82
                                })
268 82
                            ->end()
269 82
                            ->fixXmlConfig('format', 'formats')
270 82
                            ->children()
271 82
                                ->scalarNode('service')->defaultNull()->end()
272 82
                                ->arrayNode('formats')
273 82
                                    ->useAttributeAsKey('name')
274 82
                                    ->prototype('array')
275 82
                                        ->beforeNormalization()
276 82
                                            ->ifString()
277
                                            ->then(function ($v) { return array($v); })
278 82
                                        ->end()
279 82
                                        ->prototype('scalar')->end()
280 82
                                    ->end()
281 82
                                ->end()
282 82
                            ->end()
283 82
                        ->end()
284 82
                        ->arrayNode('formats')
285 82
                            ->useAttributeAsKey('name')
286 82
                            ->defaultValue(['json' => true, 'xml' => true])
287 82
                            ->prototype('boolean')->end()
288 82
                        ->end()
289 82
                        ->arrayNode('templating_formats')
290 82
                            ->setDeprecated('The "%path%.%node%" configuration key has been deprecated in FOSRestBundle 2.8.')
291 82
                            ->useAttributeAsKey('name')
292 82
                            ->defaultValue(['html' => true])
293 82
                            ->prototype('boolean')->end()
294 82
                        ->end()
295 82
                        ->arrayNode('view_response_listener')
296 82
                            ->beforeNormalization()
297 82
                                ->ifString()
298 View Code Duplication
                                ->then(function ($v) {
299 9
                                    return ['enabled' => in_array($v, ['force', 'true']), 'force' => 'force' === $v];
300 82
                                })
301 82
                            ->end()
302 82
                            ->canBeEnabled()
303 82
                            ->children()
304 82
                                ->booleanNode('force')->defaultFalse()->end()
305 82
                                ->scalarNode('service')->defaultNull()->end()
306 82
                            ->end()
307 82
                        ->end()
308 82
                        ->scalarNode('failed_validation')->defaultValue(Response::HTTP_BAD_REQUEST)->end()
309 82
                        ->scalarNode('empty_content')->defaultValue(Response::HTTP_NO_CONTENT)->end()
310 82
                        ->booleanNode('serialize_null')->defaultFalse()->end()
311 82
                        ->arrayNode('jsonp_handler')
312 82
                            ->canBeUnset()
313 82
                            ->children()
314 82
                                ->scalarNode('callback_param')->defaultValue('callback')->end()
315 82
                                ->scalarNode('mime_type')->defaultValue('application/javascript+jsonp')->end()
316 82
                            ->end()
317 82
                        ->end()
318 82
                    ->end()
319 82
                ->end()
320 82
            ->end();
321 82
    }
322
323 82
    private function addBodyListenerSection(ArrayNodeDefinition $rootNode)
324
    {
325 82
        $decodersDefaultValue = ['json' => 'fos_rest.decoder.json'];
326 82
        if (class_exists(XmlEncoder::class)) {
327 82
            $decodersDefaultValue['xml'] = 'fos_rest.decoder.xml';
328
        }
329
        $rootNode
330 82
            ->children()
331 82
                ->arrayNode('body_listener')
332 82
                    ->fixXmlConfig('decoder', 'decoders')
333 82
                    ->addDefaultsIfNotSet()
334 82
                    ->canBeUnset()
335 82
                    ->treatFalseLike(['enabled' => false])
336 82
                    ->treatTrueLike(['enabled' => true])
337 82
                    ->treatNullLike(['enabled' => true])
338 82
                    ->children()
339 82
                        ->booleanNode('enabled')
340
                            ->defaultValue(function () {
341 6
                                @trigger_error('The body_listener config has been enabled by default and will be disabled by default in FOSRestBundle 3.0. Please enable or disable it explicitly.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
342
343 6
                                return true;
344 82
                            })
345 82
                        ->end()
346 82
                        ->scalarNode('service')->defaultNull()->end()
347 82
                        ->scalarNode('default_format')->defaultNull()->end()
348 82
                        ->booleanNode('throw_exception_on_unsupported_content_type')
349 82
                            ->defaultFalse()
350 82
                        ->end()
351 82
                        ->arrayNode('decoders')
352 82
                            ->useAttributeAsKey('name')
353 82
                            ->defaultValue($decodersDefaultValue)
354 82
                            ->prototype('scalar')->end()
355 82
                        ->end()
356 82
                        ->arrayNode('array_normalizer')
357 82
                            ->addDefaultsIfNotSet()
358 82
                            ->beforeNormalization()
359
                                ->ifString()->then(function ($v) {
360 1
                                    return ['service' => $v];
361 82
                                })
362 82
                            ->end()
363 82
                            ->children()
364 82
                                ->scalarNode('service')->defaultNull()->end()
365 82
                                ->booleanNode('forms')->defaultFalse()->end()
366 82
                            ->end()
367 82
                        ->end()
368 82
                    ->end()
369 82
                ->end()
370 82
            ->end();
371 82
    }
372
373 82
    private function addFormatListenerSection(ArrayNodeDefinition $rootNode)
374
    {
375
        $rootNode
376 82
            ->children()
377 82
                ->arrayNode('format_listener')
378 82
                    ->fixXmlConfig('rule', 'rules')
379 82
                    ->addDefaultsIfNotSet()
380 82
                    ->canBeUnset()
381 82
                    ->beforeNormalization()
382
                        ->ifTrue(function ($v) {
383
                            // check if we got an assoc array in rules
384 9
                            return isset($v['rules'])
385 9
                                && is_array($v['rules'])
386 9
                                && array_keys($v['rules']) !== range(0, count($v['rules']) - 1);
387 82
                        })
388
                        ->then(function ($v) {
389 1
                            $v['rules'] = [$v['rules']];
390
391 1
                            return $v;
392 82
                        })
393 82
                    ->end()
394 82
                    ->canBeEnabled()
395 82
                    ->children()
396 82
                        ->scalarNode('service')->defaultNull()->end()
397 82
                        ->arrayNode('rules')
398 82
                            ->performNoDeepMerging()
399 82
                            ->prototype('array')
400 82
                                ->fixXmlConfig('priority', 'priorities')
401 82
                                ->fixXmlConfig('attribute', 'attributes')
402 82
                                ->children()
403 82
                                    ->scalarNode('path')->defaultNull()->info('URL path info')->end()
404 82
                                    ->scalarNode('host')->defaultNull()->info('URL host name')->end()
405 82
                                    ->variableNode('methods')->defaultNull()->info('Method for URL')->end()
406 82
                                    ->arrayNode('attributes')
407 82
                                        ->useAttributeAsKey('name')
408 82
                                        ->prototype('variable')->end()
409 82
                                    ->end()
410 82
                                    ->booleanNode('stop')->defaultFalse()->end()
411 82
                                    ->booleanNode('prefer_extension')->defaultTrue()->end()
412 82
                                    ->scalarNode('fallback_format')->defaultValue('html')->end()
413 82
                                    ->arrayNode('priorities')
414
                                        ->beforeNormalization()->ifString()->then(function ($v) {
415
                                            return preg_split('/\s*,\s*/', $v);
416 82
                                        })->end()
417 82
                                        ->prototype('scalar')->end()
418 82
                                    ->end()
419 82
                                ->end()
420 82
                            ->end()
421 82
                        ->end()
422 82
                    ->end()
423 82
                ->end()
424 82
            ->end();
425 82
    }
426
427 82
    private function addVersioningSection(ArrayNodeDefinition $rootNode)
428
    {
429
        $rootNode
430 82
        ->children()
431 82
            ->arrayNode('versioning')
432 82
                ->canBeEnabled()
433 82
                ->children()
434 82
                    ->scalarNode('default_version')->defaultNull()->end()
435 82
                    ->arrayNode('resolvers')
436 82
                        ->addDefaultsIfNotSet()
437 82
                        ->children()
438 82
                            ->arrayNode('query')
439 82
                                ->canBeDisabled()
440 82
                                ->children()
441 82
                                    ->scalarNode('parameter_name')->defaultValue('version')->end()
442 82
                                ->end()
443 82
                            ->end()
444 82
                            ->arrayNode('custom_header')
445 82
                                ->canBeDisabled()
446 82
                                ->children()
447 82
                                    ->scalarNode('header_name')->defaultValue('X-Accept-Version')->end()
448 82
                                ->end()
449 82
                            ->end()
450 82
                            ->arrayNode('media_type')
451 82
                                ->canBeDisabled()
452 82
                                ->children()
453 82
                                    ->scalarNode('regex')->defaultValue('/(v|version)=(?P<version>[0-9\.]+)/')->end()
454 82
                                ->end()
455 82
                            ->end()
456 82
                        ->end()
457 82
                    ->end()
458 82
                    ->arrayNode('guessing_order')
459 82
                        ->defaultValue(['query', 'custom_header', 'media_type'])
460 82
                        ->validate()
461
                            ->ifTrue(function ($v) {
462
                                foreach ($v as $resolver) {
463
                                    if (!in_array($resolver, ['query', 'custom_header', 'media_type'])) {
464
                                        return true;
465
                                    }
466
                                }
467 82
                            })
468 82
                            ->thenInvalid('Versioning guessing order can only contain "query", "custom_header", "media_type".')
469 82
                        ->end()
470 82
                        ->prototype('scalar')->end()
471 82
                    ->end()
472 82
                ->end()
473 82
            ->end()
474 82
        ->end();
475 82
    }
476
477 82
    private function addExceptionSection(ArrayNodeDefinition $rootNode)
478
    {
479
        $rootNode
480 82
            ->children()
481 82
                ->arrayNode('exception')
482 82
                    ->fixXmlConfig('code', 'codes')
483 82
                    ->fixXmlConfig('message', 'messages')
484 82
                    ->addDefaultsIfNotSet()
485 82
                    ->canBeEnabled()
486 82
                    ->children()
487 82
                        ->booleanNode('map_exception_codes')
488 82
                            ->defaultFalse()
489 82
                            ->info('Enables an event listener that maps exception codes to response status codes based on the map configured with the "fos_rest.exception.codes" option.')
490 82
                        ->end()
491 82
                        ->booleanNode('exception_listener')
492
                            ->defaultValue(function () {
493 14
                                @trigger_error('Enabling the "fos_rest.exception.exception_listener" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
494
495 14
                                return true;
496 82
                            })
497 82
                            ->beforeNormalization()
498 82
                                ->ifTrue()
499
                                ->then(function ($v) {
500
                                    @trigger_error('Enabling the "fos_rest.exception.exception_listener" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
501
502
                                    return $v;
503 82
                                })
504 82
                            ->end()
505 82
                        ->end()
506 82
                        ->scalarNode('exception_controller')
507 82
                            ->defaultNull()
508 82
                            ->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
509 82
                        ->end()
510 82
                        ->booleanNode('serialize_exceptions')
511
                            ->defaultValue(function () {
512 9
                                @trigger_error('Enabling the "fos_rest.exception.serialize_exceptions" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
513
514 9
                                return true;
515 82
                            })
516 82
                            ->beforeNormalization()
517 82
                                ->ifTrue()
518
                                ->then(function ($v) {
519 8
                                    @trigger_error('Enabling the "fos_rest.exception.serialize_exceptions" option is deprecated since FOSRestBundle 2.8.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
520
521 8
                                    return $v;
522 82
                                })
523 82
                            ->end()
524 82
                        ->end()
525 82
                        ->enumNode('flatten_exception_format')
526 82
                            ->defaultValue('legacy')
527 82
                            ->values(['legacy', 'rfc7807'])
528 82
                        ->end()
529 82
                        ->scalarNode('service')
530 82
                            ->defaultNull()
531 82
                            ->setDeprecated('The "%path%.%node%" option is deprecated since FOSRestBundle 2.8.')
532 82
                        ->end()
533 82
                        ->booleanNode('serializer_error_renderer')->defaultValue(false)->end()
534 82
                        ->arrayNode('codes')
535 82
                            ->useAttributeAsKey('name')
536 82
                            ->beforeNormalization()
537 82
                                ->ifArray()
538
                                ->then(function (array $items) {
539 13
                                    foreach ($items as &$item) {
540 13
                                        if (is_int($item)) {
541 3
                                            continue;
542
                                        }
543
544 10
                                        if (!defined(sprintf('%s::%s', Response::class, $item))) {
545 9
                                            throw new InvalidConfigurationException(sprintf('Invalid HTTP code in fos_rest.exception.codes, see %s for all valid codes.', Response::class));
546
                                        }
547
548 1
                                        $item = constant(sprintf('%s::%s', Response::class, $item));
549
                                    }
550
551 4
                                    return $items;
552 82
                                })
553 82
                            ->end()
554 82
                            ->prototype('integer')->end()
555
556 82
                            ->validate()
557 82
                            ->ifArray()
558
                                ->then(function (array $items) {
559 4
                                    foreach ($items as $class => $code) {
560 4
                                        $this->testExceptionExists($class);
561
                                    }
562
563 3
                                    return $items;
564 82
                                })
565 82
                            ->end()
566 82
                        ->end()
567 82
                        ->arrayNode('messages')
568 82
                            ->useAttributeAsKey('name')
569 82
                            ->prototype('boolean')->end()
570 82
                            ->validate()
571 82
                                ->ifArray()
572
                                ->then(function (array $items) {
573 12
                                    foreach ($items as $class => $nomatter) {
574 12
                                        $this->testExceptionExists($class);
575
                                    }
576
577 11
                                    return $items;
578 82
                                })
579 82
                            ->end()
580 82
                        ->end()
581 82
                        ->booleanNode('debug')
582 82
                            ->defaultValue($this->debug)
583 82
                        ->end()
584 82
                    ->end()
585 82
                ->end()
586 82
            ->end();
587 82
    }
588
589 16
    private function testExceptionExists(string $throwable)
590
    {
591 16
        if (!is_subclass_of($throwable, \Throwable::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Throwable::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
592 2
            throw new InvalidConfigurationException(sprintf('FOSRestBundle exception mapper: Could not load class "%s" or the class does not extend from "%s". Most probably this is a configuration problem.', $throwable, \Throwable::class));
593
        }
594 14
    }
595
}
596