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

JMSSerializerExtension   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 149
Duplicated Lines 8.05 %

Coupling/Cohesion

Components 0
Dependencies 10

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 25
lcom 0
cbo 10
dl 12
loc 149
rs 10
c 0
b 0
f 0
ccs 92
cts 100
cp 0.92

2 Methods

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