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
Push — 0.8 ( cb3474...3da337 )
by Jérémiah
02:10
created

OverblogGraphQLExtension   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 31
c 0
b 0
f 0
lcom 1
cbo 9
dl 0
loc 182
ccs 117
cts 117
cp 1
rs 9.8

16 Methods

Rating   Name   Duplication   Size   Complexity  
A treatConfigs() 0 11 3
A setSecurity() 0 5 1
A setGraphiQLTemplate() 0 4 1
A load() 0 22 1
A prepend() 0 10 1
A setExpressionLanguageDefaultParser() 0 10 2
A setShowDebug() 0 4 1
A setVersions() 0 6 1
A setConfigBuilders() 0 13 4
A setErrorHandlerArguments() 0 16 3
A setSchemaBuilderArguments() 0 5 1
A setSchemaArguments() 0 17 3
A setServicesAliases() 0 9 3
A getAlias() 0 4 1
A getConfiguration() 0 4 1
A buildExceptionMap() 0 17 4
1
<?php
2
3
/*
4
 * This file is part of the OverblogGraphQLBundle package.
5
 *
6
 * (c) Overblog <http://github.com/overblog/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Overblog\GraphQLBundle\DependencyInjection;
13
14
use Overblog\GraphQLBundle\Config\TypeWithOutputFieldsDefinition;
15
use Symfony\Component\Config\FileLocator;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
19
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
20
use Symfony\Component\DependencyInjection\Reference;
21
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
22
use Symfony\Component\HttpKernel\Kernel;
23
24
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface
25
{
26
    public function load(array $configs, ContainerBuilder $container)
27
    {
28
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
29 16
        $loader->load('services.yml');
30
        $loader->load('graphql_types.yml');
31 16
        $loader->load('graphql_resolvers.yml');
32 16
33 16
        $config = $this->treatConfigs($configs, $container);
34
35 16
        $this->setExpressionLanguageDefaultParser($container);
36
        $this->setServicesAliases($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 33 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...
37 16
        $this->setSchemaBuilderArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 33 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...
38 16
        $this->setSchemaArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 33 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...
39 16
        $this->setErrorHandlerArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 33 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...
40 16
        $this->setGraphiQLTemplate($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 33 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...
41 16
        $this->setSecurity($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 33 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...
42 16
        $this->setConfigBuilders($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 33 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...
43 16
        $this->setVersions($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 33 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...
44 16
        $this->setShowDebug($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 33 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...
45 16
46 16
        $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources'));
47 16
    }
48 16
49
    public function prepend(ContainerBuilder $container)
50 16
    {
51 16
        $configs = $container->getExtensionConfig($this->getAlias());
52
        $configs = $container->getParameterBag()->resolveValue($configs);
53 14
        $config = $this->treatConfigs($configs, $container, true);
54
55 14
        /** @var OverblogGraphQLTypesExtension $typesExtension */
56 14
        $typesExtension = $container->getExtension($this->getAlias().'_types');
57 14
        $typesExtension->containerPrependExtensionConfig($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container, true) on line 53 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...
58
    }
59
60 14
    private function setExpressionLanguageDefaultParser(ContainerBuilder $container)
61 14
    {
62 14
        $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ?
63
            'Symfony\\Component\\Cache\Adapter\\ArrayAdapter'
64 16
            : 'Symfony\\Component\\ExpressionLanguage\\ParserCache\\ArrayParserCache'
65
        ;
66 16
        $definition = new Definition($class);
67
        $definition->setPublic(false);
68
        $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition);
69 16
    }
70
71 16
    private function setShowDebug(array $config, ContainerBuilder $container)
72
    {
73
        $container->getDefinition($this->getAlias().'.request_executor')->replaceArgument(4, $config['definitions']['show_debug_info']);
74 16
    }
75
76 16
    private function setVersions(array $config, ContainerBuilder $container)
77 16
    {
78 16
        $container->setParameter($this->getAlias().'.versions.graphiql', $config['versions']['graphiql']);
79
        $container->setParameter($this->getAlias().'.versions.react', $config['versions']['react']);
80 16
        $container->setParameter($this->getAlias().'.versions.fetch', $config['versions']['fetch']);
81
    }
82 16
83 16
    private function setConfigBuilders(array $config, ContainerBuilder $container)
84
    {
85 16
        foreach (['args', 'field'] as $category) {
86
            if (!empty($config['definitions']['builders'][$category])) {
87 16
                $method = 'add'.ucfirst($category).'BuilderClass';
88 16
89 16
                foreach ($config['definitions']['builders'][$category] as $params) {
90 16
                    $container->addClassResource(new \ReflectionClass($params['class']));
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Depend...der::addClassResource() has been deprecated with message: since version 3.3, to be removed in 4.0. Use addObjectResource() or getReflectionClass() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
91 16
                    TypeWithOutputFieldsDefinition::$method($params['alias'], $params['class']);
92
                }
93 16
            }
94
        }
95 16
    }
96 16
97
    private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false)
98 16
    {
99
        static $config = null;
100 16
101 16
        if ($forceReload || null === $config) {
102 16
            $configuration = $this->getConfiguration($configs, $container);
103 16
            $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 102 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...
104
        }
105 16
106
        return $config;
107 16
    }
108 16
109 1
    private function setSecurity(array $config, ContainerBuilder $container)
110
    {
111 1
        $container->setParameter($this->getAlias().'.query_max_depth', $config['security']['query_max_depth']);
112 1
        $container->setParameter($this->getAlias().'.query_max_complexity', $config['security']['query_max_complexity']);
113 1
    }
114 1
115 1
    private function setGraphiQLTemplate(array $config, ContainerBuilder $container)
116 16
    {
117 16
        $container->setParameter($this->getAlias().'.graphiql_template', $config['templates']['graphiql']);
118
    }
119 16
120
    private function setErrorHandlerArguments(array $config, ContainerBuilder $container)
121 16
    {
122
        $errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler');
123 16
124 16
        if (isset($config['definitions']['internal_error_message'])) {
125 16
            $errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']);
126 16
        }
127
128 16
        if (isset($config['definitions']['exceptions'])) {
129
            $errorHandlerDefinition
130
                ->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions']))
131 16
                ->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']])
132
                ->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']])
133 16
            ;
134 16
        }
135 16
    }
136 16
137
    private function setSchemaBuilderArguments(array $config, ContainerBuilder $container)
138 16
    {
139
        $container->getDefinition($this->getAlias().'.schema_builder')
140 16
            ->replaceArgument(1, $config['definitions']['config_validation']);
141 16
    }
142
143 16
    private function setSchemaArguments(array $config, ContainerBuilder $container)
144
    {
145 16
        if (isset($config['definitions']['schema'])) {
146
            $executorDefinition = $container->getDefinition($this->getAlias().'.request_executor');
147 16
148 1
            foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) {
149 1
                $schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName);
150
                $definition = new Definition('GraphQL\Schema');
151 16
                $definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']);
152
                $definition->setArguments([$schemaConfig['query'], $schemaConfig['mutation'], $schemaConfig['subscription']]);
153 16
                $definition->setPublic(false);
154 16
                $container->setDefinition($schemaID, $definition);
155 16
156
                $executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]);
157 16
            }
158 16
        }
159
    }
160 16
161
    private function setServicesAliases(array $config, ContainerBuilder $container)
162 16
    {
163 16
        if (isset($config['services'])) {
164 16
            foreach ($config['services'] as $name => $id) {
165
                $alias = sprintf('%s.%s', $this->getAlias(), $name);
166 16
                $container->setAlias($alias, $id);
167
            }
168 16
        }
169 16
    }
170
171 16
    public function getAlias()
172 13
    {
173 13
        return 'overblog_graphql';
174 13
    }
175 13
176 13
    public function getConfiguration(array $config, ContainerBuilder $container)
177 13
    {
178
        return new Configuration($container->getParameter('kernel.debug'));
179 13
    }
180 16
181 16
    /**
182 16
     * Returns a list of custom exceptions mapped to error/warning classes.
183
     *
184 16
     * @param array $exceptionConfig
185
     *
186 16
     * @return array Custom exception map, [exception => UserError/UserWarning]
187 16
     */
188 16
    private function buildExceptionMap(array $exceptionConfig)
189 16
    {
190 16
        $exceptionMap = [];
191 16
        $typeMap = $exceptionConfig['types'];
192 16
193
        foreach ($exceptionConfig as $type => $exceptionList) {
194
            if ('types' === $type) {
195
                continue;
196
            }
197
198
            foreach ($exceptionList as $exception) {
199
                $exceptionMap[$exception] = $typeMap[$type];
200
            }
201 16
        }
202
203 16
        return $exceptionMap;
204 16
    }
205
}
206