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

Completed
Push — master ( ddaabd...e014e6 )
by Jérémiah
08:04
created

OverblogGraphQLExtension   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 34
lcom 1
cbo 9
dl 0
loc 186
ccs 121
cts 121
cp 1
rs 9.2
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A buildExceptionMap() 0 17 4
A load() 0 22 1
A prepend() 0 10 1
A getAlias() 0 4 1
A getConfiguration() 0 4 1
A setAutoMappingParameters() 0 5 1
A setExpressionLanguageDefaultParser() 0 7 2
A setShowDebug() 0 4 1
A setVersions() 0 6 2
A setConfigBuilders() 0 13 4
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
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 GraphQL\Schema;
15
use Overblog\GraphQLBundle\Config\TypeWithOutputFieldsDefinition;
16
use Symfony\Component\Cache\Adapter\ArrayAdapter;
17
use Symfony\Component\Config\FileLocator;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Definition;
20
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
21
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
22
use Symfony\Component\DependencyInjection\Reference;
23
use Symfony\Component\ExpressionLanguage\ParserCache\ArrayParserCache;
24
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
25
use Symfony\Component\HttpKernel\Kernel;
26
27
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface
28
{
29 16
    public function load(array $configs, ContainerBuilder $container)
30
    {
31 16
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
32 16
        $loader->load('services.yml');
33 16
        $loader->load('graphql_types.yml');
34
35 16
        $config = $this->treatConfigs($configs, $container);
36
37 16
        $this->setExpressionLanguageDefaultParser($container);
38 16
        $this->setServicesAliases($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 35 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...
39 16
        $this->setSchemaBuilderArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 35 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...
40 16
        $this->setSchemaArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 35 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...
41 16
        $this->setErrorHandlerArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 35 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...
42 16
        $this->setGraphiQLTemplate($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 35 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...
43 16
        $this->setSecurity($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 35 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...
44 16
        $this->setConfigBuilders($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 35 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...
45 16
        $this->setVersions($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 35 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...
46 16
        $this->setShowDebug($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 35 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...
47 16
        $this->setAutoMappingParameters($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 35 can also be of type null; however, Overblog\GraphQLBundle\D...AutoMappingParameters() 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...
48
49 16
        $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources'));
50 16
    }
51
52 14
    public function prepend(ContainerBuilder $container)
53
    {
54 14
        $configs = $container->getExtensionConfig($this->getAlias());
55 14
        $configs = $container->getParameterBag()->resolveValue($configs);
56 14
        $config = $this->treatConfigs($configs, $container, true);
57
58
        /** @var OverblogGraphQLTypesExtension $typesExtension */
59 14
        $typesExtension = $container->getExtension($this->getAlias().'_types');
60 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 56 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...
61 14
    }
62
63 16
    public function getAlias()
64
    {
65 16
        return 'overblog_graphql';
66
    }
67
68 16
    public function getConfiguration(array $config, ContainerBuilder $container)
69
    {
70 16
        return new Configuration($container->getParameter('kernel.debug'));
71
    }
72
73 16
    private function setAutoMappingParameters(array $config, ContainerBuilder $container)
74
    {
75 16
        $container->setParameter($this->getAlias().'.auto_mapping.enabled', $config['definitions']['auto_mapping']['enabled']);
76 16
        $container->setParameter($this->getAlias().'.auto_mapping.directories', $config['definitions']['auto_mapping']['directories']);
77 16
    }
78
79 16
    private function setExpressionLanguageDefaultParser(ContainerBuilder $container)
80
    {
81 16
        $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class;
82 16
        $definition = new Definition($class);
83 16
        $definition->setPublic(false);
84 16
        $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition);
85 16
    }
86
87 16
    private function setShowDebug(array $config, ContainerBuilder $container)
88
    {
89 16
        $container->getDefinition($this->getAlias().'.request_executor')->replaceArgument(4, $config['definitions']['show_debug_info']);
90 16
    }
91
92 16
    private function setVersions(array $config, ContainerBuilder $container)
93
    {
94 16
        foreach ($config['versions'] as $key => $version) {
95 16
            $container->setParameter(sprintf('%s.versions.%s', $this->getAlias(), $key), $version);
96 16
        }
97 16
    }
98
99 16
    private function setConfigBuilders(array $config, ContainerBuilder $container)
100
    {
101 16
        foreach (['args', 'field'] as $category) {
102 16
            if (!empty($config['definitions']['builders'][$category])) {
103 1
                $method = 'add'.ucfirst($category).'BuilderClass';
104
105 1
                foreach ($config['definitions']['builders'][$category] as $params) {
106 1
                    $container->addClassResource(new \ReflectionClass($params['class']));
107 1
                    TypeWithOutputFieldsDefinition::$method($params['alias'], $params['class']);
108 1
                }
109 1
            }
110 16
        }
111 16
    }
112
113 16
    private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false)
114
    {
115 16
        static $config = null;
116
117 16
        if ($forceReload || null === $config) {
118 16
            $configuration = $this->getConfiguration($configs, $container);
119 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 118 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...
120 16
        }
121
122 16
        return $config;
123
    }
124
125 16
    private function setSecurity(array $config, ContainerBuilder $container)
126
    {
127 16
        foreach ($config['security'] as $key => $value) {
128 16
            $container->setParameter(sprintf('%s.%s', $this->getAlias(), $key), $value);
129 16
        }
130 16
    }
131
132 16
    private function setGraphiQLTemplate(array $config, ContainerBuilder $container)
133
    {
134 16
        $container->setParameter($this->getAlias().'.graphiql_template', $config['templates']['graphiql']);
135 16
    }
136
137 16
    private function setErrorHandlerArguments(array $config, ContainerBuilder $container)
138
    {
139 16
        $errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler');
140
141 16
        if (isset($config['definitions']['internal_error_message'])) {
142 1
            $errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']);
143 1
        }
144
145 16
        if (isset($config['definitions']['exceptions'])) {
146
            $errorHandlerDefinition
147 16
                ->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions']))
148 16
                ->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']])
149 16
                ->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']])
150
            ;
151 16
        }
152 16
    }
153
154 16
    private function setSchemaBuilderArguments(array $config, ContainerBuilder $container)
155
    {
156 16
        $container->getDefinition($this->getAlias().'.schema_builder')
157 16
            ->replaceArgument(1, $config['definitions']['config_validation']);
158 16
    }
159
160 16
    private function setSchemaArguments(array $config, ContainerBuilder $container)
161
    {
162 16
        if (isset($config['definitions']['schema'])) {
163 16
            $executorDefinition = $container->getDefinition($this->getAlias().'.request_executor');
164
165 16
            foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) {
166 13
                $schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName);
167 13
                $definition = new Definition(Schema::class);
168 13
                $definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']);
169 13
                $definition->setArguments([$schemaConfig['query'], $schemaConfig['mutation'], $schemaConfig['subscription']]);
170 13
                $definition->setPublic(false);
171 13
                $container->setDefinition($schemaID, $definition);
172
173 13
                $executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]);
174 16
            }
175 16
        }
176 16
    }
177
178 16
    private function setServicesAliases(array $config, ContainerBuilder $container)
179
    {
180 16
        if (isset($config['services'])) {
181 16
            foreach ($config['services'] as $name => $id) {
182 16
                $alias = sprintf('%s.%s', $this->getAlias(), $name);
183 16
                $container->setAlias($alias, $id);
184 16
            }
185 16
        }
186 16
    }
187
188
    /**
189
     * Returns a list of custom exceptions mapped to error/warning classes.
190
     *
191
     * @param array $exceptionConfig
192
     *
193
     * @return array Custom exception map, [exception => UserError/UserWarning]
194
     */
195 16
    private function buildExceptionMap(array $exceptionConfig)
196
    {
197 16
        $exceptionMap = [];
198 16
        $typeMap = $exceptionConfig['types'];
199
200 16
        foreach ($exceptionConfig as $type => $exceptionList) {
201 16
            if ('types' === $type) {
202 16
                continue;
203
            }
204
205 16
            foreach ($exceptionList as $exception) {
206 2
                $exceptionMap[$exception] = $typeMap[$type];
207 16
            }
208 16
        }
209
210 16
        return $exceptionMap;
211
    }
212
}
213