Completed
Push — master ( edf55a...cda77c )
by Asmir
03:02
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 Symfony\Component\Config\Definition\Builder\NodeBuilder;
22
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
23
use Symfony\Component\Config\Definition\ConfigurationInterface;
24
use JMS\Serializer\Exception\InvalidArgumentException;
25
26
class Configuration implements ConfigurationInterface
27
{
28
    private $debug;
29
30
    /**
31
     * @param boolean $debug
32
     */
33 14
    public function __construct($debug = false)
34
    {
35 14
        $this->debug = $debug;
36 14
    }
37
38 14
    public function getConfigTreeBuilder()
39
    {
40 14
        $tb = new TreeBuilder();
41
42
        $root = $tb
43 14
            ->root('jms_serializer', 'array')
44 14
                ->children()
45 14
                    ->booleanNode('enable_short_alias')->defaultTrue()->end()
46 14
        ;
47
48 14
        $this->addHandlersSection($root);
49 14
        $this->addSerializersSection($root);
50 14
        $this->addMetadataSection($root);
51 14
        $this->addVisitorsSection($root);
52
53 14
        return $tb;
54
    }
55
56 14
    private function addHandlersSection(NodeBuilder $builder)
57
    {
58
        $builder
59 14
            ->arrayNode('handlers')
60 14
                ->addDefaultsIfNotSet()
61 14
                ->children()
62 14
                    ->arrayNode('datetime')
63 14
                        ->addDefaultsIfNotSet()
64 14
                        ->children()
65 14
                            ->scalarNode('default_format')->defaultValue(\DateTime::ISO8601)->end()
66 14
                            ->scalarNode('default_timezone')->defaultValue(date_default_timezone_get())->end()
67 14
                            ->scalarNode('cdata')->defaultTrue()->end()
68 14
                        ->end()
69 14
                   ->end()
70 14
                ->end()
71 14
            ->end()
72
        ;
73 14
    }
74
75 14
    private function addSerializersSection(NodeBuilder $builder)
76
    {
77
        $builder
78 14
            ->arrayNode('property_naming')
79 14
                ->addDefaultsIfNotSet()
80 14
                ->children()
81 14
                    ->scalarNode('id')->cannotBeEmpty()->end()
82 14
                    ->scalarNode('separator')->defaultValue('_')->end()
83 14
                    ->booleanNode('lower_case')->defaultTrue()->end()
84 14
                    ->booleanNode('enable_cache')->defaultTrue()->end()
85 14
                ->end()
86 14
            ->end()
87 14
            ->arrayNode('expression_evaluator')
88 14
                ->addDefaultsIfNotSet()
89 14
                ->children()
90 14
                    ->scalarNode('id')
91
                        ->defaultValue(function () {
92 12
                            if (interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) {
93 12
                                return 'jms_serializer.expression_evaluator';
94
                            }
95
                            return null;
96 14
                        })
97 14
                        ->validate()
98
                            ->always(function($v) {
99 2
                                if (!empty($v) && !class_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) {
100 1
                                    throw new InvalidArgumentException('You need at least symfony/expression language v2.6 or v3.0 to use the expression evaluator features');
101
                                }
102 1
                                return $v;
103 14
                            })
104 14
                        ->end()
105 14
                ->end()
106 14
            ->end()
107
        ;
108 14
    }
109
110 14
    private function addMetadataSection(NodeBuilder $builder)
111
    {
112
        $builder
113 14
            ->arrayNode('metadata')
114 14
                ->addDefaultsIfNotSet()
115 14
                ->fixXmlConfig('directory', 'directories')
116 14
                ->children()
117 14
                    ->scalarNode('cache')->defaultValue('file')->end()
118 14
                    ->booleanNode('debug')->defaultValue($this->debug)->end()
119 14
                    ->arrayNode('file_cache')
120 14
                        ->addDefaultsIfNotSet()
121 14
                        ->children()
122 14
                            ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end()
123 14
                        ->end()
124 14
                    ->end()
125 14
                    ->booleanNode('auto_detection')->defaultTrue()->end()
126 14
                    ->booleanNode('infer_types_from_doctrine_metadata')
127 14
                        ->info('Infers type information from Doctrine metadata if no explicit type has been defined for a property.')
128 14
                        ->defaultTrue()
129 14
                    ->end()
130 14
                    ->arrayNode('directories')
131 14
                        ->prototype('array')
132 14
                            ->children()
133 14
                                ->scalarNode('path')->isRequired()->end()
134 14
                                ->scalarNode('namespace_prefix')->defaultValue('')->end()
135 14
                            ->end()
136 14
                        ->end()
137 14
                    ->end()
138 14
                ->end()
139 14
            ->end()
140
        ;
141 14
    }
142
143 14
    private function addVisitorsSection(NodeBuilder $builder)
144
    {
145
        $builder
146 14
            ->arrayNode('visitors')
147 14
                ->addDefaultsIfNotSet()
148 14
                ->children()
149 14
                    ->arrayNode('json')
150 14
                        ->addDefaultsIfNotSet()
151 14
                        ->children()
152 14
                            ->scalarNode('options')
153 14
                                ->defaultValue(0)
154 14
                                ->beforeNormalization()
155
                                    ->ifArray()->then(function($v) {
156 1
                                        $options = 0;
157 1
                                        foreach ($v as $option) {
158 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...
159
                                                $options |= (int) $option;
160 1
                                            } elseif (defined($option)) {
161 1
                                                $options |= constant($option);
162 1
                                            } else {
163
                                                throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
164
                                            }
165 1
                                        }
166
167 1
                                        return $options;
168 14
                                    })
169 14
                                ->end()
170 14
                                ->beforeNormalization()
171
                                    ->ifString()->then(function($v) {
172 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...
173
                                            $value = (int) $v;
174 1
                                        } elseif (defined($v)) {
175 1
                                            $value = constant($v);
176 1
                                        } else {
177
                                            throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
178
                                        }
179
180 1
                                        return $value;
181 14
                                    })
182 14
                                ->end()
183 14
                                ->validate()
184 14
                                    ->always(function($v) {
185 3
                                        if (!is_int($v)) {
186
                                            throw new InvalidArgumentException('Expected either integer value or a array of the JSON_ constants.');
187
                                        }
188
189 3
                                        return $v;
190 14
                                    })
191 14
                                ->end()
192 14
                            ->end()
193 14
                        ->end()
194 14
                    ->end()
195 14
                    ->arrayNode('xml')
196 14
                        ->fixXmlConfig('whitelisted-doctype', 'doctype_whitelist')
197 14
                        ->addDefaultsIfNotSet()
198 14
                        ->children()
199 14
                            ->arrayNode('doctype_whitelist')
200 14
                                ->prototype('scalar')->end()
201 14
                            ->end()
202 14
                            ->booleanNode('format_output')
203 14
                                ->defaultTrue()
204 14
                            ->end()
205 14
                        ->end()
206 14
                    ->end()
207 14
                ->end()
208 14
            ->end()
209
        ;
210 14
    }
211
}
212