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 (#544)
by Asmir
04:25
created

JMSSerializerExtension::loadInternal()   F

Complexity

Conditions 15
Paths 964

Size

Total Lines 106
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 20.7235

Importance

Changes 0
Metric Value
dl 0
loc 106
ccs 48
cts 68
cp 0.7059
rs 2.4166
c 0
b 0
f 0
cc 15
eloc 64
nc 964
nop 2
crap 20.7235

How to fix   Long Method    Complexity   

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\HttpKernel\DependencyInjection\ConfigurableExtension;
22
use Symfony\Component\DependencyInjection\Alias;
23
use JMS\Serializer\Exception\RuntimeException;
24
use Symfony\Component\Config\FileLocator;
25
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
26
use Symfony\Component\DependencyInjection\Reference;
27
use Symfony\Component\DependencyInjection\ContainerBuilder;
28
29
class JMSSerializerExtension extends ConfigurableExtension
30
{
31 11
    public function loadInternal(array $config, ContainerBuilder $container)
32
    {
33 11
        $loader = new XmlFileLoader($container, new FileLocator(array(
34 11
                        __DIR__.'/../Resources/config/')));
35 11
        $loader->load('services.xml');
36
37
        // Built-in handlers.
38 11
        $container->getDefinition('jms_serializer.datetime_handler')
39 11
            ->addArgument($config['handlers']['datetime']['default_format'])
40 11
            ->addArgument($config['handlers']['datetime']['default_timezone'])
41 11
            ->addArgument($config['handlers']['datetime']['cdata'])
42
        ;
43
44
        // property naming
45
        $container
46 11
            ->getDefinition('jms_serializer.camel_case_naming_strategy')
47 11
            ->addArgument($config['property_naming']['separator'])
48 11
            ->addArgument($config['property_naming']['lower_case'])
49
        ;
50 11
        if ($config['property_naming']['enable_cache']) {
51
            $container
52 11
                ->getDefinition('jms_serializer.cache_naming_strategy')
53 11
                ->addArgument(new Reference((string) $container->getAlias('jms_serializer.naming_strategy')))
54
            ;
55 11
            $container->setAlias('jms_serializer.naming_strategy', 'jms_serializer.cache_naming_strategy');
56 11
        }
57
58 11
        $bundles = $container->getParameter('kernel.bundles');
59
60 11
        if (!empty($config['expression_evaluator']['id'])) {
61
            $container
62 10
                ->getDefinition('jms_serializer.serializer')
63 10
                ->replaceArgument(7, new Reference($config['expression_evaluator']['id']));
64 10
        } else {
65 1
            $container->removeDefinition('jms_serializer.expression_evaluator');
66
        }
67
68
        // metadata
69 11
        if ('none' === $config['metadata']['cache']) {
70
            $container->removeAlias('jms_serializer.metadata.cache');
71 11
        } elseif ('file' === $config['metadata']['cache']) {
72
            $container
73 11
                ->getDefinition('jms_serializer.metadata.cache.file_cache')
74 11
                ->replaceArgument(0, $config['metadata']['file_cache']['dir'])
75
            ;
76
77 11
            $dir = $container->getParameterBag()->resolveValue($config['metadata']['file_cache']['dir']);
78 11
            if (!file_exists($dir)) {
79 1
                if (!$rs = @mkdir($dir, 0777, true)) {
80
                    throw new RuntimeException(sprintf('Could not create cache directory "%s".', $dir));
81
                }
82 1
            }
83 11
        } else {
84
            $container->setAlias('jms_serializer.metadata.cache', new Alias($config['metadata']['cache'], false));
85
        }
86
87 11
        if ($config['metadata']['infer_types_from_doctrine_metadata'] === false) {
88
            $container->setParameter('jms_serializer.infer_types_from_doctrine_metadata', false);
89
        }
90
91
        $container
92 11
            ->getDefinition('jms_serializer.metadata_factory')
93 11
            ->replaceArgument(2, $config['metadata']['debug'])
94
        ;
95
96
        // directories
97 11
        $directories = array();
98 11
        if ($config['metadata']['auto_detection']) {
99 11
            foreach ($bundles as $name => $class) {
100
                $ref = new \ReflectionClass($class);
101
102
                $directories[$ref->getNamespaceName()] = dirname($ref->getFileName()).'/Resources/config/serializer';
103 11
            }
104 11
        }
105 11
        foreach ($config['metadata']['directories'] as $directory) {
106
            $directory['path'] = rtrim(str_replace('\\', '/', $directory['path']), '/');
107
108
            if ('@' === $directory['path'][0]) {
109
                $bundleName = substr($directory['path'], 1, strpos($directory['path'], '/') - 1);
110
111
                if (!isset($bundles[$bundleName])) {
112
                    throw new RuntimeException(sprintf('The bundle "%s" has not been registered with AppKernel. Available bundles: %s', $bundleName, implode(', ', array_keys($bundles))));
113
                }
114
115
                $ref = new \ReflectionClass($bundles[$bundleName]);
116
                $directory['path'] = dirname($ref->getFileName()).substr($directory['path'], strlen('@'.$bundleName));
117
            }
118
119
            $directories[rtrim($directory['namespace_prefix'], '\\')] = rtrim($directory['path'], '\\/');
120 11
        }
121
        $container
122 11
            ->getDefinition('jms_serializer.metadata.file_locator')
123 11
            ->replaceArgument(0, $directories)
124
        ;
125
126 11
        $container->setParameter('jms_serializer.xml_deserialization_visitor.doctype_whitelist', $config['visitors']['xml']['doctype_whitelist']);
127 11
        $container->setParameter('jms_serializer.json_serialization_visitor.options', $config['visitors']['json']['options']);
128
129 11
        if ( ! $config['enable_short_alias']) {
130
            $container->removeAlias('serializer');
131
        }
132
133 11
        if ( ! $container->getParameter('kernel.debug')) {
134
            $container->removeDefinition('jms_serializer.stopwatch_subscriber');
135
        }
136 11
    }
137
138 12
    public function getConfiguration(array $config, ContainerBuilder $container)
139
    {
140 12
        return new Configuration($container->getParameterBag()->resolveValue('%kernel.debug%'));
141
    }
142
}
143