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