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 (#234)
by Jérémiah
04:38
created

OverblogGraphQLExtension::buildExceptionMap()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 1
crap 4
1
<?php
2
3
namespace Overblog\GraphQLBundle\DependencyInjection;
4
5
use GraphQL\Type\Schema;
6
use Overblog\GraphQLBundle\CacheWarmer\GeneratorCacheWarmer;
7
use Overblog\GraphQLBundle\Config\TypeWithOutputFieldsDefinition;
8
use Overblog\GraphQLBundle\EventListener\ClassLoaderListener;
9
use Symfony\Component\Cache\Adapter\ArrayAdapter;
10
use Symfony\Component\Config\FileLocator;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Definition;
13
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
14
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
15
use Symfony\Component\DependencyInjection\Reference;
16
use Symfony\Component\ExpressionLanguage\ParserCache\ArrayParserCache;
17
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18
use Symfony\Component\HttpKernel\Kernel;
19
20
class OverblogGraphQLExtension extends Extension implements PrependExtensionInterface
21
{
22 12
    public function load(array $configs, ContainerBuilder $container)
23
    {
24 12
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
25 12
        $loader->load('services.yml');
26 12
        $loader->load('graphql_types.yml');
27
28 12
        $config = $this->treatConfigs($configs, $container);
29
30 12
        $this->setBatchingMethod($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
31 12
        $this->setExpressionLanguageDefaultParser($container);
32 12
        $this->setServicesAliases($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
33 12
        $this->setSchemaBuilderArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
34 12
        $this->setSchemaArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
35 12
        $this->setErrorHandlerArguments($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
36 12
        $this->setGraphiQLTemplate($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
37 12
        $this->setSecurity($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
38 12
        $this->setConfigBuilders($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
39 12
        $this->setVersions($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
40 12
        $this->setShowDebug($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
41 12
        $this->setDefinitionParameters($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
42 12
        $this->setClassLoaderListener($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 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...
43 12
        $this->setCompilerCacheWarmer($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container) on line 28 can also be of type null; however, Overblog\GraphQLBundle\D...etCompilerCacheWarmer() 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
45 12
        $container->setParameter($this->getAlias().'.resources_dir', realpath(__DIR__.'/../Resources'));
46 12
    }
47
48 10
    public function prepend(ContainerBuilder $container)
49
    {
50 10
        $configs = $container->getExtensionConfig($this->getAlias());
51 10
        $configs = $container->getParameterBag()->resolveValue($configs);
52 10
        $config = $this->treatConfigs($configs, $container, true);
53
54
        /** @var OverblogGraphQLTypesExtension $typesExtension */
55 10
        $typesExtension = $container->getExtension($this->getAlias().'_types');
56 10
        $typesExtension->containerPrependExtensionConfig($config, $container);
0 ignored issues
show
Bug introduced by
It seems like $config defined by $this->treatConfigs($configs, $container, true) on line 52 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...
57 10
    }
58
59 12
    public function getAlias()
60
    {
61 12
        return 'overblog_graphql';
62
    }
63
64 12
    public function getConfiguration(array $config, ContainerBuilder $container)
65
    {
66 12
        return new Configuration(
67 12
            $container->getParameter('kernel.debug'),
68 12
            $container->hasParameter('kernel.cache_dir') ? $container->getParameter('kernel.cache_dir') : null
69
        );
70
    }
71
72 12
    private function setCompilerCacheWarmer(array $config, ContainerBuilder $container)
73
    {
74 12
        if ($config['definitions']['use_generator_cache_warmer']) {
75 12
            $definition = $container->setDefinition(
76 12
                GeneratorCacheWarmer::class,
77 12
                new Definition(GeneratorCacheWarmer::class)
78
            );
79 12
            $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]);
80 12
            $definition->addTag('kernel.cache_warmer', ['priority' => 50]);
81
        }
82 12
    }
83
84 12
    private function setClassLoaderListener(array $config, ContainerBuilder $container)
85
    {
86 12
        $container->setParameter($this->getAlias().'.use_classloader_listener', $config['definitions']['use_classloader_listener']);
87 12
        if ($config['definitions']['use_classloader_listener']) {
88 11
            $definition = $container->setDefinition(
89 11
                $this->getAlias().'.event_listener.classloader_listener',
90 11
                new Definition(ClassLoaderListener::class)
91
            );
92 11
            $definition->setArguments([new Reference($this->getAlias().'.cache_compiler')]);
93 11
            $definition->addTag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'load', 'priority' => 255]);
94 11
            $definition->addTag('kernel.event_listener', ['event' => 'console.command', 'method' => 'load', 'priority' => 255]);
95
        }
96 12
    }
97
98 12
    private function setDefinitionParameters(array $config, ContainerBuilder $container)
99
    {
100
        // auto mapping
101 12
        $container->setParameter($this->getAlias().'.auto_mapping.enabled', $config['definitions']['auto_mapping']['enabled']);
102 12
        $container->setParameter($this->getAlias().'.auto_mapping.directories', $config['definitions']['auto_mapping']['directories']);
103
        // generator and config
104 12
        $container->setParameter($this->getAlias().'.default_resolver', $config['definitions']['default_resolver']);
105 12
        $container->setParameter($this->getAlias().'.class_namespace', $config['definitions']['class_namespace']);
106 12
        $container->setParameter($this->getAlias().'.cache_dir', $config['definitions']['cache_dir']);
107 12
    }
108
109 12
    private function setBatchingMethod(array $config, ContainerBuilder $container)
110
    {
111 12
        $container->setParameter($this->getAlias().'.batching_method', $config['batching_method']);
112 12
    }
113
114 12
    private function setExpressionLanguageDefaultParser(ContainerBuilder $container)
115
    {
116 12
        $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class;
117 12
        $definition = new Definition($class);
118 12
        $definition->setPublic(false);
119 12
        $container->setDefinition($this->getAlias().'.cache_expression_language_parser.default', $definition);
120 12
    }
121
122 12
    private function setShowDebug(array $config, ContainerBuilder $container)
123
    {
124 12
        $container->getDefinition($this->getAlias().'.request_executor')->replaceArgument(4, $config['definitions']['show_debug_info']);
125 12
    }
126
127 12
    private function setVersions(array $config, ContainerBuilder $container)
128
    {
129 12
        foreach ($config['versions'] as $key => $version) {
130 12
            $container->setParameter(sprintf('%s.versions.%s', $this->getAlias(), $key), $version);
131
        }
132 12
    }
133
134 12
    private function setConfigBuilders(array $config, ContainerBuilder $container)
135
    {
136 12
        $useObjectToAddResource = method_exists($container, 'addObjectResource');
137 12
        $objectToAddResourceMethod = $useObjectToAddResource ? 'addObjectResource' : 'addClassResource';
138
139 12
        foreach (['args', 'field'] as $category) {
140 12
            if (!empty($config['definitions']['builders'][$category])) {
141 1
                $method = 'add'.ucfirst($category).'BuilderClass';
142
143 1
                foreach ($config['definitions']['builders'][$category] as $params) {
144 1
                    $object = $useObjectToAddResource ? $params['class'] : new \ReflectionClass($params['class']);
145 1
                    $container->$objectToAddResourceMethod($object);
146 12
                    TypeWithOutputFieldsDefinition::$method($params['alias'], $params['class']);
147
                }
148
            }
149
        }
150 12
    }
151
152 12
    private function treatConfigs(array $configs, ContainerBuilder $container, $forceReload = false)
153
    {
154 12
        static $config = null;
155
156 12
        if ($forceReload || null === $config) {
157 12
            $configuration = $this->getConfiguration($configs, $container);
158 12
            $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->getConfiguration($configs, $container) on line 157 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...
159
        }
160
161 12
        return $config;
162
    }
163
164 12
    private function setSecurity(array $config, ContainerBuilder $container)
165
    {
166 12
        foreach ($config['security'] as $key => $value) {
167 12
            $container->setParameter(sprintf('%s.%s', $this->getAlias(), $key), $value);
168
        }
169 12
    }
170
171 12
    private function setGraphiQLTemplate(array $config, ContainerBuilder $container)
172
    {
173 12
        $container->setParameter($this->getAlias().'.graphiql_template', $config['templates']['graphiql']);
174 12
    }
175
176 12
    private function setErrorHandlerArguments(array $config, ContainerBuilder $container)
177
    {
178 12
        $errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler');
179
180 12
        if (isset($config['definitions']['internal_error_message'])) {
181
            $errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']);
182
        }
183
184 12
        if (isset($config['definitions']['exceptions'])) {
185
            $errorHandlerDefinition
186 12
                ->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions']))
187 12
                ->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']])
188 12
                ->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']])
189
            ;
190
        }
191 12
    }
192
193 12
    private function setSchemaBuilderArguments(array $config, ContainerBuilder $container)
194
    {
195 12
        $container->getDefinition($this->getAlias().'.schema_builder')
196 12
            ->replaceArgument(1, $config['definitions']['config_validation']);
197 12
    }
198
199 12
    private function setSchemaArguments(array $config, ContainerBuilder $container)
200
    {
201 12
        if (isset($config['definitions']['schema'])) {
202 12
            $executorDefinition = $container->getDefinition($this->getAlias().'.request_executor');
203
204 12
            foreach ($config['definitions']['schema'] as $schemaName => $schemaConfig) {
205 3
                $schemaID = sprintf('%s.schema_%s', $this->getAlias(), $schemaName);
206 3
                $definition = new Definition(Schema::class);
207 3
                $definition->setFactory([new Reference('overblog_graphql.schema_builder'), 'create']);
208 3
                $definition->setArguments([$schemaConfig['query'], $schemaConfig['mutation'], $schemaConfig['subscription']]);
209 3
                $definition->setPublic(false);
210 3
                $container->setDefinition($schemaID, $definition);
211
212 3
                $executorDefinition->addMethodCall('addSchema', [$schemaName, new Reference($schemaID)]);
213
            }
214
        }
215 12
    }
216
217 12
    private function setServicesAliases(array $config, ContainerBuilder $container)
218
    {
219 12
        if (isset($config['services'])) {
220 12
            foreach ($config['services'] as $name => $id) {
221 12
                $alias = sprintf('%s.%s', $this->getAlias(), $name);
222 12
                $container->setAlias($alias, $id);
223
            }
224
        }
225 12
    }
226
227
    /**
228
     * Returns a list of custom exceptions mapped to error/warning classes.
229
     *
230
     * @param array $exceptionConfig
231
     *
232
     * @return array Custom exception map, [exception => UserError/UserWarning]
233
     */
234 12
    private function buildExceptionMap(array $exceptionConfig)
235
    {
236 12
        $exceptionMap = [];
237 12
        $typeMap = $exceptionConfig['types'];
238
239 12
        foreach ($exceptionConfig as $type => $exceptionList) {
240 12
            if ('types' === $type) {
241 12
                continue;
242
            }
243
244 12
            foreach ($exceptionList as $exception) {
245 12
                $exceptionMap[$exception] = $typeMap[$type];
246
            }
247
        }
248
249 12
        return $exceptionMap;
250
    }
251
}
252