GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d118bb...8e4e23 )
by Asmir
03:06
created

Configuration::createContextNode()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 51
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 42
CRAP Score 3.0026

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.4109
c 0
b 0
f 0
ccs 42
cts 45
cp 0.9333
cc 3
eloc 45
nc 1
nop 2
crap 3.0026

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 Symfony\Component\Config\Definition\Builder\NodeBuilder;
23
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
24
use Symfony\Component\Config\Definition\ConfigurationInterface;
25
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
26
27
class Configuration implements ConfigurationInterface
28
{
29
    private $debug;
30
31
    /**
32
     * @param boolean $debug
33
     */
34 43
    public function __construct($debug = false)
35
    {
36 43
        $this->debug = $debug;
37 43
    }
38
39 43
    public function getConfigTreeBuilder()
40 1
    {
41 43
        $tb = new TreeBuilder();
42
43
        $root = $tb
44 43
            ->root('jms_serializer', 'array')
45 43
                ->children()
46 43
                    ->booleanNode('enable_short_alias')->defaultTrue()->end()
47 43
        ;
48
49 43
        $this->addHandlersSection($root);
50 43
        $this->addSerializersSection($root);
51 43
        $this->addMetadataSection($root);
52 43
        $this->addVisitorsSection($root);
53 43
        $this->addContextSection($root);
54
55 43
        return $tb;
56
    }
57
58 43
    private function addHandlersSection(NodeBuilder $builder)
59
    {
60
        $builder
61 43
            ->arrayNode('handlers')
62 43
                ->addDefaultsIfNotSet()
63 43
                ->children()
64 43
                    ->arrayNode('datetime')
65 43
                        ->addDefaultsIfNotSet()
66 43
                        ->children()
67 43
                            ->scalarNode('default_format')->defaultValue(\DateTime::ISO8601)->end()
68 43
                            ->scalarNode('default_timezone')->defaultValue(date_default_timezone_get())->end()
69 43
                            ->scalarNode('cdata')->defaultTrue()->end()
70 43
                        ->end()
71 43
                   ->end()
72 43
                ->end()
73 43
            ->end()
74
        ;
75 43
    }
76
77 43
    private function addSerializersSection(NodeBuilder $builder)
78
    {
79
        $builder
80 43
            ->arrayNode('property_naming')
81 43
                ->addDefaultsIfNotSet()
82 43
                ->beforeNormalization()
83 43
                    ->ifString()
84
                    ->then(function ($id) {
85 1
                        return array('id' => $id);
86 43
                    })
87 43
                ->end()
88 43
                ->children()
89 43
                    ->scalarNode('id')->cannotBeEmpty()->end()
90 43
                    ->scalarNode('separator')->defaultValue('_')->end()
91 43
                    ->booleanNode('lower_case')->defaultTrue()->end()
92 43
                    ->booleanNode('enable_cache')->defaultTrue()->end()
93 43
                ->end()
94 43
            ->end()
95 43
            ->arrayNode('expression_evaluator')
96 43
                ->addDefaultsIfNotSet()
97 43
                ->beforeNormalization()
98 43
                    ->ifString()
99
                    ->then(function ($id) {
100 1
                        return array('id' => $id);
101 43
                    })
102 43
                ->end()
103 43
                ->children()
104 43
                    ->scalarNode('id')
105
                        ->defaultValue(function () {
106 40
                            if (interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) {
107 40
                                return 'jms_serializer.expression_evaluator';
108
                            }
109
                            return null;
110 43
                        })
111 43
                        ->validate()
112
                            ->always(function($v) {
113 3
                                if (!empty($v) && !interface_exists('Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface')) {
114
                                    throw new InvalidArgumentException('You need at least symfony/expression language v2.6 or v3.0 to use the expression evaluator features');
115
                                }
116 3
                                return $v;
117 43
                            })
118 43
                        ->end()
119 43
                ->end()
120 43
            ->end()
121
        ;
122 43
    }
123
124 43
    private function addMetadataSection(NodeBuilder $builder)
125
    {
126
        $builder
127 43
            ->arrayNode('metadata')
128 43
                ->addDefaultsIfNotSet()
129 43
                ->fixXmlConfig('directory', 'directories')
130 43
                ->children()
131 43
                    ->scalarNode('cache')->defaultValue('file')->end()
132 43
                    ->booleanNode('debug')->defaultValue($this->debug)->end()
133 43
                    ->arrayNode('file_cache')
134 43
                        ->addDefaultsIfNotSet()
135 43
                        ->children()
136 43
                            ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end()
137 43
                        ->end()
138 43
                    ->end()
139 43
                    ->booleanNode('auto_detection')->defaultTrue()->end()
140 43
                    ->booleanNode('infer_types_from_doctrine_metadata')
141 43
                        ->info('Infers type information from Doctrine metadata if no explicit type has been defined for a property.')
142 43
                        ->defaultTrue()
143 43
                    ->end()
144 43
                    ->arrayNode('directories')
145 43
                        ->prototype('array')
146 43
                            ->children()
147 43
                                ->scalarNode('path')->isRequired()->end()
148 43
                                ->scalarNode('namespace_prefix')->defaultValue('')->end()
149 43
                            ->end()
150 43
                        ->end()
151 43
                    ->end()
152 43
                ->end()
153 43
            ->end()
154
        ;
155 43
    }
156
157 43
    private function addVisitorsSection(NodeBuilder $builder)
158
    {
159
        $builder
160 43
            ->arrayNode('visitors')
161 43
                ->addDefaultsIfNotSet()
162 43
                ->children()
163 43
                    ->arrayNode('json')
164 43
                        ->addDefaultsIfNotSet()
165 43
                        ->children()
166 43
                            ->scalarNode('options')
167 43
                                ->defaultValue(0)
168 43
                                ->beforeNormalization()
169
                                    ->ifArray()->then(function($v) {
170 1
                                        $options = 0;
171 1
                                        foreach ($v as $option) {
172 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...
173
                                                $options |= (int) $option;
174 1
                                            } elseif (defined($option)) {
175 1
                                                $options |= constant($option);
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 1
                                        }
180
181 1
                                        return $options;
182 43
                                    })
183 43
                                ->end()
184 43
                                ->beforeNormalization()
185
                                    ->ifString()->then(function($v) {
186 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...
187
                                            $value = (int) $v;
188 1
                                        } elseif (defined($v)) {
189 1
                                            $value = constant($v);
190 1
                                        } else {
191
                                            throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
192
                                        }
193
194 1
                                        return $value;
195 43
                                    })
196 43
                                ->end()
197 43
                                ->validate()
198
                                    ->always(function($v) {
199 3
                                        if (!is_int($v)) {
200
                                            throw new InvalidArgumentException('Expected either integer value or a array of the JSON_ constants.');
201
                                        }
202
203 3
                                        return $v;
204 43
                                    })
205 43
                                ->end()
206 43
                            ->end()
207 43
                        ->end()
208 43
                    ->end()
209 43
                    ->arrayNode('xml')
210 43
                        ->fixXmlConfig('whitelisted-doctype', 'doctype_whitelist')
211 43
                        ->addDefaultsIfNotSet()
212 43
                        ->children()
213 43
                            ->arrayNode('doctype_whitelist')
214 43
                                ->prototype('scalar')->end()
215 43
                            ->end()
216 43
                            ->booleanNode('format_output')
217 43
                                ->defaultTrue()
218 43
                            ->end()
219 43
                        ->end()
220 43
                    ->end()
221 43
                ->end()
222 43
            ->end()
223
        ;
224 43
    }
225
226 43
    private function addContextSection(NodeBuilder $builder)
227
    {
228
        $root = $builder
229 43
                    ->arrayNode('default_context')
230 43
                    ->addDefaultsIfNotSet();
231
232 43
        $this->createContextNode($root->children(), 'serialization');
233 43
        $this->createContextNode($root->children(), 'deserialization');
234 43
    }
235
236 43
    private function createContextNode(NodeBuilder $builder, $name)
237
    {
238
        $builder
239 43
            ->arrayNode($name)
240 43
                ->addDefaultsIfNotSet()
241 43
                ->beforeNormalization()
242 43
                    ->ifString()
243
                    ->then(function ($id) {
244 1
                        return array('id' => $id);
245 43
                    })
246 43
                ->end()
247
                ->validate()->always(function ($v) {
248 6
                    if (!empty($v['id'])) {
249 2
                        return array('id' => $v['id']);
250
                    }
251 4
                    return $v;
252 43
                })->end()
253 43
                ->children()
254 43
                    ->scalarNode('id')->cannotBeEmpty()->end()
255 43
                    ->scalarNode('serialize_null')
256 43
                        ->validate()->always(function ($v) {
257
                            if (!in_array($v, array(true, false, NULL), true)){
258
                                throw new InvalidTypeException("Expected boolean or NULL for the serialize_null option");
259
                            }
260
                            return $v;
261 43
                        })
262 43
                        ->ifNull()->thenUnset()
263 43
                        ->end()
264 43
                        ->info('Flag if null values should be serialized')
265 43
                    ->end()
266 43
                    ->scalarNode('enable_max_depth_checks')
267 43
                        ->info('Flag to enable the max-depth exclusion strategy')
268 43
                    ->end()
269 43
                    ->arrayNode('attributes')
270 43
                        ->fixXmlConfig('attribute')
271 43
                        ->useAttributeAsKey('key')
272 43
                        ->prototype('scalar')->end()
273 43
                        ->info('Arbitrary key-value data for context')
274 43
                    ->end()
275 43
                    ->arrayNode('groups')
276 43
                        ->fixXmlConfig('group')
277 43
                        ->prototype('scalar')->end()
278 43
                        ->info('Default serialization groups')
279 43
                    ->end()
280 43
                    ->scalarNode('version')
281 43
                        ->validate()->ifNull()->thenUnset()->end()
282 43
                        ->info('Application version to use in exclusion strategies')
283 43
                    ->end()
284 43
                ->end()
285 43
            ->end();
286 43
    }
287
}
288