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 (#612)
by Benjamin
08:30
created

JMSSerializerExtension   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 169
Duplicated Lines 7.1 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 26
lcom 0
cbo 11
dl 12
loc 169
ccs 104
cts 112
cp 0.9286
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
F loadInternal() 12 161 25
A getConfiguration() 0 4 1

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\RuntimeException;
22
use Symfony\Component\Config\FileLocator;
23
use Symfony\Component\DependencyInjection\Alias;
24
use Symfony\Component\DependencyInjection\ContainerBuilder;
25
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
26
use Symfony\Component\DependencyInjection\Reference;
27
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
28
29
class JMSSerializerExtension extends ConfigurableExtension
30
{
31 51
    public function loadInternal(array $config, ContainerBuilder $container)
32
    {
33
        $container
34 51
            ->registerForAutoconfiguration(EventSubscriberInterface::class)
35 51
            ->addTag('jms_serializer.event_subscriber');
36
37 51
        $loader = new XmlFileLoader($container, new FileLocator(array(
38 51
            __DIR__ . '/../Resources/config/')));
39 51
        $loader->load('services.xml');
40
41
        // Built-in handlers.
42 51
        $container->getDefinition('jms_serializer.datetime_handler')
43 51
            ->addArgument($config['handlers']['datetime']['default_format'])
44 51
            ->addArgument($config['handlers']['datetime']['default_timezone'])
45 51
            ->addArgument($config['handlers']['datetime']['cdata']);
46
47 51
        $container->getDefinition('jms_serializer.array_collection_handler')
48 51
            ->replaceArgument(0, $config['handlers']['array_collection']['initialize_excluded']);
49
50
        // Built-in subscribers.
51 51
        $container->getDefinition('jms_serializer.doctrine_proxy_subscriber')
52 51
            ->replaceArgument(0, !$config['subscribers']['doctrine_proxy']['initialize_virtual_types'])
53 51
            ->replaceArgument(1, $config['subscribers']['doctrine_proxy']['initialize_excluded']);
54
55
        // Built-in object constructor.
56 51
        $container->getDefinition('jms_serializer.doctrine_object_constructor')
57 51
            ->replaceArgument(2, $config['object_constructors']['doctrine']['fallback_strategy']);
58
59
        // property naming
60
        $container
61 51
            ->getDefinition('jms_serializer.camel_case_naming_strategy')
62 51
            ->addArgument($config['property_naming']['separator'])
63 51
            ->addArgument($config['property_naming']['lower_case']);
64
65 51
        if (!empty($config['property_naming']['id'])) {
66 1
            $container->setAlias('jms_serializer.naming_strategy', $config['property_naming']['id']);
67 1
        }
68
69 51
        if ($config['property_naming']['enable_cache']) {
70
            $container
71 50
                ->getDefinition('jms_serializer.cache_naming_strategy')
72 50
                ->addArgument(new Reference((string)$container->getAlias('jms_serializer.naming_strategy')));
73 50
            $container->setAlias('jms_serializer.naming_strategy', 'jms_serializer.cache_naming_strategy');
74 50
        }
75
76 51
        $bundles = $container->getParameter('kernel.bundles');
77
78 51
        if (!empty($config['expression_evaluator']['id'])) {
79
            $container
80 49
                ->getDefinition('jms_serializer.serializer')
81 49
                ->replaceArgument(7, new Reference($config['expression_evaluator']['id']));
82
83
            $container
84 49
                ->setAlias('jms_serializer.accessor_strategy', 'jms_serializer.accessor_strategy.expression');
85
86 49
        } else {
87 2
            $container->removeDefinition('jms_serializer.expression_evaluator');
88 2
            $container->removeDefinition('jms_serializer.accessor_strategy.expression');
89
        }
90
91
        // metadata
92 51
        if ('none' === $config['metadata']['cache']) {
93
            $container->removeAlias('jms_serializer.metadata.cache');
94 51
        } elseif ('file' === $config['metadata']['cache']) {
95
            $container
96 51
                ->getDefinition('jms_serializer.metadata.cache.file_cache')
97 51
                ->replaceArgument(0, $config['metadata']['file_cache']['dir']);
98
99 51
            $dir = $container->getParameterBag()->resolveValue($config['metadata']['file_cache']['dir']);
100 51
            if (!is_dir($dir) && !@mkdir($dir, 0777, true) && !is_dir($dir)) {
101
                throw new RuntimeException(sprintf('Could not create cache directory "%s".', $dir));
102
            }
103 51
        } else {
104
            $container->setAlias('jms_serializer.metadata.cache', new Alias($config['metadata']['cache'], false));
105
        }
106
107 51
        if ($config['metadata']['infer_types_from_doctrine_metadata'] === false) {
108 1
            $container->setParameter('jms_serializer.infer_types_from_doctrine_metadata', false);
109 1
        }
110
111
        $container
112 51
            ->getDefinition('jms_serializer.metadata_factory')
113 51
            ->replaceArgument(2, $config['metadata']['debug']);
114
115
        // directories
116 51
        $directories = array();
117 51
        if ($config['metadata']['auto_detection']) {
118 51
            foreach ($bundles as $name => $class) {
119 2
                $ref = new \ReflectionClass($class);
120
121 2
                $dir = dirname($ref->getFileName()) . '/Resources/config/serializer';
122 2
                if (file_exists($dir)) {
123
                    $directories[$ref->getNamespaceName()] = $dir;
124
                }
125 51
            }
126 51
        }
127 51
        foreach ($config['metadata']['directories'] as $directory) {
128 4
            $directory['path'] = rtrim(str_replace('\\', '/', $directory['path']), '/');
129
130 4
            if ('@' === $directory['path'][0]) {
131 2
                $pathParts = explode('/', $directory['path']);
132 2
                $bundleName = substr($pathParts[0], 1);
133
134 2
                if (!isset($bundles[$bundleName])) {
135
                    throw new RuntimeException(sprintf('The bundle "%s" has not been registered with AppKernel. Available bundles: %s', $bundleName, implode(', ', array_keys($bundles))));
136
                }
137
138 2
                $ref = new \ReflectionClass($bundles[$bundleName]);
139 2
                $directory['path'] = dirname($ref->getFileName()) . substr($directory['path'], strlen('@' . $bundleName));
140 2
            }
141
142 4
            $dir = rtrim($directory['path'], '\\/');
143 4
            if (!file_exists($dir)) {
144 1
                throw new RuntimeException(sprintf('The metadata directory "%s" does not exist for the namespace "%s"', $dir, $directory['namespace_prefix']));
145
            }
146
147 3
            $directories[rtrim($directory['namespace_prefix'], '\\')] = $dir;
148 50
        }
149
        $container
150 50
            ->getDefinition('jms_serializer.metadata.file_locator')
151 50
            ->replaceArgument(0, $directories);
152
153 50
        $container->setParameter('jms_serializer.xml_deserialization_visitor.doctype_whitelist', $config['visitors']['xml']['doctype_whitelist']);
154 50
        $container->setParameter('jms_serializer.xml_serialization_visitor.format_output', $config['visitors']['xml']['format_output']);
155 50
        $container->setParameter('jms_serializer.json_serialization_visitor.options', $config['visitors']['json']['options']);
156
157 50
        if (!$container->getParameter('kernel.debug')) {
158
            $container->removeDefinition('jms_serializer.stopwatch_subscriber');
159
        }
160
161
        // context factories
162
        $services = [
163 50
            'serialization' => 'jms_serializer.configured_serialization_context_factory',
164 50
            'deserialization' => 'jms_serializer.configured_deserialization_context_factory',
165 50
        ];
166 50
        foreach ($services as $configKey => $serviceId) {
167 50
            $contextFactory = $container->getDefinition($serviceId);
168
169 50
            if (isset($config['default_context'][$configKey]['id'])) {
170 1
                $container->setAlias('jms_serializer.' . $configKey . '_context_factory', $config['default_context'][$configKey]['id']);
171 1
                $container->removeDefinition($serviceId);
172 1
                continue;
173
            }
174
175 49 View Code Duplication
            if (isset($config['default_context'][$configKey]['version'])) {
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...
176 1
                $contextFactory->addMethodCall('setVersion', [$config['default_context'][$configKey]['version']]);
177 1
            }
178 49 View Code Duplication
            if (isset($config['default_context'][$configKey]['serialize_null'])) {
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...
179 1
                $contextFactory->addMethodCall('setSerializeNulls', [$config['default_context'][$configKey]['serialize_null']]);
180 1
            }
181 49 View Code Duplication
            if (!empty($config['default_context'][$configKey]['attributes'])) {
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...
182 1
                $contextFactory->addMethodCall('setAttributes', [$config['default_context'][$configKey]['attributes']]);
183 1
            }
184 49 View Code Duplication
            if (!empty($config['default_context'][$configKey]['groups'])) {
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...
185 1
                $contextFactory->addMethodCall('setGroups', [$config['default_context'][$configKey]['groups']]);
186 1
            }
187 49
            if (!empty($config['default_context'][$configKey]['enable_max_depth_checks'])) {
188 1
                $contextFactory->addMethodCall('enableMaxDepthChecks');
189 1
            }
190 50
        }
191 50
    }
192
193 51
    public function getConfiguration(array $config, ContainerBuilder $container)
194
    {
195 51
        return new Configuration($container->getParameterBag()->resolveValue('%kernel.debug%'));
196
    }
197
}
198