Completed
Pull Request — master (#556)
by Evgenij
02:53
created

Configuration::addVisitorsSection()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 68
Code Lines 55

Duplication

Lines 14
Ratio 20.59 %

Code Coverage

Tests 52
CRAP Score 7.033

Importance

Changes 0
Metric Value
dl 14
loc 68
ccs 52
cts 57
cp 0.9123
rs 6.9654
c 0
b 0
f 0
cc 7
eloc 55
nc 1
nop 1
crap 7.033

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
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\SerializerBundle\DependencyInjection;
20
21
use JMS\Serializer\Exception\InvalidArgumentException;
22
use JMS\Serializer\Exclusion\GroupsExclusionStrategy;
23
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
24
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
25
use Symfony\Component\Config\Definition\ConfigurationInterface;
26
27
class Configuration implements ConfigurationInterface
28
{
29
    private $debug;
30
31
    /**
32
     * @param boolean $debug
33
     */
34 35
    public function __construct($debug = false)
35
    {
36 35
        $this->debug = $debug;
37 35
    }
38
39 35
    public function getConfigTreeBuilder()
40 1
    {
41 35
        $tb = new TreeBuilder();
42
43
        $root = $tb
44 35
            ->root('jms_serializer', 'array')
45 35
                ->children()
46 35
                    ->booleanNode('enable_short_alias')->defaultTrue()->end()
47 35
        ;
48
49 35
        $this->addHandlersSection($root);
50 35
        $this->addSerializersSection($root);
51 35
        $this->addMetadataSection($root);
52 35
        $this->addVisitorsSection($root);
53 35
        $this->addContextSection($root);
54
55 35
        return $tb;
56
    }
57
58 35
    private function addHandlersSection(NodeBuilder $builder)
59
    {
60
        $builder
61 35
            ->arrayNode('handlers')
62 35
                ->addDefaultsIfNotSet()
63 35
                ->children()
64 35
                    ->arrayNode('datetime')
65 35
                        ->addDefaultsIfNotSet()
66 35
                        ->children()
67 35
                            ->scalarNode('default_format')->defaultValue(\DateTime::ISO8601)->end()
68 35
                            ->scalarNode('default_timezone')->defaultValue(date_default_timezone_get())->end()
69 35
                            ->scalarNode('cdata')->defaultTrue()->end()
70 35
                        ->end()
71 35
                   ->end()
72 35
                ->end()
73 35
            ->end()
74
        ;
75 35
    }
76
77 35
    private function addSerializersSection(NodeBuilder $builder)
78
    {
79
        $builder
80 35
            ->arrayNode('property_naming')
81 35
                ->addDefaultsIfNotSet()
82 35
                ->children()
83 35
                    ->scalarNode('id')->cannotBeEmpty()->end()
84 35
                    ->scalarNode('separator')->defaultValue('_')->end()
85 35
                    ->booleanNode('lower_case')->defaultTrue()->end()
86 35
                    ->booleanNode('enable_cache')->defaultTrue()->end()
87 35
                ->end()
88 35
            ->end()
89 35
            ->arrayNode('expression_evaluator')
90 35
                ->addDefaultsIfNotSet()
91 35
                ->children()
92 35
                    ->scalarNode('id')
93
                        ->defaultValue(function () {
94 33
                            if (interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) {
95 33
                                return 'jms_serializer.expression_evaluator';
96
                            }
97
                            return null;
98 35
                        })
99 35
                        ->validate()
100
                            ->always(function($v) {
101 2
                                if (!empty($v) && !class_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) {
102 1
                                    throw new InvalidArgumentException('You need at least symfony/expression language v2.6 or v3.0 to use the expression evaluator features');
103
                                }
104 1
                                return $v;
105 35
                            })
106 35
                        ->end()
107 35
                ->end()
108 35
            ->end()
109
        ;
110 35
    }
111
112 35
    private function addMetadataSection(NodeBuilder $builder)
113
    {
114
        $builder
115 35
            ->arrayNode('metadata')
116 35
                ->addDefaultsIfNotSet()
117 35
                ->fixXmlConfig('directory', 'directories')
118 35
                ->children()
119 35
                    ->scalarNode('cache')->defaultValue('file')->end()
120 35
                    ->booleanNode('debug')->defaultValue($this->debug)->end()
121 35
                    ->arrayNode('file_cache')
122 35
                        ->addDefaultsIfNotSet()
123 35
                        ->children()
124 35
                            ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end()
125 35
                        ->end()
126 35
                    ->end()
127 35
                    ->booleanNode('auto_detection')->defaultTrue()->end()
128 35
                    ->booleanNode('infer_types_from_doctrine_metadata')
129 35
                        ->info('Infers type information from Doctrine metadata if no explicit type has been defined for a property.')
130 35
                        ->defaultTrue()
131 35
                    ->end()
132 35
                    ->arrayNode('directories')
133 35
                        ->prototype('array')
134 35
                            ->children()
135 35
                                ->scalarNode('path')->isRequired()->end()
136 35
                                ->scalarNode('namespace_prefix')->defaultValue('')->end()
137 35
                            ->end()
138 35
                        ->end()
139 35
                    ->end()
140 35
                ->end()
141 35
            ->end()
142
        ;
143 35
    }
144
145 35
    private function addVisitorsSection(NodeBuilder $builder)
146
    {
147
        $builder
148 35
            ->arrayNode('visitors')
149 35
                ->addDefaultsIfNotSet()
150 35
                ->children()
151 35
                    ->arrayNode('json')
152 35
                        ->addDefaultsIfNotSet()
153 35
                        ->children()
154 35
                            ->scalarNode('options')
155 35
                                ->defaultValue(0)
156 35
                                ->beforeNormalization()
157
                                    ->ifArray()->then(function($v) {
158 1
                                        $options = 0;
159 1
                                        foreach ($v as $option) {
160 1 View Code Duplication
                                            if (is_numeric($option)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
161
                                                $options |= (int) $option;
162 1
                                            } elseif (defined($option)) {
163 1
                                                $options |= constant($option);
164 1
                                            } else {
165
                                                throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
166
                                            }
167 1
                                        }
168
169 1
                                        return $options;
170 35
                                    })
171 35
                                ->end()
172 35
                                ->beforeNormalization()
173
                                    ->ifString()->then(function($v) {
174 1 View Code Duplication
                                        if (is_numeric($v)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
175
                                            $value = (int) $v;
176 1
                                        } elseif (defined($v)) {
177 1
                                            $value = constant($v);
178 1
                                        } else {
179
                                            throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
180
                                        }
181
182 1
                                        return $value;
183 35
                                    })
184 35
                                ->end()
185 35
                                ->validate()
186 35
                                    ->always(function($v) {
187 3
                                        if (!is_int($v)) {
188
                                            throw new InvalidArgumentException('Expected either integer value or a array of the JSON_ constants.');
189
                                        }
190
191 3
                                        return $v;
192 35
                                    })
193 35
                                ->end()
194 35
                            ->end()
195 35
                        ->end()
196 35
                    ->end()
197 35
                    ->arrayNode('xml')
198 35
                        ->fixXmlConfig('whitelisted-doctype', 'doctype_whitelist')
199 35
                        ->addDefaultsIfNotSet()
200 35
                        ->children()
201 35
                            ->arrayNode('doctype_whitelist')
202 35
                                ->prototype('scalar')->end()
203 35
                            ->end()
204 35
                            ->booleanNode('format_output')
205 35
                                ->defaultTrue()
206 35
                            ->end()
207 35
                        ->end()
208 35
                    ->end()
209 35
                ->end()
210 35
            ->end()
211
        ;
212 35
    }
213
214 35
    private function addContextSection(NodeBuilder $builder)
215
    {
216
        $root = $builder
217 35
                    ->arrayNode('default_context')
218 35
                    ->addDefaultsIfNotSet();
219
220 35
        $this->createContextNode($root->children(), 'serialization');
221 35
        $this->createContextNode($root->children(), 'deserialization');
222 35
    }
223
224 35
    private function createContextNode(NodeBuilder $builder, $name)
225
    {
226
        $builder
227 35
            ->arrayNode($name)
228 35
                ->addDefaultsIfNotSet()
229 35
                ->children()
230 35
                    ->booleanNode('serialize_null')
231 35
                        ->defaultFalse()
232 35
                        ->treatNullLike(false)
233 35
                        ->info('Flag if null values should be serialized')
234 35
                    ->end()
235 35
                    ->arrayNode('attributes')
236 35
                        ->fixXmlConfig('attribute')
237 35
                        ->defaultValue([])
238 35
                        ->treatNullLike([])
239 35
                        ->useAttributeAsKey('key')
240 35
                        ->prototype('scalar')->end()
241 35
                        ->info('Arbitrary key-value data for context')
242 35
                    ->end()
243 35
                    ->arrayNode('groups')
244 35
                        ->fixXmlConfig('group')
245 35
                        ->defaultValue([GroupsExclusionStrategy::DEFAULT_GROUP])
246 35
                        ->treatNullLike([GroupsExclusionStrategy::DEFAULT_GROUP])
247 35
                        ->prototype('scalar')->end()
248 35
                        ->info('Default serialization groups')
249 35
                    ->end()
250 35
                    ->scalarNode('version')
251 35
                        ->defaultNull()
252 35
                        ->info('Application version to use in exclusion strategies')
253 35
                    ->end()
254 35
                ->end()
255 35
            ->end();
256 35
    }
257
}
258