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
Pull Request — master (#570)
by Asmir
02:16
created

Configuration   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 307
Duplicated Lines 4.56 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 95.87%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 10
dl 14
loc 307
ccs 232
cts 242
cp 0.9587
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConfigTreeBuilder() 0 20 1
B addHandlersSection() 0 24 1
A addSubscribersSection() 0 17 1
A addObjectConstructorsSection() 0 19 1
B addSerializersSection() 0 46 4
B addMetadataSection() 0 32 1
C addVisitorsSection() 14 68 7
A addContextSection() 0 9 1
A createContextNode() 0 51 3

How to fix   Duplicated Code   

Duplicated Code

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