Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#213)
by Jérémiah
10:54
created

OverblogGraphQLExtension   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 9
dl 0
loc 219
ccs 130
cts 130
cp 1
rs 8.2608
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A prepend() 0 10 1
A setDefinitionParameters() 0 10 1
A setVersions() 0 6 2
B load() 0 24 1
A getAlias() 0 4 1
A getConfiguration() 0 7 2
A setClassLoaderListener() 0 13 2
A setBatchingMethod() 0 4 1
A setExpressionLanguageDefaultParser() 0 7 2
A setShowDebug() 0 4 1
B setConfigBuilders() 0 17 6
A treatConfigs() 0 11 3
A setSecurity() 0 6 2
A setGraphiQLTemplate() 0 4 1
A setErrorHandlerArguments() 0 16 3
A setSchemaBuilderArguments() 0 5 1
A setSchemaArguments() 0 17 3
A setServicesAliases() 0 9 3
A buildExceptionMap() 0 17 4

How to fix   Complexity   

Complex Class

Complex classes like OverblogGraphQLExtension often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use OverblogGraphQLExtension, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Overblog\GraphQLBundle\DependencyInjection;
4
5
use GraphQL\Type\Schema;
6
use Overblog\GraphQLBundle\Config\TypeWithOutputFieldsDefinition;
7
use Overblog\GraphQLBundle\EventListener\ClassLoaderListener;
8
use Symfony\Component\Cache\Adapter\ArrayAdapter;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
13
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
14
use Symfony\Component\DependencyInjection\Reference;
15
use Symfony\Component\ExpressionLanguage\ParserCache\ArrayParserCache;
16
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
17
use Symfony\Component\HttpKernel\Kernel;
18
19
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface
20
{
21 25
    public function load(array $configs, ContainerBuilder $container)
22
    {
23 25
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
24 25
        $loader->load('services.yml');
25 25
        $loader->load('graphql_types.yml');
26
27 25
        $config = $this->treatConfigs($configs, $container);
28
29 25
        $this->setBatchingMethod($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...on::setBatchingMethod() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
30 25
        $this->setExpressionLanguageDefaultParser($container);
31 25
        $this->setServicesAliases($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...n::setServicesAliases() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32 25
        $this->setSchemaBuilderArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...chemaBuilderArguments() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
33 25
        $this->setSchemaArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...n::setSchemaArguments() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
34 25
        $this->setErrorHandlerArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...ErrorHandlerArguments() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35 25
        $this->setGraphiQLTemplate($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...::setGraphiQLTemplate() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
36 25
        $this->setSecurity($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...xtension::setSecurity() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
37 25
        $this->setConfigBuilders($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...on::setConfigBuilders() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
38 25
        $this->setVersions($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...xtension::setVersions() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
39 25
        $this->setShowDebug($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...tension::setShowDebug() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
40 25
        $this->setDefinitionParameters($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...tDefinitionParameters() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
41 25
        $this->setClassLoaderListener($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 27 can also be of type null; however, Overblog\GraphQLBundle\D...etClassLoaderListener() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42
43 25
        $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources'));
44 25
    }
45
46 23
    public function prepend(ContainerBuilder $container)
47
    {
48 23
        $configs = $container->getExtensionConfig($this->getAlias());
49 23
        $configs = $container->getParameterBag()->resolveValue($configs);
50 23
        $config = $this->treatConfigs($configs, $container, true);
51
52
        /** @var OverblogGraphQLTypesExtension $typesExtension */
53 23
        $typesExtension = $container->getExtension($this->getAlias().'_types');
54 23
        $typesExtension->containerPrependExtensionConfig($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container, true) on line 50 can also be of type null; however, Overblog\GraphQLBundle\D...rependExtensionConfig() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55 23
    }
56
57 25
    public function getAlias()
58
    {
59 25
        return 'overblog_graphql';
60
    }
61
62 25
    public function getConfiguration(array $config, ContainerBuilder $container)
63
    {
64 25
        return new Configuration(
65 25
            $container->getParameter('kernel.debug'),
66 25
            $container->hasParameter('kernel.cache_dir') ? $container->getParameter('kernel.cache_dir') : null
67
        );
68
    }
69
70 25
    private function setClassLoaderListener(array $config, ContainerBuilder $container)
71
    {
72 25
        $container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']);
73 25
        if ($config['definitions']['use_classloader_listener']) {
74 23
            $definition = $container->setDefinition(
75 23
                $this->getAlias().'.event_listener.classloader_listener',
76 23
                new Definition(ClassLoaderListener::class)
77
            );
78 23
            $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]);
79 23
            $definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]);
80 23
            $definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]);
81
        }
82 25
    }
83
84 25
    private function setDefinitionParameters(array $config, ContainerBuilder $container)
85
    {
86
        // auto mapping
87 25
        $container->setParameter($this->getAlias().'.auto_mapping.enabled', $config['definitions']['auto_mapping']['enabled']);
88 25
        $container->setParameter($this->getAlias().'.auto_mapping.directories', $config['definitions']['auto_mapping']['directories']);
89
        // generator and config
90 25
        $container->setParameter($this->getAlias().'.default_resolver', $config['definitions']['default_resolver']);
91 25
        $container->setParameter($this->getAlias().'.class_namespace', $config['definitions']['class_namespace']);
92 25
        $container->setParameter($this->getAlias().'.cache_dir', $config['definitions']['cache_dir']);
93 25
    }
94
95 25
    private function setBatchingMethod(array $config, ContainerBuilder $container)
96
    {
97 25
        $container->setParameter($this->getAlias().'.batching_method', $config['batching_method']);
98 25
    }
99
100 25
    private function setExpressionLanguageDefaultParser(ContainerBuilder $container)
101
    {
102 25
        $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class;
103 25
        $definition = new Definition($class);
104 25
        $definition->setPublic(false);
105 25
        $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition);
106 25
    }
107
108 25
    private function setShowDebug(array $config, ContainerBuilder $container)
109
    {
110 25
        $container->getDefinition($this->getAlias().'.request_executor')->replaceArgument(4, $config['definitions']['show_debug_info']);
111 25
    }
112
113 25
    private function setVersions(array $config, ContainerBuilder $container)
114
    {
115 25
        foreach ($config['versions'] as $key => $version) {
116 25
            $container->setParameter(sprintf('%s.versions.%s', $this->getAlias(), $key), $version);
117
        }
118 25
    }
119
120 25
    private function setConfigBuilders(array $config, ContainerBuilder $container)
121
    {
122 25
        $useObjectToAddResource = method_exists($container, 'addObjectResource');
123 25
        $objectToAddResourceMethod = $useObjectToAddResource ? 'addObjectResource' : 'addClassResource';
124
125 25
        foreach (['args', 'field'] as $category) {
126 25
            if (!empty($config['definitions']['builders'][$category])) {
127 1
                $method = 'add'.ucfirst($category).'BuilderClass';
128
129 1
                foreach ($config['definitions']['builders'][$category] as $params) {
130 1
                    $object = $useObjectToAddResource ? $params['class'] : new \ReflectionClass($params['class']);
131 1
                    $container->$objectToAddResourceMethod($object);
132 25
                    TypeWithOutputFieldsDefinition::$method($params['alias'], $params['class']);
133
                }
134
            }
135
        }
136 25
    }
137
138 25
    private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false)
139
    {
140 25
        static $config = null;
141
142 25
        if ($forceReload || null === $config) {
143 25
            $configuration = $this->getConfiguration($configs, $container);
144 25
            $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 143 can be null; however, Symfony\Component\Depend...:processConfiguration() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
145
        }
146
147 25
        return $config;
148
    }
149
150 25
    private function setSecurity(array $config, ContainerBuilder $container)
151
    {
152 25
        foreach ($config['security'] as $key => $value) {
153 25
            $container->setParameter(sprintf('%s.%s', $this->getAlias(), $key), $value);
154
        }
155 25
    }
156
157 25
    private function setGraphiQLTemplate(array $config, ContainerBuilder $container)
158
    {
159 25
        $container->setParameter($this->getAlias().'.graphiql_template', $config['templates']['graphiql']);
160 25
    }
161
162 25
    private function setErrorHandlerArguments(array $config, ContainerBuilder $container)
163
    {
164 25
        $errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler');
165
166 25
        if (isset($config['definitions']['internal_error_message'])) {
167 2
            $errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']);
168
        }
169
170 25
        if (isset($config['definitions']['exceptions'])) {
171
            $errorHandlerDefinition
172 25
                ->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions']))
173 25
                ->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']])
174 25
                ->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']])
175
            ;
176
        }
177 25
    }
178
179 25
    private function setSchemaBuilderArguments(array $config, ContainerBuilder $container)
180
    {
181 25
        $container->getDefinition($this->getAlias().'.schema_builder')
182 25
            ->replaceArgument(1, $config['definitions']['config_validation']);
183 25
    }
184
185 25
    private function setSchemaArguments(array $config, ContainerBuilder $container)
186
    {
187 25
        if (isset($config['definitions']['schema'])) {
188 25
            $executorDefinition = $container->getDefinition($this->getAlias().'.request_executor');
189
190 25
            foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) {
191 20
                $schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName);
192 20
                $definition = new Definition(Schema::class);
193 20
                $definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']);
194 20
                $definition->setArguments([$schemaConfig['query'], $schemaConfig['mutation'], $schemaConfig['subscription']]);
195 20
                $definition->setPublic(false);
196 20
                $container->setDefinition($schemaID, $definition);
197
198 20
                $executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]);
199
            }
200
        }
201 25
    }
202
203 25
    private function setServicesAliases(array $config, ContainerBuilder $container)
204
    {
205 25
        if (isset($config['services'])) {
206 25
            foreach ($config['services'] as $name => $id) {
207 25
                $alias = sprintf('%s.%s', $this->getAlias(), $name);
208 25
                $container->setAlias($alias, $id);
209
            }
210
        }
211 25
    }
212
213
    /**
214
     * Returns a list of custom exceptions mapped to error/warning classes.
215
     *
216
     * @param array $exceptionConfig
217
     *
218
     * @return array Custom exception map, [exception => UserError/UserWarning]
219
     */
220 25
    private function buildExceptionMap(array $exceptionConfig)
221
    {
222 25
        $exceptionMap = [];
223 25
        $typeMap = $exceptionConfig['types'];
224
225 25
        foreach ($exceptionConfig as $type => $exceptionList) {
226 25
            if ('types' === $type) {
227 25
                continue;
228
            }
229
230 25
            foreach ($exceptionList as $exception) {
231 25
                $exceptionMap[$exception] = $typeMap[$type];
232
            }
233
        }
234
235 25
        return $exceptionMap;
236
    }
237
}
238